blob: 4352f6a884e8f3eec93dd324336a50c4c021dd84 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * adm1025.c
4 *
5 * Copyright (C) 2000 Chen-Yuan Wu <gwu@esoft.com>
Jean Delvare7c81c60f2014-01-29 20:40:08 +01006 * Copyright (C) 2003-2009 Jean Delvare <jdelvare@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6
9 * voltages (including its own power source) and up to two temperatures
10 * (its own plus up to one external one). Voltages are scaled internally
11 * (which is not the common way) with ratios such that the nominal value
12 * of each voltage correspond to a register value of 192 (which means a
13 * resolution of about 0.5% of the nominal value). Temperature values are
14 * reported with a 1 deg resolution and a 3 deg accuracy. Complete
15 * datasheet can be obtained from Analog's website at:
Alexander A. Klimov0303d062020-07-13 16:00:28 +020016 * https://www.onsemi.com/PowerSolutions/product.do?id=ADM1025
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This driver also supports the ADM1025A, which differs from the ADM1025
19 * only in that it has "open-drain VID inputs while the ADM1025 has
20 * on-chip 100k pull-ups on the VID inputs". It doesn't make any
21 * difference for us.
22 *
23 * This driver also supports the NE1619, a sensor chip made by Philips.
24 * That chip is similar to the ADM1025A, with a few differences. The only
25 * difference that matters to us is that the NE1619 has only two possible
26 * addresses while the ADM1025A has a third one. Complete datasheet can be
27 * obtained from Philips's website at:
28 * http://www.semiconductors.philips.com/pip/NE1619DS.html
29 *
30 * Since the ADM1025 was the first chipset supported by this driver, most
31 * comments will refer to this chipset, but are actually general and
32 * concern all supported chipsets, unless mentioned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/jiffies.h>
39#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040040#include <linux/hwmon.h>
Jean Delvared8543e72007-10-10 21:11:01 +020041#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020042#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010044#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * Addresses to scan
48 * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e.
49 * NE1619 has two possible addresses: 0x2c and 0x2d.
50 */
51
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050052static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jean Delvaree5e9f442009-12-14 21:17:27 +010054enum chips { adm1025, ne1619 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * The ADM1025 registers
58 */
59
60#define ADM1025_REG_MAN_ID 0x3E
Jean Delvare4e952792007-10-10 21:14:11 +020061#define ADM1025_REG_CHIP_ID 0x3F
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define ADM1025_REG_CONFIG 0x40
63#define ADM1025_REG_STATUS1 0x41
64#define ADM1025_REG_STATUS2 0x42
65#define ADM1025_REG_IN(nr) (0x20 + (nr))
66#define ADM1025_REG_IN_MAX(nr) (0x2B + (nr) * 2)
67#define ADM1025_REG_IN_MIN(nr) (0x2C + (nr) * 2)
68#define ADM1025_REG_TEMP(nr) (0x26 + (nr))
69#define ADM1025_REG_TEMP_HIGH(nr) (0x37 + (nr) * 2)
70#define ADM1025_REG_TEMP_LOW(nr) (0x38 + (nr) * 2)
71#define ADM1025_REG_VID 0x47
72#define ADM1025_REG_VID4 0x49
73
74/*
75 * Conversions and various macros
76 * The ADM1025 uses signed 8-bit values for temperatures.
77 */
78
Jean Delvare4e952792007-10-10 21:14:11 +020079static const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Guenter Roeck2b22de52012-01-14 12:49:22 -080081#define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192)
82#define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \
Guenter Roeck7cb53e22016-12-03 11:07:14 -080083 (val) >= (scale) * 255 / 192 ? 255 : \
Guenter Roeck2b22de52012-01-14 12:49:22 -080084 ((val) * 192 + (scale) / 2) / (scale))
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#define TEMP_FROM_REG(reg) ((reg) * 1000)
87#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
88 (val) >= 126500 ? 127 : \
Guenter Roeck2b22de52012-01-14 12:49:22 -080089 (((val) < 0 ? (val) - 500 : \
90 (val) + 500) / 1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Client data (each client gets its own)
94 */
95
96struct adm1025_data {
Axel Linaab3f572014-07-03 21:49:27 +080097 struct i2c_client *client;
98 const struct attribute_group *groups[3];
Ingo Molnar9a61bf62006-01-18 23:19:26 +010099 struct mutex update_lock;
Paul Fertser952a11c2021-09-24 22:52:02 +0300100 bool valid; /* false until following fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 unsigned long last_updated; /* in jiffies */
102
103 u8 in[6]; /* register value */
104 u8 in_max[6]; /* register value */
105 u8 in_min[6]; /* register value */
106 s8 temp[2]; /* register value */
107 s8 temp_min[2]; /* register value */
108 s8 temp_max[2]; /* register value */
109 u16 alarms; /* register values, combined */
110 u8 vid; /* register values, combined */
111 u8 vrm;
112};
113
Axel Lin851a7af2014-07-03 21:48:15 +0800114static struct adm1025_data *adm1025_update_device(struct device *dev)
115{
Axel Linaab3f572014-07-03 21:49:27 +0800116 struct adm1025_data *data = dev_get_drvdata(dev);
117 struct i2c_client *client = data->client;
Axel Lin851a7af2014-07-03 21:48:15 +0800118
119 mutex_lock(&data->update_lock);
120
121 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
122 int i;
123
124 dev_dbg(&client->dev, "Updating data.\n");
125 for (i = 0; i < 6; i++) {
126 data->in[i] = i2c_smbus_read_byte_data(client,
127 ADM1025_REG_IN(i));
128 data->in_min[i] = i2c_smbus_read_byte_data(client,
129 ADM1025_REG_IN_MIN(i));
130 data->in_max[i] = i2c_smbus_read_byte_data(client,
131 ADM1025_REG_IN_MAX(i));
132 }
133 for (i = 0; i < 2; i++) {
134 data->temp[i] = i2c_smbus_read_byte_data(client,
135 ADM1025_REG_TEMP(i));
136 data->temp_min[i] = i2c_smbus_read_byte_data(client,
137 ADM1025_REG_TEMP_LOW(i));
138 data->temp_max[i] = i2c_smbus_read_byte_data(client,
139 ADM1025_REG_TEMP_HIGH(i));
140 }
141 data->alarms = i2c_smbus_read_byte_data(client,
142 ADM1025_REG_STATUS1)
143 | (i2c_smbus_read_byte_data(client,
144 ADM1025_REG_STATUS2) << 8);
145 data->vid = (i2c_smbus_read_byte_data(client,
146 ADM1025_REG_VID) & 0x0f)
147 | ((i2c_smbus_read_byte_data(client,
148 ADM1025_REG_VID4) & 0x01) << 4);
149
150 data->last_updated = jiffies;
Paul Fertser952a11c2021-09-24 22:52:02 +0300151 data->valid = true;
Axel Lin851a7af2014-07-03 21:48:15 +0800152 }
153
154 mutex_unlock(&data->update_lock);
155
156 return data;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * Sysfs stuff
161 */
162
Jean Delvared8543e72007-10-10 21:11:01 +0200163static ssize_t
Guenter Roeck66efcd22019-01-21 18:33:47 -0800164in_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvared8543e72007-10-10 21:11:01 +0200165{
166 int index = to_sensor_dev_attr(attr)->index;
167 struct adm1025_data *data = adm1025_update_device(dev);
168 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[index],
169 in_scale[index]));
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jean Delvared8543e72007-10-10 21:11:01 +0200172static ssize_t
Guenter Roeck66efcd22019-01-21 18:33:47 -0800173in_min_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvared8543e72007-10-10 21:11:01 +0200174{
175 int index = to_sensor_dev_attr(attr)->index;
176 struct adm1025_data *data = adm1025_update_device(dev);
177 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[index],
178 in_scale[index]));
179}
180
181static ssize_t
Guenter Roeck66efcd22019-01-21 18:33:47 -0800182in_max_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvared8543e72007-10-10 21:11:01 +0200183{
184 int index = to_sensor_dev_attr(attr)->index;
185 struct adm1025_data *data = adm1025_update_device(dev);
186 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[index],
187 in_scale[index]));
188}
189
190static ssize_t
Guenter Roeck66efcd22019-01-21 18:33:47 -0800191temp_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvared8543e72007-10-10 21:11:01 +0200192{
193 int index = to_sensor_dev_attr(attr)->index;
194 struct adm1025_data *data = adm1025_update_device(dev);
195 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[index]));
196}
197
198static ssize_t
Guenter Roeck66efcd22019-01-21 18:33:47 -0800199temp_min_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvared8543e72007-10-10 21:11:01 +0200200{
201 int index = to_sensor_dev_attr(attr)->index;
202 struct adm1025_data *data = adm1025_update_device(dev);
203 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[index]));
204}
205
206static ssize_t
Guenter Roeck66efcd22019-01-21 18:33:47 -0800207temp_max_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvared8543e72007-10-10 21:11:01 +0200208{
209 int index = to_sensor_dev_attr(attr)->index;
210 struct adm1025_data *data = adm1025_update_device(dev);
211 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
212}
213
Guenter Roeck66efcd22019-01-21 18:33:47 -0800214static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
215 const char *buf, size_t count)
Jean Delvared8543e72007-10-10 21:11:01 +0200216{
217 int index = to_sensor_dev_attr(attr)->index;
Axel Linaab3f572014-07-03 21:49:27 +0800218 struct adm1025_data *data = dev_get_drvdata(dev);
219 struct i2c_client *client = data->client;
Guenter Roeck2b22de52012-01-14 12:49:22 -0800220 long val;
221 int err;
222
223 err = kstrtol(buf, 10, &val);
224 if (err)
225 return err;
Jean Delvared8543e72007-10-10 21:11:01 +0200226
227 mutex_lock(&data->update_lock);
228 data->in_min[index] = IN_TO_REG(val, in_scale[index]);
229 i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(index),
230 data->in_min[index]);
231 mutex_unlock(&data->update_lock);
232 return count;
233}
234
Guenter Roeck66efcd22019-01-21 18:33:47 -0800235static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
236 const char *buf, size_t count)
Jean Delvared8543e72007-10-10 21:11:01 +0200237{
238 int index = to_sensor_dev_attr(attr)->index;
Axel Linaab3f572014-07-03 21:49:27 +0800239 struct adm1025_data *data = dev_get_drvdata(dev);
240 struct i2c_client *client = data->client;
Guenter Roeck2b22de52012-01-14 12:49:22 -0800241 long val;
242 int err;
243
244 err = kstrtol(buf, 10, &val);
245 if (err)
246 return err;
Jean Delvared8543e72007-10-10 21:11:01 +0200247
248 mutex_lock(&data->update_lock);
249 data->in_max[index] = IN_TO_REG(val, in_scale[index]);
250 i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(index),
251 data->in_max[index]);
252 mutex_unlock(&data->update_lock);
253 return count;
254}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Guenter Roeck66efcd22019-01-21 18:33:47 -0800256static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
257static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
258static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
259static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
260static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
261static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
262static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
263static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
264static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
265static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
266static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
267static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
268static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
269static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
270static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
271static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
272static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
273static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Guenter Roeck66efcd22019-01-21 18:33:47 -0800275static ssize_t temp_min_store(struct device *dev,
276 struct device_attribute *attr, const char *buf,
277 size_t count)
Jean Delvared8543e72007-10-10 21:11:01 +0200278{
279 int index = to_sensor_dev_attr(attr)->index;
Axel Linaab3f572014-07-03 21:49:27 +0800280 struct adm1025_data *data = dev_get_drvdata(dev);
281 struct i2c_client *client = data->client;
Guenter Roeck2b22de52012-01-14 12:49:22 -0800282 long val;
283 int err;
284
285 err = kstrtol(buf, 10, &val);
286 if (err)
287 return err;
Jean Delvared8543e72007-10-10 21:11:01 +0200288
289 mutex_lock(&data->update_lock);
290 data->temp_min[index] = TEMP_TO_REG(val);
291 i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(index),
292 data->temp_min[index]);
293 mutex_unlock(&data->update_lock);
294 return count;
295}
296
Guenter Roeck66efcd22019-01-21 18:33:47 -0800297static ssize_t temp_max_store(struct device *dev,
298 struct device_attribute *attr, const char *buf,
299 size_t count)
Jean Delvared8543e72007-10-10 21:11:01 +0200300{
301 int index = to_sensor_dev_attr(attr)->index;
Axel Linaab3f572014-07-03 21:49:27 +0800302 struct adm1025_data *data = dev_get_drvdata(dev);
303 struct i2c_client *client = data->client;
Guenter Roeck2b22de52012-01-14 12:49:22 -0800304 long val;
305 int err;
306
307 err = kstrtol(buf, 10, &val);
308 if (err)
309 return err;
Jean Delvared8543e72007-10-10 21:11:01 +0200310
311 mutex_lock(&data->update_lock);
312 data->temp_max[index] = TEMP_TO_REG(val);
313 i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(index),
314 data->temp_max[index]);
315 mutex_unlock(&data->update_lock);
316 return count;
317}
318
Guenter Roeck66efcd22019-01-21 18:33:47 -0800319static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
320static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
321static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
322static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
323static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
324static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Jean Delvare4e952792007-10-10 21:14:11 +0200326static ssize_t
Julia Lawall41919a52016-12-22 13:05:28 +0100327alarms_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct adm1025_data *data = adm1025_update_device(dev);
330 return sprintf(buf, "%u\n", data->alarms);
331}
Julia Lawall41919a52016-12-22 13:05:28 +0100332static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Jean Delvarebb081302007-10-10 21:11:52 +0200334static ssize_t
Guenter Roeck66efcd22019-01-21 18:33:47 -0800335alarm_show(struct device *dev, struct device_attribute *attr, char *buf)
Jean Delvarebb081302007-10-10 21:11:52 +0200336{
337 int bitnr = to_sensor_dev_attr(attr)->index;
338 struct adm1025_data *data = adm1025_update_device(dev);
339 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
340}
Guenter Roeck66efcd22019-01-21 18:33:47 -0800341static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
342static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
343static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
344static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
345static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
346static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
347static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 5);
348static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 4);
349static SENSOR_DEVICE_ATTR_RO(temp1_fault, alarm, 14);
Jean Delvarebb081302007-10-10 21:11:52 +0200350
Jean Delvare4e952792007-10-10 21:14:11 +0200351static ssize_t
Julia Lawall41919a52016-12-22 13:05:28 +0100352cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct adm1025_data *data = adm1025_update_device(dev);
355 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
356}
Julia Lawall41919a52016-12-22 13:05:28 +0100357static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Jean Delvare4e952792007-10-10 21:14:11 +0200359static ssize_t
Julia Lawall41919a52016-12-22 13:05:28 +0100360vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Jean Delvare90d66192007-10-08 18:24:35 +0200362 struct adm1025_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return sprintf(buf, "%u\n", data->vrm);
364}
Julia Lawall41919a52016-12-22 13:05:28 +0100365static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
366 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100368 struct adm1025_data *data = dev_get_drvdata(dev);
Guenter Roeck2b22de52012-01-14 12:49:22 -0800369 unsigned long val;
370 int err;
371
372 err = kstrtoul(buf, 10, &val);
373 if (err)
374 return err;
375
Axel Lin9c8ae722014-08-06 08:19:43 +0800376 if (val > 255)
377 return -EINVAL;
378
Guenter Roeck2b22de52012-01-14 12:49:22 -0800379 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return count;
381}
Julia Lawall41919a52016-12-22 13:05:28 +0100382static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384/*
385 * Real code
386 */
387
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200388static struct attribute *adm1025_attributes[] = {
Jean Delvared8543e72007-10-10 21:11:01 +0200389 &sensor_dev_attr_in0_input.dev_attr.attr,
390 &sensor_dev_attr_in1_input.dev_attr.attr,
391 &sensor_dev_attr_in2_input.dev_attr.attr,
392 &sensor_dev_attr_in3_input.dev_attr.attr,
393 &sensor_dev_attr_in5_input.dev_attr.attr,
394 &sensor_dev_attr_in0_min.dev_attr.attr,
395 &sensor_dev_attr_in1_min.dev_attr.attr,
396 &sensor_dev_attr_in2_min.dev_attr.attr,
397 &sensor_dev_attr_in3_min.dev_attr.attr,
398 &sensor_dev_attr_in5_min.dev_attr.attr,
399 &sensor_dev_attr_in0_max.dev_attr.attr,
400 &sensor_dev_attr_in1_max.dev_attr.attr,
401 &sensor_dev_attr_in2_max.dev_attr.attr,
402 &sensor_dev_attr_in3_max.dev_attr.attr,
403 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvarebb081302007-10-10 21:11:52 +0200404 &sensor_dev_attr_in0_alarm.dev_attr.attr,
405 &sensor_dev_attr_in1_alarm.dev_attr.attr,
406 &sensor_dev_attr_in2_alarm.dev_attr.attr,
407 &sensor_dev_attr_in3_alarm.dev_attr.attr,
408 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Jean Delvared8543e72007-10-10 21:11:01 +0200409 &sensor_dev_attr_temp1_input.dev_attr.attr,
410 &sensor_dev_attr_temp2_input.dev_attr.attr,
411 &sensor_dev_attr_temp1_min.dev_attr.attr,
412 &sensor_dev_attr_temp2_min.dev_attr.attr,
413 &sensor_dev_attr_temp1_max.dev_attr.attr,
414 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvarebb081302007-10-10 21:11:52 +0200415 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
416 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
417 &sensor_dev_attr_temp1_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200418 &dev_attr_alarms.attr,
419 &dev_attr_cpu0_vid.attr,
420 &dev_attr_vrm.attr,
421 NULL
422};
423
424static const struct attribute_group adm1025_group = {
425 .attrs = adm1025_attributes,
426};
427
Jean Delvare4e952792007-10-10 21:14:11 +0200428static struct attribute *adm1025_attributes_in4[] = {
Jean Delvared8543e72007-10-10 21:11:01 +0200429 &sensor_dev_attr_in4_input.dev_attr.attr,
430 &sensor_dev_attr_in4_min.dev_attr.attr,
431 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebb081302007-10-10 21:11:52 +0200432 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200433 NULL
434};
435
Jean Delvare4e952792007-10-10 21:14:11 +0200436static const struct attribute_group adm1025_group_in4 = {
437 .attrs = adm1025_attributes_in4,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200438};
439
Jean Delvare7dbafe02008-07-16 19:30:08 +0200440/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100441static int adm1025_detect(struct i2c_client *client,
Jean Delvare7dbafe02008-07-16 19:30:08 +0200442 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Jean Delvare7dbafe02008-07-16 19:30:08 +0200444 struct i2c_adapter *adapter = client->adapter;
Jean Delvare95606722009-12-09 20:35:51 +0100445 const char *name;
446 u8 man_id, chip_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare7dbafe02008-07-16 19:30:08 +0200449 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jean Delvare95606722009-12-09 20:35:51 +0100451 /* Check for unused bits */
452 if ((i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG) & 0x80)
453 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS1) & 0xC0)
454 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS2) & 0xBC)) {
455 dev_dbg(&adapter->dev, "ADM1025 detection failed at 0x%02x\n",
456 client->addr);
457 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Jean Delvare95606722009-12-09 20:35:51 +0100460 /* Identification */
461 chip_id = i2c_smbus_read_byte_data(client, ADM1025_REG_CHIP_ID);
462 if ((chip_id & 0xF0) != 0x20)
463 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Jean Delvare95606722009-12-09 20:35:51 +0100465 man_id = i2c_smbus_read_byte_data(client, ADM1025_REG_MAN_ID);
466 if (man_id == 0x41)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 name = "adm1025";
Jean Delvare95606722009-12-09 20:35:51 +0100468 else if (man_id == 0xA1 && client->addr != 0x2E)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 name = "ne1619";
Jean Delvare95606722009-12-09 20:35:51 +0100470 else
471 return -ENODEV;
472
Jean Delvare7dbafe02008-07-16 19:30:08 +0200473 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Jean Delvare7dbafe02008-07-16 19:30:08 +0200475 return 0;
476}
477
Axel Lin851a7af2014-07-03 21:48:15 +0800478static void adm1025_init_client(struct i2c_client *client)
479{
480 u8 reg;
481 struct adm1025_data *data = i2c_get_clientdata(client);
482 int i;
483
484 data->vrm = vid_which_vrm();
485
486 /*
487 * Set high limits
488 * Usually we avoid setting limits on driver init, but it happens
489 * that the ADM1025 comes with stupid default limits (all registers
490 * set to 0). In case the chip has not gone through any limit
491 * setting yet, we better set the high limits to the max so that
492 * no alarm triggers.
493 */
494 for (i = 0; i < 6; i++) {
495 reg = i2c_smbus_read_byte_data(client,
496 ADM1025_REG_IN_MAX(i));
497 if (reg == 0)
498 i2c_smbus_write_byte_data(client,
499 ADM1025_REG_IN_MAX(i),
500 0xFF);
501 }
502 for (i = 0; i < 2; i++) {
503 reg = i2c_smbus_read_byte_data(client,
504 ADM1025_REG_TEMP_HIGH(i));
505 if (reg == 0)
506 i2c_smbus_write_byte_data(client,
507 ADM1025_REG_TEMP_HIGH(i),
508 0x7F);
509 }
510
511 /*
512 * Start the conversions
513 */
514 reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
515 if (!(reg & 0x01))
516 i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG,
517 (reg&0x7E)|0x01);
518}
519
Stephen Kitt67487032020-08-13 18:02:22 +0200520static int adm1025_probe(struct i2c_client *client)
Jean Delvare7dbafe02008-07-16 19:30:08 +0200521{
Axel Linaab3f572014-07-03 21:49:27 +0800522 struct device *dev = &client->dev;
523 struct device *hwmon_dev;
Jean Delvare7dbafe02008-07-16 19:30:08 +0200524 struct adm1025_data *data;
Jean Delvare7dbafe02008-07-16 19:30:08 +0200525 u8 config;
526
Axel Linaab3f572014-07-03 21:49:27 +0800527 data = devm_kzalloc(dev, sizeof(struct adm1025_data), GFP_KERNEL);
Guenter Roeck6702c992012-06-02 09:57:58 -0700528 if (!data)
529 return -ENOMEM;
Jean Delvare7dbafe02008-07-16 19:30:08 +0200530
531 i2c_set_clientdata(client, data);
Axel Linaab3f572014-07-03 21:49:27 +0800532 data->client = client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100533 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* Initialize the ADM1025 chip */
Jean Delvare4e952792007-10-10 21:14:11 +0200536 adm1025_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Axel Linaab3f572014-07-03 21:49:27 +0800538 /* sysfs hooks */
539 data->groups[0] = &adm1025_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* Pin 11 is either in4 (+12V) or VID4 */
Jean Delvare7dbafe02008-07-16 19:30:08 +0200541 config = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
Axel Linaab3f572014-07-03 21:49:27 +0800542 if (!(config & 0x20))
543 data->groups[1] = &adm1025_group_in4;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200544
Axel Linaab3f572014-07-03 21:49:27 +0800545 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
546 data, data->groups);
547 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Axel Lin851a7af2014-07-03 21:48:15 +0800550static const struct i2c_device_id adm1025_id[] = {
551 { "adm1025", adm1025 },
552 { "ne1619", ne1619 },
553 { }
554};
555MODULE_DEVICE_TABLE(i2c, adm1025_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Axel Lin851a7af2014-07-03 21:48:15 +0800557static struct i2c_driver adm1025_driver = {
558 .class = I2C_CLASS_HWMON,
559 .driver = {
560 .name = "adm1025",
561 },
Stephen Kitt67487032020-08-13 18:02:22 +0200562 .probe_new = adm1025_probe,
Axel Lin851a7af2014-07-03 21:48:15 +0800563 .id_table = adm1025_id,
564 .detect = adm1025_detect,
565 .address_list = normal_i2c,
566};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Axel Linf0967ee2012-01-20 15:38:18 +0800568module_i2c_driver(adm1025_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jean Delvare7c81c60f2014-01-29 20:40:08 +0100570MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571MODULE_DESCRIPTION("ADM1025 driver");
572MODULE_LICENSE("GPL");