Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 2 | /* |
| 3 | * adm9240.c Part of lm_sensors, Linux kernel modules for hardware |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 4 | * monitoring |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl> |
| 7 | * Philip Edelbrock <phil@netroedge.com> |
| 8 | * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl> |
Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 9 | * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 10 | * guidance from Jean Delvare |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 11 | * |
| 12 | * Driver supports Analog Devices ADM9240 |
| 13 | * Dallas Semiconductor DS1780 |
| 14 | * National Semiconductor LM81 |
| 15 | * |
| 16 | * ADM9240 is the reference, DS1780 and LM81 are register compatibles |
| 17 | * |
| 18 | * Voltage Six inputs are scaled by chip, VID also reported |
| 19 | * Temperature Chip temperature to 0.5'C, maximum and max_hysteris |
| 20 | * Fans 2 fans, low speed alarm, automatic fan clock divider |
| 21 | * Alarms 16-bit map of active alarms |
| 22 | * Analog Out 0..1250 mV output |
| 23 | * |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 24 | * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm' |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 25 | * |
| 26 | * Test hardware: Intel SE440BX-2 desktop motherboard --Grant |
| 27 | * |
| 28 | * LM81 extended temp reading not implemented |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/i2c.h> |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 35 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 36 | #include <linux/hwmon.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 37 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 38 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 40 | #include <linux/jiffies.h> |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 41 | #include <linux/regmap.h> |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 42 | |
| 43 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 44 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 45 | I2C_CLIENT_END }; |
| 46 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 47 | enum chips { adm9240, ds1780, lm81 }; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 48 | |
| 49 | /* ADM9240 registers */ |
| 50 | #define ADM9240_REG_MAN_ID 0x3e |
| 51 | #define ADM9240_REG_DIE_REV 0x3f |
| 52 | #define ADM9240_REG_CONFIG 0x40 |
| 53 | |
| 54 | #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */ |
| 55 | #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2) |
| 56 | #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2) |
| 57 | #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */ |
| 58 | #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 59 | #define ADM9240_REG_INT(nr) (0x41 + (nr)) |
| 60 | #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr)) |
| 61 | #define ADM9240_REG_TEMP 0x27 |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 62 | #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 63 | #define ADM9240_REG_ANALOG_OUT 0x19 |
| 64 | #define ADM9240_REG_CHASSIS_CLEAR 0x46 |
| 65 | #define ADM9240_REG_VID_FAN_DIV 0x47 |
| 66 | #define ADM9240_REG_I2C_ADDR 0x48 |
| 67 | #define ADM9240_REG_VID4 0x49 |
| 68 | #define ADM9240_REG_TEMP_CONF 0x4b |
| 69 | |
| 70 | /* generalised scaling with integer rounding */ |
| 71 | static inline int SCALE(long val, int mul, int div) |
| 72 | { |
| 73 | if (val < 0) |
| 74 | return (val * mul - div / 2) / div; |
| 75 | else |
| 76 | return (val * mul + div / 2) / div; |
| 77 | } |
| 78 | |
| 79 | /* adm9240 internally scales voltage measurements */ |
| 80 | static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 }; |
| 81 | |
| 82 | static inline unsigned int IN_FROM_REG(u8 reg, int n) |
| 83 | { |
| 84 | return SCALE(reg, nom_mv[n], 192); |
| 85 | } |
| 86 | |
| 87 | static inline u8 IN_TO_REG(unsigned long val, int n) |
| 88 | { |
Guenter Roeck | 0fb620c | 2016-11-20 10:14:09 -0800 | [diff] [blame] | 89 | val = clamp_val(val, 0, nom_mv[n] * 255 / 192); |
| 90 | return SCALE(val, 192, nom_mv[n]); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* temperature range: -40..125, 127 disables temperature alarm */ |
| 94 | static inline s8 TEMP_TO_REG(long val) |
| 95 | { |
Guenter Roeck | 0fb620c | 2016-11-20 10:14:09 -0800 | [diff] [blame] | 96 | val = clamp_val(val, -40000, 127000); |
| 97 | return SCALE(val, 1, 1000); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* two fans, each with low fan speed limit */ |
| 101 | static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) |
| 102 | { |
| 103 | if (!reg) /* error */ |
| 104 | return -1; |
| 105 | |
| 106 | if (reg == 255) |
| 107 | return 0; |
| 108 | |
| 109 | return SCALE(1350000, 1, reg * div); |
| 110 | } |
| 111 | |
| 112 | /* analog out 0..1250mV */ |
| 113 | static inline u8 AOUT_TO_REG(unsigned long val) |
| 114 | { |
Guenter Roeck | 0fb620c | 2016-11-20 10:14:09 -0800 | [diff] [blame] | 115 | val = clamp_val(val, 0, 1250); |
| 116 | return SCALE(val, 255, 1250); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static inline unsigned int AOUT_FROM_REG(u8 reg) |
| 120 | { |
| 121 | return SCALE(reg, 1250, 255); |
| 122 | } |
| 123 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 124 | /* per client data */ |
| 125 | struct adm9240_data { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 126 | struct i2c_client *client; |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 127 | struct regmap *regmap; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 128 | struct mutex update_lock; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 129 | char valid; |
| 130 | unsigned long last_updated_measure; |
| 131 | unsigned long last_updated_config; |
| 132 | |
| 133 | u8 in[6]; /* ro in0_input */ |
| 134 | u8 in_max[6]; /* rw in0_max */ |
| 135 | u8 in_min[6]; /* rw in0_min */ |
| 136 | u8 fan[2]; /* ro fan1_input */ |
| 137 | u8 fan_min[2]; /* rw fan1_min */ |
| 138 | u8 fan_div[2]; /* rw fan1_div, read-only accessor */ |
| 139 | s16 temp; /* ro temp1_input, 9-bit sign-extended */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 140 | s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 141 | u16 alarms; /* ro alarms */ |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 142 | u8 aout; /* rw aout_output */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 143 | u8 vid; /* ro vid */ |
| 144 | u8 vrm; /* -- vrm set on startup, no accessor */ |
| 145 | }; |
| 146 | |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 147 | /* write new fan div, callers must hold data->update_lock */ |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 148 | static int adm9240_write_fan_div(struct adm9240_data *data, int nr, |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 149 | u8 fan_div) |
| 150 | { |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 151 | unsigned int reg, old, shift = (nr + 2) * 2; |
| 152 | int err; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 153 | |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 154 | err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, ®); |
| 155 | if (err < 0) |
| 156 | return err; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 157 | old = (reg >> shift) & 3; |
| 158 | reg &= ~(3 << shift); |
| 159 | reg |= (fan_div << shift); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 160 | err = regmap_write(data->regmap, ADM9240_REG_VID_FAN_DIV, reg); |
| 161 | if (err < 0) |
| 162 | return err; |
| 163 | dev_dbg(&data->client->dev, |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 164 | "fan%d clock divider changed from %u to %u\n", |
| 165 | nr + 1, 1 << old, 1 << fan_div); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 166 | |
| 167 | return 0; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 168 | } |
| 169 | |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 170 | static int adm9240_update_measure(struct adm9240_data *data) |
| 171 | { |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 172 | unsigned int val; |
| 173 | u8 regs[2]; |
| 174 | int err; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 175 | int i; |
| 176 | |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 177 | err = regmap_bulk_read(data->regmap, ADM9240_REG_IN(0), &data->in[0], 6); |
| 178 | if (err < 0) |
| 179 | return err; |
| 180 | err = regmap_bulk_read(data->regmap, ADM9240_REG_INT(0), ®s, 2); |
| 181 | if (err < 0) |
| 182 | return err; |
| 183 | |
| 184 | data->alarms = regs[0] | regs[1] << 8; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * read temperature: assume temperature changes less than |
| 188 | * 0.5'C per two measurement cycles thus ignore possible |
| 189 | * but unlikely aliasing error on lsb reading. --Grant |
| 190 | */ |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 191 | err = regmap_read(data->regmap, ADM9240_REG_TEMP, &val); |
| 192 | if (err < 0) |
| 193 | return err; |
| 194 | data->temp = val << 8; |
| 195 | err = regmap_read(data->regmap, ADM9240_REG_TEMP_CONF, &val); |
| 196 | if (err < 0) |
| 197 | return err; |
| 198 | data->temp |= val; |
| 199 | |
| 200 | err = regmap_bulk_read(data->regmap, ADM9240_REG_FAN(0), |
| 201 | &data->fan[0], 2); |
| 202 | if (err < 0) |
| 203 | return err; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 204 | |
| 205 | for (i = 0; i < 2; i++) { /* read fans */ |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 206 | /* adjust fan clock divider on overflow */ |
| 207 | if (data->valid && data->fan[i] == 255 && |
| 208 | data->fan_div[i] < 3) { |
| 209 | |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 210 | err = adm9240_write_fan_div(data, i, |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 211 | ++data->fan_div[i]); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 212 | if (err < 0) |
| 213 | return err; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 214 | |
| 215 | /* adjust fan_min if active, but not to 0 */ |
| 216 | if (data->fan_min[i] < 255 && |
| 217 | data->fan_min[i] >= 2) |
| 218 | data->fan_min[i] /= 2; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int adm9240_update_config(struct adm9240_data *data) |
| 226 | { |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 227 | unsigned int val; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 228 | int i; |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 229 | int err; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 230 | |
| 231 | for (i = 0; i < 6; i++) { |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 232 | err = regmap_raw_read(data->regmap, ADM9240_REG_IN_MIN(i), |
| 233 | &data->in_min[i], 1); |
| 234 | if (err < 0) |
| 235 | return err; |
| 236 | err = regmap_raw_read(data->regmap, ADM9240_REG_IN_MAX(i), |
| 237 | &data->in_max[i], 1); |
| 238 | if (err < 0) |
| 239 | return err; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 240 | } |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 241 | err = regmap_bulk_read(data->regmap, ADM9240_REG_FAN_MIN(0), |
| 242 | &data->fan_min[0], 2); |
| 243 | if (err < 0) |
| 244 | return err; |
| 245 | err = regmap_bulk_read(data->regmap, ADM9240_REG_TEMP_MAX(0), |
| 246 | &data->temp_max[0], 2); |
| 247 | if (err < 0) |
| 248 | return err; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 249 | |
| 250 | /* read fan divs and 5-bit VID */ |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 251 | err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, &val); |
| 252 | if (err < 0) |
| 253 | return err; |
| 254 | data->fan_div[0] = (val >> 4) & 3; |
| 255 | data->fan_div[1] = (val >> 6) & 3; |
| 256 | data->vid = val & 0x0f; |
| 257 | err = regmap_read(data->regmap, ADM9240_REG_VID4, &val); |
| 258 | if (err < 0) |
| 259 | return err; |
| 260 | data->vid |= (val & 1) << 4; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 261 | /* read analog out */ |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 262 | err = regmap_raw_read(data->regmap, ADM9240_REG_ANALOG_OUT, |
| 263 | &data->aout, 1); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 264 | |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 265 | return err; |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 266 | } |
| 267 | |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 268 | static struct adm9240_data *adm9240_update_device(struct device *dev) |
| 269 | { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 270 | struct adm9240_data *data = dev_get_drvdata(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 271 | int err; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 272 | |
| 273 | mutex_lock(&data->update_lock); |
| 274 | |
| 275 | /* minimum measurement cycle: 1.75 seconds */ |
| 276 | if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4)) |
| 277 | || !data->valid) { |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 278 | err = adm9240_update_measure(data); |
| 279 | if (err < 0) { |
| 280 | data->valid = 0; |
| 281 | mutex_unlock(&data->update_lock); |
| 282 | return ERR_PTR(err); |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 283 | } |
| 284 | data->last_updated_measure = jiffies; |
| 285 | } |
| 286 | |
| 287 | /* minimum config reading cycle: 300 seconds */ |
| 288 | if (time_after(jiffies, data->last_updated_config + (HZ * 300)) |
| 289 | || !data->valid) { |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 290 | err = adm9240_update_config(data); |
| 291 | if (err < 0) { |
| 292 | data->valid = 0; |
| 293 | mutex_unlock(&data->update_lock); |
| 294 | return ERR_PTR(err); |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 295 | } |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 296 | data->last_updated_config = jiffies; |
| 297 | data->valid = 1; |
| 298 | } |
| 299 | mutex_unlock(&data->update_lock); |
| 300 | return data; |
| 301 | } |
| 302 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 303 | /*** sysfs accessors ***/ |
| 304 | |
| 305 | /* temperature */ |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 306 | static ssize_t temp1_input_show(struct device *dev, |
| 307 | struct device_attribute *dummy, char *buf) |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 308 | { |
| 309 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 310 | |
| 311 | if (IS_ERR(data)) |
| 312 | return PTR_ERR(data); |
| 313 | |
Chris Packham | 667f4ba | 2016-10-05 10:40:54 +1300 | [diff] [blame] | 314 | return sprintf(buf, "%d\n", data->temp / 128 * 500); /* 9-bit value */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 315 | } |
| 316 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 317 | static ssize_t max_show(struct device *dev, struct device_attribute *devattr, |
| 318 | char *buf) |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 319 | { |
| 320 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 321 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 322 | |
| 323 | if (IS_ERR(data)) |
| 324 | return PTR_ERR(data); |
| 325 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 326 | return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000); |
| 327 | } |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 328 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 329 | static ssize_t max_store(struct device *dev, struct device_attribute *devattr, |
| 330 | const char *buf, size_t count) |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 331 | { |
| 332 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 333 | struct adm9240_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 334 | long val; |
| 335 | int err; |
| 336 | |
| 337 | err = kstrtol(buf, 10, &val); |
| 338 | if (err) |
| 339 | return err; |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 340 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 341 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 342 | data->temp_max[attr->index] = TEMP_TO_REG(val); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 343 | err = regmap_write(data->regmap, ADM9240_REG_TEMP_MAX(attr->index), |
| 344 | data->temp_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 345 | mutex_unlock(&data->update_lock); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 346 | return err < 0 ? err : count; |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 347 | } |
| 348 | |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 349 | static DEVICE_ATTR_RO(temp1_input); |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 350 | static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0); |
| 351 | static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, max, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 352 | |
| 353 | /* voltage */ |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 354 | static ssize_t in_show(struct device *dev, struct device_attribute *devattr, |
| 355 | char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 356 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 357 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 358 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 359 | |
| 360 | if (IS_ERR(data)) |
| 361 | return PTR_ERR(data); |
| 362 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 363 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index], |
| 364 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 365 | } |
| 366 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 367 | static ssize_t in_min_show(struct device *dev, |
| 368 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 369 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 370 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 371 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 372 | |
| 373 | if (IS_ERR(data)) |
| 374 | return PTR_ERR(data); |
| 375 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 376 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index], |
| 377 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 378 | } |
| 379 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 380 | static ssize_t in_max_show(struct device *dev, |
| 381 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 382 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 383 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 384 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 385 | |
| 386 | if (IS_ERR(data)) |
| 387 | return PTR_ERR(data); |
| 388 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 389 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index], |
| 390 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 391 | } |
| 392 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 393 | static ssize_t in_min_store(struct device *dev, |
| 394 | struct device_attribute *devattr, const char *buf, |
| 395 | size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 396 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 397 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 398 | struct adm9240_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 399 | unsigned long val; |
| 400 | int err; |
| 401 | |
| 402 | err = kstrtoul(buf, 10, &val); |
| 403 | if (err) |
| 404 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 405 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 406 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 407 | data->in_min[attr->index] = IN_TO_REG(val, attr->index); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 408 | err = regmap_write(data->regmap, ADM9240_REG_IN_MIN(attr->index), |
| 409 | data->in_min[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 410 | mutex_unlock(&data->update_lock); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 411 | return err < 0 ? err : count; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 412 | } |
| 413 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 414 | static ssize_t in_max_store(struct device *dev, |
| 415 | struct device_attribute *devattr, const char *buf, |
| 416 | size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 417 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 418 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 419 | struct adm9240_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 420 | unsigned long val; |
| 421 | int err; |
| 422 | |
| 423 | err = kstrtoul(buf, 10, &val); |
| 424 | if (err) |
| 425 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 426 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 427 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 428 | data->in_max[attr->index] = IN_TO_REG(val, attr->index); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 429 | err = regmap_write(data->regmap, ADM9240_REG_IN_MAX(attr->index), |
| 430 | data->in_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 431 | mutex_unlock(&data->update_lock); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 432 | return err < 0 ? err : count; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 433 | } |
| 434 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 435 | static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0); |
| 436 | static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0); |
| 437 | static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0); |
| 438 | static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1); |
| 439 | static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1); |
| 440 | static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1); |
| 441 | static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2); |
| 442 | static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2); |
| 443 | static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2); |
| 444 | static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3); |
| 445 | static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3); |
| 446 | static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3); |
| 447 | static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4); |
| 448 | static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4); |
| 449 | static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4); |
| 450 | static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5); |
| 451 | static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5); |
| 452 | static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 453 | |
| 454 | /* fans */ |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 455 | static ssize_t fan_show(struct device *dev, struct device_attribute *devattr, |
| 456 | char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 457 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 458 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 459 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 460 | |
| 461 | if (IS_ERR(data)) |
| 462 | return PTR_ERR(data); |
| 463 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 464 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], |
| 465 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 466 | } |
| 467 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 468 | static ssize_t fan_min_show(struct device *dev, |
| 469 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 470 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 471 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 472 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 473 | |
| 474 | if (IS_ERR(data)) |
| 475 | return PTR_ERR(data); |
| 476 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 477 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index], |
| 478 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 479 | } |
| 480 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 481 | static ssize_t fan_div_show(struct device *dev, |
| 482 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 483 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 484 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 485 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 486 | |
| 487 | if (IS_ERR(data)) |
| 488 | return PTR_ERR(data); |
| 489 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 490 | return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 491 | } |
| 492 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 493 | /* |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 494 | * set fan speed low limit: |
| 495 | * |
| 496 | * - value is zero: disable fan speed low limit alarm |
| 497 | * |
| 498 | * - value is below fan speed measurement range: enable fan speed low |
| 499 | * limit alarm to be asserted while fan speed too slow to measure |
| 500 | * |
| 501 | * - otherwise: select fan clock divider to suit fan speed low limit, |
| 502 | * measurement code may adjust registers to ensure fan speed reading |
| 503 | */ |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 504 | static ssize_t fan_min_store(struct device *dev, |
| 505 | struct device_attribute *devattr, |
| 506 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 507 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 508 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 509 | struct adm9240_data *data = dev_get_drvdata(dev); |
| 510 | struct i2c_client *client = data->client; |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 511 | int nr = attr->index; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 512 | u8 new_div; |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 513 | unsigned long val; |
| 514 | int err; |
| 515 | |
| 516 | err = kstrtoul(buf, 10, &val); |
| 517 | if (err) |
| 518 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 519 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 520 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 521 | |
| 522 | if (!val) { |
| 523 | data->fan_min[nr] = 255; |
| 524 | new_div = data->fan_div[nr]; |
| 525 | |
| 526 | dev_dbg(&client->dev, "fan%u low limit set disabled\n", |
| 527 | nr + 1); |
| 528 | |
| 529 | } else if (val < 1350000 / (8 * 254)) { |
| 530 | new_div = 3; |
| 531 | data->fan_min[nr] = 254; |
| 532 | |
| 533 | dev_dbg(&client->dev, "fan%u low limit set minimum %u\n", |
| 534 | nr + 1, FAN_FROM_REG(254, 1 << new_div)); |
| 535 | |
| 536 | } else { |
| 537 | unsigned int new_min = 1350000 / val; |
| 538 | |
| 539 | new_div = 0; |
| 540 | while (new_min > 192 && new_div < 3) { |
| 541 | new_div++; |
| 542 | new_min /= 2; |
| 543 | } |
| 544 | if (!new_min) /* keep > 0 */ |
| 545 | new_min++; |
| 546 | |
| 547 | data->fan_min[nr] = new_min; |
| 548 | |
| 549 | dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n", |
| 550 | nr + 1, FAN_FROM_REG(new_min, 1 << new_div)); |
| 551 | } |
| 552 | |
| 553 | if (new_div != data->fan_div[nr]) { |
| 554 | data->fan_div[nr] = new_div; |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 555 | adm9240_write_fan_div(data, nr, new_div); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 556 | } |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 557 | err = regmap_write(data->regmap, ADM9240_REG_FAN_MIN(nr), |
| 558 | data->fan_min[nr]); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 559 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 560 | mutex_unlock(&data->update_lock); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 561 | return err < 0 ? err : count; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 562 | } |
| 563 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 564 | static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0); |
| 565 | static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0); |
| 566 | static SENSOR_DEVICE_ATTR_RO(fan1_div, fan_div, 0); |
| 567 | static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1); |
| 568 | static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1); |
| 569 | static SENSOR_DEVICE_ATTR_RO(fan2_div, fan_div, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 570 | |
| 571 | /* alarms */ |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 572 | static ssize_t alarms_show(struct device *dev, |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 573 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 574 | { |
| 575 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 576 | |
| 577 | if (IS_ERR(data)) |
| 578 | return PTR_ERR(data); |
| 579 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 580 | return sprintf(buf, "%u\n", data->alarms); |
| 581 | } |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 582 | static DEVICE_ATTR_RO(alarms); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 583 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 584 | static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, |
| 585 | char *buf) |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 586 | { |
| 587 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 588 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 589 | |
| 590 | if (IS_ERR(data)) |
| 591 | return PTR_ERR(data); |
| 592 | |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 593 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 594 | } |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 595 | static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0); |
| 596 | static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1); |
| 597 | static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2); |
| 598 | static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3); |
| 599 | static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8); |
| 600 | static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9); |
| 601 | static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4); |
| 602 | static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6); |
| 603 | static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7); |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 604 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 605 | /* vid */ |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 606 | static ssize_t cpu0_vid_show(struct device *dev, |
| 607 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 608 | { |
| 609 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 610 | |
| 611 | if (IS_ERR(data)) |
| 612 | return PTR_ERR(data); |
| 613 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 614 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 615 | } |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 616 | static DEVICE_ATTR_RO(cpu0_vid); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 617 | |
| 618 | /* analog output */ |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 619 | static ssize_t aout_output_show(struct device *dev, |
| 620 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 621 | { |
| 622 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 6a8cdd1 | 2020-09-24 20:51:01 +1200 | [diff] [blame] | 623 | |
| 624 | if (IS_ERR(data)) |
| 625 | return PTR_ERR(data); |
| 626 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 627 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 628 | } |
| 629 | |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 630 | static ssize_t aout_output_store(struct device *dev, |
| 631 | struct device_attribute *attr, |
| 632 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 633 | { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 634 | struct adm9240_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 635 | long val; |
| 636 | int err; |
| 637 | |
| 638 | err = kstrtol(buf, 10, &val); |
| 639 | if (err) |
| 640 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 641 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 642 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 643 | data->aout = AOUT_TO_REG(val); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 644 | err = regmap_write(data->regmap, ADM9240_REG_ANALOG_OUT, data->aout); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 645 | mutex_unlock(&data->update_lock); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 646 | return err < 0 ? err : count; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 647 | } |
Julia Lawall | b575111 | 2016-12-22 13:04:33 +0100 | [diff] [blame] | 648 | static DEVICE_ATTR_RW(aout_output); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 649 | |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 650 | static ssize_t alarm_store(struct device *dev, struct device_attribute *attr, |
| 651 | const char *buf, size_t count) |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 652 | { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 653 | struct adm9240_data *data = dev_get_drvdata(dev); |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 654 | unsigned long val; |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 655 | int err; |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 656 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 657 | if (kstrtoul(buf, 10, &val) || val != 0) |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 658 | return -EINVAL; |
| 659 | |
| 660 | mutex_lock(&data->update_lock); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 661 | err = regmap_write(data->regmap, ADM9240_REG_CHASSIS_CLEAR, 0x80); |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 662 | data->valid = 0; /* Force cache refresh */ |
| 663 | mutex_unlock(&data->update_lock); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 664 | if (err < 0) |
| 665 | return err; |
| 666 | dev_dbg(&data->client->dev, "chassis intrusion latch cleared\n"); |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 667 | |
| 668 | return count; |
| 669 | } |
Guenter Roeck | 7352ae8 | 2019-01-21 20:18:52 -0800 | [diff] [blame] | 670 | static SENSOR_DEVICE_ATTR_RW(intrusion0_alarm, alarm, 12); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 671 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 672 | static struct attribute *adm9240_attrs[] = { |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 673 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 674 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 675 | &sensor_dev_attr_in0_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 676 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 677 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 678 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 679 | &sensor_dev_attr_in1_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 680 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 681 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 682 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 683 | &sensor_dev_attr_in2_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 684 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 685 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 686 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 687 | &sensor_dev_attr_in3_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 688 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 689 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 690 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 691 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 692 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 693 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 694 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 695 | &sensor_dev_attr_in5_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 696 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 697 | &dev_attr_temp1_input.attr, |
| 698 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 699 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 700 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 701 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 702 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 703 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 704 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 705 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 706 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
| 707 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 708 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 709 | &dev_attr_alarms.attr, |
| 710 | &dev_attr_aout_output.attr, |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 711 | &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 712 | &dev_attr_cpu0_vid.attr, |
| 713 | NULL |
| 714 | }; |
| 715 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 716 | ATTRIBUTE_GROUPS(adm9240); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 717 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 718 | /*** sensor chip detect and driver install ***/ |
| 719 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 720 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 721 | static int adm9240_detect(struct i2c_client *new_client, |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 722 | struct i2c_board_info *info) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 723 | { |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 724 | struct i2c_adapter *adapter = new_client->adapter; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 725 | const char *name = ""; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 726 | int address = new_client->addr; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 727 | u8 man_id, die_rev; |
| 728 | |
| 729 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 730 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 731 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 732 | /* verify chip: reg address should match i2c address */ |
| 733 | if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR) |
| 734 | != address) { |
| 735 | dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n", |
| 736 | address); |
| 737 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 738 | } |
| 739 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 740 | /* check known chip manufacturer */ |
| 741 | man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID); |
| 742 | if (man_id == 0x23) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 743 | name = "adm9240"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 744 | } else if (man_id == 0xda) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 745 | name = "ds1780"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 746 | } else if (man_id == 0x01) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 747 | name = "lm81"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 748 | } else { |
| 749 | dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n", |
| 750 | man_id); |
| 751 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 752 | } |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 753 | |
| 754 | /* successful detect, print chip info */ |
| 755 | die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV); |
| 756 | dev_info(&adapter->dev, "found %s revision %u\n", |
| 757 | man_id == 0x23 ? "ADM9240" : |
| 758 | man_id == 0xda ? "DS1780" : "LM81", die_rev); |
| 759 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 760 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 761 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 762 | return 0; |
| 763 | } |
| 764 | |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 765 | static int adm9240_init_client(struct i2c_client *client, struct adm9240_data *data) |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 766 | { |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 767 | u8 conf, mode; |
| 768 | int err; |
| 769 | |
| 770 | err = regmap_raw_read(data->regmap, ADM9240_REG_CONFIG, &conf, 1); |
| 771 | if (err < 0) |
| 772 | return err; |
| 773 | err = regmap_raw_read(data->regmap, ADM9240_REG_TEMP_CONF, &mode, 1); |
| 774 | if (err < 0) |
| 775 | return err; |
| 776 | mode &= 3; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 777 | |
| 778 | data->vrm = vid_which_vrm(); /* need this to report vid as mV */ |
| 779 | |
| 780 | dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10, |
| 781 | data->vrm % 10); |
| 782 | |
| 783 | if (conf & 1) { /* measurement cycle running: report state */ |
| 784 | |
| 785 | dev_info(&client->dev, "status: config 0x%02x mode %u\n", |
| 786 | conf, mode); |
| 787 | |
| 788 | } else { /* cold start: open limits before starting chip */ |
| 789 | int i; |
| 790 | |
| 791 | for (i = 0; i < 6; i++) { |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 792 | err = regmap_write(data->regmap, |
| 793 | ADM9240_REG_IN_MIN(i), 0); |
| 794 | if (err < 0) |
| 795 | return err; |
| 796 | err = regmap_write(data->regmap, |
| 797 | ADM9240_REG_IN_MAX(i), 255); |
| 798 | if (err < 0) |
| 799 | return err; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 800 | } |
Chris Packham | 10d0977 | 2020-09-24 20:51:00 +1200 | [diff] [blame] | 801 | for (i = 0; i < 2; i++) { |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 802 | err = regmap_write(data->regmap, |
Chris Packham | 10d0977 | 2020-09-24 20:51:00 +1200 | [diff] [blame] | 803 | ADM9240_REG_FAN_MIN(i), 255); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 804 | if (err < 0) |
| 805 | return err; |
Chris Packham | 10d0977 | 2020-09-24 20:51:00 +1200 | [diff] [blame] | 806 | } |
| 807 | for (i = 0; i < 2; i++) { |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 808 | err = regmap_write(data->regmap, |
Chris Packham | 10d0977 | 2020-09-24 20:51:00 +1200 | [diff] [blame] | 809 | ADM9240_REG_TEMP_MAX(i), 127); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 810 | if (err < 0) |
| 811 | return err; |
Chris Packham | 10d0977 | 2020-09-24 20:51:00 +1200 | [diff] [blame] | 812 | } |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 813 | |
| 814 | /* start measurement cycle */ |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 815 | err = regmap_write(data->regmap, ADM9240_REG_CONFIG, 1); |
| 816 | if (err < 0) |
| 817 | return err; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 818 | |
| 819 | dev_info(&client->dev, |
| 820 | "cold start: config was 0x%02x mode %u\n", conf, mode); |
| 821 | } |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 822 | |
| 823 | return 0; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 824 | } |
| 825 | |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 826 | static const struct regmap_config adm9240_regmap_config = { |
| 827 | .reg_bits = 8, |
| 828 | .val_bits = 8, |
| 829 | .use_single_read = true, |
| 830 | .use_single_write = true, |
| 831 | }; |
| 832 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 833 | static int adm9240_probe(struct i2c_client *new_client) |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 834 | { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 835 | struct device *dev = &new_client->dev; |
| 836 | struct device *hwmon_dev; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 837 | struct adm9240_data *data; |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 838 | int err; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 839 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 840 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
Guenter Roeck | e5ebb77 | 2012-06-02 09:58:00 -0700 | [diff] [blame] | 841 | if (!data) |
| 842 | return -ENOMEM; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 843 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 844 | data->client = new_client; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 845 | mutex_init(&data->update_lock); |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 846 | data->regmap = devm_regmap_init_i2c(new_client, &adm9240_regmap_config); |
| 847 | if (IS_ERR(data->regmap)) |
| 848 | return PTR_ERR(data->regmap); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 849 | |
Chris Packham | df885d9 | 2020-09-24 20:51:02 +1200 | [diff] [blame^] | 850 | err = adm9240_init_client(new_client, data); |
| 851 | if (err < 0) |
| 852 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 853 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 854 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, |
| 855 | new_client->name, |
| 856 | data, |
| 857 | adm9240_groups); |
| 858 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 859 | } |
| 860 | |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 861 | static const struct i2c_device_id adm9240_id[] = { |
| 862 | { "adm9240", adm9240 }, |
| 863 | { "ds1780", ds1780 }, |
| 864 | { "lm81", lm81 }, |
| 865 | { } |
| 866 | }; |
| 867 | MODULE_DEVICE_TABLE(i2c, adm9240_id); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 868 | |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 869 | static struct i2c_driver adm9240_driver = { |
| 870 | .class = I2C_CLASS_HWMON, |
| 871 | .driver = { |
| 872 | .name = "adm9240", |
| 873 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 874 | .probe_new = adm9240_probe, |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 875 | .id_table = adm9240_id, |
| 876 | .detect = adm9240_detect, |
| 877 | .address_list = normal_i2c, |
| 878 | }; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 879 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 880 | module_i2c_driver(adm9240_driver); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 881 | |
| 882 | MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, " |
Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 883 | "Grant Coady <gcoady.lk@gmail.com> and others"); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 884 | MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver"); |
| 885 | MODULE_LICENSE("GPL"); |