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