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