blob: 656a77102ca6f422e903c2d43a5ba9729d64cfa3 [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 * w83l785ts.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
Jean Delvare7c81c60f2014-01-29 20:40:08 +01005 * Copyright (C) 2003-2009 Jean Delvare <jdelvare@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Inspired from the lm83 driver. The W83L785TS-S is a sensor chip made
8 * by Winbond. It reports a single external temperature with a 1 deg
9 * resolution and a 3 deg accuracy. Datasheet can be obtained from
10 * Winbond's website at:
11 * http://www.winbond-usa.com/products/winbond_products/pdfs/PCIC/W83L785TS-S.pdf
12 *
13 * Ported to Linux 2.6 by Wolfgang Ziegler <nuppla@gmx.at> and Jean Delvare
Jean Delvare7c81c60f2014-01-29 20:40:08 +010014 * <jdelvare@suse.de>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * Thanks to James Bolt <james@evilpenguin.com> for benchmarking the read
17 * error handling mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/delay.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.h>
25#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040026#include <linux/hwmon.h>
Jean Delvare709439a2005-09-25 16:41:18 +020027#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040028#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* How many retries on register read error */
32#define MAX_RETRIES 5
33
34/*
35 * Address to scan
36 * Address is fully defined internally and cannot be changed.
37 */
38
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050039static const unsigned short normal_i2c[] = { 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * The W83L785TS-S registers
43 * Manufacturer ID is 0x5CA3 for Winbond.
44 */
45
46#define W83L785TS_REG_MAN_ID1 0x4D
47#define W83L785TS_REG_MAN_ID2 0x4C
48#define W83L785TS_REG_CHIP_ID 0x4E
49#define W83L785TS_REG_CONFIG 0x40
50#define W83L785TS_REG_TYPE 0x52
51#define W83L785TS_REG_TEMP 0x27
52#define W83L785TS_REG_TEMP_OVER 0x53 /* not sure about this one */
53
54/*
55 * Conversions
56 * The W83L785TS-S uses signed 8-bit values.
57 */
58
Jean Delvarecb929ea2005-09-25 16:45:03 +020059#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
62 * Functions declaration
63 */
64
Stephen Kitt67487032020-08-13 18:02:22 +020065static int w83l785ts_probe(struct i2c_client *client);
Jean Delvare310ec792009-12-14 21:17:23 +010066static int w83l785ts_detect(struct i2c_client *client,
Jean Delvaredc18a412008-07-16 19:30:18 +020067 struct i2c_board_info *info);
68static int w83l785ts_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval);
70static struct w83l785ts_data *w83l785ts_update_device(struct device *dev);
71
72/*
73 * Driver data (common to all clients)
74 */
Frans Meulenbroeks59965422012-01-08 19:34:16 +010075
Jean Delvaredc18a412008-07-16 19:30:18 +020076static const struct i2c_device_id w83l785ts_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +010077 { "w83l785ts", 0 },
Jean Delvaredc18a412008-07-16 19:30:18 +020078 { }
79};
80MODULE_DEVICE_TABLE(i2c, w83l785ts_id);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static struct i2c_driver w83l785ts_driver = {
Jean Delvaredc18a412008-07-16 19:30:18 +020083 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +010084 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010085 .name = "w83l785ts",
86 },
Stephen Kitt67487032020-08-13 18:02:22 +020087 .probe_new = w83l785ts_probe,
Jean Delvaredc18a412008-07-16 19:30:18 +020088 .remove = w83l785ts_remove,
89 .id_table = w83l785ts_id,
90 .detect = w83l785ts_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +010091 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94/*
95 * Client data (each client gets its own)
96 */
97
98struct w83l785ts_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070099 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100100 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 char valid; /* zero until following fields are valid */
102 unsigned long last_updated; /* in jiffies */
103
104 /* registers values */
Guenter Roeck130067d2012-01-19 11:02:28 -0800105 s8 temp[2]; /* 0: input, 1: critical limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108/*
109 * Sysfs stuff
110 */
111
Jean Delvare709439a2005-09-25 16:41:18 +0200112static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
113 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Jean Delvare709439a2005-09-25 16:41:18 +0200115 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct w83l785ts_data *data = w83l785ts_update_device(dev);
Jean Delvare709439a2005-09-25 16:41:18 +0200117 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Jean Delvare709439a2005-09-25 16:41:18 +0200120static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
121static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * Real code
125 */
126
Jean Delvaredc18a412008-07-16 19:30:18 +0200127/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100128static int w83l785ts_detect(struct i2c_client *client,
Jean Delvaredc18a412008-07-16 19:30:18 +0200129 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Jean Delvarea1fa4cd2009-12-09 20:35:56 +0100131 struct i2c_adapter *adapter = client->adapter;
132 u16 man_id;
133 u8 chip_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvaredc18a412008-07-16 19:30:18 +0200136 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jean Delvarea1fa4cd2009-12-09 20:35:56 +0100138 /* detection */
139 if ((w83l785ts_read_value(client, W83L785TS_REG_CONFIG, 0) & 0x80)
140 || (w83l785ts_read_value(client, W83L785TS_REG_TYPE, 0) & 0xFC)) {
141 dev_dbg(&adapter->dev,
142 "W83L785TS-S detection failed at 0x%02x\n",
143 client->addr);
144 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Jean Delvarea1fa4cd2009-12-09 20:35:56 +0100147 /* Identification */
148 man_id = (w83l785ts_read_value(client, W83L785TS_REG_MAN_ID1, 0) << 8)
149 + w83l785ts_read_value(client, W83L785TS_REG_MAN_ID2, 0);
150 chip_id = w83l785ts_read_value(client, W83L785TS_REG_CHIP_ID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Jean Delvarea1fa4cd2009-12-09 20:35:56 +0100152 if (man_id != 0x5CA3 /* Winbond */
153 || chip_id != 0x70) { /* W83L785TS-S */
154 dev_dbg(&adapter->dev,
155 "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
156 man_id, chip_id);
157 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159
Jean Delvaredc18a412008-07-16 19:30:18 +0200160 strlcpy(info->type, "w83l785ts", I2C_NAME_SIZE);
161
162 return 0;
163}
164
Stephen Kitt67487032020-08-13 18:02:22 +0200165static int w83l785ts_probe(struct i2c_client *client)
Jean Delvaredc18a412008-07-16 19:30:18 +0200166{
167 struct w83l785ts_data *data;
Guenter Roeckf187fc02012-06-02 11:48:01 -0700168 struct device *dev = &client->dev;
Guenter Roecke2730be2012-06-18 08:26:05 -0700169 int err;
Jean Delvaredc18a412008-07-16 19:30:18 +0200170
Guenter Roecke2730be2012-06-18 08:26:05 -0700171 data = devm_kzalloc(dev, sizeof(struct w83l785ts_data), GFP_KERNEL);
172 if (!data)
173 return -ENOMEM;
Jean Delvaredc18a412008-07-16 19:30:18 +0200174
Guenter Roeckf187fc02012-06-02 11:48:01 -0700175 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100176 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /*
179 * Initialize the W83L785TS chip
180 * Nothing yet, assume it is already started.
181 */
182
Guenter Roeckf187fc02012-06-02 11:48:01 -0700183 err = device_create_file(dev, &sensor_dev_attr_temp1_input.dev_attr);
Rudolf Marekccc5c302006-09-24 21:23:26 +0200184 if (err)
Guenter Roecke2730be2012-06-18 08:26:05 -0700185 return err;
Rudolf Marekccc5c302006-09-24 21:23:26 +0200186
Guenter Roeckf187fc02012-06-02 11:48:01 -0700187 err = device_create_file(dev, &sensor_dev_attr_temp1_max.dev_attr);
Rudolf Marekccc5c302006-09-24 21:23:26 +0200188 if (err)
189 goto exit_remove;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Register sysfs hooks */
Guenter Roeckf187fc02012-06-02 11:48:01 -0700192 data->hwmon_dev = hwmon_device_register(dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700193 if (IS_ERR(data->hwmon_dev)) {
194 err = PTR_ERR(data->hwmon_dev);
Rudolf Marekccc5c302006-09-24 21:23:26 +0200195 goto exit_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199
Rudolf Marekccc5c302006-09-24 21:23:26 +0200200exit_remove:
Guenter Roeckf187fc02012-06-02 11:48:01 -0700201 device_remove_file(dev, &sensor_dev_attr_temp1_input.dev_attr);
202 device_remove_file(dev, &sensor_dev_attr_temp1_max.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return err;
204}
205
Jean Delvaredc18a412008-07-16 19:30:18 +0200206static int w83l785ts_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400208 struct w83l785ts_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Tony Jones1beeffe2007-08-20 13:46:20 -0700210 hwmon_device_unregister(data->hwmon_dev);
Rudolf Marekccc5c302006-09-24 21:23:26 +0200211 device_remove_file(&client->dev,
212 &sensor_dev_attr_temp1_input.dev_attr);
213 device_remove_file(&client->dev,
214 &sensor_dev_attr_temp1_max.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217}
218
219static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval)
220{
221 int value, i;
Jean Delvaredc18a412008-07-16 19:30:18 +0200222 struct device *dev;
223 const char *prefix;
224
Guenter Roeck130067d2012-01-19 11:02:28 -0800225 /*
226 * We might be called during detection, at which point the client
227 * isn't yet fully initialized, so we can't use dev_dbg on it
228 */
Jean Delvaredc18a412008-07-16 19:30:18 +0200229 if (i2c_get_clientdata(client)) {
230 dev = &client->dev;
231 prefix = "";
232 } else {
233 dev = &client->adapter->dev;
234 prefix = "w83l785ts: ";
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Guenter Roeck130067d2012-01-19 11:02:28 -0800237 /*
238 * Frequent read errors have been reported on Asus boards, so we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * retry on read errors. If it still fails (unlikely), return the
Guenter Roeck130067d2012-01-19 11:02:28 -0800240 * default value requested by the caller.
241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 for (i = 1; i <= MAX_RETRIES; i++) {
243 value = i2c_smbus_read_byte_data(client, reg);
244 if (value >= 0) {
Jean Delvaredc18a412008-07-16 19:30:18 +0200245 dev_dbg(dev, "%sRead 0x%02x from register 0x%02x.\n",
246 prefix, value, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return value;
248 }
Jean Delvaredc18a412008-07-16 19:30:18 +0200249 dev_dbg(dev, "%sRead failed, will retry in %d.\n", prefix, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 msleep(i);
251 }
252
Jean Delvaredc18a412008-07-16 19:30:18 +0200253 dev_err(dev, "%sCouldn't read value from register 0x%02x.\n", prefix,
Jean Delvare4040c412008-02-12 11:17:26 +0100254 reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return defval;
256}
257
258static struct w83l785ts_data *w83l785ts_update_device(struct device *dev)
259{
260 struct i2c_client *client = to_i2c_client(dev);
261 struct w83l785ts_data *data = i2c_get_clientdata(client);
262
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100263 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (!data->valid || time_after(jiffies, data->last_updated + HZ * 2)) {
266 dev_dbg(&client->dev, "Updating w83l785ts data.\n");
Jean Delvare709439a2005-09-25 16:41:18 +0200267 data->temp[0] = w83l785ts_read_value(client,
268 W83L785TS_REG_TEMP, data->temp[0]);
269 data->temp[1] = w83l785ts_read_value(client,
270 W83L785TS_REG_TEMP_OVER, data->temp[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 data->last_updated = jiffies;
273 data->valid = 1;
274 }
275
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100276 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return data;
279}
280
Axel Linf0967ee2012-01-20 15:38:18 +0800281module_i2c_driver(w83l785ts_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Jean Delvare7c81c60f2014-01-29 20:40:08 +0100283MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284MODULE_DESCRIPTION("W83L785TS-S driver");
285MODULE_LICENSE("GPL");