Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 2 | * asb100.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * |
| 5 | * Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com> |
| 6 | * |
| 7 | * (derived from w83781d.c) |
| 8 | * |
| 9 | * Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>, |
| 10 | * Philip Edelbrock <phil@netroedge.com>, and |
| 11 | * Mark Studebaker <mdsxyz123@yahoo.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 29 | * This driver supports the hardware sensor chips: Asus ASB100 and |
| 30 | * ASB100-A "BACH". |
| 31 | * |
| 32 | * ASB100-A supports pwm1, while plain ASB100 does not. There is no known |
| 33 | * way for the driver to tell which one is there. |
| 34 | * |
Jean Delvare | 4101ece | 2012-11-05 21:54:40 +0100 | [diff] [blame] | 35 | * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 36 | * asb100 7 3 1 4 0x31 0x0694 yes no |
| 37 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Joe Perches | 4d630e2 | 2010-10-20 06:51:29 +0000 | [diff] [blame] | 39 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/module.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 44 | #include <linux/hwmon.h> |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 45 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 46 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 47 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/init.h> |
Dominik Hackl | ff32409 | 2005-05-16 18:12:18 +0200 | [diff] [blame] | 49 | #include <linux/jiffies.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 50 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "lm75.h" |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* I2C addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 54 | static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Jean Delvare | 3aed198 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 56 | static unsigned short force_subclients[4]; |
| 57 | module_param_array(force_subclients, short, NULL, 0); |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 58 | MODULE_PARM_DESC(force_subclients, |
| 59 | "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* Voltage IN registers 0-6 */ |
| 62 | #define ASB100_REG_IN(nr) (0x20 + (nr)) |
| 63 | #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2)) |
| 64 | #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2)) |
| 65 | |
| 66 | /* FAN IN registers 1-3 */ |
| 67 | #define ASB100_REG_FAN(nr) (0x28 + (nr)) |
| 68 | #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 69 | |
| 70 | /* TEMPERATURE registers 1-4 */ |
| 71 | static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17}; |
| 72 | static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18}; |
| 73 | static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19}; |
| 74 | |
| 75 | #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr]) |
| 76 | #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr]) |
| 77 | #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr]) |
| 78 | |
| 79 | #define ASB100_REG_TEMP2_CONFIG 0x0152 |
| 80 | #define ASB100_REG_TEMP3_CONFIG 0x0252 |
| 81 | |
| 82 | |
| 83 | #define ASB100_REG_CONFIG 0x40 |
| 84 | #define ASB100_REG_ALARM1 0x41 |
| 85 | #define ASB100_REG_ALARM2 0x42 |
| 86 | #define ASB100_REG_SMIM1 0x43 |
| 87 | #define ASB100_REG_SMIM2 0x44 |
| 88 | #define ASB100_REG_VID_FANDIV 0x47 |
| 89 | #define ASB100_REG_I2C_ADDR 0x48 |
| 90 | #define ASB100_REG_CHIPID 0x49 |
| 91 | #define ASB100_REG_I2C_SUBADDR 0x4a |
| 92 | #define ASB100_REG_PIN 0x4b |
| 93 | #define ASB100_REG_IRQ 0x4c |
| 94 | #define ASB100_REG_BANK 0x4e |
| 95 | #define ASB100_REG_CHIPMAN 0x4f |
| 96 | |
| 97 | #define ASB100_REG_WCHIPID 0x58 |
| 98 | |
| 99 | /* bit 7 -> enable, bits 0-3 -> duty cycle */ |
| 100 | #define ASB100_REG_PWM1 0x59 |
| 101 | |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 102 | /* |
| 103 | * CONVERSIONS |
| 104 | * Rounding and limit checking is only done on the TO_REG variants. |
| 105 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* These constants are a guess, consistent w/ w83781d */ |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 108 | #define ASB100_IN_MIN 0 |
| 109 | #define ASB100_IN_MAX 4080 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 111 | /* |
| 112 | * IN: 1/1000 V (0V to 4.08V) |
| 113 | * REG: 16mV/bit |
| 114 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | static u8 IN_TO_REG(unsigned val) |
| 116 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 117 | unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return (nval + 8) / 16; |
| 119 | } |
| 120 | |
| 121 | static unsigned IN_FROM_REG(u8 reg) |
| 122 | { |
| 123 | return reg * 16; |
| 124 | } |
| 125 | |
| 126 | static u8 FAN_TO_REG(long rpm, int div) |
| 127 | { |
| 128 | if (rpm == -1) |
| 129 | return 0; |
| 130 | if (rpm == 0) |
| 131 | return 255; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 132 | rpm = clamp_val(rpm, 1, 1000000); |
| 133 | return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static int FAN_FROM_REG(u8 val, int div) |
| 137 | { |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 138 | return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* These constants are a guess, consistent w/ w83781d */ |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 142 | #define ASB100_TEMP_MIN -128000 |
| 143 | #define ASB100_TEMP_MAX 127000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 145 | /* |
| 146 | * TEMP: 0.001C/bit (-128C to +127C) |
| 147 | * REG: 1C/bit, two's complement |
| 148 | */ |
Christian Hohnstaedt | 5bfedac | 2007-08-16 11:40:10 +0200 | [diff] [blame] | 149 | static u8 TEMP_TO_REG(long temp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 151 | int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX); |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 152 | ntemp += (ntemp < 0 ? -500 : 500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return (u8)(ntemp / 1000); |
| 154 | } |
| 155 | |
| 156 | static int TEMP_FROM_REG(u8 reg) |
| 157 | { |
| 158 | return (s8)reg * 1000; |
| 159 | } |
| 160 | |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 161 | /* |
| 162 | * PWM: 0 - 255 per sensors documentation |
| 163 | * REG: (6.25% duty cycle per bit) |
| 164 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | static u8 ASB100_PWM_TO_REG(int pwm) |
| 166 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 167 | pwm = clamp_val(pwm, 0, 255); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return (u8)(pwm / 16); |
| 169 | } |
| 170 | |
| 171 | static int ASB100_PWM_FROM_REG(u8 reg) |
| 172 | { |
| 173 | return reg * 16; |
| 174 | } |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | #define DIV_FROM_REG(val) (1 << (val)) |
| 177 | |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 178 | /* |
| 179 | * FAN DIV: 1, 2, 4, or 8 (defaults to 2) |
| 180 | * REG: 0, 1, 2, or 3 (respectively) (defaults to 1) |
| 181 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | static u8 DIV_TO_REG(long val) |
| 183 | { |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 184 | return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 187 | /* |
| 188 | * For each registered client, we need to keep some data in memory. That |
| 189 | * data is pointed to by client->data. The structure itself is |
| 190 | * dynamically allocated, at the same time the client itself is allocated. |
| 191 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | struct asb100_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 193 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 194 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 196 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | unsigned long last_updated; /* In jiffies */ |
| 198 | |
| 199 | /* array of 2 pointers to subclients */ |
| 200 | struct i2c_client *lm75[2]; |
| 201 | |
| 202 | char valid; /* !=0 if following fields are valid */ |
| 203 | u8 in[7]; /* Register value */ |
| 204 | u8 in_max[7]; /* Register value */ |
| 205 | u8 in_min[7]; /* Register value */ |
| 206 | u8 fan[3]; /* Register value */ |
| 207 | u8 fan_min[3]; /* Register value */ |
| 208 | u16 temp[4]; /* Register value (0 and 3 are u8 only) */ |
| 209 | u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */ |
| 210 | u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */ |
| 211 | u8 fan_div[3]; /* Register encoding, right justified */ |
| 212 | u8 pwm; /* Register encoding */ |
| 213 | u8 vid; /* Register encoding, combined */ |
| 214 | u32 alarms; /* Register encoding, combined */ |
| 215 | u8 vrm; |
| 216 | }; |
| 217 | |
| 218 | static int asb100_read_value(struct i2c_client *client, u16 reg); |
| 219 | static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val); |
| 220 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 221 | static int asb100_probe(struct i2c_client *client, |
| 222 | const struct i2c_device_id *id); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 223 | static int asb100_detect(struct i2c_client *client, |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 224 | struct i2c_board_info *info); |
| 225 | static int asb100_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | static struct asb100_data *asb100_update_device(struct device *dev); |
| 227 | static void asb100_init_client(struct i2c_client *client); |
| 228 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 229 | static const struct i2c_device_id asb100_id[] = { |
Jean Delvare | 1f86df4 | 2009-12-14 21:17:26 +0100 | [diff] [blame] | 230 | { "asb100", 0 }, |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 231 | { } |
| 232 | }; |
| 233 | MODULE_DEVICE_TABLE(i2c, asb100_id); |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | static struct i2c_driver asb100_driver = { |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 236 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 237 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 238 | .name = "asb100", |
| 239 | }, |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 240 | .probe = asb100_probe, |
| 241 | .remove = asb100_remove, |
| 242 | .id_table = asb100_id, |
| 243 | .detect = asb100_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 244 | .address_list = normal_i2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | /* 7 Voltages */ |
| 248 | #define show_in_reg(reg) \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 249 | static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ |
| 250 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 252 | int nr = to_sensor_dev_attr(attr)->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | struct asb100_data *data = asb100_update_device(dev); \ |
| 254 | return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \ |
| 255 | } |
| 256 | |
| 257 | show_in_reg(in) |
| 258 | show_in_reg(in_min) |
| 259 | show_in_reg(in_max) |
| 260 | |
| 261 | #define set_in_reg(REG, reg) \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 262 | static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \ |
| 263 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 265 | int nr = to_sensor_dev_attr(attr)->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | struct i2c_client *client = to_i2c_client(dev); \ |
| 267 | struct asb100_data *data = i2c_get_clientdata(client); \ |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 268 | unsigned long val; \ |
| 269 | int err = kstrtoul(buf, 10, &val); \ |
| 270 | if (err) \ |
| 271 | return err; \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 272 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | data->in_##reg[nr] = IN_TO_REG(val); \ |
| 274 | asb100_write_value(client, ASB100_REG_IN_##REG(nr), \ |
| 275 | data->in_##reg[nr]); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 276 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return count; \ |
| 278 | } |
| 279 | |
| 280 | set_in_reg(MIN, min) |
| 281 | set_in_reg(MAX, max) |
| 282 | |
| 283 | #define sysfs_in(offset) \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 284 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 285 | show_in, NULL, offset); \ |
| 286 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 287 | show_in_min, set_in_min, offset); \ |
| 288 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 289 | show_in_max, set_in_max, offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | sysfs_in(0); |
| 292 | sysfs_in(1); |
| 293 | sysfs_in(2); |
| 294 | sysfs_in(3); |
| 295 | sysfs_in(4); |
| 296 | sysfs_in(5); |
| 297 | sysfs_in(6); |
| 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | /* 3 Fans */ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 300 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
| 301 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 303 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | struct asb100_data *data = asb100_update_device(dev); |
| 305 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
| 306 | DIV_FROM_REG(data->fan_div[nr]))); |
| 307 | } |
| 308 | |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 309 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
| 310 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 312 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | struct asb100_data *data = asb100_update_device(dev); |
| 314 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
| 315 | DIV_FROM_REG(data->fan_div[nr]))); |
| 316 | } |
| 317 | |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 318 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
| 319 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 321 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | struct asb100_data *data = asb100_update_device(dev); |
| 323 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
| 324 | } |
| 325 | |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 326 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 327 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 329 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | struct i2c_client *client = to_i2c_client(dev); |
| 331 | struct asb100_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 332 | unsigned long val; |
| 333 | int err; |
| 334 | |
| 335 | err = kstrtoul(buf, 10, &val); |
| 336 | if (err) |
| 337 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 339 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
| 341 | asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 342 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return count; |
| 344 | } |
| 345 | |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 346 | /* |
| 347 | * Note: we save and restore the fan minimum here, because its value is |
| 348 | * determined in part by the fan divisor. This follows the principle of |
| 349 | * least surprise; the user doesn't expect the fan minimum to change just |
| 350 | * because the divisor changed. |
| 351 | */ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 352 | static ssize_t set_fan_div(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 | { |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 355 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | struct i2c_client *client = to_i2c_client(dev); |
| 357 | struct asb100_data *data = i2c_get_clientdata(client); |
| 358 | unsigned long min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | int reg; |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 360 | unsigned long val; |
| 361 | int err; |
| 362 | |
| 363 | err = kstrtoul(buf, 10, &val); |
| 364 | if (err) |
| 365 | return err; |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 366 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 367 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | min = FAN_FROM_REG(data->fan_min[nr], |
| 370 | DIV_FROM_REG(data->fan_div[nr])); |
| 371 | data->fan_div[nr] = DIV_TO_REG(val); |
| 372 | |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 373 | switch (nr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | case 0: /* fan 1 */ |
| 375 | reg = asb100_read_value(client, ASB100_REG_VID_FANDIV); |
| 376 | reg = (reg & 0xcf) | (data->fan_div[0] << 4); |
| 377 | asb100_write_value(client, ASB100_REG_VID_FANDIV, reg); |
| 378 | break; |
| 379 | |
| 380 | case 1: /* fan 2 */ |
| 381 | reg = asb100_read_value(client, ASB100_REG_VID_FANDIV); |
| 382 | reg = (reg & 0x3f) | (data->fan_div[1] << 6); |
| 383 | asb100_write_value(client, ASB100_REG_VID_FANDIV, reg); |
| 384 | break; |
| 385 | |
| 386 | case 2: /* fan 3 */ |
| 387 | reg = asb100_read_value(client, ASB100_REG_PIN); |
| 388 | reg = (reg & 0x3f) | (data->fan_div[2] << 6); |
| 389 | asb100_write_value(client, ASB100_REG_PIN, reg); |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | data->fan_min[nr] = |
| 394 | FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
| 395 | asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]); |
| 396 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 397 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | return count; |
| 400 | } |
| 401 | |
| 402 | #define sysfs_fan(offset) \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 403 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 404 | show_fan, NULL, offset - 1); \ |
| 405 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 406 | show_fan_min, set_fan_min, offset - 1); \ |
| 407 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 408 | show_fan_div, set_fan_div, offset - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | sysfs_fan(1); |
| 411 | sysfs_fan(2); |
| 412 | sysfs_fan(3); |
| 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | /* 4 Temp. Sensors */ |
| 415 | static int sprintf_temp_from_reg(u16 reg, char *buf, int nr) |
| 416 | { |
| 417 | int ret = 0; |
| 418 | |
| 419 | switch (nr) { |
| 420 | case 1: case 2: |
| 421 | ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg)); |
| 422 | break; |
| 423 | case 0: case 3: default: |
| 424 | ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg)); |
| 425 | break; |
| 426 | } |
| 427 | return ret; |
| 428 | } |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | #define show_temp_reg(reg) \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 431 | static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ |
| 432 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 434 | int nr = to_sensor_dev_attr(attr)->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | struct asb100_data *data = asb100_update_device(dev); \ |
| 436 | return sprintf_temp_from_reg(data->reg[nr], buf, nr); \ |
| 437 | } |
| 438 | |
| 439 | show_temp_reg(temp); |
| 440 | show_temp_reg(temp_max); |
| 441 | show_temp_reg(temp_hyst); |
| 442 | |
| 443 | #define set_temp_reg(REG, reg) \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 444 | static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \ |
| 445 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 447 | int nr = to_sensor_dev_attr(attr)->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | struct i2c_client *client = to_i2c_client(dev); \ |
| 449 | struct asb100_data *data = i2c_get_clientdata(client); \ |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 450 | long val; \ |
| 451 | int err = kstrtol(buf, 10, &val); \ |
| 452 | if (err) \ |
| 453 | return err; \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 454 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | switch (nr) { \ |
| 456 | case 1: case 2: \ |
| 457 | data->reg[nr] = LM75_TEMP_TO_REG(val); \ |
| 458 | break; \ |
| 459 | case 0: case 3: default: \ |
| 460 | data->reg[nr] = TEMP_TO_REG(val); \ |
| 461 | break; \ |
| 462 | } \ |
| 463 | asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \ |
| 464 | data->reg[nr]); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 465 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return count; \ |
| 467 | } |
| 468 | |
| 469 | set_temp_reg(MAX, temp_max); |
| 470 | set_temp_reg(HYST, temp_hyst); |
| 471 | |
| 472 | #define sysfs_temp(num) \ |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 473 | static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \ |
| 474 | show_temp, NULL, num - 1); \ |
| 475 | static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \ |
| 476 | show_temp_max, set_temp_max, num - 1); \ |
| 477 | static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \ |
| 478 | show_temp_hyst, set_temp_hyst, num - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | sysfs_temp(1); |
| 481 | sysfs_temp(2); |
| 482 | sysfs_temp(3); |
| 483 | sysfs_temp(4); |
| 484 | |
| 485 | /* VID */ |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 486 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, |
| 487 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 489 | struct asb100_data *data = asb100_update_device(dev); |
| 490 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 491 | } |
| 492 | |
| 493 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | /* VRM */ |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 496 | static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, |
| 497 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame] | 499 | struct asb100_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | return sprintf(buf, "%d\n", data->vrm); |
| 501 | } |
| 502 | |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 503 | static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, |
| 504 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Jean Delvare | 8f74efe | 2007-12-01 11:25:33 +0100 | [diff] [blame] | 506 | struct asb100_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 507 | unsigned long val; |
| 508 | int err; |
| 509 | |
| 510 | err = kstrtoul(buf, 10, &val); |
| 511 | if (err) |
| 512 | return err; |
| 513 | data->vrm = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | return count; |
| 515 | } |
| 516 | |
| 517 | /* Alarms */ |
| 518 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 520 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, |
| 521 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
| 523 | struct asb100_data *data = asb100_update_device(dev); |
Jean Delvare | 68188ba | 2005-05-16 18:52:38 +0200 | [diff] [blame] | 524 | return sprintf(buf, "%u\n", data->alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
Jean Delvare | 636866b | 2008-01-03 23:24:24 +0100 | [diff] [blame] | 529 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 530 | char *buf) |
| 531 | { |
| 532 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 533 | struct asb100_data *data = asb100_update_device(dev); |
| 534 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 535 | } |
| 536 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 537 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 538 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 539 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 540 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 541 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 542 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 543 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 544 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 545 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 546 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13); |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | /* 1 PWM */ |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 549 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, |
| 550 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | { |
| 552 | struct asb100_data *data = asb100_update_device(dev); |
| 553 | return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f)); |
| 554 | } |
| 555 | |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 556 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, |
| 557 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
| 559 | struct i2c_client *client = to_i2c_client(dev); |
| 560 | struct asb100_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 561 | unsigned long val; |
| 562 | int err; |
| 563 | |
| 564 | err = kstrtoul(buf, 10, &val); |
| 565 | if (err) |
| 566 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 568 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | data->pwm &= 0x80; /* keep the enable bit */ |
| 570 | data->pwm |= (0x0f & ASB100_PWM_TO_REG(val)); |
| 571 | asb100_write_value(client, ASB100_REG_PWM1, data->pwm); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 572 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | return count; |
| 574 | } |
| 575 | |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 576 | static ssize_t show_pwm_enable1(struct device *dev, |
| 577 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | struct asb100_data *data = asb100_update_device(dev); |
| 580 | return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0); |
| 581 | } |
| 582 | |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 583 | static ssize_t set_pwm_enable1(struct device *dev, |
| 584 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
| 586 | struct i2c_client *client = to_i2c_client(dev); |
| 587 | struct asb100_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 588 | unsigned long val; |
| 589 | int err; |
| 590 | |
| 591 | err = kstrtoul(buf, 10, &val); |
| 592 | if (err) |
| 593 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 595 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | data->pwm &= 0x0f; /* keep the duty cycle bits */ |
| 597 | data->pwm |= (val ? 0x80 : 0x00); |
| 598 | asb100_write_value(client, ASB100_REG_PWM1, data->pwm); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 599 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | return count; |
| 601 | } |
| 602 | |
| 603 | static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1); |
| 604 | static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, |
| 605 | show_pwm_enable1, set_pwm_enable1); |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 606 | |
| 607 | static struct attribute *asb100_attributes[] = { |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 608 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 609 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 610 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 611 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 612 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 613 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 614 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 615 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 616 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 617 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 618 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 619 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 620 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 621 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 622 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 623 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 624 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 625 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 626 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 627 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 628 | &sensor_dev_attr_in6_max.dev_attr.attr, |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 629 | |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 630 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 631 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 632 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 633 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 634 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 635 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
| 636 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 637 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 638 | &sensor_dev_attr_fan3_div.dev_attr.attr, |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 639 | |
Jean Delvare | fad33c5 | 2008-01-03 23:21:07 +0100 | [diff] [blame] | 640 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 641 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 642 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 643 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 644 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 645 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, |
| 646 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 647 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 648 | &sensor_dev_attr_temp3_max_hyst.dev_attr.attr, |
| 649 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 650 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 651 | &sensor_dev_attr_temp4_max_hyst.dev_attr.attr, |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 652 | |
Jean Delvare | 636866b | 2008-01-03 23:24:24 +0100 | [diff] [blame] | 653 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 654 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 655 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 656 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 657 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
| 658 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 659 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 660 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
| 661 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 662 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 663 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 664 | |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 665 | &dev_attr_cpu0_vid.attr, |
| 666 | &dev_attr_vrm.attr, |
| 667 | &dev_attr_alarms.attr, |
| 668 | &dev_attr_pwm1.attr, |
| 669 | &dev_attr_pwm1_enable.attr, |
| 670 | |
| 671 | NULL |
| 672 | }; |
| 673 | |
| 674 | static const struct attribute_group asb100_group = { |
| 675 | .attrs = asb100_attributes, |
| 676 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 678 | static int asb100_detect_subclients(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
| 680 | int i, id, err; |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 681 | int address = client->addr; |
| 682 | unsigned short sc_addr[2]; |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 683 | struct asb100_data *data = i2c_get_clientdata(client); |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 684 | struct i2c_adapter *adapter = client->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
| 686 | id = i2c_adapter_id(adapter); |
| 687 | |
| 688 | if (force_subclients[0] == id && force_subclients[1] == address) { |
| 689 | for (i = 2; i <= 3; i++) { |
| 690 | if (force_subclients[i] < 0x48 || |
| 691 | force_subclients[i] > 0x4f) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 692 | dev_err(&client->dev, |
| 693 | "invalid subclient address %d; must be 0x48-0x4f\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | force_subclients[i]); |
| 695 | err = -ENODEV; |
| 696 | goto ERROR_SC_2; |
| 697 | } |
| 698 | } |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 699 | asb100_write_value(client, ASB100_REG_I2C_SUBADDR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | (force_subclients[2] & 0x07) | |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 701 | ((force_subclients[3] & 0x07) << 4)); |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 702 | sc_addr[0] = force_subclients[2]; |
| 703 | sc_addr[1] = force_subclients[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } else { |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 705 | int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR); |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 706 | sc_addr[0] = 0x48 + (val & 0x07); |
| 707 | sc_addr[1] = 0x48 + ((val >> 4) & 0x07); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 710 | if (sc_addr[0] == sc_addr[1]) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 711 | dev_err(&client->dev, |
| 712 | "duplicate addresses 0x%x for subclients\n", |
| 713 | sc_addr[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | err = -ENODEV; |
| 715 | goto ERROR_SC_2; |
| 716 | } |
| 717 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 718 | data->lm75[0] = i2c_new_dummy(adapter, sc_addr[0]); |
| 719 | if (!data->lm75[0]) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 720 | dev_err(&client->dev, |
| 721 | "subclient %d registration at address 0x%x failed.\n", |
| 722 | 1, sc_addr[0]); |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 723 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | goto ERROR_SC_2; |
| 725 | } |
| 726 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 727 | data->lm75[1] = i2c_new_dummy(adapter, sc_addr[1]); |
| 728 | if (!data->lm75[1]) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 729 | dev_err(&client->dev, |
| 730 | "subclient %d registration at address 0x%x failed.\n", |
| 731 | 2, sc_addr[1]); |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 732 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | goto ERROR_SC_3; |
| 734 | } |
| 735 | |
| 736 | return 0; |
| 737 | |
| 738 | /* Undo inits in case of errors */ |
| 739 | ERROR_SC_3: |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 740 | i2c_unregister_device(data->lm75[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | ERROR_SC_2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | return err; |
| 743 | } |
| 744 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 745 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 746 | static int asb100_detect(struct i2c_client *client, |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 747 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 749 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 750 | int val1, val2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
Joe Perches | 4d630e2 | 2010-10-20 06:51:29 +0000 | [diff] [blame] | 753 | pr_debug("detect failed, smbus byte data not supported!\n"); |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 754 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 757 | val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK); |
| 758 | val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 760 | /* If we're in bank 0 */ |
| 761 | if ((!(val1 & 0x07)) && |
| 762 | /* Check for ASB100 ID (low byte) */ |
| 763 | (((!(val1 & 0x80)) && (val2 != 0x94)) || |
| 764 | /* Check for ASB100 ID (high byte ) */ |
| 765 | ((val1 & 0x80) && (val2 != 0x06)))) { |
Joe Perches | 4d630e2 | 2010-10-20 06:51:29 +0000 | [diff] [blame] | 766 | pr_debug("detect failed, bad chip id 0x%02x!\n", val2); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 767 | return -ENODEV; |
| 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 770 | /* Put it now into bank 0 and Vendor ID High Byte */ |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 771 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, |
| 772 | (i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78) |
| 773 | | 0x80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | /* Determine the chip type. */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 776 | val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID); |
| 777 | val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 779 | if (val1 != 0x31 || val2 != 0x06) |
| 780 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 782 | strlcpy(info->type, "asb100", I2C_NAME_SIZE); |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static int asb100_probe(struct i2c_client *client, |
| 788 | const struct i2c_device_id *id) |
| 789 | { |
| 790 | int err; |
| 791 | struct asb100_data *data; |
| 792 | |
Guenter Roeck | 64adf39 | 2012-06-02 09:58:02 -0700 | [diff] [blame] | 793 | data = devm_kzalloc(&client->dev, sizeof(struct asb100_data), |
| 794 | GFP_KERNEL); |
| 795 | if (!data) |
| 796 | return -ENOMEM; |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 797 | |
| 798 | i2c_set_clientdata(client, data); |
| 799 | mutex_init(&data->lock); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 800 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | /* Attach secondary lm75 clients */ |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 803 | err = asb100_detect_subclients(client); |
| 804 | if (err) |
Guenter Roeck | 64adf39 | 2012-06-02 09:58:02 -0700 | [diff] [blame] | 805 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | /* Initialize the chip */ |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 808 | asb100_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
| 810 | /* A few vars need to be filled upon startup */ |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 811 | data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0)); |
| 812 | data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1)); |
| 813 | data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
| 815 | /* Register sysfs hooks */ |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 816 | err = sysfs_create_group(&client->dev.kobj, &asb100_group); |
| 817 | if (err) |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 818 | goto ERROR3; |
| 819 | |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 820 | data->hwmon_dev = hwmon_device_register(&client->dev); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 821 | if (IS_ERR(data->hwmon_dev)) { |
| 822 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 823 | goto ERROR4; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 824 | } |
| 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | return 0; |
| 827 | |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 828 | ERROR4: |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 829 | sysfs_remove_group(&client->dev.kobj, &asb100_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 830 | ERROR3: |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 831 | i2c_unregister_device(data->lm75[1]); |
| 832 | i2c_unregister_device(data->lm75[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | return err; |
| 834 | } |
| 835 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 836 | static int asb100_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 838 | struct asb100_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 840 | hwmon_device_unregister(data->hwmon_dev); |
| 841 | sysfs_remove_group(&client->dev.kobj, &asb100_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 842 | |
Jean Delvare | 063675b | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 843 | i2c_unregister_device(data->lm75[1]); |
| 844 | i2c_unregister_device(data->lm75[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
Guenter Roeck | 8c10369 | 2012-01-14 13:20:00 -0800 | [diff] [blame] | 849 | /* |
| 850 | * The SMBus locks itself, usually, but nothing may access the chip between |
| 851 | * bank switches. |
| 852 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | static int asb100_read_value(struct i2c_client *client, u16 reg) |
| 854 | { |
| 855 | struct asb100_data *data = i2c_get_clientdata(client); |
| 856 | struct i2c_client *cl; |
| 857 | int res, bank; |
| 858 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 859 | mutex_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | |
| 861 | bank = (reg >> 8) & 0x0f; |
| 862 | if (bank > 2) |
| 863 | /* switch banks */ |
| 864 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank); |
| 865 | |
| 866 | if (bank == 0 || bank > 2) { |
| 867 | res = i2c_smbus_read_byte_data(client, reg & 0xff); |
| 868 | } else { |
| 869 | /* switch to subclient */ |
| 870 | cl = data->lm75[bank - 1]; |
| 871 | |
| 872 | /* convert from ISA to LM75 I2C addresses */ |
| 873 | switch (reg & 0xff) { |
| 874 | case 0x50: /* TEMP */ |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 875 | res = i2c_smbus_read_word_swapped(cl, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | break; |
| 877 | case 0x52: /* CONFIG */ |
| 878 | res = i2c_smbus_read_byte_data(cl, 1); |
| 879 | break; |
| 880 | case 0x53: /* HYST */ |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 881 | res = i2c_smbus_read_word_swapped(cl, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | break; |
| 883 | case 0x55: /* MAX */ |
| 884 | default: |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 885 | res = i2c_smbus_read_word_swapped(cl, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | break; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | if (bank > 2) |
| 891 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0); |
| 892 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 893 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | return res; |
| 896 | } |
| 897 | |
| 898 | static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value) |
| 899 | { |
| 900 | struct asb100_data *data = i2c_get_clientdata(client); |
| 901 | struct i2c_client *cl; |
| 902 | int bank; |
| 903 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 904 | mutex_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
| 906 | bank = (reg >> 8) & 0x0f; |
| 907 | if (bank > 2) |
| 908 | /* switch banks */ |
| 909 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank); |
| 910 | |
| 911 | if (bank == 0 || bank > 2) { |
| 912 | i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff); |
| 913 | } else { |
| 914 | /* switch to subclient */ |
| 915 | cl = data->lm75[bank - 1]; |
| 916 | |
| 917 | /* convert from ISA to LM75 I2C addresses */ |
| 918 | switch (reg & 0xff) { |
| 919 | case 0x52: /* CONFIG */ |
| 920 | i2c_smbus_write_byte_data(cl, 1, value & 0xff); |
| 921 | break; |
| 922 | case 0x53: /* HYST */ |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 923 | i2c_smbus_write_word_swapped(cl, 2, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | break; |
| 925 | case 0x55: /* MAX */ |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 926 | i2c_smbus_write_word_swapped(cl, 3, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | break; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | if (bank > 2) |
| 932 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0); |
| 933 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 934 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static void asb100_init_client(struct i2c_client *client) |
| 938 | { |
| 939 | struct asb100_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 941 | data->vrm = vid_which_vrm(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
| 943 | /* Start monitoring */ |
Jean Delvare | af221931 | 2008-01-03 23:15:49 +0100 | [diff] [blame] | 944 | asb100_write_value(client, ASB100_REG_CONFIG, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01); |
| 946 | } |
| 947 | |
| 948 | static struct asb100_data *asb100_update_device(struct device *dev) |
| 949 | { |
| 950 | struct i2c_client *client = to_i2c_client(dev); |
| 951 | struct asb100_data *data = i2c_get_clientdata(client); |
| 952 | int i; |
| 953 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 954 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
| 956 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 957 | || !data->valid) { |
| 958 | |
| 959 | dev_dbg(&client->dev, "starting device update...\n"); |
| 960 | |
| 961 | /* 7 voltage inputs */ |
| 962 | for (i = 0; i < 7; i++) { |
| 963 | data->in[i] = asb100_read_value(client, |
| 964 | ASB100_REG_IN(i)); |
| 965 | data->in_min[i] = asb100_read_value(client, |
| 966 | ASB100_REG_IN_MIN(i)); |
| 967 | data->in_max[i] = asb100_read_value(client, |
| 968 | ASB100_REG_IN_MAX(i)); |
| 969 | } |
| 970 | |
| 971 | /* 3 fan inputs */ |
| 972 | for (i = 0; i < 3; i++) { |
| 973 | data->fan[i] = asb100_read_value(client, |
| 974 | ASB100_REG_FAN(i)); |
| 975 | data->fan_min[i] = asb100_read_value(client, |
| 976 | ASB100_REG_FAN_MIN(i)); |
| 977 | } |
| 978 | |
| 979 | /* 4 temperature inputs */ |
| 980 | for (i = 1; i <= 4; i++) { |
| 981 | data->temp[i-1] = asb100_read_value(client, |
| 982 | ASB100_REG_TEMP(i)); |
| 983 | data->temp_max[i-1] = asb100_read_value(client, |
| 984 | ASB100_REG_TEMP_MAX(i)); |
| 985 | data->temp_hyst[i-1] = asb100_read_value(client, |
| 986 | ASB100_REG_TEMP_HYST(i)); |
| 987 | } |
| 988 | |
| 989 | /* VID and fan divisors */ |
| 990 | i = asb100_read_value(client, ASB100_REG_VID_FANDIV); |
| 991 | data->vid = i & 0x0f; |
| 992 | data->vid |= (asb100_read_value(client, |
| 993 | ASB100_REG_CHIPID) & 0x01) << 4; |
| 994 | data->fan_div[0] = (i >> 4) & 0x03; |
| 995 | data->fan_div[1] = (i >> 6) & 0x03; |
| 996 | data->fan_div[2] = (asb100_read_value(client, |
| 997 | ASB100_REG_PIN) >> 6) & 0x03; |
| 998 | |
| 999 | /* PWM */ |
| 1000 | data->pwm = asb100_read_value(client, ASB100_REG_PWM1); |
| 1001 | |
| 1002 | /* alarms */ |
| 1003 | data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) + |
| 1004 | (asb100_read_value(client, ASB100_REG_ALARM2) << 8); |
| 1005 | |
| 1006 | data->last_updated = jiffies; |
| 1007 | data->valid = 1; |
| 1008 | |
| 1009 | dev_dbg(&client->dev, "... device update complete\n"); |
| 1010 | } |
| 1011 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1012 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
| 1014 | return data; |
| 1015 | } |
| 1016 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 1017 | module_i2c_driver(asb100_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
| 1019 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 1020 | MODULE_DESCRIPTION("ASB100 Bach driver"); |
| 1021 | MODULE_LICENSE("GPL"); |