blob: cc3e0184e720aaca4c35b7fccdd72e745ec99636 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Grant Coady40b5cda2005-04-30 21:41:29 +10002/*
3 * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
Guenter Roeckc387e4e2012-01-14 13:12:38 -08004 * monitoring
Grant Coady40b5cda2005-04-30 21:41:29 +10005 *
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 Coady2ca7b962006-10-08 21:57:41 +02009 * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable
Guenter Roeckc387e4e2012-01-14 13:12:38 -080010 * guidance from Jean Delvare
Grant Coady40b5cda2005-04-30 21:41:29 +100011 *
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 Delvare0de2b242011-01-12 21:55:09 +010024 * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm'
Grant Coady40b5cda2005-04-30 21:41:29 +100025 *
26 * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
27 *
28 * LM81 extended temp reading not implemented
Grant Coady40b5cda2005-04-30 21:41:29 +100029 */
30
31#include <linux/init.h>
32#include <linux/module.h>
33#include <linux/slab.h>
34#include <linux/i2c.h>
Grant Coadyc7461a62005-09-17 05:32:57 +100035#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040036#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020037#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040038#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010039#include <linux/mutex.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020040#include <linux/jiffies.h>
Chris Packhamdf885d92020-09-24 20:51:02 +120041#include <linux/regmap.h>
Grant Coady40b5cda2005-04-30 21:41:29 +100042
43/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050044static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
Grant Coady40b5cda2005-04-30 21:41:29 +100045 I2C_CLIENT_END };
46
Jean Delvaree5e9f442009-12-14 21:17:27 +010047enum chips { adm9240, ds1780, lm81 };
Grant Coady40b5cda2005-04-30 21:41:29 +100048
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 Coadyc7461a62005-09-17 05:32:57 +100062#define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
Grant Coady40b5cda2005-04-30 21:41:29 +100063#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 */
71static 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 */
80static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
81
82static inline unsigned int IN_FROM_REG(u8 reg, int n)
83{
84 return SCALE(reg, nom_mv[n], 192);
85}
86
87static inline u8 IN_TO_REG(unsigned long val, int n)
88{
Guenter Roeck0fb620c2016-11-20 10:14:09 -080089 val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
90 return SCALE(val, 192, nom_mv[n]);
Grant Coady40b5cda2005-04-30 21:41:29 +100091}
92
93/* temperature range: -40..125, 127 disables temperature alarm */
94static inline s8 TEMP_TO_REG(long val)
95{
Guenter Roeck0fb620c2016-11-20 10:14:09 -080096 val = clamp_val(val, -40000, 127000);
97 return SCALE(val, 1, 1000);
Grant Coady40b5cda2005-04-30 21:41:29 +100098}
99
100/* two fans, each with low fan speed limit */
101static 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 */
113static inline u8 AOUT_TO_REG(unsigned long val)
114{
Guenter Roeck0fb620c2016-11-20 10:14:09 -0800115 val = clamp_val(val, 0, 1250);
116 return SCALE(val, 255, 1250);
Grant Coady40b5cda2005-04-30 21:41:29 +1000117}
118
119static inline unsigned int AOUT_FROM_REG(u8 reg)
120{
121 return SCALE(reg, 1250, 255);
122}
123
Grant Coady40b5cda2005-04-30 21:41:29 +1000124/* per client data */
125struct adm9240_data {
Axel Lin715f69b2014-07-03 22:01:48 +0800126 struct i2c_client *client;
Chris Packhamdf885d92020-09-24 20:51:02 +1200127 struct regmap *regmap;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100128 struct mutex update_lock;
Grant Coady40b5cda2005-04-30 21:41:29 +1000129 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 Coadyc7461a62005-09-17 05:32:57 +1000140 s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
Grant Coady40b5cda2005-04-30 21:41:29 +1000141 u16 alarms; /* ro alarms */
Grant Coady8e8f9282005-05-13 20:26:10 +1000142 u8 aout; /* rw aout_output */
Grant Coady40b5cda2005-04-30 21:41:29 +1000143 u8 vid; /* ro vid */
144 u8 vrm; /* -- vrm set on startup, no accessor */
145};
146
Axel Lin4341fc32014-07-03 21:59:49 +0800147/* write new fan div, callers must hold data->update_lock */
Chris Packhamdf885d92020-09-24 20:51:02 +1200148static int adm9240_write_fan_div(struct adm9240_data *data, int nr,
Axel Lin4341fc32014-07-03 21:59:49 +0800149 u8 fan_div)
150{
Chris Packhamdf885d92020-09-24 20:51:02 +1200151 unsigned int reg, old, shift = (nr + 2) * 2;
152 int err;
Axel Lin4341fc32014-07-03 21:59:49 +0800153
Chris Packhamdf885d92020-09-24 20:51:02 +1200154 err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, &reg);
155 if (err < 0)
156 return err;
Axel Lin4341fc32014-07-03 21:59:49 +0800157 old = (reg >> shift) & 3;
158 reg &= ~(3 << shift);
159 reg |= (fan_div << shift);
Chris Packhamdf885d92020-09-24 20:51:02 +1200160 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 Lin4341fc32014-07-03 21:59:49 +0800164 "fan%d clock divider changed from %u to %u\n",
165 nr + 1, 1 << old, 1 << fan_div);
Chris Packhamdf885d92020-09-24 20:51:02 +1200166
167 return 0;
Axel Lin4341fc32014-07-03 21:59:49 +0800168}
169
Chris Packham6a8cdd12020-09-24 20:51:01 +1200170static int adm9240_update_measure(struct adm9240_data *data)
171{
Chris Packhamdf885d92020-09-24 20:51:02 +1200172 unsigned int val;
173 u8 regs[2];
174 int err;
Chris Packham6a8cdd12020-09-24 20:51:01 +1200175 int i;
176
Chris Packhamdf885d92020-09-24 20:51:02 +1200177 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), &regs, 2);
181 if (err < 0)
182 return err;
183
184 data->alarms = regs[0] | regs[1] << 8;
Chris Packham6a8cdd12020-09-24 20:51:01 +1200185
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 Packhamdf885d92020-09-24 20:51:02 +1200191 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 Packham6a8cdd12020-09-24 20:51:01 +1200204
205 for (i = 0; i < 2; i++) { /* read fans */
Chris Packham6a8cdd12020-09-24 20:51:01 +1200206 /* adjust fan clock divider on overflow */
207 if (data->valid && data->fan[i] == 255 &&
208 data->fan_div[i] < 3) {
209
Chris Packhamdf885d92020-09-24 20:51:02 +1200210 err = adm9240_write_fan_div(data, i,
Chris Packham6a8cdd12020-09-24 20:51:01 +1200211 ++data->fan_div[i]);
Chris Packhamdf885d92020-09-24 20:51:02 +1200212 if (err < 0)
213 return err;
Chris Packham6a8cdd12020-09-24 20:51:01 +1200214
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
225static int adm9240_update_config(struct adm9240_data *data)
226{
Chris Packhamdf885d92020-09-24 20:51:02 +1200227 unsigned int val;
Chris Packham6a8cdd12020-09-24 20:51:01 +1200228 int i;
Chris Packhamdf885d92020-09-24 20:51:02 +1200229 int err;
Chris Packham6a8cdd12020-09-24 20:51:01 +1200230
231 for (i = 0; i < 6; i++) {
Chris Packhamdf885d92020-09-24 20:51:02 +1200232 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 Packham6a8cdd12020-09-24 20:51:01 +1200240 }
Chris Packhamdf885d92020-09-24 20:51:02 +1200241 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 Packham6a8cdd12020-09-24 20:51:01 +1200249
250 /* read fan divs and 5-bit VID */
Chris Packhamdf885d92020-09-24 20:51:02 +1200251 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 Packham6a8cdd12020-09-24 20:51:01 +1200261 /* read analog out */
Chris Packhamdf885d92020-09-24 20:51:02 +1200262 err = regmap_raw_read(data->regmap, ADM9240_REG_ANALOG_OUT,
263 &data->aout, 1);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200264
Chris Packhamdf885d92020-09-24 20:51:02 +1200265 return err;
Chris Packham6a8cdd12020-09-24 20:51:01 +1200266}
267
Axel Lin4341fc32014-07-03 21:59:49 +0800268static struct adm9240_data *adm9240_update_device(struct device *dev)
269{
Axel Lin715f69b2014-07-03 22:01:48 +0800270 struct adm9240_data *data = dev_get_drvdata(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200271 int err;
Axel Lin4341fc32014-07-03 21:59:49 +0800272
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 Packham6a8cdd12020-09-24 20:51:01 +1200278 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 Lin4341fc32014-07-03 21:59:49 +0800283 }
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 Packham6a8cdd12020-09-24 20:51:01 +1200290 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 Lin4341fc32014-07-03 21:59:49 +0800295 }
Axel Lin4341fc32014-07-03 21:59:49 +0800296 data->last_updated_config = jiffies;
297 data->valid = 1;
298 }
299 mutex_unlock(&data->update_lock);
300 return data;
301}
302
Grant Coady40b5cda2005-04-30 21:41:29 +1000303/*** sysfs accessors ***/
304
305/* temperature */
Julia Lawallb5751112016-12-22 13:04:33 +0100306static ssize_t temp1_input_show(struct device *dev,
307 struct device_attribute *dummy, char *buf)
Grant Coadyc7461a62005-09-17 05:32:57 +1000308{
309 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200310
311 if (IS_ERR(data))
312 return PTR_ERR(data);
313
Chris Packham667f4ba2016-10-05 10:40:54 +1300314 return sprintf(buf, "%d\n", data->temp / 128 * 500); /* 9-bit value */
Grant Coady40b5cda2005-04-30 21:41:29 +1000315}
316
Guenter Roeck7352ae82019-01-21 20:18:52 -0800317static ssize_t max_show(struct device *dev, struct device_attribute *devattr,
318 char *buf)
Grant Coadyc7461a62005-09-17 05:32:57 +1000319{
320 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
321 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200322
323 if (IS_ERR(data))
324 return PTR_ERR(data);
325
Grant Coadyc7461a62005-09-17 05:32:57 +1000326 return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
327}
Grant Coady40b5cda2005-04-30 21:41:29 +1000328
Guenter Roeck7352ae82019-01-21 20:18:52 -0800329static ssize_t max_store(struct device *dev, struct device_attribute *devattr,
330 const char *buf, size_t count)
Grant Coadyc7461a62005-09-17 05:32:57 +1000331{
332 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin715f69b2014-07-03 22:01:48 +0800333 struct adm9240_data *data = dev_get_drvdata(dev);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800334 long val;
335 int err;
336
337 err = kstrtol(buf, 10, &val);
338 if (err)
339 return err;
Grant Coadyc7461a62005-09-17 05:32:57 +1000340
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100341 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000342 data->temp_max[attr->index] = TEMP_TO_REG(val);
Chris Packhamdf885d92020-09-24 20:51:02 +1200343 err = regmap_write(data->regmap, ADM9240_REG_TEMP_MAX(attr->index),
344 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100345 mutex_unlock(&data->update_lock);
Chris Packhamdf885d92020-09-24 20:51:02 +1200346 return err < 0 ? err : count;
Grant Coadyc7461a62005-09-17 05:32:57 +1000347}
348
Julia Lawallb5751112016-12-22 13:04:33 +0100349static DEVICE_ATTR_RO(temp1_input);
Guenter Roeck7352ae82019-01-21 20:18:52 -0800350static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
351static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, max, 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000352
353/* voltage */
Guenter Roeck7352ae82019-01-21 20:18:52 -0800354static ssize_t in_show(struct device *dev, struct device_attribute *devattr,
355 char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000356{
Grant Coadyc7461a62005-09-17 05:32:57 +1000357 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000358 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200359
360 if (IS_ERR(data))
361 return PTR_ERR(data);
362
Grant Coadyc7461a62005-09-17 05:32:57 +1000363 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
364 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000365}
366
Guenter Roeck7352ae82019-01-21 20:18:52 -0800367static ssize_t in_min_show(struct device *dev,
368 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000369{
Grant Coadyc7461a62005-09-17 05:32:57 +1000370 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000371 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200372
373 if (IS_ERR(data))
374 return PTR_ERR(data);
375
Grant Coadyc7461a62005-09-17 05:32:57 +1000376 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
377 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000378}
379
Guenter Roeck7352ae82019-01-21 20:18:52 -0800380static ssize_t in_max_show(struct device *dev,
381 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000382{
Grant Coadyc7461a62005-09-17 05:32:57 +1000383 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000384 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200385
386 if (IS_ERR(data))
387 return PTR_ERR(data);
388
Grant Coadyc7461a62005-09-17 05:32:57 +1000389 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
390 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000391}
392
Guenter Roeck7352ae82019-01-21 20:18:52 -0800393static ssize_t in_min_store(struct device *dev,
394 struct device_attribute *devattr, const char *buf,
395 size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000396{
Grant Coadyc7461a62005-09-17 05:32:57 +1000397 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin715f69b2014-07-03 22:01:48 +0800398 struct adm9240_data *data = dev_get_drvdata(dev);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800399 unsigned long val;
400 int err;
401
402 err = kstrtoul(buf, 10, &val);
403 if (err)
404 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000405
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000407 data->in_min[attr->index] = IN_TO_REG(val, attr->index);
Chris Packhamdf885d92020-09-24 20:51:02 +1200408 err = regmap_write(data->regmap, ADM9240_REG_IN_MIN(attr->index),
409 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100410 mutex_unlock(&data->update_lock);
Chris Packhamdf885d92020-09-24 20:51:02 +1200411 return err < 0 ? err : count;
Grant Coady40b5cda2005-04-30 21:41:29 +1000412}
413
Guenter Roeck7352ae82019-01-21 20:18:52 -0800414static ssize_t in_max_store(struct device *dev,
415 struct device_attribute *devattr, const char *buf,
416 size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000417{
Grant Coadyc7461a62005-09-17 05:32:57 +1000418 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin715f69b2014-07-03 22:01:48 +0800419 struct adm9240_data *data = dev_get_drvdata(dev);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800420 unsigned long val;
421 int err;
422
423 err = kstrtoul(buf, 10, &val);
424 if (err)
425 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000426
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100427 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000428 data->in_max[attr->index] = IN_TO_REG(val, attr->index);
Chris Packhamdf885d92020-09-24 20:51:02 +1200429 err = regmap_write(data->regmap, ADM9240_REG_IN_MAX(attr->index),
430 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100431 mutex_unlock(&data->update_lock);
Chris Packhamdf885d92020-09-24 20:51:02 +1200432 return err < 0 ? err : count;
Grant Coady40b5cda2005-04-30 21:41:29 +1000433}
434
Guenter Roeck7352ae82019-01-21 20:18:52 -0800435static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
436static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
437static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
438static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
439static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
440static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
441static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
442static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
443static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
444static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
445static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
446static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
447static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
448static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
449static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
450static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
451static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
452static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
Grant Coady40b5cda2005-04-30 21:41:29 +1000453
454/* fans */
Guenter Roeck7352ae82019-01-21 20:18:52 -0800455static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
456 char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000457{
Grant Coadyc7461a62005-09-17 05:32:57 +1000458 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000459 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200460
461 if (IS_ERR(data))
462 return PTR_ERR(data);
463
Grant Coadyc7461a62005-09-17 05:32:57 +1000464 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
465 1 << data->fan_div[attr->index]));
Grant Coady40b5cda2005-04-30 21:41:29 +1000466}
467
Guenter Roeck7352ae82019-01-21 20:18:52 -0800468static ssize_t fan_min_show(struct device *dev,
469 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000470{
Grant Coadyc7461a62005-09-17 05:32:57 +1000471 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000472 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200473
474 if (IS_ERR(data))
475 return PTR_ERR(data);
476
Grant Coadyc7461a62005-09-17 05:32:57 +1000477 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
478 1 << data->fan_div[attr->index]));
Grant Coady40b5cda2005-04-30 21:41:29 +1000479}
480
Guenter Roeck7352ae82019-01-21 20:18:52 -0800481static ssize_t fan_div_show(struct device *dev,
482 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000483{
Grant Coadyc7461a62005-09-17 05:32:57 +1000484 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000485 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200486
487 if (IS_ERR(data))
488 return PTR_ERR(data);
489
Grant Coadyc7461a62005-09-17 05:32:57 +1000490 return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
Grant Coady40b5cda2005-04-30 21:41:29 +1000491}
492
Jean Delvaree415e482005-09-25 16:14:18 +0200493/*
Grant Coady40b5cda2005-04-30 21:41:29 +1000494 * 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 Roeck7352ae82019-01-21 20:18:52 -0800504static ssize_t fan_min_store(struct device *dev,
505 struct device_attribute *devattr,
506 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000507{
Grant Coadyc7461a62005-09-17 05:32:57 +1000508 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin715f69b2014-07-03 22:01:48 +0800509 struct adm9240_data *data = dev_get_drvdata(dev);
510 struct i2c_client *client = data->client;
Grant Coadyc7461a62005-09-17 05:32:57 +1000511 int nr = attr->index;
Grant Coady40b5cda2005-04-30 21:41:29 +1000512 u8 new_div;
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800513 unsigned long val;
514 int err;
515
516 err = kstrtoul(buf, 10, &val);
517 if (err)
518 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000519
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100520 mutex_lock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000521
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 Packhamdf885d92020-09-24 20:51:02 +1200555 adm9240_write_fan_div(data, nr, new_div);
Grant Coady40b5cda2005-04-30 21:41:29 +1000556 }
Chris Packhamdf885d92020-09-24 20:51:02 +1200557 err = regmap_write(data->regmap, ADM9240_REG_FAN_MIN(nr),
558 data->fan_min[nr]);
Grant Coady40b5cda2005-04-30 21:41:29 +1000559
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100560 mutex_unlock(&data->update_lock);
Chris Packhamdf885d92020-09-24 20:51:02 +1200561 return err < 0 ? err : count;
Grant Coady40b5cda2005-04-30 21:41:29 +1000562}
563
Guenter Roeck7352ae82019-01-21 20:18:52 -0800564static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
565static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
566static SENSOR_DEVICE_ATTR_RO(fan1_div, fan_div, 0);
567static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
568static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
569static SENSOR_DEVICE_ATTR_RO(fan2_div, fan_div, 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000570
571/* alarms */
Julia Lawallb5751112016-12-22 13:04:33 +0100572static ssize_t alarms_show(struct device *dev,
Jean Delvaree415e482005-09-25 16:14:18 +0200573 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000574{
575 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200576
577 if (IS_ERR(data))
578 return PTR_ERR(data);
579
Grant Coady40b5cda2005-04-30 21:41:29 +1000580 return sprintf(buf, "%u\n", data->alarms);
581}
Julia Lawallb5751112016-12-22 13:04:33 +0100582static DEVICE_ATTR_RO(alarms);
Grant Coady40b5cda2005-04-30 21:41:29 +1000583
Guenter Roeck7352ae82019-01-21 20:18:52 -0800584static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
585 char *buf)
Jean Delvare360f9452008-01-06 15:49:19 +0100586{
587 int bitnr = to_sensor_dev_attr(attr)->index;
588 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200589
590 if (IS_ERR(data))
591 return PTR_ERR(data);
592
Jean Delvare360f9452008-01-06 15:49:19 +0100593 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
594}
Guenter Roeck7352ae82019-01-21 20:18:52 -0800595static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
596static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
597static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
598static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
599static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
600static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
601static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
602static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
603static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
Jean Delvare360f9452008-01-06 15:49:19 +0100604
Grant Coady40b5cda2005-04-30 21:41:29 +1000605/* vid */
Julia Lawallb5751112016-12-22 13:04:33 +0100606static ssize_t cpu0_vid_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000608{
609 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200610
611 if (IS_ERR(data))
612 return PTR_ERR(data);
613
Grant Coady40b5cda2005-04-30 21:41:29 +1000614 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
615}
Julia Lawallb5751112016-12-22 13:04:33 +0100616static DEVICE_ATTR_RO(cpu0_vid);
Grant Coady40b5cda2005-04-30 21:41:29 +1000617
618/* analog output */
Julia Lawallb5751112016-12-22 13:04:33 +0100619static ssize_t aout_output_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000621{
622 struct adm9240_data *data = adm9240_update_device(dev);
Chris Packham6a8cdd12020-09-24 20:51:01 +1200623
624 if (IS_ERR(data))
625 return PTR_ERR(data);
626
Grant Coady40b5cda2005-04-30 21:41:29 +1000627 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
628}
629
Julia Lawallb5751112016-12-22 13:04:33 +0100630static ssize_t aout_output_store(struct device *dev,
631 struct device_attribute *attr,
632 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000633{
Axel Lin715f69b2014-07-03 22:01:48 +0800634 struct adm9240_data *data = dev_get_drvdata(dev);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800635 long val;
636 int err;
637
638 err = kstrtol(buf, 10, &val);
639 if (err)
640 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000641
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100642 mutex_lock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000643 data->aout = AOUT_TO_REG(val);
Chris Packhamdf885d92020-09-24 20:51:02 +1200644 err = regmap_write(data->regmap, ADM9240_REG_ANALOG_OUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100645 mutex_unlock(&data->update_lock);
Chris Packhamdf885d92020-09-24 20:51:02 +1200646 return err < 0 ? err : count;
Grant Coady40b5cda2005-04-30 21:41:29 +1000647}
Julia Lawallb5751112016-12-22 13:04:33 +0100648static DEVICE_ATTR_RW(aout_output);
Grant Coady40b5cda2005-04-30 21:41:29 +1000649
Guenter Roeck7352ae82019-01-21 20:18:52 -0800650static ssize_t alarm_store(struct device *dev, struct device_attribute *attr,
651 const char *buf, size_t count)
Jean Delvare0de2b242011-01-12 21:55:09 +0100652{
Axel Lin715f69b2014-07-03 22:01:48 +0800653 struct adm9240_data *data = dev_get_drvdata(dev);
Jean Delvare0de2b242011-01-12 21:55:09 +0100654 unsigned long val;
Chris Packhamdf885d92020-09-24 20:51:02 +1200655 int err;
Jean Delvare0de2b242011-01-12 21:55:09 +0100656
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100657 if (kstrtoul(buf, 10, &val) || val != 0)
Jean Delvare0de2b242011-01-12 21:55:09 +0100658 return -EINVAL;
659
660 mutex_lock(&data->update_lock);
Chris Packhamdf885d92020-09-24 20:51:02 +1200661 err = regmap_write(data->regmap, ADM9240_REG_CHASSIS_CLEAR, 0x80);
Jean Delvare0de2b242011-01-12 21:55:09 +0100662 data->valid = 0; /* Force cache refresh */
663 mutex_unlock(&data->update_lock);
Chris Packhamdf885d92020-09-24 20:51:02 +1200664 if (err < 0)
665 return err;
666 dev_dbg(&data->client->dev, "chassis intrusion latch cleared\n");
Jean Delvare0de2b242011-01-12 21:55:09 +0100667
668 return count;
669}
Guenter Roeck7352ae82019-01-21 20:18:52 -0800670static SENSOR_DEVICE_ATTR_RW(intrusion0_alarm, alarm, 12);
Grant Coady40b5cda2005-04-30 21:41:29 +1000671
Axel Lin715f69b2014-07-03 22:01:48 +0800672static struct attribute *adm9240_attrs[] = {
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200673 &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 Delvare360f9452008-01-06 15:49:19 +0100676 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200677 &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 Delvare360f9452008-01-06 15:49:19 +0100680 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200681 &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 Delvare360f9452008-01-06 15:49:19 +0100684 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200685 &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 Delvare360f9452008-01-06 15:49:19 +0100688 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200689 &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 Delvare360f9452008-01-06 15:49:19 +0100692 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200693 &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 Delvare360f9452008-01-06 15:49:19 +0100696 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200697 &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 Delvare360f9452008-01-06 15:49:19 +0100700 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200701 &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 Delvare360f9452008-01-06 15:49:19 +0100704 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200705 &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 Delvare360f9452008-01-06 15:49:19 +0100708 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200709 &dev_attr_alarms.attr,
710 &dev_attr_aout_output.attr,
Jean Delvare0de2b242011-01-12 21:55:09 +0100711 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200712 &dev_attr_cpu0_vid.attr,
713 NULL
714};
715
Axel Lin715f69b2014-07-03 22:01:48 +0800716ATTRIBUTE_GROUPS(adm9240);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200717
Grant Coady40b5cda2005-04-30 21:41:29 +1000718/*** sensor chip detect and driver install ***/
719
Jean Delvare7fae8282008-07-16 19:30:09 +0200720/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100721static int adm9240_detect(struct i2c_client *new_client,
Jean Delvare7fae8282008-07-16 19:30:09 +0200722 struct i2c_board_info *info)
Grant Coady40b5cda2005-04-30 21:41:29 +1000723{
Jean Delvare7fae8282008-07-16 19:30:09 +0200724 struct i2c_adapter *adapter = new_client->adapter;
Grant Coady40b5cda2005-04-30 21:41:29 +1000725 const char *name = "";
Jean Delvare7fae8282008-07-16 19:30:09 +0200726 int address = new_client->addr;
Grant Coady40b5cda2005-04-30 21:41:29 +1000727 u8 man_id, die_rev;
728
729 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare7fae8282008-07-16 19:30:09 +0200730 return -ENODEV;
Grant Coady40b5cda2005-04-30 21:41:29 +1000731
Jean Delvare52df6442009-12-09 20:35:57 +0100732 /* 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 Coady40b5cda2005-04-30 21:41:29 +1000738 }
739
Jean Delvare52df6442009-12-09 20:35:57 +0100740 /* check known chip manufacturer */
741 man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
742 if (man_id == 0x23) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000743 name = "adm9240";
Jean Delvare52df6442009-12-09 20:35:57 +0100744 } else if (man_id == 0xda) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000745 name = "ds1780";
Jean Delvare52df6442009-12-09 20:35:57 +0100746 } else if (man_id == 0x01) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000747 name = "lm81";
Jean Delvare52df6442009-12-09 20:35:57 +0100748 } else {
749 dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
750 man_id);
751 return -ENODEV;
Grant Coady40b5cda2005-04-30 21:41:29 +1000752 }
Jean Delvare52df6442009-12-09 20:35:57 +0100753
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 Delvare7fae8282008-07-16 19:30:09 +0200760 strlcpy(info->type, name, I2C_NAME_SIZE);
Grant Coady40b5cda2005-04-30 21:41:29 +1000761
Jean Delvare7fae8282008-07-16 19:30:09 +0200762 return 0;
763}
764
Chris Packhamdf885d92020-09-24 20:51:02 +1200765static int adm9240_init_client(struct i2c_client *client, struct adm9240_data *data)
Axel Lin4341fc32014-07-03 21:59:49 +0800766{
Chris Packhamdf885d92020-09-24 20:51:02 +1200767 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 Lin4341fc32014-07-03 21:59:49 +0800777
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 Packhamdf885d92020-09-24 20:51:02 +1200792 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 Lin4341fc32014-07-03 21:59:49 +0800800 }
Chris Packham10d09772020-09-24 20:51:00 +1200801 for (i = 0; i < 2; i++) {
Chris Packhamdf885d92020-09-24 20:51:02 +1200802 err = regmap_write(data->regmap,
Chris Packham10d09772020-09-24 20:51:00 +1200803 ADM9240_REG_FAN_MIN(i), 255);
Chris Packhamdf885d92020-09-24 20:51:02 +1200804 if (err < 0)
805 return err;
Chris Packham10d09772020-09-24 20:51:00 +1200806 }
807 for (i = 0; i < 2; i++) {
Chris Packhamdf885d92020-09-24 20:51:02 +1200808 err = regmap_write(data->regmap,
Chris Packham10d09772020-09-24 20:51:00 +1200809 ADM9240_REG_TEMP_MAX(i), 127);
Chris Packhamdf885d92020-09-24 20:51:02 +1200810 if (err < 0)
811 return err;
Chris Packham10d09772020-09-24 20:51:00 +1200812 }
Axel Lin4341fc32014-07-03 21:59:49 +0800813
814 /* start measurement cycle */
Chris Packhamdf885d92020-09-24 20:51:02 +1200815 err = regmap_write(data->regmap, ADM9240_REG_CONFIG, 1);
816 if (err < 0)
817 return err;
Axel Lin4341fc32014-07-03 21:59:49 +0800818
819 dev_info(&client->dev,
820 "cold start: config was 0x%02x mode %u\n", conf, mode);
821 }
Chris Packhamdf885d92020-09-24 20:51:02 +1200822
823 return 0;
Axel Lin4341fc32014-07-03 21:59:49 +0800824}
825
Chris Packhamdf885d92020-09-24 20:51:02 +1200826static 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 Kitt67487032020-08-13 18:02:22 +0200833static int adm9240_probe(struct i2c_client *new_client)
Jean Delvare7fae8282008-07-16 19:30:09 +0200834{
Axel Lin715f69b2014-07-03 22:01:48 +0800835 struct device *dev = &new_client->dev;
836 struct device *hwmon_dev;
Jean Delvare7fae8282008-07-16 19:30:09 +0200837 struct adm9240_data *data;
Chris Packhamdf885d92020-09-24 20:51:02 +1200838 int err;
Jean Delvare7fae8282008-07-16 19:30:09 +0200839
Axel Lin715f69b2014-07-03 22:01:48 +0800840 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Guenter Roecke5ebb772012-06-02 09:58:00 -0700841 if (!data)
842 return -ENOMEM;
Jean Delvare7fae8282008-07-16 19:30:09 +0200843
Axel Lin715f69b2014-07-03 22:01:48 +0800844 data->client = new_client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100845 mutex_init(&data->update_lock);
Chris Packhamdf885d92020-09-24 20:51:02 +1200846 data->regmap = devm_regmap_init_i2c(new_client, &adm9240_regmap_config);
847 if (IS_ERR(data->regmap))
848 return PTR_ERR(data->regmap);
Grant Coady40b5cda2005-04-30 21:41:29 +1000849
Chris Packhamdf885d92020-09-24 20:51:02 +1200850 err = adm9240_init_client(new_client, data);
851 if (err < 0)
852 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000853
Axel Lin715f69b2014-07-03 22:01:48 +0800854 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 Coady40b5cda2005-04-30 21:41:29 +1000859}
860
Axel Lin4341fc32014-07-03 21:59:49 +0800861static const struct i2c_device_id adm9240_id[] = {
862 { "adm9240", adm9240 },
863 { "ds1780", ds1780 },
864 { "lm81", lm81 },
865 { }
866};
867MODULE_DEVICE_TABLE(i2c, adm9240_id);
Grant Coady40b5cda2005-04-30 21:41:29 +1000868
Axel Lin4341fc32014-07-03 21:59:49 +0800869static struct i2c_driver adm9240_driver = {
870 .class = I2C_CLASS_HWMON,
871 .driver = {
872 .name = "adm9240",
873 },
Stephen Kitt67487032020-08-13 18:02:22 +0200874 .probe_new = adm9240_probe,
Axel Lin4341fc32014-07-03 21:59:49 +0800875 .id_table = adm9240_id,
876 .detect = adm9240_detect,
877 .address_list = normal_i2c,
878};
Grant Coady40b5cda2005-04-30 21:41:29 +1000879
Axel Linf0967ee2012-01-20 15:38:18 +0800880module_i2c_driver(adm9240_driver);
Grant Coady40b5cda2005-04-30 21:41:29 +1000881
882MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
Grant Coady2ca7b962006-10-08 21:57:41 +0200883 "Grant Coady <gcoady.lk@gmail.com> and others");
Grant Coady40b5cda2005-04-30 21:41:29 +1000884MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
885MODULE_LICENSE("GPL");