Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 2 | * lm78.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 5 | * Copyright (c) 2007, 2011 Jean Delvare <jdelvare@suse.de> |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Joe Perches | ce47da7 | 2011-01-12 21:55:10 +0100 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/hwmon.h> |
Jean Delvare | 19f673e | 2005-07-31 22:12:09 +0200 | [diff] [blame] | 30 | #include <linux/hwmon-vid.h> |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 31 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 32 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 35 | #ifdef CONFIG_ISA |
| 36 | #include <linux/platform_device.h> |
| 37 | #include <linux/ioport.h> |
| 38 | #include <linux/io.h> |
| 39 | #endif |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 42 | static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, |
| 43 | 0x2e, 0x2f, I2C_CLIENT_END }; |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 44 | enum chips { lm78, lm79 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* Many LM78 constants specified below */ |
| 47 | |
| 48 | /* Length of ISA address segment */ |
| 49 | #define LM78_EXTENT 8 |
| 50 | |
| 51 | /* Where are the ISA address/data registers relative to the base address */ |
| 52 | #define LM78_ADDR_REG_OFFSET 5 |
| 53 | #define LM78_DATA_REG_OFFSET 6 |
| 54 | |
| 55 | /* The LM78 registers */ |
| 56 | #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2) |
| 57 | #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2) |
| 58 | #define LM78_REG_IN(nr) (0x20 + (nr)) |
| 59 | |
| 60 | #define LM78_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 61 | #define LM78_REG_FAN(nr) (0x28 + (nr)) |
| 62 | |
| 63 | #define LM78_REG_TEMP 0x27 |
| 64 | #define LM78_REG_TEMP_OVER 0x39 |
| 65 | #define LM78_REG_TEMP_HYST 0x3a |
| 66 | |
| 67 | #define LM78_REG_ALARM1 0x41 |
| 68 | #define LM78_REG_ALARM2 0x42 |
| 69 | |
| 70 | #define LM78_REG_VID_FANDIV 0x47 |
| 71 | |
| 72 | #define LM78_REG_CONFIG 0x40 |
| 73 | #define LM78_REG_CHIPID 0x49 |
| 74 | #define LM78_REG_I2C_ADDR 0x48 |
| 75 | |
| 76 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 77 | /* |
| 78 | * Conversions. Rounding and limit checking is only done on the TO_REG |
| 79 | * variants. |
| 80 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 82 | /* |
| 83 | * IN: mV (0V to 4.08V) |
| 84 | * REG: 16mV/bit |
| 85 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static inline u8 IN_TO_REG(unsigned long val) |
| 87 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 88 | unsigned long nval = clamp_val(val, 0, 4080); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | return (nval + 8) / 16; |
| 90 | } |
| 91 | #define IN_FROM_REG(val) ((val) * 16) |
| 92 | |
| 93 | static inline u8 FAN_TO_REG(long rpm, int div) |
| 94 | { |
| 95 | if (rpm <= 0) |
| 96 | return 255; |
Dan Carpenter | 3806b45 | 2013-12-12 08:05:33 +0100 | [diff] [blame] | 97 | if (rpm > 1350000) |
| 98 | return 1; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 99 | return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static inline int FAN_FROM_REG(u8 val, int div) |
| 103 | { |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 104 | return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 107 | /* |
| 108 | * TEMP: mC (-128C to +127C) |
| 109 | * REG: 1C/bit, two's complement |
| 110 | */ |
Guenter Roeck | 1074d68 | 2014-07-29 20:48:59 -0700 | [diff] [blame] | 111 | static inline s8 TEMP_TO_REG(long val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 113 | int nval = clamp_val(val, -128000, 127000) ; |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 114 | return nval < 0 ? (nval - 500) / 1000 : (nval + 500) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static inline int TEMP_FROM_REG(s8 val) |
| 118 | { |
| 119 | return val * 1000; |
| 120 | } |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #define DIV_FROM_REG(val) (1 << (val)) |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | struct lm78_data { |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 125 | struct i2c_client *client; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 126 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | enum chips type; |
| 128 | |
Jean Delvare | 6e1b502 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 129 | /* For ISA device only */ |
| 130 | const char *name; |
| 131 | int isa_addr; |
| 132 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 133 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | char valid; /* !=0 if following fields are valid */ |
| 135 | unsigned long last_updated; /* In jiffies */ |
| 136 | |
| 137 | u8 in[7]; /* Register value */ |
| 138 | u8 in_max[7]; /* Register value */ |
| 139 | u8 in_min[7]; /* Register value */ |
| 140 | u8 fan[3]; /* Register value */ |
| 141 | u8 fan_min[3]; /* Register value */ |
| 142 | s8 temp; /* Register value */ |
| 143 | s8 temp_over; /* Register value */ |
| 144 | s8 temp_hyst; /* Register value */ |
| 145 | u8 fan_div[3]; /* Register encoding, shifted right */ |
| 146 | u8 vid; /* Register encoding, combined */ |
| 147 | u16 alarms; /* Register encoding, combined */ |
| 148 | }; |
| 149 | |
| 150 | |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 151 | static int lm78_read_value(struct lm78_data *data, u8 reg); |
| 152 | static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | static struct lm78_data *lm78_update_device(struct device *dev); |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 154 | static void lm78_init_device(struct lm78_data *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* 7 Voltages */ |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 158 | static ssize_t show_in(struct device *dev, struct device_attribute *da, |
| 159 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 161 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | struct lm78_data *data = lm78_update_device(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 163 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 166 | static ssize_t show_in_min(struct device *dev, struct device_attribute *da, |
| 167 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 169 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | struct lm78_data *data = lm78_update_device(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 171 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 174 | static ssize_t show_in_max(struct device *dev, struct device_attribute *da, |
| 175 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 177 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | struct lm78_data *data = lm78_update_device(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 179 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 182 | static ssize_t set_in_min(struct device *dev, struct device_attribute *da, |
| 183 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 185 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 186 | struct lm78_data *data = dev_get_drvdata(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 187 | int nr = attr->index; |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 188 | unsigned long val; |
| 189 | int err; |
| 190 | |
| 191 | err = kstrtoul(buf, 10, &val); |
| 192 | if (err) |
| 193 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 195 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | data->in_min[nr] = IN_TO_REG(val); |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 197 | lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 198 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return count; |
| 200 | } |
| 201 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 202 | static ssize_t set_in_max(struct device *dev, struct device_attribute *da, |
| 203 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 205 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 206 | struct lm78_data *data = dev_get_drvdata(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 207 | int nr = attr->index; |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 208 | unsigned long val; |
| 209 | int err; |
| 210 | |
| 211 | err = kstrtoul(buf, 10, &val); |
| 212 | if (err) |
| 213 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 215 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | data->in_max[nr] = IN_TO_REG(val); |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 217 | lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 218 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return count; |
| 220 | } |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | #define show_in_offset(offset) \ |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 223 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 224 | show_in, NULL, offset); \ |
| 225 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 226 | show_in_min, set_in_min, offset); \ |
| 227 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 228 | show_in_max, set_in_max, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | show_in_offset(0); |
| 231 | show_in_offset(1); |
| 232 | show_in_offset(2); |
| 233 | show_in_offset(3); |
| 234 | show_in_offset(4); |
| 235 | show_in_offset(5); |
| 236 | show_in_offset(6); |
| 237 | |
| 238 | /* Temperature */ |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 239 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
| 240 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | struct lm78_data *data = lm78_update_device(dev); |
| 243 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp)); |
| 244 | } |
| 245 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 246 | static ssize_t show_temp_over(struct device *dev, struct device_attribute *da, |
| 247 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
| 249 | struct lm78_data *data = lm78_update_device(dev); |
| 250 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over)); |
| 251 | } |
| 252 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 253 | static ssize_t set_temp_over(struct device *dev, struct device_attribute *da, |
| 254 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 256 | struct lm78_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 257 | long val; |
| 258 | int err; |
| 259 | |
| 260 | err = kstrtol(buf, 10, &val); |
| 261 | if (err) |
| 262 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 264 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | data->temp_over = TEMP_TO_REG(val); |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 266 | lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 267 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return count; |
| 269 | } |
| 270 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 271 | static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da, |
| 272 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
| 274 | struct lm78_data *data = lm78_update_device(dev); |
| 275 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst)); |
| 276 | } |
| 277 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 278 | static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da, |
| 279 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 281 | struct lm78_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 282 | long val; |
| 283 | int err; |
| 284 | |
| 285 | err = kstrtol(buf, 10, &val); |
| 286 | if (err) |
| 287 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 289 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | data->temp_hyst = TEMP_TO_REG(val); |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 291 | lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 292 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return count; |
| 294 | } |
| 295 | |
| 296 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); |
| 297 | static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, |
| 298 | show_temp_over, set_temp_over); |
| 299 | static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, |
| 300 | show_temp_hyst, set_temp_hyst); |
| 301 | |
| 302 | /* 3 Fans */ |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 303 | static ssize_t show_fan(struct device *dev, struct device_attribute *da, |
| 304 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 306 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | struct lm78_data *data = lm78_update_device(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 308 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 310 | DIV_FROM_REG(data->fan_div[nr]))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 313 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *da, |
| 314 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 316 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | struct lm78_data *data = lm78_update_device(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 318 | int nr = attr->index; |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 319 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
| 320 | DIV_FROM_REG(data->fan_div[nr]))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 323 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *da, |
| 324 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 326 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 327 | struct lm78_data *data = dev_get_drvdata(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 328 | int nr = attr->index; |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 329 | unsigned long val; |
| 330 | int err; |
| 331 | |
| 332 | err = kstrtoul(buf, 10, &val); |
| 333 | if (err) |
| 334 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 336 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 338 | lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 339 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return count; |
| 341 | } |
| 342 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 343 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *da, |
| 344 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 346 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | struct lm78_data *data = lm78_update_device(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 348 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 351 | /* |
| 352 | * Note: we save and restore the fan minimum here, because its value is |
| 353 | * determined in part by the fan divisor. This follows the principle of |
| 354 | * least surprise; the user doesn't expect the fan minimum to change just |
| 355 | * because the divisor changed. |
| 356 | */ |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 357 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *da, |
| 358 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 360 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 361 | struct lm78_data *data = dev_get_drvdata(dev); |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 362 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | unsigned long min; |
| 364 | u8 reg; |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 365 | unsigned long val; |
| 366 | int err; |
| 367 | |
| 368 | err = kstrtoul(buf, 10, &val); |
| 369 | if (err) |
| 370 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 372 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | min = FAN_FROM_REG(data->fan_min[nr], |
| 374 | DIV_FROM_REG(data->fan_div[nr])); |
| 375 | |
| 376 | switch (val) { |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 377 | case 1: |
| 378 | data->fan_div[nr] = 0; |
| 379 | break; |
| 380 | case 2: |
| 381 | data->fan_div[nr] = 1; |
| 382 | break; |
| 383 | case 4: |
| 384 | data->fan_div[nr] = 2; |
| 385 | break; |
| 386 | case 8: |
| 387 | data->fan_div[nr] = 3; |
| 388 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | default: |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 390 | dev_err(dev, |
| 391 | "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", |
| 392 | val); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 393 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return -EINVAL; |
| 395 | } |
| 396 | |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 397 | reg = lm78_read_value(data, LM78_REG_VID_FANDIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | switch (nr) { |
| 399 | case 0: |
| 400 | reg = (reg & 0xcf) | (data->fan_div[nr] << 4); |
| 401 | break; |
| 402 | case 1: |
| 403 | reg = (reg & 0x3f) | (data->fan_div[nr] << 6); |
| 404 | break; |
| 405 | } |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 406 | lm78_write_value(data, LM78_REG_VID_FANDIV, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | data->fan_min[nr] = |
| 409 | FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 410 | lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 411 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | return count; |
| 414 | } |
| 415 | |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 416 | #define show_fan_offset(offset) \ |
| 417 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 418 | show_fan, NULL, offset - 1); \ |
| 419 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 420 | show_fan_min, set_fan_min, offset - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
| 422 | show_fan_offset(1); |
| 423 | show_fan_offset(2); |
| 424 | show_fan_offset(3); |
| 425 | |
| 426 | /* Fan 3 divisor is locked in H/W */ |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 427 | static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, |
| 428 | show_fan_div, set_fan_div, 0); |
| 429 | static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, |
| 430 | show_fan_div, set_fan_div, 1); |
| 431 | static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | /* VID */ |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 434 | static ssize_t show_vid(struct device *dev, struct device_attribute *da, |
| 435 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
| 437 | struct lm78_data *data = lm78_update_device(dev); |
Jean Delvare | d0d3cd6 | 2005-11-23 15:44:26 -0800 | [diff] [blame] | 438 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 441 | |
| 442 | /* Alarms */ |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 443 | static ssize_t show_alarms(struct device *dev, struct device_attribute *da, |
| 444 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
| 446 | struct lm78_data *data = lm78_update_device(dev); |
| 447 | return sprintf(buf, "%u\n", data->alarms); |
| 448 | } |
| 449 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 450 | |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 451 | static ssize_t show_alarm(struct device *dev, struct device_attribute *da, |
| 452 | char *buf) |
| 453 | { |
| 454 | struct lm78_data *data = lm78_update_device(dev); |
| 455 | int nr = to_sensor_dev_attr(da)->index; |
| 456 | return sprintf(buf, "%u\n", (data->alarms >> nr) & 1); |
| 457 | } |
| 458 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 459 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 460 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 461 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 462 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 463 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 464 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10); |
| 465 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 466 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 467 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 468 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 469 | |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 470 | static struct attribute *lm78_attrs[] = { |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 471 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 472 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 473 | &sensor_dev_attr_in0_max.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 474 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 475 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 476 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 477 | &sensor_dev_attr_in1_max.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 478 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 479 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 480 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 481 | &sensor_dev_attr_in2_max.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 482 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 483 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 484 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 485 | &sensor_dev_attr_in3_max.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 486 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 487 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 488 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 489 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 490 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 491 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 492 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 493 | &sensor_dev_attr_in5_max.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 494 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 495 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 496 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 497 | &sensor_dev_attr_in6_max.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 498 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 499 | &dev_attr_temp1_input.attr, |
| 500 | &dev_attr_temp1_max.attr, |
| 501 | &dev_attr_temp1_max_hyst.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 502 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 503 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 504 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 505 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 506 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 507 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 508 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 509 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 510 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 511 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 512 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 513 | &sensor_dev_attr_fan3_div.dev_attr.attr, |
Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 514 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 515 | &dev_attr_alarms.attr, |
| 516 | &dev_attr_cpu0_vid.attr, |
| 517 | |
| 518 | NULL |
| 519 | }; |
| 520 | |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 521 | ATTRIBUTE_GROUPS(lm78); |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 522 | |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 523 | /* |
| 524 | * ISA related code |
| 525 | */ |
| 526 | #ifdef CONFIG_ISA |
| 527 | |
| 528 | /* ISA device, if found */ |
| 529 | static struct platform_device *pdev; |
| 530 | |
| 531 | static unsigned short isa_address = 0x290; |
| 532 | |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 533 | static struct lm78_data *lm78_data_if_isa(void) |
| 534 | { |
| 535 | return pdev ? platform_get_drvdata(pdev) : NULL; |
| 536 | } |
| 537 | |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 538 | /* Returns 1 if the I2C chip appears to be an alias of the ISA chip */ |
| 539 | static int lm78_alias_detect(struct i2c_client *client, u8 chipid) |
| 540 | { |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 541 | struct lm78_data *isa; |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 542 | int i; |
| 543 | |
| 544 | if (!pdev) /* No ISA chip */ |
| 545 | return 0; |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 546 | isa = platform_get_drvdata(pdev); |
| 547 | |
| 548 | if (lm78_read_value(isa, LM78_REG_I2C_ADDR) != client->addr) |
| 549 | return 0; /* Address doesn't match */ |
| 550 | if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe)) |
| 551 | return 0; /* Chip type doesn't match */ |
| 552 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 553 | /* |
| 554 | * We compare all the limit registers, the config register and the |
| 555 | * interrupt mask registers |
| 556 | */ |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 557 | for (i = 0x2b; i <= 0x3d; i++) { |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 558 | if (lm78_read_value(isa, i) != |
| 559 | i2c_smbus_read_byte_data(client, i)) |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | if (lm78_read_value(isa, LM78_REG_CONFIG) != |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 563 | i2c_smbus_read_byte_data(client, LM78_REG_CONFIG)) |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 564 | return 0; |
| 565 | for (i = 0x43; i <= 0x46; i++) { |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 566 | if (lm78_read_value(isa, i) != |
| 567 | i2c_smbus_read_byte_data(client, i)) |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | return 1; |
| 572 | } |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 573 | #else /* !CONFIG_ISA */ |
| 574 | |
| 575 | static int lm78_alias_detect(struct i2c_client *client, u8 chipid) |
| 576 | { |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static struct lm78_data *lm78_data_if_isa(void) |
| 581 | { |
| 582 | return NULL; |
| 583 | } |
| 584 | #endif /* CONFIG_ISA */ |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 585 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 586 | static int lm78_i2c_detect(struct i2c_client *client, |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 587 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | { |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 589 | int i; |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 590 | struct lm78_data *isa = lm78_data_if_isa(); |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 591 | const char *client_name; |
| 592 | struct i2c_adapter *adapter = client->adapter; |
| 593 | int address = client->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 595 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 596 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 598 | /* |
| 599 | * We block updates of the ISA device to minimize the risk of |
| 600 | * concurrent access to the same LM78 chip through different |
| 601 | * interfaces. |
| 602 | */ |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 603 | if (isa) |
| 604 | mutex_lock(&isa->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 606 | if ((i2c_smbus_read_byte_data(client, LM78_REG_CONFIG) & 0x80) |
| 607 | || i2c_smbus_read_byte_data(client, LM78_REG_I2C_ADDR) != address) |
| 608 | goto err_nodev; |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 609 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 610 | /* Explicitly prevent the misdetection of Winbond chips */ |
| 611 | i = i2c_smbus_read_byte_data(client, 0x4f); |
| 612 | if (i == 0xa3 || i == 0x5c) |
| 613 | goto err_nodev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
| 615 | /* Determine the chip type. */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 616 | i = i2c_smbus_read_byte_data(client, LM78_REG_CHIPID); |
| 617 | if (i == 0x00 || i == 0x20 /* LM78 */ |
| 618 | || i == 0x40) /* LM78-J */ |
| 619 | client_name = "lm78"; |
| 620 | else if ((i & 0xfe) == 0xc0) |
| 621 | client_name = "lm79"; |
| 622 | else |
| 623 | goto err_nodev; |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 624 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 625 | if (lm78_alias_detect(client, i)) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 626 | dev_dbg(&adapter->dev, |
| 627 | "Device at 0x%02x appears to be the same as ISA device\n", |
| 628 | address); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 629 | goto err_nodev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 632 | if (isa) |
| 633 | mutex_unlock(&isa->update_lock); |
| 634 | |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 635 | strlcpy(info->type, client_name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 637 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 639 | err_nodev: |
| 640 | if (isa) |
| 641 | mutex_unlock(&isa->update_lock); |
| 642 | return -ENODEV; |
| 643 | } |
| 644 | |
| 645 | static int lm78_i2c_probe(struct i2c_client *client, |
| 646 | const struct i2c_device_id *id) |
| 647 | { |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 648 | struct device *dev = &client->dev; |
| 649 | struct device *hwmon_dev; |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 650 | struct lm78_data *data; |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 651 | |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 652 | data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL); |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 653 | if (!data) |
| 654 | return -ENOMEM; |
| 655 | |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 656 | data->client = client; |
| 657 | data->type = id->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | /* Initialize the LM78 chip */ |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 660 | lm78_init_device(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 662 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 663 | data, lm78_groups); |
| 664 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 665 | } |
| 666 | |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 667 | static const struct i2c_device_id lm78_i2c_id[] = { |
| 668 | { "lm78", lm78 }, |
| 669 | { "lm79", lm79 }, |
| 670 | { } |
| 671 | }; |
| 672 | MODULE_DEVICE_TABLE(i2c, lm78_i2c_id); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 673 | |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 674 | static struct i2c_driver lm78_driver = { |
| 675 | .class = I2C_CLASS_HWMON, |
| 676 | .driver = { |
| 677 | .name = "lm78", |
| 678 | }, |
| 679 | .probe = lm78_i2c_probe, |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 680 | .id_table = lm78_i2c_id, |
| 681 | .detect = lm78_i2c_detect, |
| 682 | .address_list = normal_i2c, |
| 683 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 685 | /* |
| 686 | * The SMBus locks itself, but ISA access must be locked explicitly! |
| 687 | * We don't want to lock the whole ISA bus, so we lock each client |
| 688 | * separately. |
| 689 | * We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, |
| 690 | * would slow down the LM78 access and should not be necessary. |
| 691 | */ |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 692 | static int lm78_read_value(struct lm78_data *data, u8 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 694 | struct i2c_client *client = data->client; |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 695 | |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 696 | #ifdef CONFIG_ISA |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 697 | if (!client) { /* ISA device */ |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 698 | int res; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 699 | mutex_lock(&data->lock); |
Jean Delvare | 6e1b502 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 700 | outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET); |
| 701 | res = inb_p(data->isa_addr + LM78_DATA_REG_OFFSET); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 702 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | return res; |
| 704 | } else |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 705 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | return i2c_smbus_read_byte_data(client, reg); |
| 707 | } |
| 708 | |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 709 | static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 711 | struct i2c_client *client = data->client; |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 712 | |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 713 | #ifdef CONFIG_ISA |
Jean Delvare | 0c6e973 | 2008-10-17 17:51:16 +0200 | [diff] [blame] | 714 | if (!client) { /* ISA device */ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 715 | mutex_lock(&data->lock); |
Jean Delvare | 6e1b502 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 716 | outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET); |
| 717 | outb_p(value, data->isa_addr + LM78_DATA_REG_OFFSET); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 718 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return 0; |
| 720 | } else |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 721 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | return i2c_smbus_write_byte_data(client, reg, value); |
| 723 | } |
| 724 | |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 725 | static void lm78_init_device(struct lm78_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 727 | u8 config; |
| 728 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
| 730 | /* Start monitoring */ |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 731 | config = lm78_read_value(data, LM78_REG_CONFIG); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 732 | if ((config & 0x09) != 0x01) |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 733 | lm78_write_value(data, LM78_REG_CONFIG, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | (config & 0xf7) | 0x01); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 735 | |
| 736 | /* A few vars need to be filled upon startup */ |
| 737 | for (i = 0; i < 3; i++) { |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 738 | data->fan_min[i] = lm78_read_value(data, |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 739 | LM78_REG_FAN_MIN(i)); |
| 740 | } |
| 741 | |
| 742 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static struct lm78_data *lm78_update_device(struct device *dev) |
| 746 | { |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 747 | struct lm78_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | int i; |
| 749 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 750 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 753 | || !data->valid) { |
| 754 | |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 755 | dev_dbg(dev, "Starting lm78 update\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
| 757 | for (i = 0; i <= 6; i++) { |
| 758 | data->in[i] = |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 759 | lm78_read_value(data, LM78_REG_IN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | data->in_min[i] = |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 761 | lm78_read_value(data, LM78_REG_IN_MIN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | data->in_max[i] = |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 763 | lm78_read_value(data, LM78_REG_IN_MAX(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | for (i = 0; i < 3; i++) { |
| 766 | data->fan[i] = |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 767 | lm78_read_value(data, LM78_REG_FAN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | data->fan_min[i] = |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 769 | lm78_read_value(data, LM78_REG_FAN_MIN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 771 | data->temp = lm78_read_value(data, LM78_REG_TEMP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | data->temp_over = |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 773 | lm78_read_value(data, LM78_REG_TEMP_OVER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | data->temp_hyst = |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 775 | lm78_read_value(data, LM78_REG_TEMP_HYST); |
| 776 | i = lm78_read_value(data, LM78_REG_VID_FANDIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | data->vid = i & 0x0f; |
| 778 | if (data->type == lm79) |
| 779 | data->vid |= |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 780 | (lm78_read_value(data, LM78_REG_CHIPID) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | 0x01) << 4; |
| 782 | else |
| 783 | data->vid |= 0x10; |
| 784 | data->fan_div[0] = (i >> 4) & 0x03; |
| 785 | data->fan_div[1] = i >> 6; |
Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 786 | data->alarms = lm78_read_value(data, LM78_REG_ALARM1) + |
| 787 | (lm78_read_value(data, LM78_REG_ALARM2) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | data->last_updated = jiffies; |
| 789 | data->valid = 1; |
| 790 | |
| 791 | data->fan_div[2] = 1; |
| 792 | } |
| 793 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 794 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
| 796 | return data; |
| 797 | } |
| 798 | |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 799 | #ifdef CONFIG_ISA |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 800 | static int lm78_isa_probe(struct platform_device *pdev) |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 801 | { |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 802 | struct device *dev = &pdev->dev; |
| 803 | struct device *hwmon_dev; |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 804 | struct lm78_data *data; |
| 805 | struct resource *res; |
| 806 | |
| 807 | /* Reserve the ISA region */ |
| 808 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 809 | if (!devm_request_region(dev, res->start + LM78_ADDR_REG_OFFSET, |
Guenter Roeck | 7b7bb90 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 810 | 2, "lm78")) |
| 811 | return -EBUSY; |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 812 | |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 813 | data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL); |
Guenter Roeck | 7b7bb90 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 814 | if (!data) |
| 815 | return -ENOMEM; |
| 816 | |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 817 | mutex_init(&data->lock); |
| 818 | data->isa_addr = res->start; |
| 819 | platform_set_drvdata(pdev, data); |
| 820 | |
| 821 | if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) { |
| 822 | data->type = lm79; |
| 823 | data->name = "lm79"; |
| 824 | } else { |
| 825 | data->type = lm78; |
| 826 | data->name = "lm78"; |
| 827 | } |
| 828 | |
| 829 | /* Initialize the LM78 chip */ |
| 830 | lm78_init_device(data); |
| 831 | |
Axel Lin | 8eb4061 | 2014-07-23 12:39:21 +0800 | [diff] [blame] | 832 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name, |
| 833 | data, lm78_groups); |
| 834 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static struct platform_driver lm78_isa_driver = { |
| 838 | .driver = { |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 839 | .name = "lm78", |
| 840 | }, |
| 841 | .probe = lm78_isa_probe, |
Jean Delvare | ed4cebd | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 842 | }; |
| 843 | |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 844 | /* return 1 if a supported chip is found, 0 otherwise */ |
| 845 | static int __init lm78_isa_found(unsigned short address) |
| 846 | { |
| 847 | int val, save, found = 0; |
Jean Delvare | 197027e | 2010-02-05 19:58:36 +0100 | [diff] [blame] | 848 | int port; |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 849 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 850 | /* |
| 851 | * Some boards declare base+0 to base+7 as a PNP device, some base+4 |
Jean Delvare | 197027e | 2010-02-05 19:58:36 +0100 | [diff] [blame] | 852 | * to base+7 and some base+5 to base+6. So we better request each port |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 853 | * individually for the probing phase. |
| 854 | */ |
Jean Delvare | 197027e | 2010-02-05 19:58:36 +0100 | [diff] [blame] | 855 | for (port = address; port < address + LM78_EXTENT; port++) { |
| 856 | if (!request_region(port, 1, "lm78")) { |
Joe Perches | ce47da7 | 2011-01-12 21:55:10 +0100 | [diff] [blame] | 857 | pr_debug("Failed to request port 0x%x\n", port); |
Jean Delvare | 197027e | 2010-02-05 19:58:36 +0100 | [diff] [blame] | 858 | goto release; |
| 859 | } |
Jean Delvare | 47c1553 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 860 | } |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 861 | |
| 862 | #define REALLY_SLOW_IO |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 863 | /* |
| 864 | * We need the timeouts for at least some LM78-like |
| 865 | * chips. But only if we read 'undefined' registers. |
| 866 | */ |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 867 | val = inb_p(address + 1); |
| 868 | if (inb_p(address + 2) != val |
| 869 | || inb_p(address + 3) != val |
| 870 | || inb_p(address + 7) != val) |
| 871 | goto release; |
| 872 | #undef REALLY_SLOW_IO |
| 873 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 874 | /* |
| 875 | * We should be able to change the 7 LSB of the address port. The |
| 876 | * MSB (busy flag) should be clear initially, set after the write. |
| 877 | */ |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 878 | save = inb_p(address + LM78_ADDR_REG_OFFSET); |
| 879 | if (save & 0x80) |
| 880 | goto release; |
| 881 | val = ~save & 0x7f; |
| 882 | outb_p(val, address + LM78_ADDR_REG_OFFSET); |
| 883 | if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) { |
| 884 | outb_p(save, address + LM78_ADDR_REG_OFFSET); |
| 885 | goto release; |
| 886 | } |
| 887 | |
| 888 | /* We found a device, now see if it could be an LM78 */ |
| 889 | outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET); |
| 890 | val = inb_p(address + LM78_DATA_REG_OFFSET); |
| 891 | if (val & 0x80) |
| 892 | goto release; |
| 893 | outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET); |
| 894 | val = inb_p(address + LM78_DATA_REG_OFFSET); |
| 895 | if (val < 0x03 || val > 0x77) /* Not a valid I2C address */ |
| 896 | goto release; |
| 897 | |
| 898 | /* The busy flag should be clear again */ |
| 899 | if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80) |
| 900 | goto release; |
| 901 | |
| 902 | /* Explicitly prevent the misdetection of Winbond chips */ |
| 903 | outb_p(0x4f, address + LM78_ADDR_REG_OFFSET); |
| 904 | val = inb_p(address + LM78_DATA_REG_OFFSET); |
| 905 | if (val == 0xa3 || val == 0x5c) |
| 906 | goto release; |
| 907 | |
| 908 | /* Explicitly prevent the misdetection of ITE chips */ |
| 909 | outb_p(0x58, address + LM78_ADDR_REG_OFFSET); |
| 910 | val = inb_p(address + LM78_DATA_REG_OFFSET); |
| 911 | if (val == 0x90) |
| 912 | goto release; |
| 913 | |
| 914 | /* Determine the chip type */ |
| 915 | outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET); |
| 916 | val = inb_p(address + LM78_DATA_REG_OFFSET); |
Hans de Goede | acf346a | 2007-07-24 23:36:00 +0200 | [diff] [blame] | 917 | if (val == 0x00 || val == 0x20 /* LM78 */ |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 918 | || val == 0x40 /* LM78-J */ |
| 919 | || (val & 0xfe) == 0xc0) /* LM79 */ |
| 920 | found = 1; |
| 921 | |
| 922 | if (found) |
Joe Perches | ce47da7 | 2011-01-12 21:55:10 +0100 | [diff] [blame] | 923 | pr_info("Found an %s chip at %#x\n", |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 924 | val & 0x80 ? "LM79" : "LM78", (int)address); |
| 925 | |
| 926 | release: |
Jean Delvare | 197027e | 2010-02-05 19:58:36 +0100 | [diff] [blame] | 927 | for (port--; port >= address; port--) |
| 928 | release_region(port, 1); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 929 | return found; |
| 930 | } |
| 931 | |
| 932 | static int __init lm78_isa_device_add(unsigned short address) |
| 933 | { |
| 934 | struct resource res = { |
| 935 | .start = address, |
Jean Delvare | 15bde2f | 2007-08-29 10:39:57 +0200 | [diff] [blame] | 936 | .end = address + LM78_EXTENT - 1, |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 937 | .name = "lm78", |
| 938 | .flags = IORESOURCE_IO, |
| 939 | }; |
| 940 | int err; |
| 941 | |
| 942 | pdev = platform_device_alloc("lm78", address); |
| 943 | if (!pdev) { |
| 944 | err = -ENOMEM; |
Joe Perches | ce47da7 | 2011-01-12 21:55:10 +0100 | [diff] [blame] | 945 | pr_err("Device allocation failed\n"); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 946 | goto exit; |
| 947 | } |
| 948 | |
| 949 | err = platform_device_add_resources(pdev, &res, 1); |
| 950 | if (err) { |
Joe Perches | ce47da7 | 2011-01-12 21:55:10 +0100 | [diff] [blame] | 951 | pr_err("Device resource addition failed (%d)\n", err); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 952 | goto exit_device_put; |
| 953 | } |
| 954 | |
| 955 | err = platform_device_add(pdev); |
| 956 | if (err) { |
Joe Perches | ce47da7 | 2011-01-12 21:55:10 +0100 | [diff] [blame] | 957 | pr_err("Device addition failed (%d)\n", err); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 958 | goto exit_device_put; |
| 959 | } |
| 960 | |
| 961 | return 0; |
| 962 | |
| 963 | exit_device_put: |
| 964 | platform_device_put(pdev); |
| 965 | exit: |
| 966 | pdev = NULL; |
| 967 | return err; |
| 968 | } |
| 969 | |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 970 | static int __init lm78_isa_register(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | { |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 972 | int res; |
| 973 | |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 974 | if (lm78_isa_found(isa_address)) { |
| 975 | res = platform_driver_register(&lm78_isa_driver); |
| 976 | if (res) |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 977 | goto exit; |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 978 | |
| 979 | /* Sets global pdev as a side effect */ |
| 980 | res = lm78_isa_device_add(isa_address); |
| 981 | if (res) |
| 982 | goto exit_unreg_isa_driver; |
| 983 | } |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 984 | |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 985 | return 0; |
| 986 | |
| 987 | exit_unreg_isa_driver: |
| 988 | platform_driver_unregister(&lm78_isa_driver); |
| 989 | exit: |
| 990 | return res; |
| 991 | } |
| 992 | |
| 993 | static void lm78_isa_unregister(void) |
| 994 | { |
| 995 | if (pdev) { |
| 996 | platform_device_unregister(pdev); |
| 997 | platform_driver_unregister(&lm78_isa_driver); |
| 998 | } |
| 999 | } |
| 1000 | #else /* !CONFIG_ISA */ |
| 1001 | |
| 1002 | static int __init lm78_isa_register(void) |
| 1003 | { |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | static void lm78_isa_unregister(void) |
| 1008 | { |
| 1009 | } |
| 1010 | #endif /* CONFIG_ISA */ |
| 1011 | |
| 1012 | static int __init sm_lm78_init(void) |
| 1013 | { |
| 1014 | int res; |
| 1015 | |
Guenter Roeck | 9b03079 | 2012-01-14 20:39:24 -0800 | [diff] [blame] | 1016 | /* |
| 1017 | * We register the ISA device first, so that we can skip the |
| 1018 | * registration of an I2C interface to the same device. |
| 1019 | */ |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 1020 | res = lm78_isa_register(); |
| 1021 | if (res) |
| 1022 | goto exit; |
| 1023 | |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 1024 | res = i2c_add_driver(&lm78_driver); |
| 1025 | if (res) |
| 1026 | goto exit_unreg_isa_device; |
| 1027 | |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1028 | return 0; |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 1029 | |
Jean Delvare | 18c73f9 | 2008-10-17 17:51:15 +0200 | [diff] [blame] | 1030 | exit_unreg_isa_device: |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 1031 | lm78_isa_unregister(); |
Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 1032 | exit: |
| 1033 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static void __exit sm_lm78_exit(void) |
| 1037 | { |
Jean Delvare | 90534c5 | 2011-07-25 21:46:11 +0200 | [diff] [blame] | 1038 | lm78_isa_unregister(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | i2c_del_driver(&lm78_driver); |
| 1040 | } |
| 1041 | |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 1042 | MODULE_AUTHOR("Frodo Looijaard, Jean Delvare <jdelvare@suse.de>"); |
Jean Delvare | 27fe048 | 2005-07-27 21:30:16 +0200 | [diff] [blame] | 1043 | MODULE_DESCRIPTION("LM78/LM79 driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | MODULE_LICENSE("GPL"); |
| 1045 | |
| 1046 | module_init(sm_lm78_init); |
| 1047 | module_exit(sm_lm78_exit); |