blob: e85e062bbf327dbca2d4e41225cd1515e99b17bd [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 * lm80.c - From lm_sensors, Linux kernel modules for hardware
Guenter Roeck11606312012-01-19 11:02:20 -08004 * monitoring
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Guenter Roeck11606312012-01-19 11:02:20 -08006 * and Philip Edelbrock <phil@netroedge.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/slab.h>
14#include <linux/jiffies.h>
15#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040016#include <linux/hwmon.h>
Jean Delvaref8181762008-01-05 15:37:05 +010017#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040018#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050022static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
23 0x2e, 0x2f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Many LM80 constants specified below */
26
27/* The LM80 registers */
28#define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
29#define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
30#define LM80_REG_IN(nr) (0x20 + (nr))
31
32#define LM80_REG_FAN1 0x28
33#define LM80_REG_FAN2 0x29
34#define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
35
36#define LM80_REG_TEMP 0x27
37#define LM80_REG_TEMP_HOT_MAX 0x38
38#define LM80_REG_TEMP_HOT_HYST 0x39
39#define LM80_REG_TEMP_OS_MAX 0x3a
40#define LM80_REG_TEMP_OS_HYST 0x3b
41
42#define LM80_REG_CONFIG 0x00
43#define LM80_REG_ALARM1 0x01
44#define LM80_REG_ALARM2 0x02
45#define LM80_REG_MASK1 0x03
46#define LM80_REG_MASK2 0x04
47#define LM80_REG_FANDIV 0x05
48#define LM80_REG_RES 0x06
49
Jean Delvare9908ad42012-01-31 09:27:11 -050050#define LM96080_REG_CONV_RATE 0x07
51#define LM96080_REG_MAN_ID 0x3e
52#define LM96080_REG_DEV_ID 0x3f
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Guenter Roeck11606312012-01-19 11:02:20 -080055/*
56 * Conversions. Rounding and limit checking is only done on the TO_REG
57 * variants. Note that you should be a bit careful with which arguments
58 * these macros are called: arguments may be evaluated more than once.
59 * Fixing this is just not worth it.
60 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Guenter Roeck2a844c12013-01-09 08:09:34 -080062#define IN_TO_REG(val) (clamp_val(((val) + 5) / 10, 0, 255))
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010063#define IN_FROM_REG(val) ((val) * 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
66{
67 if (rpm == 0)
68 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -080069 rpm = clamp_val(rpm, 1, 1000000);
70 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010073#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
74 (val) == 255 ? 0 : 1350000/((div) * (val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Guenter Roeck688a3172014-04-13 09:30:09 -070076#define TEMP_FROM_REG(reg) ((reg) * 125 / 32)
77#define TEMP_TO_REG(temp) (DIV_ROUND_CLOSEST(clamp_val((temp), \
78 -128000, 127000), 1000) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define DIV_FROM_REG(val) (1 << (val))
81
Guenter Roeck9711bcd2014-04-13 09:45:15 -070082enum temp_index {
83 t_input = 0,
84 t_hot_max,
85 t_hot_hyst,
86 t_os_max,
87 t_os_hyst,
88 t_num_temp
89};
90
91static const u8 temp_regs[t_num_temp] = {
92 [t_input] = LM80_REG_TEMP,
93 [t_hot_max] = LM80_REG_TEMP_HOT_MAX,
94 [t_hot_hyst] = LM80_REG_TEMP_HOT_HYST,
95 [t_os_max] = LM80_REG_TEMP_OS_MAX,
96 [t_os_hyst] = LM80_REG_TEMP_OS_HYST,
97};
98
Guenter Roeck1adf3a32014-04-13 10:53:50 -070099enum in_index {
100 i_input = 0,
101 i_max,
102 i_min,
103 i_num_in
104};
105
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700106enum fan_index {
107 f_input,
108 f_min,
109 f_num_fan
110};
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * Client data (each client gets its own)
114 */
115
116struct lm80_data {
Guenter Roeck118c9a62014-04-04 18:01:34 +0200117 struct i2c_client *client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100118 struct mutex update_lock;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100119 char error; /* !=0 if error occurred during last update */
Paul Fertser952a11c2021-09-24 22:52:02 +0300120 bool valid; /* true if following fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long last_updated; /* In jiffies */
122
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700123 u8 in[i_num_in][7]; /* Register value, 1st index is enum in_index */
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700124 u8 fan[f_num_fan][2]; /* Register value, 1st index enum fan_index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 u8 fan_div[2]; /* Register encoding, shifted right */
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700126 s16 temp[t_num_temp]; /* Register values, normalized to 16 bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 u16 alarms; /* Register encoding, combined */
128};
129
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700130static int lm80_read_value(struct i2c_client *client, u8 reg)
131{
132 return i2c_smbus_read_byte_data(client, reg);
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700135static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
136{
137 return i2c_smbus_write_byte_data(client, reg, value);
138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700140/* Called when we have found a new LM80 and after read errors */
141static void lm80_init_client(struct i2c_client *client)
142{
143 /*
144 * Reset all except Watchdog values and last conversion values
145 * This sets fan-divs to 2, among others. This makes most other
146 * initializations unnecessary
147 */
148 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
149 /* Set 11-bit temperature resolution */
150 lm80_write_value(client, LM80_REG_RES, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700152 /* Start monitoring */
153 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
154}
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200155
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700156static struct lm80_data *lm80_update_device(struct device *dev)
157{
158 struct lm80_data *data = dev_get_drvdata(dev);
159 struct i2c_client *client = data->client;
160 int i;
161 int rv;
162 int prev_rv;
163 struct lm80_data *ret = data;
164
165 mutex_lock(&data->update_lock);
166
167 if (data->error)
168 lm80_init_client(client);
169
170 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
171 dev_dbg(dev, "Starting lm80 update\n");
172 for (i = 0; i <= 6; i++) {
173 rv = lm80_read_value(client, LM80_REG_IN(i));
174 if (rv < 0)
175 goto abort;
176 data->in[i_input][i] = rv;
177
178 rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
179 if (rv < 0)
180 goto abort;
181 data->in[i_min][i] = rv;
182
183 rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
184 if (rv < 0)
185 goto abort;
186 data->in[i_max][i] = rv;
187 }
188
189 rv = lm80_read_value(client, LM80_REG_FAN1);
190 if (rv < 0)
191 goto abort;
192 data->fan[f_input][0] = rv;
193
194 rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
195 if (rv < 0)
196 goto abort;
197 data->fan[f_min][0] = rv;
198
199 rv = lm80_read_value(client, LM80_REG_FAN2);
200 if (rv < 0)
201 goto abort;
202 data->fan[f_input][1] = rv;
203
204 rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
205 if (rv < 0)
206 goto abort;
207 data->fan[f_min][1] = rv;
208
209 prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
210 if (rv < 0)
211 goto abort;
212 rv = lm80_read_value(client, LM80_REG_RES);
213 if (rv < 0)
214 goto abort;
215 data->temp[t_input] = (prev_rv << 8) | (rv & 0xf0);
216
217 for (i = t_input + 1; i < t_num_temp; i++) {
218 rv = lm80_read_value(client, temp_regs[i]);
219 if (rv < 0)
220 goto abort;
221 data->temp[i] = rv << 8;
222 }
223
224 rv = lm80_read_value(client, LM80_REG_FANDIV);
225 if (rv < 0)
226 goto abort;
227 data->fan_div[0] = (rv >> 2) & 0x03;
228 data->fan_div[1] = (rv >> 4) & 0x03;
229
230 prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
231 if (rv < 0)
232 goto abort;
233 rv = lm80_read_value(client, LM80_REG_ALARM2);
234 if (rv < 0)
235 goto abort;
236 data->alarms = prev_rv + (rv << 8);
237
238 data->last_updated = jiffies;
Paul Fertser952a11c2021-09-24 22:52:02 +0300239 data->valid = true;
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700240 data->error = 0;
241 }
242 goto done;
243
244abort:
245 ret = ERR_PTR(rv);
Paul Fertser952a11c2021-09-24 22:52:02 +0300246 data->valid = false;
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700247 data->error = 1;
248
249done:
250 mutex_unlock(&data->update_lock);
251
252 return ret;
253}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/*
256 * Sysfs stuff
257 */
258
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800259static ssize_t in_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700260 char *buf)
261{
262 struct lm80_data *data = lm80_update_device(dev);
263 int index = to_sensor_dev_attr_2(attr)->index;
264 int nr = to_sensor_dev_attr_2(attr)->nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700266 if (IS_ERR(data))
267 return PTR_ERR(data);
268 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700270
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800271static ssize_t in_store(struct device *dev, struct device_attribute *attr,
272 const char *buf, size_t count)
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700273{
274 struct lm80_data *data = dev_get_drvdata(dev);
275 struct i2c_client *client = data->client;
276 int index = to_sensor_dev_attr_2(attr)->index;
277 int nr = to_sensor_dev_attr_2(attr)->nr;
278 long val;
279 u8 reg;
280 int err = kstrtol(buf, 10, &val);
281 if (err < 0)
282 return err;
283
284 reg = nr == i_min ? LM80_REG_IN_MIN(index) : LM80_REG_IN_MAX(index);
285
286 mutex_lock(&data->update_lock);
287 data->in[nr][index] = IN_TO_REG(val);
288 lm80_write_value(client, reg, data->in[nr][index]);
289 mutex_unlock(&data->update_lock);
290 return count;
291}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800293static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700294 char *buf)
295{
296 int index = to_sensor_dev_attr_2(attr)->index;
297 int nr = to_sensor_dev_attr_2(attr)->nr;
298 struct lm80_data *data = lm80_update_device(dev);
299 if (IS_ERR(data))
300 return PTR_ERR(data);
301 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr][index],
302 DIV_FROM_REG(data->fan_div[index])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800305static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
306 char *buf)
Jean Delvaref8181762008-01-05 15:37:05 +0100307{
308 int nr = to_sensor_dev_attr(attr)->index;
309 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100310 if (IS_ERR(data))
311 return PTR_ERR(data);
Jean Delvaref8181762008-01-05 15:37:05 +0100312 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800315static ssize_t fan_store(struct device *dev, struct device_attribute *attr,
316 const char *buf, size_t count)
Jean Delvaref8181762008-01-05 15:37:05 +0100317{
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700318 int index = to_sensor_dev_attr_2(attr)->index;
319 int nr = to_sensor_dev_attr_2(attr)->nr;
Guenter Roeck118c9a62014-04-04 18:01:34 +0200320 struct lm80_data *data = dev_get_drvdata(dev);
321 struct i2c_client *client = data->client;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100322 unsigned long val;
323 int err = kstrtoul(buf, 10, &val);
324 if (err < 0)
325 return err;
Jean Delvaref8181762008-01-05 15:37:05 +0100326
327 mutex_lock(&data->update_lock);
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700328 data->fan[nr][index] = FAN_TO_REG(val,
329 DIV_FROM_REG(data->fan_div[index]));
330 lm80_write_value(client, LM80_REG_FAN_MIN(index + 1),
331 data->fan[nr][index]);
Jean Delvaref8181762008-01-05 15:37:05 +0100332 mutex_unlock(&data->update_lock);
333 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Guenter Roeck11606312012-01-19 11:02:20 -0800336/*
337 * Note: we save and restore the fan minimum here, because its value is
338 * determined in part by the fan divisor. This follows the principle of
339 * least surprise; the user doesn't expect the fan minimum to change just
340 * because the divisor changed.
341 */
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800342static ssize_t fan_div_store(struct device *dev,
343 struct device_attribute *attr, const char *buf,
344 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Jean Delvaref8181762008-01-05 15:37:05 +0100346 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck118c9a62014-04-04 18:01:34 +0200347 struct lm80_data *data = dev_get_drvdata(dev);
348 struct i2c_client *client = data->client;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100349 unsigned long min, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 u8 reg;
Kangjie Luc9c63912018-12-21 13:01:33 -0600351 int rv;
352
353 rv = kstrtoul(buf, 10, &val);
354 if (rv < 0)
355 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Save fan_min */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100358 mutex_lock(&data->update_lock);
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700359 min = FAN_FROM_REG(data->fan[f_min][nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 DIV_FROM_REG(data->fan_div[nr]));
361
362 switch (val) {
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100363 case 1:
364 data->fan_div[nr] = 0;
365 break;
366 case 2:
367 data->fan_div[nr] = 1;
368 break;
369 case 4:
370 data->fan_div[nr] = 2;
371 break;
372 case 8:
373 data->fan_div[nr] = 3;
374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 default:
Guenter Roeck118c9a62014-04-04 18:01:34 +0200376 dev_err(dev,
Guenter Roeckb55f3752013-01-10 10:01:24 -0800377 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
378 val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100379 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -EINVAL;
381 }
382
Kangjie Luc9c63912018-12-21 13:01:33 -0600383 rv = lm80_read_value(client, LM80_REG_FANDIV);
Wei Yongjun07bd14c2018-12-26 11:28:24 +0000384 if (rv < 0) {
385 mutex_unlock(&data->update_lock);
Kangjie Luc9c63912018-12-21 13:01:33 -0600386 return rv;
Wei Yongjun07bd14c2018-12-26 11:28:24 +0000387 }
Kangjie Luc9c63912018-12-21 13:01:33 -0600388 reg = (rv & ~(3 << (2 * (nr + 1))))
389 | (data->fan_div[nr] << (2 * (nr + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 lm80_write_value(client, LM80_REG_FANDIV, reg);
391
392 /* Restore fan_min */
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700393 data->fan[f_min][nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
394 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1),
395 data->fan[f_min][nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 return count;
399}
400
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800401static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700402 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700404 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100406 if (IS_ERR(data))
407 return PTR_ERR(data);
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700408 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800411static ssize_t temp_store(struct device *dev,
412 struct device_attribute *devattr, const char *buf,
413 size_t count)
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700414{
415 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
416 struct lm80_data *data = dev_get_drvdata(dev);
417 struct i2c_client *client = data->client;
418 int nr = attr->index;
419 long val;
420 int err = kstrtol(buf, 10, &val);
421 if (err < 0)
422 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700424 mutex_lock(&data->update_lock);
425 data->temp[nr] = TEMP_TO_REG(val);
426 lm80_write_value(client, temp_regs[nr], data->temp[nr] >> 8);
427 mutex_unlock(&data->update_lock);
428 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Julia Lawall15476902016-12-22 13:04:51 +0100431static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100432 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100435 if (IS_ERR(data))
436 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return sprintf(buf, "%u\n", data->alarms);
438}
439
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800440static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
Jean Delvaree84542f2008-01-05 15:40:38 +0100441 char *buf)
442{
443 int bitnr = to_sensor_dev_attr(attr)->index;
444 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100445 if (IS_ERR(data))
446 return PTR_ERR(data);
Jean Delvaree84542f2008-01-05 15:40:38 +0100447 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
448}
449
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800450static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, i_min, 0);
451static SENSOR_DEVICE_ATTR_2_RW(in1_min, in, i_min, 1);
452static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, i_min, 2);
453static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, i_min, 3);
454static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, i_min, 4);
455static SENSOR_DEVICE_ATTR_2_RW(in5_min, in, i_min, 5);
456static SENSOR_DEVICE_ATTR_2_RW(in6_min, in, i_min, 6);
457static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, i_max, 0);
458static SENSOR_DEVICE_ATTR_2_RW(in1_max, in, i_max, 1);
459static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, i_max, 2);
460static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, i_max, 3);
461static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, i_max, 4);
462static SENSOR_DEVICE_ATTR_2_RW(in5_max, in, i_max, 5);
463static SENSOR_DEVICE_ATTR_2_RW(in6_max, in, i_max, 6);
464static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, i_input, 0);
465static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, i_input, 1);
466static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, i_input, 2);
467static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, i_input, 3);
468static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, i_input, 4);
469static SENSOR_DEVICE_ATTR_2_RO(in5_input, in, i_input, 5);
470static SENSOR_DEVICE_ATTR_2_RO(in6_input, in, i_input, 6);
471static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan, f_min, 0);
472static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan, f_min, 1);
473static SENSOR_DEVICE_ATTR_2_RO(fan1_input, fan, f_input, 0);
474static SENSOR_DEVICE_ATTR_2_RO(fan2_input, fan, f_input, 1);
475static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
476static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
477static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
478static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_hot_max);
479static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, t_hot_hyst);
480static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_os_max);
481static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp, t_os_hyst);
Julia Lawall15476902016-12-22 13:04:51 +0100482static DEVICE_ATTR_RO(alarms);
Guenter Roeckfdb97a02018-12-10 14:02:12 -0800483static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
484static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
485static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
486static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
487static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 4);
488static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 5);
489static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 6);
490static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 10);
491static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 11);
492static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 8);
493static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/*
496 * Real code
497 */
498
Guenter Roeck118c9a62014-04-04 18:01:34 +0200499static struct attribute *lm80_attrs[] = {
Jean Delvaref8181762008-01-05 15:37:05 +0100500 &sensor_dev_attr_in0_min.dev_attr.attr,
501 &sensor_dev_attr_in1_min.dev_attr.attr,
502 &sensor_dev_attr_in2_min.dev_attr.attr,
503 &sensor_dev_attr_in3_min.dev_attr.attr,
504 &sensor_dev_attr_in4_min.dev_attr.attr,
505 &sensor_dev_attr_in5_min.dev_attr.attr,
506 &sensor_dev_attr_in6_min.dev_attr.attr,
507 &sensor_dev_attr_in0_max.dev_attr.attr,
508 &sensor_dev_attr_in1_max.dev_attr.attr,
509 &sensor_dev_attr_in2_max.dev_attr.attr,
510 &sensor_dev_attr_in3_max.dev_attr.attr,
511 &sensor_dev_attr_in4_max.dev_attr.attr,
512 &sensor_dev_attr_in5_max.dev_attr.attr,
513 &sensor_dev_attr_in6_max.dev_attr.attr,
514 &sensor_dev_attr_in0_input.dev_attr.attr,
515 &sensor_dev_attr_in1_input.dev_attr.attr,
516 &sensor_dev_attr_in2_input.dev_attr.attr,
517 &sensor_dev_attr_in3_input.dev_attr.attr,
518 &sensor_dev_attr_in4_input.dev_attr.attr,
519 &sensor_dev_attr_in5_input.dev_attr.attr,
520 &sensor_dev_attr_in6_input.dev_attr.attr,
521 &sensor_dev_attr_fan1_min.dev_attr.attr,
522 &sensor_dev_attr_fan2_min.dev_attr.attr,
523 &sensor_dev_attr_fan1_input.dev_attr.attr,
524 &sensor_dev_attr_fan2_input.dev_attr.attr,
525 &sensor_dev_attr_fan1_div.dev_attr.attr,
526 &sensor_dev_attr_fan2_div.dev_attr.attr,
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700527 &sensor_dev_attr_temp1_input.dev_attr.attr,
528 &sensor_dev_attr_temp1_max.dev_attr.attr,
529 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
530 &sensor_dev_attr_temp1_crit.dev_attr.attr,
531 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200532 &dev_attr_alarms.attr,
Jean Delvaree84542f2008-01-05 15:40:38 +0100533 &sensor_dev_attr_in0_alarm.dev_attr.attr,
534 &sensor_dev_attr_in1_alarm.dev_attr.attr,
535 &sensor_dev_attr_in2_alarm.dev_attr.attr,
536 &sensor_dev_attr_in3_alarm.dev_attr.attr,
537 &sensor_dev_attr_in4_alarm.dev_attr.attr,
538 &sensor_dev_attr_in5_alarm.dev_attr.attr,
539 &sensor_dev_attr_in6_alarm.dev_attr.attr,
540 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
541 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
542 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
543 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200544 NULL
545};
Guenter Roeck118c9a62014-04-04 18:01:34 +0200546ATTRIBUTE_GROUPS(lm80);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200547
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200548/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100549static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200551 struct i2c_adapter *adapter = client->adapter;
Jean Delvare9908ad42012-01-31 09:27:11 -0500552 int i, cur, man_id, dev_id;
553 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200556 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jean Delvare9908ad42012-01-31 09:27:11 -0500558 /* First check for unused bits, common to both chip types */
559 if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
560 || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200561 return -ENODEV;
Jean Delvare9908ad42012-01-31 09:27:11 -0500562
563 /*
564 * The LM96080 has manufacturer and stepping/die rev registers so we
565 * can just check that. The LM80 does not have such registers so we
566 * have to use a more expensive trick.
567 */
568 man_id = lm80_read_value(client, LM96080_REG_MAN_ID);
569 dev_id = lm80_read_value(client, LM96080_REG_DEV_ID);
570 if (man_id == 0x01 && dev_id == 0x08) {
571 /* Check more unused bits for confirmation */
572 if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe)
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100573 return -ENODEV;
Jean Delvare9908ad42012-01-31 09:27:11 -0500574
575 name = "lm96080";
576 } else {
577 /* Check 6-bit addressing */
578 for (i = 0x2a; i <= 0x3d; i++) {
579 cur = i2c_smbus_read_byte_data(client, i);
580 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
581 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
582 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
583 return -ENODEV;
584 }
585
586 name = "lm80";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Jean Delvare9908ad42012-01-31 09:27:11 -0500589 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200591 return 0;
592}
593
Stephen Kitt67487032020-08-13 18:02:22 +0200594static int lm80_probe(struct i2c_client *client)
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200595{
Guenter Roeck118c9a62014-04-04 18:01:34 +0200596 struct device *dev = &client->dev;
597 struct device *hwmon_dev;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200598 struct lm80_data *data;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200599
Guenter Roeck118c9a62014-04-04 18:01:34 +0200600 data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL);
Guenter Roeckc38b2822012-06-02 09:58:09 -0700601 if (!data)
602 return -ENOMEM;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200603
Guenter Roeck118c9a62014-04-04 18:01:34 +0200604 data->client = client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100605 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Initialize the LM80 chip */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100608 lm80_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* A few vars need to be filled upon startup */
Greg Kroah-Hartman99ae3412021-05-03 13:56:31 +0200611 data->fan[f_min][0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
612 data->fan[f_min][1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Guenter Roeck118c9a62014-04-04 18:01:34 +0200614 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
615 data, lm80_groups);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200616
Guenter Roeck118c9a62014-04-04 18:01:34 +0200617 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700620/*
621 * Driver data (common to all clients)
622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700624static const struct i2c_device_id lm80_id[] = {
625 { "lm80", 0 },
626 { "lm96080", 1 },
627 { }
628};
629MODULE_DEVICE_TABLE(i2c, lm80_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700631static struct i2c_driver lm80_driver = {
632 .class = I2C_CLASS_HWMON,
633 .driver = {
634 .name = "lm80",
635 },
Stephen Kitt67487032020-08-13 18:02:22 +0200636 .probe_new = lm80_probe,
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700637 .id_table = lm80_id,
638 .detect = lm80_detect,
639 .address_list = normal_i2c,
640};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Axel Linf0967ee2012-01-20 15:38:18 +0800642module_i2c_driver(lm80_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
645 "Philip Edelbrock <phil@netroedge.com>");
646MODULE_DESCRIPTION("LM80 driver");
647MODULE_LICENSE("GPL");