blob: 759661c7d480e575f653a460ce98c1a721c96727 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck9b030792012-01-14 20:39:24 -08002 * lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Jean Delvare7c81c602014-01-29 20:40:08 +01005 * Copyright (c) 2007, 2011 Jean Delvare <jdelvare@suse.de>
Guenter Roeck9b030792012-01-14 20:39:24 -08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Joe Perchesce47da72011-01-12 21:55:10 +010022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/hwmon.h>
Jean Delvare19f673e2005-07-31 22:12:09 +020030#include <linux/hwmon-vid.h>
Jean Delvare247dde42007-05-08 17:22:01 +020031#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040032#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010033#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jean Delvare90534c52011-07-25 21:46:11 +020035#ifdef CONFIG_ISA
36#include <linux/platform_device.h>
37#include <linux/ioport.h>
38#include <linux/io.h>
39#endif
Jean Delvarec40769f2007-05-08 17:22:00 +020040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050042static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 0x2e, 0x2f, I2C_CLIENT_END };
Jean Delvaree5e9f442009-12-14 21:17:27 +010044enum chips { lm78, lm79 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* Many LM78 constants specified below */
47
48/* Length of ISA address segment */
49#define LM78_EXTENT 8
50
51/* Where are the ISA address/data registers relative to the base address */
52#define LM78_ADDR_REG_OFFSET 5
53#define LM78_DATA_REG_OFFSET 6
54
55/* The LM78 registers */
56#define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
57#define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
58#define LM78_REG_IN(nr) (0x20 + (nr))
59
60#define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
61#define LM78_REG_FAN(nr) (0x28 + (nr))
62
63#define LM78_REG_TEMP 0x27
64#define LM78_REG_TEMP_OVER 0x39
65#define LM78_REG_TEMP_HYST 0x3a
66
67#define LM78_REG_ALARM1 0x41
68#define LM78_REG_ALARM2 0x42
69
70#define LM78_REG_VID_FANDIV 0x47
71
72#define LM78_REG_CONFIG 0x40
73#define LM78_REG_CHIPID 0x49
74#define LM78_REG_I2C_ADDR 0x48
75
76
Guenter Roeck9b030792012-01-14 20:39:24 -080077/*
78 * Conversions. Rounding and limit checking is only done on the TO_REG
79 * variants.
80 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Guenter Roeck9b030792012-01-14 20:39:24 -080082/*
83 * IN: mV (0V to 4.08V)
84 * REG: 16mV/bit
85 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static inline u8 IN_TO_REG(unsigned long val)
87{
Guenter Roeck2a844c12013-01-09 08:09:34 -080088 unsigned long nval = clamp_val(val, 0, 4080);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return (nval + 8) / 16;
90}
91#define IN_FROM_REG(val) ((val) * 16)
92
93static inline u8 FAN_TO_REG(long rpm, int div)
94{
95 if (rpm <= 0)
96 return 255;
Dan Carpenter3806b452013-12-12 08:05:33 +010097 if (rpm > 1350000)
98 return 1;
Guenter Roeck2a844c12013-01-09 08:09:34 -080099 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static inline int FAN_FROM_REG(u8 val, int div)
103{
Guenter Roeck9b030792012-01-14 20:39:24 -0800104 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Guenter Roeck9b030792012-01-14 20:39:24 -0800107/*
108 * TEMP: mC (-128C to +127C)
109 * REG: 1C/bit, two's complement
110 */
Guenter Roeck1074d682014-07-29 20:48:59 -0700111static inline s8 TEMP_TO_REG(long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800113 int nval = clamp_val(val, -128000, 127000) ;
Guenter Roeck9b030792012-01-14 20:39:24 -0800114 return nval < 0 ? (nval - 500) / 1000 : (nval + 500) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static inline int TEMP_FROM_REG(s8 val)
118{
119 return val * 1000;
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define DIV_FROM_REG(val) (1 << (val))
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct lm78_data {
Jean Delvare0c6e9732008-10-17 17:51:16 +0200125 struct i2c_client *client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100126 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 enum chips type;
128
Jean Delvare6e1b5022008-10-17 17:51:15 +0200129 /* For ISA device only */
130 const char *name;
131 int isa_addr;
132
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100133 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 char valid; /* !=0 if following fields are valid */
135 unsigned long last_updated; /* In jiffies */
136
137 u8 in[7]; /* Register value */
138 u8 in_max[7]; /* Register value */
139 u8 in_min[7]; /* Register value */
140 u8 fan[3]; /* Register value */
141 u8 fan_min[3]; /* Register value */
142 s8 temp; /* Register value */
143 s8 temp_over; /* Register value */
144 s8 temp_hyst; /* Register value */
145 u8 fan_div[3]; /* Register encoding, shifted right */
146 u8 vid; /* Register encoding, combined */
147 u16 alarms; /* Register encoding, combined */
148};
149
150
Jean Delvarec59cc302007-05-08 17:22:01 +0200151static int lm78_read_value(struct lm78_data *data, u8 reg);
152static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static struct lm78_data *lm78_update_device(struct device *dev);
Jean Delvarec59cc302007-05-08 17:22:01 +0200154static void lm78_init_device(struct lm78_data *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* 7 Voltages */
Jean Delvare247dde42007-05-08 17:22:01 +0200158static ssize_t show_in(struct device *dev, struct device_attribute *da,
159 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Jean Delvare247dde42007-05-08 17:22:01 +0200161 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200163 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jean Delvare247dde42007-05-08 17:22:01 +0200166static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
167 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jean Delvare247dde42007-05-08 17:22:01 +0200169 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200171 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Jean Delvare247dde42007-05-08 17:22:01 +0200174static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
175 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Jean Delvare247dde42007-05-08 17:22:01 +0200177 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200179 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Jean Delvare247dde42007-05-08 17:22:01 +0200182static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
183 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Jean Delvare247dde42007-05-08 17:22:01 +0200185 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvarec40769f2007-05-08 17:22:00 +0200186 struct lm78_data *data = dev_get_drvdata(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200187 int nr = attr->index;
Guenter Roeck9b030792012-01-14 20:39:24 -0800188 unsigned long val;
189 int err;
190
191 err = kstrtoul(buf, 10, &val);
192 if (err)
193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100195 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 data->in_min[nr] = IN_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200197 lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100198 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return count;
200}
201
Jean Delvare247dde42007-05-08 17:22:01 +0200202static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
203 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Jean Delvare247dde42007-05-08 17:22:01 +0200205 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvarec40769f2007-05-08 17:22:00 +0200206 struct lm78_data *data = dev_get_drvdata(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200207 int nr = attr->index;
Guenter Roeck9b030792012-01-14 20:39:24 -0800208 unsigned long val;
209 int err;
210
211 err = kstrtoul(buf, 10, &val);
212 if (err)
213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100215 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 data->in_max[nr] = IN_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200217 lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100218 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return count;
220}
Guenter Roeck9b030792012-01-14 20:39:24 -0800221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#define show_in_offset(offset) \
Jean Delvare247dde42007-05-08 17:22:01 +0200223static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
224 show_in, NULL, offset); \
225static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
226 show_in_min, set_in_min, offset); \
227static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
228 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230show_in_offset(0);
231show_in_offset(1);
232show_in_offset(2);
233show_in_offset(3);
234show_in_offset(4);
235show_in_offset(5);
236show_in_offset(6);
237
238/* Temperature */
Jean Delvare247dde42007-05-08 17:22:01 +0200239static ssize_t show_temp(struct device *dev, struct device_attribute *da,
240 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct lm78_data *data = lm78_update_device(dev);
243 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
244}
245
Jean Delvare247dde42007-05-08 17:22:01 +0200246static ssize_t show_temp_over(struct device *dev, struct device_attribute *da,
247 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct lm78_data *data = lm78_update_device(dev);
250 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
251}
252
Jean Delvare247dde42007-05-08 17:22:01 +0200253static ssize_t set_temp_over(struct device *dev, struct device_attribute *da,
254 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jean Delvarec40769f2007-05-08 17:22:00 +0200256 struct lm78_data *data = dev_get_drvdata(dev);
Guenter Roeck9b030792012-01-14 20:39:24 -0800257 long val;
258 int err;
259
260 err = kstrtol(buf, 10, &val);
261 if (err)
262 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100264 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 data->temp_over = TEMP_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200266 lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100267 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return count;
269}
270
Jean Delvare247dde42007-05-08 17:22:01 +0200271static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da,
272 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct lm78_data *data = lm78_update_device(dev);
275 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
276}
277
Jean Delvare247dde42007-05-08 17:22:01 +0200278static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da,
279 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Jean Delvarec40769f2007-05-08 17:22:00 +0200281 struct lm78_data *data = dev_get_drvdata(dev);
Guenter Roeck9b030792012-01-14 20:39:24 -0800282 long val;
283 int err;
284
285 err = kstrtol(buf, 10, &val);
286 if (err)
287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100289 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 data->temp_hyst = TEMP_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200291 lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100292 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return count;
294}
295
296static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
297static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
298 show_temp_over, set_temp_over);
299static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
300 show_temp_hyst, set_temp_hyst);
301
302/* 3 Fans */
Jean Delvare247dde42007-05-08 17:22:01 +0200303static ssize_t show_fan(struct device *dev, struct device_attribute *da,
304 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Jean Delvare247dde42007-05-08 17:22:01 +0200306 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200308 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Guenter Roeck9b030792012-01-14 20:39:24 -0800310 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Jean Delvare247dde42007-05-08 17:22:01 +0200313static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
314 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Jean Delvare247dde42007-05-08 17:22:01 +0200316 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200318 int nr = attr->index;
Guenter Roeck9b030792012-01-14 20:39:24 -0800319 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
320 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Jean Delvare247dde42007-05-08 17:22:01 +0200323static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
324 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Jean Delvare247dde42007-05-08 17:22:01 +0200326 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvarec40769f2007-05-08 17:22:00 +0200327 struct lm78_data *data = dev_get_drvdata(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200328 int nr = attr->index;
Guenter Roeck9b030792012-01-14 20:39:24 -0800329 unsigned long val;
330 int err;
331
332 err = kstrtoul(buf, 10, &val);
333 if (err)
334 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100336 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec59cc302007-05-08 17:22:01 +0200338 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100339 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return count;
341}
342
Jean Delvare247dde42007-05-08 17:22:01 +0200343static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
344 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Jean Delvare247dde42007-05-08 17:22:01 +0200346 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200348 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Guenter Roeck9b030792012-01-14 20:39:24 -0800351/*
352 * Note: we save and restore the fan minimum here, because its value is
353 * determined in part by the fan divisor. This follows the principle of
354 * least surprise; the user doesn't expect the fan minimum to change just
355 * because the divisor changed.
356 */
Jean Delvare247dde42007-05-08 17:22:01 +0200357static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
358 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Jean Delvare247dde42007-05-08 17:22:01 +0200360 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvarec40769f2007-05-08 17:22:00 +0200361 struct lm78_data *data = dev_get_drvdata(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200362 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unsigned long min;
364 u8 reg;
Guenter Roeck9b030792012-01-14 20:39:24 -0800365 unsigned long val;
366 int err;
367
368 err = kstrtoul(buf, 10, &val);
369 if (err)
370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100372 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 min = FAN_FROM_REG(data->fan_min[nr],
374 DIV_FROM_REG(data->fan_div[nr]));
375
376 switch (val) {
Guenter Roeck9b030792012-01-14 20:39:24 -0800377 case 1:
378 data->fan_div[nr] = 0;
379 break;
380 case 2:
381 data->fan_div[nr] = 1;
382 break;
383 case 4:
384 data->fan_div[nr] = 2;
385 break;
386 case 8:
387 data->fan_div[nr] = 3;
388 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 default:
Guenter Roeckb55f3752013-01-10 10:01:24 -0800390 dev_err(dev,
391 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
392 val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100393 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EINVAL;
395 }
396
Jean Delvarec59cc302007-05-08 17:22:01 +0200397 reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 switch (nr) {
399 case 0:
400 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
401 break;
402 case 1:
403 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
404 break;
405 }
Jean Delvarec59cc302007-05-08 17:22:01 +0200406 lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 data->fan_min[nr] =
409 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec59cc302007-05-08 17:22:01 +0200410 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100411 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 return count;
414}
415
Jean Delvare247dde42007-05-08 17:22:01 +0200416#define show_fan_offset(offset) \
417static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
418 show_fan, NULL, offset - 1); \
419static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
420 show_fan_min, set_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422show_fan_offset(1);
423show_fan_offset(2);
424show_fan_offset(3);
425
426/* Fan 3 divisor is locked in H/W */
Jean Delvare247dde42007-05-08 17:22:01 +0200427static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
428 show_fan_div, set_fan_div, 0);
429static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
430 show_fan_div, set_fan_div, 1);
431static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433/* VID */
Jean Delvare247dde42007-05-08 17:22:01 +0200434static ssize_t show_vid(struct device *dev, struct device_attribute *da,
435 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct lm78_data *data = lm78_update_device(dev);
Jean Delvared0d3cd62005-11-23 15:44:26 -0800438 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
441
442/* Alarms */
Jean Delvare247dde42007-05-08 17:22:01 +0200443static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
444 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct lm78_data *data = lm78_update_device(dev);
447 return sprintf(buf, "%u\n", data->alarms);
448}
449static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
450
Jean Delvare428a7032007-09-04 23:25:33 +0200451static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
452 char *buf)
453{
454 struct lm78_data *data = lm78_update_device(dev);
455 int nr = to_sensor_dev_attr(da)->index;
456 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
457}
458static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
459static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
460static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
461static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
462static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
463static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
464static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
465static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
466static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
467static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
468static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
469
Axel Lin8eb40612014-07-23 12:39:21 +0800470static struct attribute *lm78_attrs[] = {
Jean Delvare247dde42007-05-08 17:22:01 +0200471 &sensor_dev_attr_in0_input.dev_attr.attr,
472 &sensor_dev_attr_in0_min.dev_attr.attr,
473 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200474 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200475 &sensor_dev_attr_in1_input.dev_attr.attr,
476 &sensor_dev_attr_in1_min.dev_attr.attr,
477 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200478 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200479 &sensor_dev_attr_in2_input.dev_attr.attr,
480 &sensor_dev_attr_in2_min.dev_attr.attr,
481 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200482 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200483 &sensor_dev_attr_in3_input.dev_attr.attr,
484 &sensor_dev_attr_in3_min.dev_attr.attr,
485 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200486 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200487 &sensor_dev_attr_in4_input.dev_attr.attr,
488 &sensor_dev_attr_in4_min.dev_attr.attr,
489 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200490 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200491 &sensor_dev_attr_in5_input.dev_attr.attr,
492 &sensor_dev_attr_in5_min.dev_attr.attr,
493 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200494 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200495 &sensor_dev_attr_in6_input.dev_attr.attr,
496 &sensor_dev_attr_in6_min.dev_attr.attr,
497 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200498 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200499 &dev_attr_temp1_input.attr,
500 &dev_attr_temp1_max.attr,
501 &dev_attr_temp1_max_hyst.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200502 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200503 &sensor_dev_attr_fan1_input.dev_attr.attr,
504 &sensor_dev_attr_fan1_min.dev_attr.attr,
505 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200506 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200507 &sensor_dev_attr_fan2_input.dev_attr.attr,
508 &sensor_dev_attr_fan2_min.dev_attr.attr,
509 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200510 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200511 &sensor_dev_attr_fan3_input.dev_attr.attr,
512 &sensor_dev_attr_fan3_min.dev_attr.attr,
513 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200514 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200515 &dev_attr_alarms.attr,
516 &dev_attr_cpu0_vid.attr,
517
518 NULL
519};
520
Axel Lin8eb40612014-07-23 12:39:21 +0800521ATTRIBUTE_GROUPS(lm78);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200522
Jean Delvare90534c52011-07-25 21:46:11 +0200523/*
524 * ISA related code
525 */
526#ifdef CONFIG_ISA
527
528/* ISA device, if found */
529static struct platform_device *pdev;
530
531static unsigned short isa_address = 0x290;
532
Jean Delvare90534c52011-07-25 21:46:11 +0200533static struct lm78_data *lm78_data_if_isa(void)
534{
535 return pdev ? platform_get_drvdata(pdev) : NULL;
536}
537
Jean Delvare18c73f92008-10-17 17:51:15 +0200538/* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
539static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
540{
Jean Delvare0c6e9732008-10-17 17:51:16 +0200541 struct lm78_data *isa;
Jean Delvare18c73f92008-10-17 17:51:15 +0200542 int i;
543
544 if (!pdev) /* No ISA chip */
545 return 0;
Jean Delvare18c73f92008-10-17 17:51:15 +0200546 isa = platform_get_drvdata(pdev);
547
548 if (lm78_read_value(isa, LM78_REG_I2C_ADDR) != client->addr)
549 return 0; /* Address doesn't match */
550 if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe))
551 return 0; /* Chip type doesn't match */
552
Guenter Roeck9b030792012-01-14 20:39:24 -0800553 /*
554 * We compare all the limit registers, the config register and the
555 * interrupt mask registers
556 */
Jean Delvare18c73f92008-10-17 17:51:15 +0200557 for (i = 0x2b; i <= 0x3d; i++) {
Jean Delvare0c6e9732008-10-17 17:51:16 +0200558 if (lm78_read_value(isa, i) !=
559 i2c_smbus_read_byte_data(client, i))
Jean Delvare18c73f92008-10-17 17:51:15 +0200560 return 0;
561 }
562 if (lm78_read_value(isa, LM78_REG_CONFIG) !=
Jean Delvare0c6e9732008-10-17 17:51:16 +0200563 i2c_smbus_read_byte_data(client, LM78_REG_CONFIG))
Jean Delvare18c73f92008-10-17 17:51:15 +0200564 return 0;
565 for (i = 0x43; i <= 0x46; i++) {
Jean Delvare0c6e9732008-10-17 17:51:16 +0200566 if (lm78_read_value(isa, i) !=
567 i2c_smbus_read_byte_data(client, i))
Jean Delvare18c73f92008-10-17 17:51:15 +0200568 return 0;
569 }
570
571 return 1;
572}
Jean Delvare90534c52011-07-25 21:46:11 +0200573#else /* !CONFIG_ISA */
574
575static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
576{
577 return 0;
578}
579
580static struct lm78_data *lm78_data_if_isa(void)
581{
582 return NULL;
583}
584#endif /* CONFIG_ISA */
Jean Delvare18c73f92008-10-17 17:51:15 +0200585
Jean Delvare310ec792009-12-14 21:17:23 +0100586static int lm78_i2c_detect(struct i2c_client *client,
Jean Delvare0c6e9732008-10-17 17:51:16 +0200587 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Jean Delvare0c6e9732008-10-17 17:51:16 +0200589 int i;
Jean Delvare90534c52011-07-25 21:46:11 +0200590 struct lm78_data *isa = lm78_data_if_isa();
Jean Delvare0c6e9732008-10-17 17:51:16 +0200591 const char *client_name;
592 struct i2c_adapter *adapter = client->adapter;
593 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Jean Delvare0c6e9732008-10-17 17:51:16 +0200595 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
596 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Guenter Roeck9b030792012-01-14 20:39:24 -0800598 /*
599 * We block updates of the ISA device to minimize the risk of
600 * concurrent access to the same LM78 chip through different
601 * interfaces.
602 */
Jean Delvare0c6e9732008-10-17 17:51:16 +0200603 if (isa)
604 mutex_lock(&isa->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Jean Delvare52df6442009-12-09 20:35:57 +0100606 if ((i2c_smbus_read_byte_data(client, LM78_REG_CONFIG) & 0x80)
607 || i2c_smbus_read_byte_data(client, LM78_REG_I2C_ADDR) != address)
608 goto err_nodev;
Jean Delvare0c6e9732008-10-17 17:51:16 +0200609
Jean Delvare52df6442009-12-09 20:35:57 +0100610 /* Explicitly prevent the misdetection of Winbond chips */
611 i = i2c_smbus_read_byte_data(client, 0x4f);
612 if (i == 0xa3 || i == 0x5c)
613 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /* Determine the chip type. */
Jean Delvare52df6442009-12-09 20:35:57 +0100616 i = i2c_smbus_read_byte_data(client, LM78_REG_CHIPID);
617 if (i == 0x00 || i == 0x20 /* LM78 */
618 || i == 0x40) /* LM78-J */
619 client_name = "lm78";
620 else if ((i & 0xfe) == 0xc0)
621 client_name = "lm79";
622 else
623 goto err_nodev;
Jean Delvare18c73f92008-10-17 17:51:15 +0200624
Jean Delvare52df6442009-12-09 20:35:57 +0100625 if (lm78_alias_detect(client, i)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800626 dev_dbg(&adapter->dev,
627 "Device at 0x%02x appears to be the same as ISA device\n",
628 address);
Jean Delvare52df6442009-12-09 20:35:57 +0100629 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
Jean Delvare0c6e9732008-10-17 17:51:16 +0200632 if (isa)
633 mutex_unlock(&isa->update_lock);
634
Jean Delvare0c6e9732008-10-17 17:51:16 +0200635 strlcpy(info->type, client_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jean Delvare0c6e9732008-10-17 17:51:16 +0200637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jean Delvare0c6e9732008-10-17 17:51:16 +0200639 err_nodev:
640 if (isa)
641 mutex_unlock(&isa->update_lock);
642 return -ENODEV;
643}
644
645static int lm78_i2c_probe(struct i2c_client *client,
646 const struct i2c_device_id *id)
647{
Axel Lin8eb40612014-07-23 12:39:21 +0800648 struct device *dev = &client->dev;
649 struct device *hwmon_dev;
Jean Delvare0c6e9732008-10-17 17:51:16 +0200650 struct lm78_data *data;
Jean Delvare0c6e9732008-10-17 17:51:16 +0200651
Axel Lin8eb40612014-07-23 12:39:21 +0800652 data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
Jean Delvare0c6e9732008-10-17 17:51:16 +0200653 if (!data)
654 return -ENOMEM;
655
Jean Delvare0c6e9732008-10-17 17:51:16 +0200656 data->client = client;
657 data->type = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* Initialize the LM78 chip */
Jean Delvarec59cc302007-05-08 17:22:01 +0200660 lm78_init_device(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Axel Lin8eb40612014-07-23 12:39:21 +0800662 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
663 data, lm78_groups);
664 return PTR_ERR_OR_ZERO(hwmon_dev);
Jean Delvarec40769f2007-05-08 17:22:00 +0200665}
666
Jean Delvareed4cebd2011-07-25 21:46:11 +0200667static const struct i2c_device_id lm78_i2c_id[] = {
668 { "lm78", lm78 },
669 { "lm79", lm79 },
670 { }
671};
672MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
Jean Delvarec40769f2007-05-08 17:22:00 +0200673
Jean Delvareed4cebd2011-07-25 21:46:11 +0200674static struct i2c_driver lm78_driver = {
675 .class = I2C_CLASS_HWMON,
676 .driver = {
677 .name = "lm78",
678 },
679 .probe = lm78_i2c_probe,
Jean Delvareed4cebd2011-07-25 21:46:11 +0200680 .id_table = lm78_i2c_id,
681 .detect = lm78_i2c_detect,
682 .address_list = normal_i2c,
683};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Guenter Roeck9b030792012-01-14 20:39:24 -0800685/*
686 * The SMBus locks itself, but ISA access must be locked explicitly!
687 * We don't want to lock the whole ISA bus, so we lock each client
688 * separately.
689 * We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
690 * would slow down the LM78 access and should not be necessary.
691 */
Jean Delvarec59cc302007-05-08 17:22:01 +0200692static int lm78_read_value(struct lm78_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Jean Delvare0c6e9732008-10-17 17:51:16 +0200694 struct i2c_client *client = data->client;
Jean Delvarec59cc302007-05-08 17:22:01 +0200695
Jean Delvare90534c52011-07-25 21:46:11 +0200696#ifdef CONFIG_ISA
Jean Delvare0c6e9732008-10-17 17:51:16 +0200697 if (!client) { /* ISA device */
Jean Delvarec59cc302007-05-08 17:22:01 +0200698 int res;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100699 mutex_lock(&data->lock);
Jean Delvare6e1b5022008-10-17 17:51:15 +0200700 outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
701 res = inb_p(data->isa_addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100702 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return res;
704 } else
Jean Delvare90534c52011-07-25 21:46:11 +0200705#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return i2c_smbus_read_byte_data(client, reg);
707}
708
Jean Delvarec59cc302007-05-08 17:22:01 +0200709static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Jean Delvare0c6e9732008-10-17 17:51:16 +0200711 struct i2c_client *client = data->client;
Jean Delvarec59cc302007-05-08 17:22:01 +0200712
Jean Delvare90534c52011-07-25 21:46:11 +0200713#ifdef CONFIG_ISA
Jean Delvare0c6e9732008-10-17 17:51:16 +0200714 if (!client) { /* ISA device */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100715 mutex_lock(&data->lock);
Jean Delvare6e1b5022008-10-17 17:51:15 +0200716 outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
717 outb_p(value, data->isa_addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100718 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720 } else
Jean Delvare90534c52011-07-25 21:46:11 +0200721#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return i2c_smbus_write_byte_data(client, reg, value);
723}
724
Jean Delvarec59cc302007-05-08 17:22:01 +0200725static void lm78_init_device(struct lm78_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Jean Delvarec40769f2007-05-08 17:22:00 +0200727 u8 config;
728 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* Start monitoring */
Jean Delvarec59cc302007-05-08 17:22:01 +0200731 config = lm78_read_value(data, LM78_REG_CONFIG);
Jean Delvarec40769f2007-05-08 17:22:00 +0200732 if ((config & 0x09) != 0x01)
Jean Delvarec59cc302007-05-08 17:22:01 +0200733 lm78_write_value(data, LM78_REG_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 (config & 0xf7) | 0x01);
Jean Delvarec40769f2007-05-08 17:22:00 +0200735
736 /* A few vars need to be filled upon startup */
737 for (i = 0; i < 3; i++) {
Jean Delvarec59cc302007-05-08 17:22:01 +0200738 data->fan_min[i] = lm78_read_value(data,
Jean Delvarec40769f2007-05-08 17:22:00 +0200739 LM78_REG_FAN_MIN(i));
740 }
741
742 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745static struct lm78_data *lm78_update_device(struct device *dev)
746{
Jean Delvarec40769f2007-05-08 17:22:00 +0200747 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int i;
749
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100750 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
753 || !data->valid) {
754
Jean Delvarec40769f2007-05-08 17:22:00 +0200755 dev_dbg(dev, "Starting lm78 update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 for (i = 0; i <= 6; i++) {
758 data->in[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200759 lm78_read_value(data, LM78_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 data->in_min[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200761 lm78_read_value(data, LM78_REG_IN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 data->in_max[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200763 lm78_read_value(data, LM78_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765 for (i = 0; i < 3; i++) {
766 data->fan[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200767 lm78_read_value(data, LM78_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 data->fan_min[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200769 lm78_read_value(data, LM78_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Jean Delvarec59cc302007-05-08 17:22:01 +0200771 data->temp = lm78_read_value(data, LM78_REG_TEMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 data->temp_over =
Jean Delvarec59cc302007-05-08 17:22:01 +0200773 lm78_read_value(data, LM78_REG_TEMP_OVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 data->temp_hyst =
Jean Delvarec59cc302007-05-08 17:22:01 +0200775 lm78_read_value(data, LM78_REG_TEMP_HYST);
776 i = lm78_read_value(data, LM78_REG_VID_FANDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 data->vid = i & 0x0f;
778 if (data->type == lm79)
779 data->vid |=
Jean Delvarec59cc302007-05-08 17:22:01 +0200780 (lm78_read_value(data, LM78_REG_CHIPID) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 0x01) << 4;
782 else
783 data->vid |= 0x10;
784 data->fan_div[0] = (i >> 4) & 0x03;
785 data->fan_div[1] = i >> 6;
Jean Delvarec59cc302007-05-08 17:22:01 +0200786 data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
787 (lm78_read_value(data, LM78_REG_ALARM2) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 data->last_updated = jiffies;
789 data->valid = 1;
790
791 data->fan_div[2] = 1;
792 }
793
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100794 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 return data;
797}
798
Jean Delvare90534c52011-07-25 21:46:11 +0200799#ifdef CONFIG_ISA
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500800static int lm78_isa_probe(struct platform_device *pdev)
Jean Delvareed4cebd2011-07-25 21:46:11 +0200801{
Axel Lin8eb40612014-07-23 12:39:21 +0800802 struct device *dev = &pdev->dev;
803 struct device *hwmon_dev;
Jean Delvareed4cebd2011-07-25 21:46:11 +0200804 struct lm78_data *data;
805 struct resource *res;
806
807 /* Reserve the ISA region */
808 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Axel Lin8eb40612014-07-23 12:39:21 +0800809 if (!devm_request_region(dev, res->start + LM78_ADDR_REG_OFFSET,
Guenter Roeck7b7bb902012-06-02 09:58:09 -0700810 2, "lm78"))
811 return -EBUSY;
Jean Delvareed4cebd2011-07-25 21:46:11 +0200812
Axel Lin8eb40612014-07-23 12:39:21 +0800813 data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
Guenter Roeck7b7bb902012-06-02 09:58:09 -0700814 if (!data)
815 return -ENOMEM;
816
Jean Delvareed4cebd2011-07-25 21:46:11 +0200817 mutex_init(&data->lock);
818 data->isa_addr = res->start;
819 platform_set_drvdata(pdev, data);
820
821 if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
822 data->type = lm79;
823 data->name = "lm79";
824 } else {
825 data->type = lm78;
826 data->name = "lm78";
827 }
828
829 /* Initialize the LM78 chip */
830 lm78_init_device(data);
831
Axel Lin8eb40612014-07-23 12:39:21 +0800832 hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
833 data, lm78_groups);
834 return PTR_ERR_OR_ZERO(hwmon_dev);
Jean Delvareed4cebd2011-07-25 21:46:11 +0200835}
836
837static struct platform_driver lm78_isa_driver = {
838 .driver = {
839 .owner = THIS_MODULE,
840 .name = "lm78",
841 },
842 .probe = lm78_isa_probe,
Jean Delvareed4cebd2011-07-25 21:46:11 +0200843};
844
Jean Delvarec40769f2007-05-08 17:22:00 +0200845/* return 1 if a supported chip is found, 0 otherwise */
846static int __init lm78_isa_found(unsigned short address)
847{
848 int val, save, found = 0;
Jean Delvare197027e2010-02-05 19:58:36 +0100849 int port;
Jean Delvarec40769f2007-05-08 17:22:00 +0200850
Guenter Roeck9b030792012-01-14 20:39:24 -0800851 /*
852 * Some boards declare base+0 to base+7 as a PNP device, some base+4
Jean Delvare197027e2010-02-05 19:58:36 +0100853 * to base+7 and some base+5 to base+6. So we better request each port
Guenter Roeck9b030792012-01-14 20:39:24 -0800854 * individually for the probing phase.
855 */
Jean Delvare197027e2010-02-05 19:58:36 +0100856 for (port = address; port < address + LM78_EXTENT; port++) {
857 if (!request_region(port, 1, "lm78")) {
Joe Perchesce47da72011-01-12 21:55:10 +0100858 pr_debug("Failed to request port 0x%x\n", port);
Jean Delvare197027e2010-02-05 19:58:36 +0100859 goto release;
860 }
Jean Delvare47c15532008-10-17 17:51:15 +0200861 }
Jean Delvarec40769f2007-05-08 17:22:00 +0200862
863#define REALLY_SLOW_IO
Guenter Roeck9b030792012-01-14 20:39:24 -0800864 /*
865 * We need the timeouts for at least some LM78-like
866 * chips. But only if we read 'undefined' registers.
867 */
Jean Delvarec40769f2007-05-08 17:22:00 +0200868 val = inb_p(address + 1);
869 if (inb_p(address + 2) != val
870 || inb_p(address + 3) != val
871 || inb_p(address + 7) != val)
872 goto release;
873#undef REALLY_SLOW_IO
874
Guenter Roeck9b030792012-01-14 20:39:24 -0800875 /*
876 * We should be able to change the 7 LSB of the address port. The
877 * MSB (busy flag) should be clear initially, set after the write.
878 */
Jean Delvarec40769f2007-05-08 17:22:00 +0200879 save = inb_p(address + LM78_ADDR_REG_OFFSET);
880 if (save & 0x80)
881 goto release;
882 val = ~save & 0x7f;
883 outb_p(val, address + LM78_ADDR_REG_OFFSET);
884 if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
885 outb_p(save, address + LM78_ADDR_REG_OFFSET);
886 goto release;
887 }
888
889 /* We found a device, now see if it could be an LM78 */
890 outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
891 val = inb_p(address + LM78_DATA_REG_OFFSET);
892 if (val & 0x80)
893 goto release;
894 outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
895 val = inb_p(address + LM78_DATA_REG_OFFSET);
896 if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
897 goto release;
898
899 /* The busy flag should be clear again */
900 if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
901 goto release;
902
903 /* Explicitly prevent the misdetection of Winbond chips */
904 outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
905 val = inb_p(address + LM78_DATA_REG_OFFSET);
906 if (val == 0xa3 || val == 0x5c)
907 goto release;
908
909 /* Explicitly prevent the misdetection of ITE chips */
910 outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
911 val = inb_p(address + LM78_DATA_REG_OFFSET);
912 if (val == 0x90)
913 goto release;
914
915 /* Determine the chip type */
916 outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
917 val = inb_p(address + LM78_DATA_REG_OFFSET);
Hans de Goedeacf346a2007-07-24 23:36:00 +0200918 if (val == 0x00 || val == 0x20 /* LM78 */
Jean Delvarec40769f2007-05-08 17:22:00 +0200919 || val == 0x40 /* LM78-J */
920 || (val & 0xfe) == 0xc0) /* LM79 */
921 found = 1;
922
923 if (found)
Joe Perchesce47da72011-01-12 21:55:10 +0100924 pr_info("Found an %s chip at %#x\n",
Jean Delvarec40769f2007-05-08 17:22:00 +0200925 val & 0x80 ? "LM79" : "LM78", (int)address);
926
927 release:
Jean Delvare197027e2010-02-05 19:58:36 +0100928 for (port--; port >= address; port--)
929 release_region(port, 1);
Jean Delvarec40769f2007-05-08 17:22:00 +0200930 return found;
931}
932
933static int __init lm78_isa_device_add(unsigned short address)
934{
935 struct resource res = {
936 .start = address,
Jean Delvare15bde2f2007-08-29 10:39:57 +0200937 .end = address + LM78_EXTENT - 1,
Jean Delvarec40769f2007-05-08 17:22:00 +0200938 .name = "lm78",
939 .flags = IORESOURCE_IO,
940 };
941 int err;
942
943 pdev = platform_device_alloc("lm78", address);
944 if (!pdev) {
945 err = -ENOMEM;
Joe Perchesce47da72011-01-12 21:55:10 +0100946 pr_err("Device allocation failed\n");
Jean Delvarec40769f2007-05-08 17:22:00 +0200947 goto exit;
948 }
949
950 err = platform_device_add_resources(pdev, &res, 1);
951 if (err) {
Joe Perchesce47da72011-01-12 21:55:10 +0100952 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvarec40769f2007-05-08 17:22:00 +0200953 goto exit_device_put;
954 }
955
956 err = platform_device_add(pdev);
957 if (err) {
Joe Perchesce47da72011-01-12 21:55:10 +0100958 pr_err("Device addition failed (%d)\n", err);
Jean Delvarec40769f2007-05-08 17:22:00 +0200959 goto exit_device_put;
960 }
961
962 return 0;
963
964 exit_device_put:
965 platform_device_put(pdev);
966 exit:
967 pdev = NULL;
968 return err;
969}
970
Jean Delvare90534c52011-07-25 21:46:11 +0200971static int __init lm78_isa_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jean Delvarefde09502005-07-19 23:51:07 +0200973 int res;
974
Jean Delvarec40769f2007-05-08 17:22:00 +0200975 if (lm78_isa_found(isa_address)) {
976 res = platform_driver_register(&lm78_isa_driver);
977 if (res)
Jean Delvare18c73f92008-10-17 17:51:15 +0200978 goto exit;
Jean Delvarec40769f2007-05-08 17:22:00 +0200979
980 /* Sets global pdev as a side effect */
981 res = lm78_isa_device_add(isa_address);
982 if (res)
983 goto exit_unreg_isa_driver;
984 }
Jean Delvarefde09502005-07-19 23:51:07 +0200985
Jean Delvare90534c52011-07-25 21:46:11 +0200986 return 0;
987
988 exit_unreg_isa_driver:
989 platform_driver_unregister(&lm78_isa_driver);
990 exit:
991 return res;
992}
993
994static void lm78_isa_unregister(void)
995{
996 if (pdev) {
997 platform_device_unregister(pdev);
998 platform_driver_unregister(&lm78_isa_driver);
999 }
1000}
1001#else /* !CONFIG_ISA */
1002
1003static int __init lm78_isa_register(void)
1004{
1005 return 0;
1006}
1007
1008static void lm78_isa_unregister(void)
1009{
1010}
1011#endif /* CONFIG_ISA */
1012
1013static int __init sm_lm78_init(void)
1014{
1015 int res;
1016
Guenter Roeck9b030792012-01-14 20:39:24 -08001017 /*
1018 * We register the ISA device first, so that we can skip the
1019 * registration of an I2C interface to the same device.
1020 */
Jean Delvare90534c52011-07-25 21:46:11 +02001021 res = lm78_isa_register();
1022 if (res)
1023 goto exit;
1024
Jean Delvare18c73f92008-10-17 17:51:15 +02001025 res = i2c_add_driver(&lm78_driver);
1026 if (res)
1027 goto exit_unreg_isa_device;
1028
Jean Delvarefde09502005-07-19 23:51:07 +02001029 return 0;
Jean Delvarec40769f2007-05-08 17:22:00 +02001030
Jean Delvare18c73f92008-10-17 17:51:15 +02001031 exit_unreg_isa_device:
Jean Delvare90534c52011-07-25 21:46:11 +02001032 lm78_isa_unregister();
Jean Delvarec40769f2007-05-08 17:22:00 +02001033 exit:
1034 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037static void __exit sm_lm78_exit(void)
1038{
Jean Delvare90534c52011-07-25 21:46:11 +02001039 lm78_isa_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 i2c_del_driver(&lm78_driver);
1041}
1042
Jean Delvare7c81c602014-01-29 20:40:08 +01001043MODULE_AUTHOR("Frodo Looijaard, Jean Delvare <jdelvare@suse.de>");
Jean Delvare27fe0482005-07-27 21:30:16 +02001044MODULE_DESCRIPTION("LM78/LM79 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045MODULE_LICENSE("GPL");
1046
1047module_init(sm_lm78_init);
1048module_exit(sm_lm78_exit);