Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and |
| 5 | * Kyosti Malkki <kmalkki@cc.hut.fi> |
| 6 | * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 7 | * Jean Delvare <jdelvare@suse.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare |
| 24 | * and advice of Greg Kroah-Hartman. |
| 25 | * |
| 26 | * Notes about the port: |
| 27 | * Release 0x00 of the GL518SM chipset doesn't support reading of in0, |
| 28 | * in1 nor in2. The original driver had an ugly workaround to get them |
| 29 | * anyway (changing limits and watching alarms trigger and wear off). |
| 30 | * We did not keep that part of the original driver in the Linux 2.6 |
| 31 | * version, since it was making the driver significantly more complex |
| 32 | * with no real benefit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/module.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/jiffies.h> |
| 39 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 40 | #include <linux/hwmon.h> |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 41 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 42 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 43 | #include <linux/mutex.h> |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 44 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 47 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 49 | enum chips { gl518sm_r00, gl518sm_r80 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* Many GL518 constants specified below */ |
| 52 | |
| 53 | /* The GL518 registers */ |
| 54 | #define GL518_REG_CHIP_ID 0x00 |
| 55 | #define GL518_REG_REVISION 0x01 |
| 56 | #define GL518_REG_VENDOR_ID 0x02 |
| 57 | #define GL518_REG_CONF 0x03 |
| 58 | #define GL518_REG_TEMP_IN 0x04 |
| 59 | #define GL518_REG_TEMP_MAX 0x05 |
| 60 | #define GL518_REG_TEMP_HYST 0x06 |
| 61 | #define GL518_REG_FAN_COUNT 0x07 |
| 62 | #define GL518_REG_FAN_LIMIT 0x08 |
| 63 | #define GL518_REG_VIN1_LIMIT 0x09 |
| 64 | #define GL518_REG_VIN2_LIMIT 0x0a |
| 65 | #define GL518_REG_VIN3_LIMIT 0x0b |
| 66 | #define GL518_REG_VDD_LIMIT 0x0c |
| 67 | #define GL518_REG_VIN3 0x0d |
| 68 | #define GL518_REG_MISC 0x0f |
| 69 | #define GL518_REG_ALARM 0x10 |
| 70 | #define GL518_REG_MASK 0x11 |
| 71 | #define GL518_REG_INT 0x12 |
| 72 | #define GL518_REG_VIN2 0x13 |
| 73 | #define GL518_REG_VIN1 0x14 |
| 74 | #define GL518_REG_VDD 0x15 |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * Conversions. Rounding and limit checking is only done on the TO_REG |
| 79 | * variants. Note that you should be a bit careful with which arguments |
| 80 | * these macros are called: arguments may be evaluated more than once. |
| 81 | * Fixing this is just not worth it. |
| 82 | */ |
| 83 | |
| 84 | #define RAW_FROM_REG(val) val |
| 85 | |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 86 | #define BOOL_FROM_REG(val) ((val) ? 0 : 1) |
| 87 | #define BOOL_TO_REG(val) ((val) ? 0 : 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 89 | #define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 90 | (val) - 500 : \ |
| 91 | (val) + 500) / 1000) + 119), 0, 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #define TEMP_FROM_REG(val) (((val) - 119) * 1000) |
| 93 | |
| 94 | static inline u8 FAN_TO_REG(long rpm, int div) |
| 95 | { |
| 96 | long rpmdiv; |
| 97 | if (rpm == 0) |
| 98 | return 0; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 99 | rpmdiv = clamp_val(rpm, 1, 960000) * div; |
| 100 | return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 102 | #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) * (div)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 104 | #define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255) |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 105 | #define IN_FROM_REG(val) ((val) * 19) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 107 | #define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255) |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 108 | #define VDD_FROM_REG(val) (((val) * 95 + 2) / 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #define DIV_FROM_REG(val) (1 << (val)) |
| 111 | |
| 112 | #define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask) |
| 113 | #define BEEP_MASK_FROM_REG(val) ((val) & 0x7f) |
| 114 | |
| 115 | /* Each client has this additional data */ |
| 116 | struct gl518_data { |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 117 | struct i2c_client *client; |
| 118 | const struct attribute_group *groups[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | enum chips type; |
| 120 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 121 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | char valid; /* !=0 if following fields are valid */ |
| 123 | unsigned long last_updated; /* In jiffies */ |
| 124 | |
| 125 | u8 voltage_in[4]; /* Register values; [0] = VDD */ |
| 126 | u8 voltage_min[4]; /* Register values; [0] = VDD */ |
| 127 | u8 voltage_max[4]; /* Register values; [0] = VDD */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | u8 fan_in[2]; |
| 129 | u8 fan_min[2]; |
| 130 | u8 fan_div[2]; /* Register encoding, shifted right */ |
| 131 | u8 fan_auto1; /* Boolean */ |
| 132 | u8 temp_in; /* Register values */ |
| 133 | u8 temp_max; /* Register values */ |
| 134 | u8 temp_hyst; /* Register values */ |
| 135 | u8 alarms; /* Register value */ |
Jean Delvare | a5955ed | 2007-10-22 17:45:08 +0200 | [diff] [blame] | 136 | u8 alarm_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | u8 beep_mask; /* Register value */ |
| 138 | u8 beep_enable; /* Boolean */ |
| 139 | }; |
| 140 | |
Axel Lin | d4a972a | 2014-07-03 09:46:51 +0800 | [diff] [blame] | 141 | /* |
| 142 | * Registers 0x07 to 0x0c are word-sized, others are byte-sized |
| 143 | * GL518 uses a high-byte first convention, which is exactly opposite to |
| 144 | * the SMBus standard. |
| 145 | */ |
| 146 | static int gl518_read_value(struct i2c_client *client, u8 reg) |
| 147 | { |
| 148 | if ((reg >= 0x07) && (reg <= 0x0c)) |
| 149 | return i2c_smbus_read_word_swapped(client, reg); |
| 150 | else |
| 151 | return i2c_smbus_read_byte_data(client, reg); |
| 152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Axel Lin | d4a972a | 2014-07-03 09:46:51 +0800 | [diff] [blame] | 154 | static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value) |
| 155 | { |
| 156 | if ((reg >= 0x07) && (reg <= 0x0c)) |
| 157 | return i2c_smbus_write_word_swapped(client, reg, value); |
| 158 | else |
| 159 | return i2c_smbus_write_byte_data(client, reg, value); |
| 160 | } |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 161 | |
Axel Lin | d4a972a | 2014-07-03 09:46:51 +0800 | [diff] [blame] | 162 | static struct gl518_data *gl518_update_device(struct device *dev) |
| 163 | { |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 164 | struct gl518_data *data = dev_get_drvdata(dev); |
| 165 | struct i2c_client *client = data->client; |
Axel Lin | d4a972a | 2014-07-03 09:46:51 +0800 | [diff] [blame] | 166 | int val; |
| 167 | |
| 168 | mutex_lock(&data->update_lock); |
| 169 | |
| 170 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 171 | || !data->valid) { |
| 172 | dev_dbg(&client->dev, "Starting gl518 update\n"); |
| 173 | |
| 174 | data->alarms = gl518_read_value(client, GL518_REG_INT); |
| 175 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); |
| 176 | |
| 177 | val = gl518_read_value(client, GL518_REG_VDD_LIMIT); |
| 178 | data->voltage_min[0] = val & 0xff; |
| 179 | data->voltage_max[0] = (val >> 8) & 0xff; |
| 180 | val = gl518_read_value(client, GL518_REG_VIN1_LIMIT); |
| 181 | data->voltage_min[1] = val & 0xff; |
| 182 | data->voltage_max[1] = (val >> 8) & 0xff; |
| 183 | val = gl518_read_value(client, GL518_REG_VIN2_LIMIT); |
| 184 | data->voltage_min[2] = val & 0xff; |
| 185 | data->voltage_max[2] = (val >> 8) & 0xff; |
| 186 | val = gl518_read_value(client, GL518_REG_VIN3_LIMIT); |
| 187 | data->voltage_min[3] = val & 0xff; |
| 188 | data->voltage_max[3] = (val >> 8) & 0xff; |
| 189 | |
| 190 | val = gl518_read_value(client, GL518_REG_FAN_COUNT); |
| 191 | data->fan_in[0] = (val >> 8) & 0xff; |
| 192 | data->fan_in[1] = val & 0xff; |
| 193 | |
| 194 | val = gl518_read_value(client, GL518_REG_FAN_LIMIT); |
| 195 | data->fan_min[0] = (val >> 8) & 0xff; |
| 196 | data->fan_min[1] = val & 0xff; |
| 197 | |
| 198 | data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN); |
| 199 | data->temp_max = |
| 200 | gl518_read_value(client, GL518_REG_TEMP_MAX); |
| 201 | data->temp_hyst = |
| 202 | gl518_read_value(client, GL518_REG_TEMP_HYST); |
| 203 | |
| 204 | val = gl518_read_value(client, GL518_REG_MISC); |
| 205 | data->fan_div[0] = (val >> 6) & 0x03; |
| 206 | data->fan_div[1] = (val >> 4) & 0x03; |
| 207 | data->fan_auto1 = (val >> 3) & 0x01; |
| 208 | |
| 209 | data->alarms &= data->alarm_mask; |
| 210 | |
| 211 | val = gl518_read_value(client, GL518_REG_CONF); |
| 212 | data->beep_enable = (val >> 2) & 1; |
| 213 | |
| 214 | if (data->type != gl518sm_r00) { |
| 215 | data->voltage_in[0] = |
| 216 | gl518_read_value(client, GL518_REG_VDD); |
| 217 | data->voltage_in[1] = |
| 218 | gl518_read_value(client, GL518_REG_VIN1); |
| 219 | data->voltage_in[2] = |
| 220 | gl518_read_value(client, GL518_REG_VIN2); |
| 221 | } |
| 222 | data->voltage_in[3] = |
| 223 | gl518_read_value(client, GL518_REG_VIN3); |
| 224 | |
| 225 | data->last_updated = jiffies; |
| 226 | data->valid = 1; |
| 227 | } |
| 228 | |
| 229 | mutex_unlock(&data->update_lock); |
| 230 | |
| 231 | return data; |
| 232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * Sysfs stuff |
| 236 | */ |
| 237 | |
| 238 | #define show(type, suffix, value) \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 239 | static ssize_t show_##suffix(struct device *dev, \ |
| 240 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { \ |
| 242 | struct gl518_data *data = gl518_update_device(dev); \ |
| 243 | return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \ |
| 244 | } |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | show(TEMP, temp_input1, temp_in); |
| 247 | show(TEMP, temp_max1, temp_max); |
| 248 | show(TEMP, temp_hyst1, temp_hyst); |
| 249 | show(BOOL, fan_auto1, fan_auto1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | show(VDD, in_input0, voltage_in[0]); |
| 251 | show(IN, in_input1, voltage_in[1]); |
| 252 | show(IN, in_input2, voltage_in[2]); |
| 253 | show(IN, in_input3, voltage_in[3]); |
| 254 | show(VDD, in_min0, voltage_min[0]); |
| 255 | show(IN, in_min1, voltage_min[1]); |
| 256 | show(IN, in_min2, voltage_min[2]); |
| 257 | show(IN, in_min3, voltage_min[3]); |
| 258 | show(VDD, in_max0, voltage_max[0]); |
| 259 | show(IN, in_max1, voltage_max[1]); |
| 260 | show(IN, in_max2, voltage_max[2]); |
| 261 | show(IN, in_max3, voltage_max[3]); |
| 262 | show(RAW, alarms, alarms); |
| 263 | show(BOOL, beep_enable, beep_enable); |
| 264 | show(BEEP_MASK, beep_mask, beep_mask); |
| 265 | |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 266 | static ssize_t show_fan_input(struct device *dev, |
| 267 | struct device_attribute *attr, char *buf) |
| 268 | { |
| 269 | int nr = to_sensor_dev_attr(attr)->index; |
| 270 | struct gl518_data *data = gl518_update_device(dev); |
| 271 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr], |
| 272 | DIV_FROM_REG(data->fan_div[nr]))); |
| 273 | } |
| 274 | |
| 275 | static ssize_t show_fan_min(struct device *dev, |
| 276 | struct device_attribute *attr, char *buf) |
| 277 | { |
| 278 | int nr = to_sensor_dev_attr(attr)->index; |
| 279 | struct gl518_data *data = gl518_update_device(dev); |
| 280 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
| 281 | DIV_FROM_REG(data->fan_div[nr]))); |
| 282 | } |
| 283 | |
| 284 | static ssize_t show_fan_div(struct device *dev, |
| 285 | struct device_attribute *attr, char *buf) |
| 286 | { |
| 287 | int nr = to_sensor_dev_attr(attr)->index; |
| 288 | struct gl518_data *data = gl518_update_device(dev); |
| 289 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
| 290 | } |
| 291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | #define set(type, suffix, value, reg) \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 293 | static ssize_t set_##suffix(struct device *dev, \ |
| 294 | struct device_attribute *attr, \ |
| 295 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { \ |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 297 | struct gl518_data *data = dev_get_drvdata(dev); \ |
| 298 | struct i2c_client *client = data->client; \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 299 | long val; \ |
| 300 | int err = kstrtol(buf, 10, &val); \ |
| 301 | if (err) \ |
| 302 | return err; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 304 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | data->value = type##_TO_REG(val); \ |
| 306 | gl518_write_value(client, reg, data->value); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 307 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return count; \ |
| 309 | } |
| 310 | |
| 311 | #define set_bits(type, suffix, value, reg, mask, shift) \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 312 | static ssize_t set_##suffix(struct device *dev, \ |
| 313 | struct device_attribute *attr, \ |
| 314 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { \ |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 316 | struct gl518_data *data = dev_get_drvdata(dev); \ |
| 317 | struct i2c_client *client = data->client; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | int regvalue; \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 319 | unsigned long val; \ |
| 320 | int err = kstrtoul(buf, 10, &val); \ |
| 321 | if (err) \ |
| 322 | return err; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 324 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | regvalue = gl518_read_value(client, reg); \ |
| 326 | data->value = type##_TO_REG(val); \ |
| 327 | regvalue = (regvalue & ~mask) | (data->value << shift); \ |
| 328 | gl518_write_value(client, reg, regvalue); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 329 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return count; \ |
| 331 | } |
| 332 | |
| 333 | #define set_low(type, suffix, value, reg) \ |
| 334 | set_bits(type, suffix, value, reg, 0x00ff, 0) |
| 335 | #define set_high(type, suffix, value, reg) \ |
| 336 | set_bits(type, suffix, value, reg, 0xff00, 8) |
| 337 | |
| 338 | set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX); |
| 339 | set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST); |
| 340 | set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT); |
| 342 | set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT); |
| 343 | set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT); |
| 344 | set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT); |
| 345 | set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT); |
| 346 | set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT); |
| 347 | set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT); |
| 348 | set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT); |
| 349 | set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2); |
| 350 | set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM); |
| 351 | |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 352 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 353 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 355 | struct gl518_data *data = dev_get_drvdata(dev); |
| 356 | struct i2c_client *client = data->client; |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 357 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | int regvalue; |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 359 | unsigned long val; |
| 360 | int err; |
| 361 | |
| 362 | err = kstrtoul(buf, 10, &val); |
| 363 | if (err) |
| 364 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 366 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 368 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
| 369 | regvalue = (regvalue & (0xff << (8 * nr))) |
| 370 | | (data->fan_min[nr] << (8 * (1 - nr))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); |
| 372 | |
| 373 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 374 | if (data->fan_min[nr] == 0) |
| 375 | data->alarm_mask &= ~(0x20 << nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | else |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 377 | data->alarm_mask |= (0x20 << nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | data->beep_mask &= data->alarm_mask; |
| 379 | gl518_write_value(client, GL518_REG_ALARM, data->beep_mask); |
| 380 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 381 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return count; |
| 383 | } |
| 384 | |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 385 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
| 386 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 388 | struct gl518_data *data = dev_get_drvdata(dev); |
| 389 | struct i2c_client *client = data->client; |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 390 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | int regvalue; |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 392 | unsigned long val; |
| 393 | int err; |
| 394 | |
| 395 | err = kstrtoul(buf, 10, &val); |
| 396 | if (err) |
| 397 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Jean Delvare | 21df67b | 2007-10-22 17:47:58 +0200 | [diff] [blame] | 399 | switch (val) { |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 400 | case 1: |
| 401 | val = 0; |
| 402 | break; |
| 403 | case 2: |
| 404 | val = 1; |
| 405 | break; |
| 406 | case 4: |
| 407 | val = 2; |
| 408 | break; |
| 409 | case 8: |
| 410 | val = 3; |
| 411 | break; |
Jean Delvare | 21df67b | 2007-10-22 17:47:58 +0200 | [diff] [blame] | 412 | default: |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 413 | dev_err(dev, |
| 414 | "Invalid fan clock divider %lu, choose one of 1, 2, 4 or 8\n", |
| 415 | val); |
Jean Delvare | 21df67b | 2007-10-22 17:47:58 +0200 | [diff] [blame] | 416 | return -EINVAL; |
| 417 | } |
| 418 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 419 | mutex_lock(&data->update_lock); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 420 | regvalue = gl518_read_value(client, GL518_REG_MISC); |
Jean Delvare | 21df67b | 2007-10-22 17:47:58 +0200 | [diff] [blame] | 421 | data->fan_div[nr] = val; |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 422 | regvalue = (regvalue & ~(0xc0 >> (2 * nr))) |
| 423 | | (data->fan_div[nr] << (6 - 2 * nr)); |
| 424 | gl518_write_value(client, GL518_REG_MISC, regvalue); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 425 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return count; |
| 427 | } |
| 428 | |
| 429 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL); |
| 430 | static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1); |
| 431 | static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO, |
| 432 | show_temp_hyst1, set_temp_hyst1); |
| 433 | static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 434 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0); |
| 435 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1); |
| 436 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, |
| 437 | show_fan_min, set_fan_min, 0); |
| 438 | static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, |
| 439 | show_fan_min, set_fan_min, 1); |
| 440 | static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, |
| 441 | show_fan_div, set_fan_div, 0); |
| 442 | static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, |
| 443 | show_fan_div, set_fan_div, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL); |
| 445 | static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL); |
| 446 | static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL); |
| 447 | static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL); |
| 448 | static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0); |
| 449 | static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1); |
| 450 | static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2); |
| 451 | static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3); |
| 452 | static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0); |
| 453 | static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1); |
| 454 | static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2); |
| 455 | static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3); |
| 456 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 457 | static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO, |
| 458 | show_beep_enable, set_beep_enable); |
| 459 | static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO, |
| 460 | show_beep_mask, set_beep_mask); |
| 461 | |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 462 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 463 | char *buf) |
| 464 | { |
| 465 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 466 | struct gl518_data *data = gl518_update_device(dev); |
| 467 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 468 | } |
| 469 | |
| 470 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 471 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 472 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 473 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 474 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 475 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 476 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 477 | |
| 478 | static ssize_t show_beep(struct device *dev, struct device_attribute *attr, |
| 479 | char *buf) |
| 480 | { |
| 481 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 482 | struct gl518_data *data = gl518_update_device(dev); |
| 483 | return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1); |
| 484 | } |
| 485 | |
| 486 | static ssize_t set_beep(struct device *dev, struct device_attribute *attr, |
| 487 | const char *buf, size_t count) |
| 488 | { |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 489 | struct gl518_data *data = dev_get_drvdata(dev); |
| 490 | struct i2c_client *client = data->client; |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 491 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 492 | unsigned long bit; |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 493 | int err; |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 494 | |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 495 | err = kstrtoul(buf, 10, &bit); |
| 496 | if (err) |
| 497 | return err; |
| 498 | |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 499 | if (bit & ~1) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | mutex_lock(&data->update_lock); |
| 503 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); |
| 504 | if (bit) |
| 505 | data->beep_mask |= (1 << bitnr); |
| 506 | else |
| 507 | data->beep_mask &= ~(1 << bitnr); |
| 508 | gl518_write_value(client, GL518_REG_ALARM, data->beep_mask); |
| 509 | mutex_unlock(&data->update_lock); |
| 510 | return count; |
| 511 | } |
| 512 | |
| 513 | static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0); |
| 514 | static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1); |
| 515 | static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2); |
| 516 | static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3); |
| 517 | static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4); |
| 518 | static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5); |
| 519 | static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6); |
| 520 | |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 521 | static struct attribute *gl518_attributes[] = { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 522 | &dev_attr_in3_input.attr, |
| 523 | &dev_attr_in0_min.attr, |
| 524 | &dev_attr_in1_min.attr, |
| 525 | &dev_attr_in2_min.attr, |
| 526 | &dev_attr_in3_min.attr, |
| 527 | &dev_attr_in0_max.attr, |
| 528 | &dev_attr_in1_max.attr, |
| 529 | &dev_attr_in2_max.attr, |
| 530 | &dev_attr_in3_max.attr, |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 531 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 532 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 533 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 534 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 535 | &sensor_dev_attr_in0_beep.dev_attr.attr, |
| 536 | &sensor_dev_attr_in1_beep.dev_attr.attr, |
| 537 | &sensor_dev_attr_in2_beep.dev_attr.attr, |
| 538 | &sensor_dev_attr_in3_beep.dev_attr.attr, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 539 | |
| 540 | &dev_attr_fan1_auto.attr, |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 541 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 542 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 543 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 544 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 545 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 546 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 547 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 548 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 549 | &sensor_dev_attr_fan1_beep.dev_attr.attr, |
| 550 | &sensor_dev_attr_fan2_beep.dev_attr.attr, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 551 | |
| 552 | &dev_attr_temp1_input.attr, |
| 553 | &dev_attr_temp1_max.attr, |
| 554 | &dev_attr_temp1_max_hyst.attr, |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 555 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 556 | &sensor_dev_attr_temp1_beep.dev_attr.attr, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 557 | |
| 558 | &dev_attr_alarms.attr, |
| 559 | &dev_attr_beep_enable.attr, |
| 560 | &dev_attr_beep_mask.attr, |
| 561 | NULL |
| 562 | }; |
| 563 | |
| 564 | static const struct attribute_group gl518_group = { |
| 565 | .attrs = gl518_attributes, |
| 566 | }; |
| 567 | |
Jean Delvare | 5314f5c | 2007-10-22 17:46:17 +0200 | [diff] [blame] | 568 | static struct attribute *gl518_attributes_r80[] = { |
| 569 | &dev_attr_in0_input.attr, |
| 570 | &dev_attr_in1_input.attr, |
| 571 | &dev_attr_in2_input.attr, |
| 572 | NULL |
| 573 | }; |
| 574 | |
| 575 | static const struct attribute_group gl518_group_r80 = { |
| 576 | .attrs = gl518_attributes_r80, |
| 577 | }; |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* |
| 580 | * Real code |
| 581 | */ |
| 582 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 583 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 584 | static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 586 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 587 | int rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 590 | I2C_FUNC_SMBUS_WORD_DATA)) |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 591 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
| 593 | /* Now, we do the remaining detection. */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 594 | if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80) |
| 595 | || (gl518_read_value(client, GL518_REG_CONF) & 0x80)) |
| 596 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | /* Determine the chip type. */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 599 | rev = gl518_read_value(client, GL518_REG_REVISION); |
| 600 | if (rev != 0x00 && rev != 0x80) |
| 601 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 603 | strlcpy(info->type, "gl518sm", I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
Axel Lin | d4a972a | 2014-07-03 09:46:51 +0800 | [diff] [blame] | 608 | /* |
| 609 | * Called when we have found a new GL518SM. |
| 610 | * Note that we preserve D4:NoFan2 and D2:beep_enable. |
| 611 | */ |
| 612 | static void gl518_init_client(struct i2c_client *client) |
| 613 | { |
| 614 | /* Make sure we leave D7:Reset untouched */ |
| 615 | u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f; |
| 616 | |
| 617 | /* Comparator mode (D3=0), standby mode (D6=0) */ |
| 618 | gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37)); |
| 619 | |
| 620 | /* Never interrupts */ |
| 621 | gl518_write_value(client, GL518_REG_MASK, 0x00); |
| 622 | |
| 623 | /* Clear status register (D5=1), start (D6=1) */ |
| 624 | gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue); |
| 625 | gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue); |
| 626 | } |
| 627 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 628 | static int gl518_probe(struct i2c_client *client, |
| 629 | const struct i2c_device_id *id) |
| 630 | { |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 631 | struct device *dev = &client->dev; |
| 632 | struct device *hwmon_dev; |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 633 | struct gl518_data *data; |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 634 | int revision; |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 635 | |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 636 | data = devm_kzalloc(dev, sizeof(struct gl518_data), GFP_KERNEL); |
Guenter Roeck | bba636d | 2012-06-02 09:58:06 -0700 | [diff] [blame] | 637 | if (!data) |
| 638 | return -ENOMEM; |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 639 | |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 640 | data->client = client; |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 641 | revision = gl518_read_value(client, GL518_REG_REVISION); |
| 642 | data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00; |
| 643 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
| 645 | /* Initialize the GL518SM chip */ |
| 646 | data->alarm_mask = 0xff; |
Jean Delvare | a5955ed | 2007-10-22 17:45:08 +0200 | [diff] [blame] | 647 | gl518_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 649 | /* sysfs hooks */ |
| 650 | data->groups[0] = &gl518_group; |
Jean Delvare | 5314f5c | 2007-10-22 17:46:17 +0200 | [diff] [blame] | 651 | if (data->type == gl518sm_r80) |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 652 | data->groups[1] = &gl518_group_r80; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Axel Lin | 18faf5b | 2014-07-03 09:48:04 +0800 | [diff] [blame] | 654 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 655 | data, data->groups); |
| 656 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Axel Lin | d4a972a | 2014-07-03 09:46:51 +0800 | [diff] [blame] | 659 | static const struct i2c_device_id gl518_id[] = { |
| 660 | { "gl518sm", 0 }, |
| 661 | { } |
| 662 | }; |
| 663 | MODULE_DEVICE_TABLE(i2c, gl518_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Axel Lin | d4a972a | 2014-07-03 09:46:51 +0800 | [diff] [blame] | 665 | static struct i2c_driver gl518_driver = { |
| 666 | .class = I2C_CLASS_HWMON, |
| 667 | .driver = { |
| 668 | .name = "gl518sm", |
| 669 | }, |
| 670 | .probe = gl518_probe, |
Axel Lin | d4a972a | 2014-07-03 09:46:51 +0800 | [diff] [blame] | 671 | .id_table = gl518_id, |
| 672 | .detect = gl518_detect, |
| 673 | .address_list = normal_i2c, |
| 674 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 676 | module_i2c_driver(gl518_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " |
| 679 | "Kyosti Malkki <kmalkki@cc.hut.fi> and " |
| 680 | "Hong-Gunn Chew <hglinux@gunnet.org>"); |
| 681 | MODULE_DESCRIPTION("GL518SM driver"); |
| 682 | MODULE_LICENSE("GPL"); |