Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 2 | * lm85.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
| 5 | * Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com> |
| 6 | * Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de> |
| 7 | * Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com> |
Jean Delvare | 590e853 | 2014-06-11 18:35:56 +0200 | [diff] [blame] | 8 | * Copyright (C) 2007--2014 Jean Delvare <jdelvare@suse.de> |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 9 | * |
| 10 | * Chip details at <http://www.national.com/ds/LM/LM85.pdf> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/jiffies.h> |
| 31 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 32 | #include <linux/hwmon.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 33 | #include <linux/hwmon-vid.h> |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 34 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 35 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 39 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 41 | enum chips { |
Jean Delvare | 590e853 | 2014-06-11 18:35:56 +0200 | [diff] [blame] | 42 | lm85, |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 43 | adm1027, adt7463, adt7468, |
Guenter Roeck | 06923f8 | 2011-02-19 08:27:47 -0800 | [diff] [blame] | 44 | emc6d100, emc6d102, emc6d103, emc6d103s |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 45 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* The LM85 registers */ |
| 48 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 49 | #define LM85_REG_IN(nr) (0x20 + (nr)) |
| 50 | #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2) |
| 51 | #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 53 | #define LM85_REG_TEMP(nr) (0x25 + (nr)) |
| 54 | #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2) |
| 55 | #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* Fan speeds are LSB, MSB (2 bytes) */ |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 58 | #define LM85_REG_FAN(nr) (0x28 + (nr) * 2) |
| 59 | #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 61 | #define LM85_REG_PWM(nr) (0x30 + (nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 63 | #define LM85_REG_COMPANY 0x3e |
| 64 | #define LM85_REG_VERSTEP 0x3f |
Darrick J. Wong | 79b92f2 | 2008-11-12 13:26:59 -0800 | [diff] [blame] | 65 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 66 | #define ADT7468_REG_CFG5 0x7c |
| 67 | #define ADT7468_OFF64 (1 << 0) |
| 68 | #define ADT7468_HFPWM (1 << 1) |
| 69 | #define IS_ADT7468_OFF64(data) \ |
Darrick J. Wong | 79b92f2 | 2008-11-12 13:26:59 -0800 | [diff] [blame] | 70 | ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64)) |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 71 | #define IS_ADT7468_HFPWM(data) \ |
Jean Delvare | f6c61cf | 2010-10-28 20:31:50 +0200 | [diff] [blame] | 72 | ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM)) |
Darrick J. Wong | 79b92f2 | 2008-11-12 13:26:59 -0800 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* These are the recognized values for the above regs */ |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 75 | #define LM85_COMPANY_NATIONAL 0x01 |
| 76 | #define LM85_COMPANY_ANALOG_DEV 0x41 |
| 77 | #define LM85_COMPANY_SMSC 0x5c |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 78 | #define LM85_VERSTEP_LM85C 0x60 |
| 79 | #define LM85_VERSTEP_LM85B 0x62 |
| 80 | #define LM85_VERSTEP_LM96000_1 0x68 |
| 81 | #define LM85_VERSTEP_LM96000_2 0x69 |
| 82 | #define LM85_VERSTEP_ADM1027 0x60 |
| 83 | #define LM85_VERSTEP_ADT7463 0x62 |
| 84 | #define LM85_VERSTEP_ADT7463C 0x6A |
| 85 | #define LM85_VERSTEP_ADT7468_1 0x71 |
| 86 | #define LM85_VERSTEP_ADT7468_2 0x72 |
| 87 | #define LM85_VERSTEP_EMC6D100_A0 0x60 |
| 88 | #define LM85_VERSTEP_EMC6D100_A1 0x61 |
| 89 | #define LM85_VERSTEP_EMC6D102 0x65 |
| 90 | #define LM85_VERSTEP_EMC6D103_A0 0x68 |
| 91 | #define LM85_VERSTEP_EMC6D103_A1 0x69 |
| 92 | #define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 94 | #define LM85_REG_CONFIG 0x40 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 96 | #define LM85_REG_ALARM1 0x41 |
| 97 | #define LM85_REG_ALARM2 0x42 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 99 | #define LM85_REG_VID 0x43 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | /* Automated FAN control */ |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 102 | #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr)) |
| 103 | #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr)) |
| 104 | #define LM85_REG_AFAN_SPIKE1 0x62 |
| 105 | #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr)) |
| 106 | #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr)) |
| 107 | #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr)) |
| 108 | #define LM85_REG_AFAN_HYST1 0x6d |
| 109 | #define LM85_REG_AFAN_HYST2 0x6e |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 111 | #define ADM1027_REG_EXTEND_ADC1 0x76 |
| 112 | #define ADM1027_REG_EXTEND_ADC2 0x77 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | #define EMC6D100_REG_ALARM3 0x7d |
| 115 | /* IN5, IN6 and IN7 */ |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 116 | #define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5)) |
| 117 | #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2) |
| 118 | #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2) |
| 119 | #define EMC6D102_REG_EXTEND_ADC1 0x85 |
| 120 | #define EMC6D102_REG_EXTEND_ADC2 0x86 |
| 121 | #define EMC6D102_REG_EXTEND_ADC3 0x87 |
| 122 | #define EMC6D102_REG_EXTEND_ADC4 0x88 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 124 | /* |
| 125 | * Conversions. Rounding and limit checking is only done on the TO_REG |
| 126 | * variants. Note that you should be a bit careful with which arguments |
| 127 | * these macros are called: arguments may be evaluated more than once. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | */ |
| 129 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 130 | /* IN are scaled according to built-in resistors */ |
Jean Delvare | e89e22b | 2008-06-25 08:47:35 -0400 | [diff] [blame] | 131 | static const int lm85_scaling[] = { /* .001 Volts */ |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 132 | 2500, 2250, 3300, 5000, 12000, |
| 133 | 3300, 1500, 1800 /*EMC6D100*/ |
| 134 | }; |
| 135 | #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 137 | #define INS_TO_REG(n, val) \ |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 138 | clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 140 | #define INSEXT_FROM_REG(n, val, ext) \ |
Jean Delvare | 5a4d3ef | 2007-07-05 20:38:32 +0200 | [diff] [blame] | 141 | SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 143 | #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | /* FAN speed is measured using 90kHz clock */ |
Jean Delvare | 63f281a | 2007-07-05 20:39:40 +0200 | [diff] [blame] | 146 | static inline u16 FAN_TO_REG(unsigned long val) |
| 147 | { |
| 148 | if (!val) |
| 149 | return 0xffff; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 150 | return clamp_val(5400000 / val, 1, 0xfffe); |
Jean Delvare | 63f281a | 2007-07-05 20:39:40 +0200 | [diff] [blame] | 151 | } |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 152 | #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \ |
| 153 | 5400000 / (val)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | /* Temperature is reported in .001 degC increments */ |
| 156 | #define TEMP_TO_REG(val) \ |
Guenter Roeck | 3248c3b | 2014-07-29 22:23:12 -0700 | [diff] [blame] | 157 | DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000) |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 158 | #define TEMPEXT_FROM_REG(val, ext) \ |
Jean Delvare | 5a4d3ef | 2007-07-05 20:38:32 +0200 | [diff] [blame] | 159 | SCALE(((val) << 4) + (ext), 16, 1000) |
| 160 | #define TEMP_FROM_REG(val) ((val) * 1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 162 | #define PWM_TO_REG(val) clamp_val(val, 0, 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | #define PWM_FROM_REG(val) (val) |
| 164 | |
| 165 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 166 | /* |
| 167 | * ZONEs have the following parameters: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * Limit (low) temp, 1. degC |
| 169 | * Hysteresis (below limit), 1. degC (0-15) |
| 170 | * Range of speed control, .1 degC (2-80) |
| 171 | * Critical (high) temp, 1. degC |
| 172 | * |
| 173 | * FAN PWMs have the following parameters: |
| 174 | * Reference Zone, 1, 2, 3, etc. |
| 175 | * Spinup time, .05 sec |
| 176 | * PWM value at limit/low temp, 1 count |
| 177 | * PWM Frequency, 1. Hz |
| 178 | * PWM is Min or OFF below limit, flag |
| 179 | * Invert PWM output, flag |
| 180 | * |
| 181 | * Some chips filter the temp, others the fan. |
| 182 | * Filter constant (or disabled) .1 seconds |
| 183 | */ |
| 184 | |
| 185 | /* These are the zone temperature range encodings in .001 degree C */ |
Jean Delvare | e89e22b | 2008-06-25 08:47:35 -0400 | [diff] [blame] | 186 | static const int lm85_range_map[] = { |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 187 | 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000, |
| 188 | 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000 |
| 189 | }; |
| 190 | |
Guenter Roeck | 3248c3b | 2014-07-29 22:23:12 -0700 | [diff] [blame] | 191 | static int RANGE_TO_REG(long range) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | int i; |
| 194 | |
Jean Delvare | d38b149 | 2008-04-03 10:40:39 +0200 | [diff] [blame] | 195 | /* Find the closest match */ |
Jean Delvare | 1b92ada | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 196 | for (i = 0; i < 15; ++i) { |
| 197 | if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2) |
| 198 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Jean Delvare | d38b149 | 2008-04-03 10:40:39 +0200 | [diff] [blame] | 200 | |
Jean Delvare | 1b92ada | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 201 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 203 | #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | /* These are the PWM frequency encodings */ |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 206 | static const int lm85_freq_map[8] = { /* 1 Hz */ |
Jean Delvare | 8a0795d | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 207 | 10, 15, 23, 30, 38, 47, 61, 94 |
| 208 | }; |
| 209 | static const int adm1027_freq_map[8] = { /* 1 Hz */ |
| 210 | 11, 15, 22, 29, 35, 44, 59, 88 |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 211 | }; |
| 212 | |
Guenter Roeck | 3248c3b | 2014-07-29 22:23:12 -0700 | [diff] [blame] | 213 | static int FREQ_TO_REG(const int *map, unsigned long freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
| 215 | int i; |
| 216 | |
Jean Delvare | 86010c9 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 217 | /* Find the closest match */ |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 218 | for (i = 0; i < 7; ++i) |
Jean Delvare | 8a0795d | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 219 | if (freq <= (map[i] + map[i + 1]) / 2) |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 220 | break; |
Jean Delvare | e89e22b | 2008-06-25 08:47:35 -0400 | [diff] [blame] | 221 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
Jean Delvare | 8a0795d | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 223 | |
| 224 | static int FREQ_FROM_REG(const int *map, u8 reg) |
| 225 | { |
| 226 | return map[reg & 0x07]; |
| 227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 229 | /* |
| 230 | * Since we can't use strings, I'm abusing these numbers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | * to stand in for the following meanings: |
| 232 | * 1 -- PWM responds to Zone 1 |
| 233 | * 2 -- PWM responds to Zone 2 |
| 234 | * 3 -- PWM responds to Zone 3 |
| 235 | * 23 -- PWM responds to the higher temp of Zone 2 or 3 |
| 236 | * 123 -- PWM responds to highest of Zone 1, 2, or 3 |
| 237 | * 0 -- PWM is always at 0% (ie, off) |
| 238 | * -1 -- PWM is always at 100% |
| 239 | * -2 -- PWM responds to manual control |
| 240 | */ |
| 241 | |
Jean Delvare | e89e22b | 2008-06-25 08:47:35 -0400 | [diff] [blame] | 242 | static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 }; |
| 243 | #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 245 | static int ZONE_TO_REG(int zone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
| 247 | int i; |
| 248 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 249 | for (i = 0; i <= 7; ++i) |
| 250 | if (zone == lm85_zone_map[i]) |
| 251 | break; |
| 252 | if (i > 7) /* Not found. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | i = 3; /* Always 100% */ |
Jean Delvare | e89e22b | 2008-06-25 08:47:35 -0400 | [diff] [blame] | 254 | return i << 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 257 | #define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15) |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 258 | #define HYST_FROM_REG(val) ((val) * 1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 260 | /* |
| 261 | * Chip sampling rates |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | * |
| 263 | * Some sensors are not updated more frequently than once per second |
| 264 | * so it doesn't make sense to read them more often than that. |
| 265 | * We cache the results and return the saved data if the driver |
| 266 | * is called again before a second has elapsed. |
| 267 | * |
| 268 | * Also, there is significant configuration data for this chip |
| 269 | * given the automatic PWM fan control that is possible. There |
| 270 | * are about 47 bytes of config data to only 22 bytes of actual |
| 271 | * readings. So, we keep the config data up to date in the cache |
| 272 | * when it is written and only sample it once every 1 *minute* |
| 273 | */ |
| 274 | #define LM85_DATA_INTERVAL (HZ + HZ / 2) |
| 275 | #define LM85_CONFIG_INTERVAL (1 * 60 * HZ) |
| 276 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 277 | /* |
| 278 | * LM85 can automatically adjust fan speeds based on temperature |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | * This structure encapsulates an entire Zone config. There are |
| 280 | * three zones (one for each temperature input) on the lm85 |
| 281 | */ |
| 282 | struct lm85_zone { |
| 283 | s8 limit; /* Low temp limit */ |
| 284 | u8 hyst; /* Low limit hysteresis. (0-15) */ |
| 285 | u8 range; /* Temp range, encoded */ |
| 286 | s8 critical; /* "All fans ON" temp limit */ |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 287 | u8 max_desired; /* |
| 288 | * Actual "max" temperature specified. Preserved |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | * to prevent "drift" as other autofan control |
| 290 | * values change. |
| 291 | */ |
| 292 | }; |
| 293 | |
| 294 | struct lm85_autofan { |
| 295 | u8 config; /* Register value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | u8 min_pwm; /* Minimum PWM value, encoded */ |
| 297 | u8 min_off; /* Min PWM or OFF below "limit", flag */ |
| 298 | }; |
| 299 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 300 | /* |
| 301 | * For each registered chip, we need to keep some data in memory. |
| 302 | * The structure is dynamically allocated. |
| 303 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | struct lm85_data { |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 305 | struct i2c_client *client; |
| 306 | const struct attribute_group *groups[6]; |
Jean Delvare | 8a0795d | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 307 | const int *freq_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | enum chips type; |
| 309 | |
Guenter Roeck | de24880 | 2011-02-25 08:19:42 -0800 | [diff] [blame] | 310 | bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */ |
| 311 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 312 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | int valid; /* !=0 if following fields are valid */ |
| 314 | unsigned long last_reading; /* In jiffies */ |
| 315 | unsigned long last_config; /* In jiffies */ |
| 316 | |
| 317 | u8 in[8]; /* Register value */ |
| 318 | u8 in_max[8]; /* Register value */ |
| 319 | u8 in_min[8]; /* Register value */ |
| 320 | s8 temp[3]; /* Register value */ |
| 321 | s8 temp_min[3]; /* Register value */ |
| 322 | s8 temp_max[3]; /* Register value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | u16 fan[4]; /* Register value */ |
| 324 | u16 fan_min[4]; /* Register value */ |
| 325 | u8 pwm[3]; /* Register value */ |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 326 | u8 pwm_freq[3]; /* Register encoding */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | u8 temp_ext[3]; /* Decoded values */ |
| 328 | u8 in_ext[8]; /* Decoded values */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | u8 vid; /* Register value */ |
| 330 | u8 vrm; /* VRM version */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | u32 alarms; /* Register encoding, combined */ |
Darrick J. Wong | 79b92f2 | 2008-11-12 13:26:59 -0800 | [diff] [blame] | 332 | u8 cfg5; /* Config Register 5 on ADT7468 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | struct lm85_autofan autofan[3]; |
| 334 | struct lm85_zone zone[3]; |
| 335 | }; |
| 336 | |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 337 | static int lm85_read_value(struct i2c_client *client, u8 reg) |
| 338 | { |
| 339 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 341 | /* What size location is it? */ |
| 342 | switch (reg) { |
| 343 | case LM85_REG_FAN(0): /* Read WORD data */ |
| 344 | case LM85_REG_FAN(1): |
| 345 | case LM85_REG_FAN(2): |
| 346 | case LM85_REG_FAN(3): |
| 347 | case LM85_REG_FAN_MIN(0): |
| 348 | case LM85_REG_FAN_MIN(1): |
| 349 | case LM85_REG_FAN_MIN(2): |
| 350 | case LM85_REG_FAN_MIN(3): |
| 351 | case LM85_REG_ALARM1: /* Read both bytes at once */ |
| 352 | res = i2c_smbus_read_byte_data(client, reg) & 0xff; |
| 353 | res |= i2c_smbus_read_byte_data(client, reg + 1) << 8; |
| 354 | break; |
| 355 | default: /* Read BYTE data */ |
| 356 | res = i2c_smbus_read_byte_data(client, reg); |
| 357 | break; |
| 358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 360 | return res; |
| 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 363 | static void lm85_write_value(struct i2c_client *client, u8 reg, int value) |
| 364 | { |
| 365 | switch (reg) { |
| 366 | case LM85_REG_FAN(0): /* Write WORD data */ |
| 367 | case LM85_REG_FAN(1): |
| 368 | case LM85_REG_FAN(2): |
| 369 | case LM85_REG_FAN(3): |
| 370 | case LM85_REG_FAN_MIN(0): |
| 371 | case LM85_REG_FAN_MIN(1): |
| 372 | case LM85_REG_FAN_MIN(2): |
| 373 | case LM85_REG_FAN_MIN(3): |
| 374 | /* NOTE: ALARM is read only, so not included here */ |
| 375 | i2c_smbus_write_byte_data(client, reg, value & 0xff); |
| 376 | i2c_smbus_write_byte_data(client, reg + 1, value >> 8); |
| 377 | break; |
| 378 | default: /* Write BYTE data */ |
| 379 | i2c_smbus_write_byte_data(client, reg, value); |
| 380 | break; |
| 381 | } |
| 382 | } |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 383 | |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 384 | static struct lm85_data *lm85_update_device(struct device *dev) |
| 385 | { |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 386 | struct lm85_data *data = dev_get_drvdata(dev); |
| 387 | struct i2c_client *client = data->client; |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 388 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 390 | mutex_lock(&data->update_lock); |
| 391 | |
| 392 | if (!data->valid || |
| 393 | time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) { |
| 394 | /* Things that change quickly */ |
| 395 | dev_dbg(&client->dev, "Reading sensor values\n"); |
| 396 | |
| 397 | /* |
| 398 | * Have to read extended bits first to "freeze" the |
| 399 | * more significant bits that are read later. |
| 400 | * There are 2 additional resolution bits per channel and we |
| 401 | * have room for 4, so we shift them to the left. |
| 402 | */ |
| 403 | if (data->type == adm1027 || data->type == adt7463 || |
| 404 | data->type == adt7468) { |
| 405 | int ext1 = lm85_read_value(client, |
| 406 | ADM1027_REG_EXTEND_ADC1); |
| 407 | int ext2 = lm85_read_value(client, |
| 408 | ADM1027_REG_EXTEND_ADC2); |
| 409 | int val = (ext1 << 8) + ext2; |
| 410 | |
| 411 | for (i = 0; i <= 4; i++) |
| 412 | data->in_ext[i] = |
| 413 | ((val >> (i * 2)) & 0x03) << 2; |
| 414 | |
| 415 | for (i = 0; i <= 2; i++) |
| 416 | data->temp_ext[i] = |
| 417 | (val >> ((i + 4) * 2)) & 0x0c; |
| 418 | } |
| 419 | |
| 420 | data->vid = lm85_read_value(client, LM85_REG_VID); |
| 421 | |
| 422 | for (i = 0; i <= 3; ++i) { |
| 423 | data->in[i] = |
| 424 | lm85_read_value(client, LM85_REG_IN(i)); |
| 425 | data->fan[i] = |
| 426 | lm85_read_value(client, LM85_REG_FAN(i)); |
| 427 | } |
| 428 | |
| 429 | if (!data->has_vid5) |
| 430 | data->in[4] = lm85_read_value(client, LM85_REG_IN(4)); |
| 431 | |
| 432 | if (data->type == adt7468) |
| 433 | data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5); |
| 434 | |
| 435 | for (i = 0; i <= 2; ++i) { |
| 436 | data->temp[i] = |
| 437 | lm85_read_value(client, LM85_REG_TEMP(i)); |
| 438 | data->pwm[i] = |
| 439 | lm85_read_value(client, LM85_REG_PWM(i)); |
| 440 | |
| 441 | if (IS_ADT7468_OFF64(data)) |
| 442 | data->temp[i] -= 64; |
| 443 | } |
| 444 | |
| 445 | data->alarms = lm85_read_value(client, LM85_REG_ALARM1); |
| 446 | |
| 447 | if (data->type == emc6d100) { |
| 448 | /* Three more voltage sensors */ |
| 449 | for (i = 5; i <= 7; ++i) { |
| 450 | data->in[i] = lm85_read_value(client, |
| 451 | EMC6D100_REG_IN(i)); |
| 452 | } |
| 453 | /* More alarm bits */ |
| 454 | data->alarms |= lm85_read_value(client, |
| 455 | EMC6D100_REG_ALARM3) << 16; |
| 456 | } else if (data->type == emc6d102 || data->type == emc6d103 || |
| 457 | data->type == emc6d103s) { |
| 458 | /* |
| 459 | * Have to read LSB bits after the MSB ones because |
| 460 | * the reading of the MSB bits has frozen the |
| 461 | * LSBs (backward from the ADM1027). |
| 462 | */ |
| 463 | int ext1 = lm85_read_value(client, |
| 464 | EMC6D102_REG_EXTEND_ADC1); |
| 465 | int ext2 = lm85_read_value(client, |
| 466 | EMC6D102_REG_EXTEND_ADC2); |
| 467 | int ext3 = lm85_read_value(client, |
| 468 | EMC6D102_REG_EXTEND_ADC3); |
| 469 | int ext4 = lm85_read_value(client, |
| 470 | EMC6D102_REG_EXTEND_ADC4); |
| 471 | data->in_ext[0] = ext3 & 0x0f; |
| 472 | data->in_ext[1] = ext4 & 0x0f; |
| 473 | data->in_ext[2] = ext4 >> 4; |
| 474 | data->in_ext[3] = ext3 >> 4; |
| 475 | data->in_ext[4] = ext2 >> 4; |
| 476 | |
| 477 | data->temp_ext[0] = ext1 & 0x0f; |
| 478 | data->temp_ext[1] = ext2 & 0x0f; |
| 479 | data->temp_ext[2] = ext1 >> 4; |
| 480 | } |
| 481 | |
| 482 | data->last_reading = jiffies; |
| 483 | } /* last_reading */ |
| 484 | |
| 485 | if (!data->valid || |
| 486 | time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) { |
| 487 | /* Things that don't change often */ |
| 488 | dev_dbg(&client->dev, "Reading config values\n"); |
| 489 | |
| 490 | for (i = 0; i <= 3; ++i) { |
| 491 | data->in_min[i] = |
| 492 | lm85_read_value(client, LM85_REG_IN_MIN(i)); |
| 493 | data->in_max[i] = |
| 494 | lm85_read_value(client, LM85_REG_IN_MAX(i)); |
| 495 | data->fan_min[i] = |
| 496 | lm85_read_value(client, LM85_REG_FAN_MIN(i)); |
| 497 | } |
| 498 | |
| 499 | if (!data->has_vid5) { |
| 500 | data->in_min[4] = lm85_read_value(client, |
| 501 | LM85_REG_IN_MIN(4)); |
| 502 | data->in_max[4] = lm85_read_value(client, |
| 503 | LM85_REG_IN_MAX(4)); |
| 504 | } |
| 505 | |
| 506 | if (data->type == emc6d100) { |
| 507 | for (i = 5; i <= 7; ++i) { |
| 508 | data->in_min[i] = lm85_read_value(client, |
| 509 | EMC6D100_REG_IN_MIN(i)); |
| 510 | data->in_max[i] = lm85_read_value(client, |
| 511 | EMC6D100_REG_IN_MAX(i)); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | for (i = 0; i <= 2; ++i) { |
| 516 | int val; |
| 517 | |
| 518 | data->temp_min[i] = |
| 519 | lm85_read_value(client, LM85_REG_TEMP_MIN(i)); |
| 520 | data->temp_max[i] = |
| 521 | lm85_read_value(client, LM85_REG_TEMP_MAX(i)); |
| 522 | |
| 523 | data->autofan[i].config = |
| 524 | lm85_read_value(client, LM85_REG_AFAN_CONFIG(i)); |
| 525 | val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i)); |
| 526 | data->pwm_freq[i] = val & 0x07; |
| 527 | data->zone[i].range = val >> 4; |
| 528 | data->autofan[i].min_pwm = |
| 529 | lm85_read_value(client, LM85_REG_AFAN_MINPWM(i)); |
| 530 | data->zone[i].limit = |
| 531 | lm85_read_value(client, LM85_REG_AFAN_LIMIT(i)); |
| 532 | data->zone[i].critical = |
| 533 | lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i)); |
| 534 | |
| 535 | if (IS_ADT7468_OFF64(data)) { |
| 536 | data->temp_min[i] -= 64; |
| 537 | data->temp_max[i] -= 64; |
| 538 | data->zone[i].limit -= 64; |
| 539 | data->zone[i].critical -= 64; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (data->type != emc6d103s) { |
| 544 | i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1); |
| 545 | data->autofan[0].min_off = (i & 0x20) != 0; |
| 546 | data->autofan[1].min_off = (i & 0x40) != 0; |
| 547 | data->autofan[2].min_off = (i & 0x80) != 0; |
| 548 | |
| 549 | i = lm85_read_value(client, LM85_REG_AFAN_HYST1); |
| 550 | data->zone[0].hyst = i >> 4; |
| 551 | data->zone[1].hyst = i & 0x0f; |
| 552 | |
| 553 | i = lm85_read_value(client, LM85_REG_AFAN_HYST2); |
| 554 | data->zone[2].hyst = i >> 4; |
| 555 | } |
| 556 | |
| 557 | data->last_config = jiffies; |
| 558 | } /* last_config */ |
| 559 | |
| 560 | data->valid = 1; |
| 561 | |
| 562 | mutex_unlock(&data->update_lock); |
| 563 | |
| 564 | return data; |
| 565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
| 567 | /* 4 Fans */ |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 568 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
| 569 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 571 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 573 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 575 | |
| 576 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
| 577 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 579 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 581 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 583 | |
| 584 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 585 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 587 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 588 | struct lm85_data *data = dev_get_drvdata(dev); |
| 589 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 590 | unsigned long val; |
| 591 | int err; |
| 592 | |
| 593 | err = kstrtoul(buf, 10, &val); |
| 594 | if (err) |
| 595 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 597 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | data->fan_min[nr] = FAN_TO_REG(val); |
| 599 | lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 600 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return count; |
| 602 | } |
| 603 | |
| 604 | #define show_fan_offset(offset) \ |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 605 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 606 | show_fan, NULL, offset - 1); \ |
| 607 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 608 | show_fan_min, set_fan_min, offset - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
| 610 | show_fan_offset(1); |
| 611 | show_fan_offset(2); |
| 612 | show_fan_offset(3); |
| 613 | show_fan_offset(4); |
| 614 | |
| 615 | /* vid, vrm, alarms */ |
| 616 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 617 | static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, |
| 618 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
| 620 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 9c516ef | 2005-11-26 20:07:54 +0100 | [diff] [blame] | 621 | int vid; |
| 622 | |
Guenter Roeck | de24880 | 2011-02-25 08:19:42 -0800 | [diff] [blame] | 623 | if (data->has_vid5) { |
Jean Delvare | 9c516ef | 2005-11-26 20:07:54 +0100 | [diff] [blame] | 624 | /* 6-pin VID (VRM 10) */ |
| 625 | vid = vid_from_reg(data->vid & 0x3f, data->vrm); |
| 626 | } else { |
| 627 | /* 5-pin VID (VRM 9) */ |
| 628 | vid = vid_from_reg(data->vid & 0x1f, data->vrm); |
| 629 | } |
| 630 | |
| 631 | return sprintf(buf, "%d\n", vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); |
| 635 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 636 | static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, |
| 637 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame] | 639 | struct lm85_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | return sprintf(buf, "%ld\n", (long) data->vrm); |
| 641 | } |
| 642 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 643 | static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, |
| 644 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
Jean Delvare | 8f74efe | 2007-12-01 11:25:33 +0100 | [diff] [blame] | 646 | struct lm85_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 647 | unsigned long val; |
| 648 | int err; |
| 649 | |
| 650 | err = kstrtoul(buf, 10, &val); |
| 651 | if (err) |
| 652 | return err; |
| 653 | |
Guenter Roeck | 3248c3b | 2014-07-29 22:23:12 -0700 | [diff] [blame] | 654 | if (val > 255) |
| 655 | return -EINVAL; |
| 656 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 657 | data->vrm = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return count; |
| 659 | } |
| 660 | |
| 661 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); |
| 662 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 663 | static ssize_t show_alarms_reg(struct device *dev, struct device_attribute |
| 664 | *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
| 666 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 68188ba | 2005-05-16 18:52:38 +0200 | [diff] [blame] | 667 | return sprintf(buf, "%u\n", data->alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL); |
| 671 | |
Jean Delvare | bf76e9d | 2007-07-05 20:37:58 +0200 | [diff] [blame] | 672 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 673 | char *buf) |
| 674 | { |
| 675 | int nr = to_sensor_dev_attr(attr)->index; |
| 676 | struct lm85_data *data = lm85_update_device(dev); |
| 677 | return sprintf(buf, "%u\n", (data->alarms >> nr) & 1); |
| 678 | } |
| 679 | |
| 680 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 681 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 682 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 683 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 684 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 685 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18); |
| 686 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16); |
| 687 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17); |
| 688 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 689 | static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14); |
| 690 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 691 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 692 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15); |
| 693 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10); |
| 694 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 695 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12); |
| 696 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13); |
| 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | /* pwm */ |
| 699 | |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 700 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
| 701 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 703 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 705 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 707 | |
| 708 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
| 709 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 711 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 712 | struct lm85_data *data = dev_get_drvdata(dev); |
| 713 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 714 | unsigned long val; |
| 715 | int err; |
| 716 | |
| 717 | err = kstrtoul(buf, 10, &val); |
| 718 | if (err) |
| 719 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 721 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | data->pwm[nr] = PWM_TO_REG(val); |
| 723 | lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 724 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return count; |
| 726 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 727 | |
| 728 | static ssize_t show_pwm_enable(struct device *dev, struct device_attribute |
| 729 | *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 731 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 4b4df95 | 2007-12-03 23:23:21 +0100 | [diff] [blame] | 733 | int pwm_zone, enable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | pwm_zone = ZONE_FROM_REG(data->autofan[nr].config); |
Jean Delvare | 4b4df95 | 2007-12-03 23:23:21 +0100 | [diff] [blame] | 736 | switch (pwm_zone) { |
| 737 | case -1: /* PWM is always at 100% */ |
| 738 | enable = 0; |
| 739 | break; |
| 740 | case 0: /* PWM is always at 0% */ |
| 741 | case -2: /* PWM responds to manual control */ |
| 742 | enable = 1; |
| 743 | break; |
| 744 | default: /* PWM in automatic mode */ |
| 745 | enable = 2; |
| 746 | } |
| 747 | return sprintf(buf, "%d\n", enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Jean Delvare | 455f791 | 2007-12-03 23:28:42 +0100 | [diff] [blame] | 750 | static ssize_t set_pwm_enable(struct device *dev, struct device_attribute |
| 751 | *attr, const char *buf, size_t count) |
| 752 | { |
| 753 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 754 | struct lm85_data *data = dev_get_drvdata(dev); |
| 755 | struct i2c_client *client = data->client; |
Jean Delvare | 455f791 | 2007-12-03 23:28:42 +0100 | [diff] [blame] | 756 | u8 config; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 757 | unsigned long val; |
| 758 | int err; |
| 759 | |
| 760 | err = kstrtoul(buf, 10, &val); |
| 761 | if (err) |
| 762 | return err; |
Jean Delvare | 455f791 | 2007-12-03 23:28:42 +0100 | [diff] [blame] | 763 | |
| 764 | switch (val) { |
| 765 | case 0: |
| 766 | config = 3; |
| 767 | break; |
| 768 | case 1: |
| 769 | config = 7; |
| 770 | break; |
| 771 | case 2: |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 772 | /* |
| 773 | * Here we have to choose arbitrarily one of the 5 possible |
| 774 | * configurations; I go for the safest |
| 775 | */ |
Jean Delvare | 455f791 | 2007-12-03 23:28:42 +0100 | [diff] [blame] | 776 | config = 6; |
| 777 | break; |
| 778 | default: |
| 779 | return -EINVAL; |
| 780 | } |
| 781 | |
| 782 | mutex_lock(&data->update_lock); |
| 783 | data->autofan[nr].config = lm85_read_value(client, |
| 784 | LM85_REG_AFAN_CONFIG(nr)); |
| 785 | data->autofan[nr].config = (data->autofan[nr].config & ~0xe0) |
| 786 | | (config << 5); |
| 787 | lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr), |
| 788 | data->autofan[nr].config); |
| 789 | mutex_unlock(&data->update_lock); |
| 790 | return count; |
| 791 | } |
| 792 | |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 793 | static ssize_t show_pwm_freq(struct device *dev, |
| 794 | struct device_attribute *attr, char *buf) |
| 795 | { |
| 796 | int nr = to_sensor_dev_attr(attr)->index; |
| 797 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | f6c61cf | 2010-10-28 20:31:50 +0200 | [diff] [blame] | 798 | int freq; |
| 799 | |
| 800 | if (IS_ADT7468_HFPWM(data)) |
| 801 | freq = 22500; |
| 802 | else |
| 803 | freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]); |
| 804 | |
| 805 | return sprintf(buf, "%d\n", freq); |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static ssize_t set_pwm_freq(struct device *dev, |
| 809 | struct device_attribute *attr, const char *buf, size_t count) |
| 810 | { |
| 811 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 812 | struct lm85_data *data = dev_get_drvdata(dev); |
| 813 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 814 | unsigned long val; |
| 815 | int err; |
| 816 | |
| 817 | err = kstrtoul(buf, 10, &val); |
| 818 | if (err) |
| 819 | return err; |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 820 | |
| 821 | mutex_lock(&data->update_lock); |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 822 | /* |
| 823 | * The ADT7468 has a special high-frequency PWM output mode, |
Jean Delvare | f6c61cf | 2010-10-28 20:31:50 +0200 | [diff] [blame] | 824 | * where all PWM outputs are driven by a 22.5 kHz clock. |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 825 | * This might confuse the user, but there's not much we can do. |
| 826 | */ |
Jean Delvare | f6c61cf | 2010-10-28 20:31:50 +0200 | [diff] [blame] | 827 | if (data->type == adt7468 && val >= 11300) { /* High freq. mode */ |
| 828 | data->cfg5 &= ~ADT7468_HFPWM; |
| 829 | lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5); |
| 830 | } else { /* Low freq. mode */ |
| 831 | data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val); |
| 832 | lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), |
| 833 | (data->zone[nr].range << 4) |
| 834 | | data->pwm_freq[nr]); |
| 835 | if (data->type == adt7468) { |
| 836 | data->cfg5 |= ADT7468_HFPWM; |
| 837 | lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5); |
| 838 | } |
| 839 | } |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 840 | mutex_unlock(&data->update_lock); |
| 841 | return count; |
| 842 | } |
| 843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | #define show_pwm_reg(offset) \ |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 845 | static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ |
| 846 | show_pwm, set_pwm, offset - 1); \ |
Jean Delvare | 455f791 | 2007-12-03 23:28:42 +0100 | [diff] [blame] | 847 | static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 848 | show_pwm_enable, set_pwm_enable, offset - 1); \ |
| 849 | static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \ |
| 850 | show_pwm_freq, set_pwm_freq, offset - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
| 852 | show_pwm_reg(1); |
| 853 | show_pwm_reg(2); |
| 854 | show_pwm_reg(3); |
| 855 | |
| 856 | /* Voltages */ |
| 857 | |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 858 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
| 859 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 861 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 863 | return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr], |
| 864 | data->in_ext[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 866 | |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 867 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 868 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 870 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 872 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 874 | |
| 875 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
| 876 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 878 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 879 | struct lm85_data *data = dev_get_drvdata(dev); |
| 880 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 881 | long val; |
| 882 | int err; |
| 883 | |
| 884 | err = kstrtol(buf, 10, &val); |
| 885 | if (err) |
| 886 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 888 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | data->in_min[nr] = INS_TO_REG(nr, val); |
| 890 | lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 891 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | return count; |
| 893 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 894 | |
| 895 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
| 896 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 898 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 900 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 902 | |
| 903 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
| 904 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 906 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 907 | struct lm85_data *data = dev_get_drvdata(dev); |
| 908 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 909 | long val; |
| 910 | int err; |
| 911 | |
| 912 | err = kstrtol(buf, 10, &val); |
| 913 | if (err) |
| 914 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 916 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | data->in_max[nr] = INS_TO_REG(nr, val); |
| 918 | lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 919 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | return count; |
| 921 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | #define show_in_reg(offset) \ |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 924 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 925 | show_in, NULL, offset); \ |
| 926 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 927 | show_in_min, set_in_min, offset); \ |
| 928 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 929 | show_in_max, set_in_max, offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
| 931 | show_in_reg(0); |
| 932 | show_in_reg(1); |
| 933 | show_in_reg(2); |
| 934 | show_in_reg(3); |
| 935 | show_in_reg(4); |
Jean Delvare | 6b9aad2 | 2007-07-05 20:37:21 +0200 | [diff] [blame] | 936 | show_in_reg(5); |
| 937 | show_in_reg(6); |
| 938 | show_in_reg(7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
| 940 | /* Temps */ |
| 941 | |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 942 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 943 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 945 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 947 | return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr], |
| 948 | data->temp_ext[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 950 | |
| 951 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
| 952 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 954 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 956 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 958 | |
| 959 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
| 960 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 962 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 963 | struct lm85_data *data = dev_get_drvdata(dev); |
| 964 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 965 | long val; |
| 966 | int err; |
| 967 | |
| 968 | err = kstrtol(buf, 10, &val); |
| 969 | if (err) |
| 970 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
Darrick J. Wong | 79b92f2 | 2008-11-12 13:26:59 -0800 | [diff] [blame] | 972 | if (IS_ADT7468_OFF64(data)) |
| 973 | val += 64; |
| 974 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 975 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | data->temp_min[nr] = TEMP_TO_REG(val); |
| 977 | lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 978 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | return count; |
| 980 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 981 | |
| 982 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
| 983 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 985 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 987 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 989 | |
| 990 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
| 991 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 993 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 994 | struct lm85_data *data = dev_get_drvdata(dev); |
| 995 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 996 | long val; |
| 997 | int err; |
| 998 | |
| 999 | err = kstrtol(buf, 10, &val); |
| 1000 | if (err) |
| 1001 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
Darrick J. Wong | 79b92f2 | 2008-11-12 13:26:59 -0800 | [diff] [blame] | 1003 | if (IS_ADT7468_OFF64(data)) |
| 1004 | val += 64; |
| 1005 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1006 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | data->temp_max[nr] = TEMP_TO_REG(val); |
| 1008 | lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1009 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | return count; |
| 1011 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | #define show_temp_reg(offset) \ |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1014 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
| 1015 | show_temp, NULL, offset - 1); \ |
| 1016 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ |
| 1017 | show_temp_min, set_temp_min, offset - 1); \ |
| 1018 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 1019 | show_temp_max, set_temp_max, offset - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | show_temp_reg(1); |
| 1022 | show_temp_reg(2); |
| 1023 | show_temp_reg(3); |
| 1024 | |
| 1025 | |
| 1026 | /* Automatic PWM control */ |
| 1027 | |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1028 | static ssize_t show_pwm_auto_channels(struct device *dev, |
| 1029 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1031 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1033 | return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1035 | |
| 1036 | static ssize_t set_pwm_auto_channels(struct device *dev, |
| 1037 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1039 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1040 | struct lm85_data *data = dev_get_drvdata(dev); |
| 1041 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 1042 | long val; |
| 1043 | int err; |
| 1044 | |
| 1045 | err = kstrtol(buf, 10, &val); |
| 1046 | if (err) |
| 1047 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1049 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | data->autofan[nr].config = (data->autofan[nr].config & (~0xe0)) |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1051 | | ZONE_TO_REG(val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr), |
| 1053 | data->autofan[nr].config); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1054 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | return count; |
| 1056 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1057 | |
| 1058 | static ssize_t show_pwm_auto_pwm_min(struct device *dev, |
| 1059 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1061 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1063 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1065 | |
| 1066 | static ssize_t set_pwm_auto_pwm_min(struct device *dev, |
| 1067 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1069 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1070 | struct lm85_data *data = dev_get_drvdata(dev); |
| 1071 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 1072 | unsigned long val; |
| 1073 | int err; |
| 1074 | |
| 1075 | err = kstrtoul(buf, 10, &val); |
| 1076 | if (err) |
| 1077 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1079 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | data->autofan[nr].min_pwm = PWM_TO_REG(val); |
| 1081 | lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr), |
| 1082 | data->autofan[nr].min_pwm); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1083 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | return count; |
| 1085 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1086 | |
| 1087 | static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, |
| 1088 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1090 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1092 | return sprintf(buf, "%d\n", data->autofan[nr].min_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1094 | |
| 1095 | static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, |
| 1096 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1098 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1099 | struct lm85_data *data = dev_get_drvdata(dev); |
| 1100 | struct i2c_client *client = data->client; |
Jean Delvare | 7133e56 | 2008-04-12 19:56:35 +0200 | [diff] [blame] | 1101 | u8 tmp; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 1102 | long val; |
| 1103 | int err; |
| 1104 | |
| 1105 | err = kstrtol(buf, 10, &val); |
| 1106 | if (err) |
| 1107 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1109 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | data->autofan[nr].min_off = val; |
Jean Delvare | 7133e56 | 2008-04-12 19:56:35 +0200 | [diff] [blame] | 1111 | tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1); |
| 1112 | tmp &= ~(0x20 << nr); |
| 1113 | if (data->autofan[nr].min_off) |
| 1114 | tmp |= 0x20 << nr; |
| 1115 | lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1116 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | return count; |
| 1118 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | #define pwm_auto(offset) \ |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1121 | static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \ |
| 1122 | S_IRUGO | S_IWUSR, show_pwm_auto_channels, \ |
| 1123 | set_pwm_auto_channels, offset - 1); \ |
| 1124 | static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \ |
| 1125 | S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \ |
| 1126 | set_pwm_auto_pwm_min, offset - 1); \ |
| 1127 | static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \ |
| 1128 | S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \ |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 1129 | set_pwm_auto_pwm_minctl, offset - 1) |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | pwm_auto(1); |
| 1132 | pwm_auto(2); |
| 1133 | pwm_auto(3); |
| 1134 | |
| 1135 | /* Temperature settings for automatic PWM control */ |
| 1136 | |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1137 | static ssize_t show_temp_auto_temp_off(struct device *dev, |
| 1138 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1140 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1142 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | HYST_FROM_REG(data->zone[nr].hyst)); |
| 1144 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1145 | |
| 1146 | static ssize_t set_temp_auto_temp_off(struct device *dev, |
| 1147 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1149 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1150 | struct lm85_data *data = dev_get_drvdata(dev); |
| 1151 | struct i2c_client *client = data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | int min; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 1153 | long val; |
| 1154 | int err; |
| 1155 | |
| 1156 | err = kstrtol(buf, 10, &val); |
| 1157 | if (err) |
| 1158 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1160 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | min = TEMP_FROM_REG(data->zone[nr].limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | data->zone[nr].hyst = HYST_TO_REG(min - val); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1163 | if (nr == 0 || nr == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | lm85_write_value(client, LM85_REG_AFAN_HYST1, |
| 1165 | (data->zone[0].hyst << 4) |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1166 | | data->zone[1].hyst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } else { |
| 1168 | lm85_write_value(client, LM85_REG_AFAN_HYST2, |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1169 | (data->zone[2].hyst << 4)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1171 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | return count; |
| 1173 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1174 | |
| 1175 | static ssize_t show_temp_auto_temp_min(struct device *dev, |
| 1176 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1178 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1180 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1182 | |
| 1183 | static ssize_t set_temp_auto_temp_min(struct device *dev, |
| 1184 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1186 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1187 | struct lm85_data *data = dev_get_drvdata(dev); |
| 1188 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 1189 | long val; |
| 1190 | int err; |
| 1191 | |
| 1192 | err = kstrtol(buf, 10, &val); |
| 1193 | if (err) |
| 1194 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1196 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | data->zone[nr].limit = TEMP_TO_REG(val); |
| 1198 | lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr), |
| 1199 | data->zone[nr].limit); |
| 1200 | |
| 1201 | /* Update temp_auto_max and temp_auto_range */ |
| 1202 | data->zone[nr].range = RANGE_TO_REG( |
| 1203 | TEMP_FROM_REG(data->zone[nr].max_desired) - |
| 1204 | TEMP_FROM_REG(data->zone[nr].limit)); |
| 1205 | lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), |
| 1206 | ((data->zone[nr].range & 0x0f) << 4) |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 1207 | | (data->pwm_freq[nr] & 0x07)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1209 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | return count; |
| 1211 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1212 | |
| 1213 | static ssize_t show_temp_auto_temp_max(struct device *dev, |
| 1214 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1216 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1218 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | RANGE_FROM_REG(data->zone[nr].range)); |
| 1220 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1221 | |
| 1222 | static ssize_t set_temp_auto_temp_max(struct device *dev, |
| 1223 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1225 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1226 | struct lm85_data *data = dev_get_drvdata(dev); |
| 1227 | struct i2c_client *client = data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | int min; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 1229 | long val; |
| 1230 | int err; |
| 1231 | |
| 1232 | err = kstrtol(buf, 10, &val); |
| 1233 | if (err) |
| 1234 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1236 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | min = TEMP_FROM_REG(data->zone[nr].limit); |
| 1238 | data->zone[nr].max_desired = TEMP_TO_REG(val); |
| 1239 | data->zone[nr].range = RANGE_TO_REG( |
| 1240 | val - min); |
| 1241 | lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), |
| 1242 | ((data->zone[nr].range & 0x0f) << 4) |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 1243 | | (data->pwm_freq[nr] & 0x07)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1244 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | return count; |
| 1246 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1247 | |
| 1248 | static ssize_t show_temp_auto_temp_crit(struct device *dev, |
| 1249 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1251 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | struct lm85_data *data = lm85_update_device(dev); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1253 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1255 | |
| 1256 | static ssize_t set_temp_auto_temp_crit(struct device *dev, |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1257 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1259 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1260 | struct lm85_data *data = dev_get_drvdata(dev); |
| 1261 | struct i2c_client *client = data->client; |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 1262 | long val; |
| 1263 | int err; |
| 1264 | |
| 1265 | err = kstrtol(buf, 10, &val); |
| 1266 | if (err) |
| 1267 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1269 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | data->zone[nr].critical = TEMP_TO_REG(val); |
| 1271 | lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr), |
| 1272 | data->zone[nr].critical); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1273 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | return count; |
| 1275 | } |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | #define temp_auto(offset) \ |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1278 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \ |
| 1279 | S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \ |
| 1280 | set_temp_auto_temp_off, offset - 1); \ |
| 1281 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \ |
| 1282 | S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \ |
| 1283 | set_temp_auto_temp_min, offset - 1); \ |
| 1284 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \ |
| 1285 | S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \ |
| 1286 | set_temp_auto_temp_max, offset - 1); \ |
| 1287 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \ |
| 1288 | S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \ |
| 1289 | set_temp_auto_temp_crit, offset - 1); |
| 1290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | temp_auto(1); |
| 1292 | temp_auto(2); |
| 1293 | temp_auto(3); |
| 1294 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 1295 | static struct attribute *lm85_attributes[] = { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1296 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 1297 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 1298 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 1299 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 1300 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 1301 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 1302 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 1303 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
Jean Delvare | bf76e9d | 2007-07-05 20:37:58 +0200 | [diff] [blame] | 1304 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 1305 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 1306 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
| 1307 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1308 | |
| 1309 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 1310 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1311 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1312 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 1313 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 1314 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
Jean Delvare | 34e7dc6 | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 1315 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, |
| 1316 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, |
| 1317 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1318 | |
| 1319 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 1320 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 1321 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 1322 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 1323 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 1324 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 1325 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 1326 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 1327 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 1328 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 1329 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 1330 | &sensor_dev_attr_in3_max.dev_attr.attr, |
Jean Delvare | bf76e9d | 2007-07-05 20:37:58 +0200 | [diff] [blame] | 1331 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 1332 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 1333 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 1334 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1335 | |
| 1336 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1337 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 1338 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 1339 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 1340 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 1341 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 1342 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 1343 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 1344 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
Jean Delvare | bf76e9d | 2007-07-05 20:37:58 +0200 | [diff] [blame] | 1345 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 1346 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 1347 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 1348 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 1349 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1350 | |
| 1351 | &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr, |
| 1352 | &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr, |
| 1353 | &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr, |
| 1354 | &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr, |
| 1355 | &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr, |
| 1356 | &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr, |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1357 | |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1358 | &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr, |
| 1359 | &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr, |
| 1360 | &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr, |
| 1361 | &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr, |
| 1362 | &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr, |
| 1363 | &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr, |
| 1364 | &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr, |
| 1365 | &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr, |
| 1366 | &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr, |
| 1367 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 1368 | &dev_attr_vrm.attr, |
| 1369 | &dev_attr_cpu0_vid.attr, |
| 1370 | &dev_attr_alarms.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 1371 | NULL |
| 1372 | }; |
| 1373 | |
| 1374 | static const struct attribute_group lm85_group = { |
| 1375 | .attrs = lm85_attributes, |
| 1376 | }; |
| 1377 | |
Guenter Roeck | 06923f8 | 2011-02-19 08:27:47 -0800 | [diff] [blame] | 1378 | static struct attribute *lm85_attributes_minctl[] = { |
| 1379 | &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr, |
| 1380 | &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr, |
| 1381 | &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr, |
Jean Delvare | 5f441e2 | 2011-04-29 16:33:36 +0200 | [diff] [blame] | 1382 | NULL |
Guenter Roeck | 06923f8 | 2011-02-19 08:27:47 -0800 | [diff] [blame] | 1383 | }; |
| 1384 | |
| 1385 | static const struct attribute_group lm85_group_minctl = { |
| 1386 | .attrs = lm85_attributes_minctl, |
| 1387 | }; |
| 1388 | |
| 1389 | static struct attribute *lm85_attributes_temp_off[] = { |
| 1390 | &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr, |
| 1391 | &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr, |
| 1392 | &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr, |
Jean Delvare | 5f441e2 | 2011-04-29 16:33:36 +0200 | [diff] [blame] | 1393 | NULL |
Guenter Roeck | 06923f8 | 2011-02-19 08:27:47 -0800 | [diff] [blame] | 1394 | }; |
| 1395 | |
| 1396 | static const struct attribute_group lm85_group_temp_off = { |
| 1397 | .attrs = lm85_attributes_temp_off, |
| 1398 | }; |
| 1399 | |
Jean Delvare | 6b9aad2 | 2007-07-05 20:37:21 +0200 | [diff] [blame] | 1400 | static struct attribute *lm85_attributes_in4[] = { |
Jean Delvare | b353a48 | 2007-07-05 20:36:12 +0200 | [diff] [blame] | 1401 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 1402 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 1403 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | bf76e9d | 2007-07-05 20:37:58 +0200 | [diff] [blame] | 1404 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 1405 | NULL |
| 1406 | }; |
| 1407 | |
Jean Delvare | 6b9aad2 | 2007-07-05 20:37:21 +0200 | [diff] [blame] | 1408 | static const struct attribute_group lm85_group_in4 = { |
| 1409 | .attrs = lm85_attributes_in4, |
| 1410 | }; |
| 1411 | |
| 1412 | static struct attribute *lm85_attributes_in567[] = { |
| 1413 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 1414 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 1415 | &sensor_dev_attr_in7_input.dev_attr.attr, |
| 1416 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 1417 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 1418 | &sensor_dev_attr_in7_min.dev_attr.attr, |
| 1419 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 1420 | &sensor_dev_attr_in6_max.dev_attr.attr, |
| 1421 | &sensor_dev_attr_in7_max.dev_attr.attr, |
Jean Delvare | bf76e9d | 2007-07-05 20:37:58 +0200 | [diff] [blame] | 1422 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 1423 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
| 1424 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
Jean Delvare | 6b9aad2 | 2007-07-05 20:37:21 +0200 | [diff] [blame] | 1425 | NULL |
| 1426 | }; |
| 1427 | |
| 1428 | static const struct attribute_group lm85_group_in567 = { |
| 1429 | .attrs = lm85_attributes_in567, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 1430 | }; |
| 1431 | |
Jean Delvare | 5f4475947 | 2008-06-25 09:10:30 -0400 | [diff] [blame] | 1432 | static void lm85_init_client(struct i2c_client *client) |
| 1433 | { |
| 1434 | int value; |
| 1435 | |
| 1436 | /* Start monitoring if needed */ |
| 1437 | value = lm85_read_value(client, LM85_REG_CONFIG); |
| 1438 | if (!(value & 0x01)) { |
| 1439 | dev_info(&client->dev, "Starting monitoring\n"); |
| 1440 | lm85_write_value(client, LM85_REG_CONFIG, value | 0x01); |
| 1441 | } |
| 1442 | |
| 1443 | /* Warn about unusual configuration bits */ |
| 1444 | if (value & 0x02) |
| 1445 | dev_warn(&client->dev, "Device configuration is locked\n"); |
| 1446 | if (!(value & 0x04)) |
| 1447 | dev_warn(&client->dev, "Device is not ready\n"); |
| 1448 | } |
| 1449 | |
Jean Delvare | 5cfaf33 | 2009-09-15 17:18:14 +0200 | [diff] [blame] | 1450 | static int lm85_is_fake(struct i2c_client *client) |
| 1451 | { |
| 1452 | /* |
| 1453 | * Differenciate between real LM96000 and Winbond WPCD377I. The latter |
| 1454 | * emulate the former except that it has no hardware monitoring function |
| 1455 | * so the readings are always 0. |
| 1456 | */ |
| 1457 | int i; |
| 1458 | u8 in_temp, fan; |
| 1459 | |
| 1460 | for (i = 0; i < 8; i++) { |
| 1461 | in_temp = i2c_smbus_read_byte_data(client, 0x20 + i); |
| 1462 | fan = i2c_smbus_read_byte_data(client, 0x28 + i); |
| 1463 | if (in_temp != 0x00 || fan != 0xff) |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | return 1; |
| 1468 | } |
| 1469 | |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1470 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 1471 | static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | { |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1473 | struct i2c_adapter *adapter = client->adapter; |
| 1474 | int address = client->addr; |
Jean Delvare | 590e853 | 2014-06-11 18:35:56 +0200 | [diff] [blame] | 1475 | const char *type_name = NULL; |
Jean Delvare | d42a2eb | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1476 | int company, verstep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
Jean Delvare | e89e22b | 2008-06-25 08:47:35 -0400 | [diff] [blame] | 1478 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | /* We need to be able to do byte I/O */ |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1480 | return -ENODEV; |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Jean Delvare | d42a2eb | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1483 | /* Determine the chip type */ |
| 1484 | company = lm85_read_value(client, LM85_REG_COMPANY); |
| 1485 | verstep = lm85_read_value(client, LM85_REG_VERSTEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 1487 | dev_dbg(&adapter->dev, |
| 1488 | "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n", |
Jean Delvare | d42a2eb | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1489 | address, company, verstep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | |
Jean Delvare | d42a2eb | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1491 | if (company == LM85_COMPANY_NATIONAL) { |
| 1492 | switch (verstep) { |
| 1493 | case LM85_VERSTEP_LM85C: |
| 1494 | type_name = "lm85c"; |
| 1495 | break; |
| 1496 | case LM85_VERSTEP_LM85B: |
| 1497 | type_name = "lm85b"; |
| 1498 | break; |
| 1499 | case LM85_VERSTEP_LM96000_1: |
| 1500 | case LM85_VERSTEP_LM96000_2: |
| 1501 | /* Check for Winbond WPCD377I */ |
| 1502 | if (lm85_is_fake(client)) { |
| 1503 | dev_dbg(&adapter->dev, |
| 1504 | "Found Winbond WPCD377I, ignoring\n"); |
| 1505 | return -ENODEV; |
| 1506 | } |
Jean Delvare | 590e853 | 2014-06-11 18:35:56 +0200 | [diff] [blame] | 1507 | type_name = "lm85"; |
Jean Delvare | d42a2eb | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1508 | break; |
Jean Delvare | 69fc1fe | 2008-10-17 17:51:13 +0200 | [diff] [blame] | 1509 | } |
Jean Delvare | d42a2eb | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1510 | } else if (company == LM85_COMPANY_ANALOG_DEV) { |
| 1511 | switch (verstep) { |
| 1512 | case LM85_VERSTEP_ADM1027: |
| 1513 | type_name = "adm1027"; |
| 1514 | break; |
| 1515 | case LM85_VERSTEP_ADT7463: |
| 1516 | case LM85_VERSTEP_ADT7463C: |
| 1517 | type_name = "adt7463"; |
| 1518 | break; |
| 1519 | case LM85_VERSTEP_ADT7468_1: |
| 1520 | case LM85_VERSTEP_ADT7468_2: |
| 1521 | type_name = "adt7468"; |
| 1522 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | } |
Jean Delvare | d42a2eb | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1524 | } else if (company == LM85_COMPANY_SMSC) { |
| 1525 | switch (verstep) { |
| 1526 | case LM85_VERSTEP_EMC6D100_A0: |
| 1527 | case LM85_VERSTEP_EMC6D100_A1: |
| 1528 | /* Note: we can't tell a '100 from a '101 */ |
| 1529 | type_name = "emc6d100"; |
| 1530 | break; |
| 1531 | case LM85_VERSTEP_EMC6D102: |
| 1532 | type_name = "emc6d102"; |
| 1533 | break; |
Jan Beulich | f065a93 | 2011-02-18 03:18:26 -0500 | [diff] [blame] | 1534 | case LM85_VERSTEP_EMC6D103_A0: |
| 1535 | case LM85_VERSTEP_EMC6D103_A1: |
| 1536 | type_name = "emc6d103"; |
| 1537 | break; |
Jan Beulich | f065a93 | 2011-02-18 03:18:26 -0500 | [diff] [blame] | 1538 | case LM85_VERSTEP_EMC6D103S: |
| 1539 | type_name = "emc6d103s"; |
| 1540 | break; |
Jean Delvare | d42a2eb | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
Jean Delvare | 590e853 | 2014-06-11 18:35:56 +0200 | [diff] [blame] | 1544 | if (!type_name) |
| 1545 | return -ENODEV; |
| 1546 | |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1547 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1549 | return 0; |
| 1550 | } |
| 1551 | |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1552 | static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id) |
Guenter Roeck | bc6db2b | 2011-02-25 08:26:47 -0800 | [diff] [blame] | 1553 | { |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1554 | struct device *dev = &client->dev; |
| 1555 | struct device *hwmon_dev; |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1556 | struct lm85_data *data; |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1557 | int idx = 0; |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1558 | |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1559 | data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL); |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1560 | if (!data) |
| 1561 | return -ENOMEM; |
| 1562 | |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1563 | data->client = client; |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1564 | data->type = id->driver_data; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1565 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1567 | /* Fill in the chip specific driver values */ |
| 1568 | switch (data->type) { |
| 1569 | case adm1027: |
| 1570 | case adt7463: |
Jean Delvare | fa7a579 | 2010-10-28 20:31:50 +0200 | [diff] [blame] | 1571 | case adt7468: |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1572 | case emc6d100: |
| 1573 | case emc6d102: |
Jan Beulich | f065a93 | 2011-02-18 03:18:26 -0500 | [diff] [blame] | 1574 | case emc6d103: |
Guenter Roeck | 06923f8 | 2011-02-19 08:27:47 -0800 | [diff] [blame] | 1575 | case emc6d103s: |
Jean Delvare | 67712d0 | 2008-10-17 17:51:14 +0200 | [diff] [blame] | 1576 | data->freq_map = adm1027_freq_map; |
| 1577 | break; |
| 1578 | default: |
| 1579 | data->freq_map = lm85_freq_map; |
| 1580 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | |
| 1582 | /* Set the VRM version */ |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 1583 | data->vrm = vid_which_vrm(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | |
| 1585 | /* Initialize the LM85 chip */ |
Jean Delvare | e89e22b | 2008-06-25 08:47:35 -0400 | [diff] [blame] | 1586 | lm85_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1588 | /* sysfs hooks */ |
| 1589 | data->groups[idx++] = &lm85_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
Guenter Roeck | 06923f8 | 2011-02-19 08:27:47 -0800 | [diff] [blame] | 1591 | /* minctl and temp_off exist on all chips except emc6d103s */ |
| 1592 | if (data->type != emc6d103s) { |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1593 | data->groups[idx++] = &lm85_group_minctl; |
| 1594 | data->groups[idx++] = &lm85_group_temp_off; |
Guenter Roeck | 06923f8 | 2011-02-19 08:27:47 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
Guenter Roeck | 09770b26 | 2012-01-14 20:47:36 -0800 | [diff] [blame] | 1597 | /* |
| 1598 | * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used |
| 1599 | * as a sixth digital VID input rather than an analog input. |
| 1600 | */ |
Guenter Roeck | de24880 | 2011-02-25 08:19:42 -0800 | [diff] [blame] | 1601 | if (data->type == adt7463 || data->type == adt7468) { |
| 1602 | u8 vid = lm85_read_value(client, LM85_REG_VID); |
| 1603 | if (vid & 0x80) |
| 1604 | data->has_vid5 = true; |
| 1605 | } |
| 1606 | |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1607 | if (!data->has_vid5) |
| 1608 | data->groups[idx++] = &lm85_group_in4; |
Jean Delvare | 6b9aad2 | 2007-07-05 20:37:21 +0200 | [diff] [blame] | 1609 | |
| 1610 | /* The EMC6D100 has 3 additional voltage inputs */ |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1611 | if (data->type == emc6d100) |
| 1612 | data->groups[idx++] = &lm85_group_in567; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 1613 | |
Axel Lin | 746f688 | 2014-07-23 12:29:11 +0800 | [diff] [blame] | 1614 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 1615 | data, data->groups); |
| 1616 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 1619 | static const struct i2c_device_id lm85_id[] = { |
| 1620 | { "adm1027", adm1027 }, |
| 1621 | { "adt7463", adt7463 }, |
| 1622 | { "adt7468", adt7468 }, |
| 1623 | { "lm85", lm85 }, |
| 1624 | { "lm85b", lm85 }, |
| 1625 | { "lm85c", lm85 }, |
| 1626 | { "emc6d100", emc6d100 }, |
| 1627 | { "emc6d101", emc6d100 }, |
| 1628 | { "emc6d102", emc6d102 }, |
| 1629 | { "emc6d103", emc6d103 }, |
| 1630 | { "emc6d103s", emc6d103s }, |
| 1631 | { } |
| 1632 | }; |
| 1633 | MODULE_DEVICE_TABLE(i2c, lm85_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 1635 | static struct i2c_driver lm85_driver = { |
| 1636 | .class = I2C_CLASS_HWMON, |
| 1637 | .driver = { |
| 1638 | .name = "lm85", |
| 1639 | }, |
| 1640 | .probe = lm85_probe, |
Axel Lin | 6fd5dd5 | 2014-07-23 12:27:39 +0800 | [diff] [blame] | 1641 | .id_table = lm85_id, |
| 1642 | .detect = lm85_detect, |
| 1643 | .address_list = normal_i2c, |
| 1644 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 1646 | module_i2c_driver(lm85_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | MODULE_LICENSE("GPL"); |
Jean Delvare | 1f44809 | 2008-04-29 14:03:37 +0200 | [diff] [blame] | 1649 | MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, " |
| 1650 | "Margit Schubert-While <margitsw@t-online.de>, " |
Jean Delvare | e89e22b | 2008-06-25 08:47:35 -0400 | [diff] [blame] | 1651 | "Justin Thiessen <jthiessen@penguincomputing.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | MODULE_DESCRIPTION("LM85-B, LM85-C driver"); |