blob: cbabab7ac7884b8dc3c9382f8931bf2f9505840e [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/*
Shubhrajyoti Dcaaa0f32010-10-28 20:31:44 +02003 * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Shubhrajyoti Dcaaa0f32010-10-28 20:31:44 +02006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/jiffies.h>
12#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040013#include <linux/hwmon.h>
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020014#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040015#include <linux/err.h>
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -030016#include <linux/of_device.h>
Eduardo Valentin22e73182013-07-16 14:54:55 -040017#include <linux/of.h>
Guenter Roecke65365f2016-06-19 17:49:19 -070018#include <linux/regmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "lm75.h"
20
21
David Brownell01a52392008-04-21 12:10:53 -070022/*
23 * This driver handles the LM75 and compatible digital temperature sensors.
David Brownell01a52392008-04-21 12:10:53 -070024 */
25
David Brownell9ebd3d82008-05-03 19:33:15 -070026enum lm75_type { /* keep sorted in alphabetical order */
Michael Henneriche96f9d82011-10-13 04:43:31 -040027 adt75,
Jean Delvare1f86df42009-12-14 21:17:26 +010028 ds1775,
David Brownell9ebd3d82008-05-03 19:33:15 -070029 ds75,
Jean Delvare3fbc81e2013-05-04 14:49:36 +020030 ds7505,
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +010031 g751,
Jean Delvare1f86df42009-12-14 21:17:26 +010032 lm75,
David Brownell9ebd3d82008-05-03 19:33:15 -070033 lm75a,
Michael Thalmeier799fc602014-11-18 17:08:04 +010034 lm75b,
David Brownell9ebd3d82008-05-03 19:33:15 -070035 max6625,
36 max6626,
Kun Yia54ca772018-09-11 13:25:58 -070037 max31725,
David Brownell9ebd3d82008-05-03 19:33:15 -070038 mcp980x,
39 stds75,
Jagan Teki2e9a41b2018-12-06 02:44:22 +053040 stlm75,
David Brownell9ebd3d82008-05-03 19:33:15 -070041 tcn75,
42 tmp100,
43 tmp101,
Shubhrajyoti Datta6d034052010-05-27 19:59:03 +020044 tmp105,
Frans Klaverc83959f2014-06-26 11:21:11 +020045 tmp112,
David Brownell9ebd3d82008-05-03 19:33:15 -070046 tmp175,
47 tmp275,
48 tmp75,
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +010049 tmp75b,
Ben Gardner9c32e812015-10-07 21:55:20 -050050 tmp75c,
David Brownell9ebd3d82008-05-03 19:33:15 -070051};
52
Jean Delvare8ff69ee2008-08-10 22:56:16 +020053/* Addresses scanned */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050054static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* The LM75 registers */
Guenter Roecke65365f2016-06-19 17:49:19 -070058#define LM75_REG_TEMP 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define LM75_REG_CONF 0x01
Guenter Roecke65365f2016-06-19 17:49:19 -070060#define LM75_REG_HYST 0x02
61#define LM75_REG_MAX 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* Each client has this additional data */
64struct lm75_data {
Guenter Roeckd663ec42014-01-13 16:00:37 -080065 struct i2c_client *client;
Guenter Roecke65365f2016-06-19 17:49:19 -070066 struct regmap *regmap;
David Brownell9ebd3d82008-05-03 19:33:15 -070067 u8 orig_conf;
Kun Yia54ca772018-09-11 13:25:58 -070068 u8 resolution; /* In bits, between 9 and 16 */
Jean Delvare87d06212013-05-04 14:49:36 +020069 u8 resolution_limits;
Guenter Roecke65365f2016-06-19 17:49:19 -070070 unsigned int sample_time; /* In ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
David Brownell01a52392008-04-21 12:10:53 -070073/*-----------------------------------------------------------------------*/
74
Eduardo Valentin22e73182013-07-16 14:54:55 -040075static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
76{
77 return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
78}
79
Guenter Roeck08b02432016-06-19 19:15:05 -070080static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
81 u32 attr, int channel, long *val)
Eduardo Valentin22e73182013-07-16 14:54:55 -040082{
Guenter Roecke65365f2016-06-19 17:49:19 -070083 struct lm75_data *data = dev_get_drvdata(dev);
Guenter Roeck08b02432016-06-19 19:15:05 -070084 unsigned int regval;
85 int err, reg;
Eduardo Valentin22e73182013-07-16 14:54:55 -040086
Guenter Roeck08b02432016-06-19 19:15:05 -070087 switch (type) {
88 case hwmon_chip:
89 switch (attr) {
90 case hwmon_chip_update_interval:
91 *val = data->sample_time;
Luis de Bethencourtccffe772018-01-17 18:24:48 +000092 break;
Guenter Roeck08b02432016-06-19 19:15:05 -070093 default:
94 return -EINVAL;
95 }
96 break;
97 case hwmon_temp:
98 switch (attr) {
99 case hwmon_temp_input:
100 reg = LM75_REG_TEMP;
101 break;
102 case hwmon_temp_max:
103 reg = LM75_REG_MAX;
104 break;
105 case hwmon_temp_max_hyst:
106 reg = LM75_REG_HYST;
107 break;
108 default:
109 return -EINVAL;
110 }
111 err = regmap_read(data->regmap, reg, &regval);
112 if (err < 0)
113 return err;
Eduardo Valentin22e73182013-07-16 14:54:55 -0400114
Guenter Roeck08b02432016-06-19 19:15:05 -0700115 *val = lm75_reg_to_mc(regval, data->resolution);
116 break;
117 default:
118 return -EINVAL;
119 }
Eduardo Valentin22e73182013-07-16 14:54:55 -0400120 return 0;
121}
122
Guenter Roeck08b02432016-06-19 19:15:05 -0700123static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
124 u32 attr, int channel, long temp)
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200125{
Guenter Roecke65365f2016-06-19 17:49:19 -0700126 struct lm75_data *data = dev_get_drvdata(dev);
Jean Delvare87d06212013-05-04 14:49:36 +0200127 u8 resolution;
Guenter Roeck08b02432016-06-19 19:15:05 -0700128 int reg;
Shubhrajyoti De3cd9522010-10-28 20:31:44 +0200129
Guenter Roeck08b02432016-06-19 19:15:05 -0700130 if (type != hwmon_temp)
131 return -EINVAL;
132
133 switch (attr) {
134 case hwmon_temp_max:
135 reg = LM75_REG_MAX;
136 break;
137 case hwmon_temp_max_hyst:
138 reg = LM75_REG_HYST;
139 break;
140 default:
141 return -EINVAL;
142 }
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200143
Jean Delvare87d06212013-05-04 14:49:36 +0200144 /*
145 * Resolution of limit registers is assumed to be the same as the
146 * temperature input register resolution unless given explicitly.
147 */
Guenter Roeck08b02432016-06-19 19:15:05 -0700148 if (data->resolution_limits)
Jean Delvare87d06212013-05-04 14:49:36 +0200149 resolution = data->resolution_limits;
150 else
151 resolution = data->resolution;
152
Jean Delvare87d06212013-05-04 14:49:36 +0200153 temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
Guenter Roecke65365f2016-06-19 17:49:19 -0700154 temp = DIV_ROUND_CLOSEST(temp << (resolution - 8),
155 1000) << (16 - resolution);
Guenter Roecke65365f2016-06-19 17:49:19 -0700156
Guenter Roeck7d82fcc2019-08-08 12:00:18 -0700157 return regmap_write(data->regmap, reg, (u16)temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Guenter Roeck08b02432016-06-19 19:15:05 -0700160static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
161 u32 attr, int channel)
Guenter Roeck5f7e5e22016-06-19 17:56:22 -0700162{
Guenter Roeck08b02432016-06-19 19:15:05 -0700163 switch (type) {
164 case hwmon_chip:
165 switch (attr) {
166 case hwmon_chip_update_interval:
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800167 return 0444;
Guenter Roeck08b02432016-06-19 19:15:05 -0700168 }
169 break;
170 case hwmon_temp:
171 switch (attr) {
172 case hwmon_temp_input:
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800173 return 0444;
Guenter Roeck08b02432016-06-19 19:15:05 -0700174 case hwmon_temp_max:
175 case hwmon_temp_max_hyst:
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800176 return 0644;
Guenter Roeck08b02432016-06-19 19:15:05 -0700177 }
178 break;
179 default:
180 break;
181 }
182 return 0;
Guenter Roeck5f7e5e22016-06-19 17:56:22 -0700183}
184
Guenter Roeck08b02432016-06-19 19:15:05 -0700185static const struct hwmon_channel_info *lm75_info[] = {
Guenter Roecke4f6fed12019-03-31 10:53:47 -0700186 HWMON_CHANNEL_INFO(chip,
187 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
188 HWMON_CHANNEL_INFO(temp,
189 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
Guenter Roeck08b02432016-06-19 19:15:05 -0700190 NULL
191};
192
193static const struct hwmon_ops lm75_hwmon_ops = {
194 .is_visible = lm75_is_visible,
195 .read = lm75_read,
196 .write = lm75_write,
197};
198
199static const struct hwmon_chip_info lm75_chip_info = {
200 .ops = &lm75_hwmon_ops,
201 .info = lm75_info,
202};
203
Guenter Roecke65365f2016-06-19 17:49:19 -0700204static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
205{
206 return reg != LM75_REG_TEMP;
207}
208
209static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
210{
211 return reg == LM75_REG_TEMP;
212}
213
214static const struct regmap_config lm75_regmap_config = {
215 .reg_bits = 8,
216 .val_bits = 16,
217 .max_register = LM75_REG_MAX,
218 .writeable_reg = lm75_is_writeable_reg,
219 .volatile_reg = lm75_is_volatile_reg,
220 .val_format_endian = REGMAP_ENDIAN_BIG,
221 .cache_type = REGCACHE_RBTREE,
David Frey1c96a2f2018-09-01 09:50:41 -0700222 .use_single_read = true,
223 .use_single_write = true,
Guenter Roecke65365f2016-06-19 17:49:19 -0700224};
225
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700226static void lm75_remove(void *data)
227{
228 struct lm75_data *lm75 = data;
229 struct i2c_client *client = lm75->client;
230
231 i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
232}
233
David Brownell9ebd3d82008-05-03 19:33:15 -0700234static int
235lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
236{
Guenter Roeckd663ec42014-01-13 16:00:37 -0800237 struct device *dev = &client->dev;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700238 struct device *hwmon_dev;
David Brownell9ebd3d82008-05-03 19:33:15 -0700239 struct lm75_data *data;
Guenter Roeck90e2b542016-07-25 14:56:00 -0700240 int status, err;
David Brownell9ebd3d82008-05-03 19:33:15 -0700241 u8 set_mask, clr_mask;
242 int new;
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300243 enum lm75_type kind;
244
245 if (client->dev.of_node)
246 kind = (enum lm75_type)of_device_get_match_data(&client->dev);
247 else
248 kind = id->driver_data;
David Brownell9ebd3d82008-05-03 19:33:15 -0700249
250 if (!i2c_check_functionality(client->adapter,
251 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
252 return -EIO;
253
Guenter Roeckd663ec42014-01-13 16:00:37 -0800254 data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
David Brownell9ebd3d82008-05-03 19:33:15 -0700255 if (!data)
256 return -ENOMEM;
257
Guenter Roeckd663ec42014-01-13 16:00:37 -0800258 data->client = client;
Guenter Roecke65365f2016-06-19 17:49:19 -0700259
260 data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
261 if (IS_ERR(data->regmap))
262 return PTR_ERR(data->regmap);
David Brownell9ebd3d82008-05-03 19:33:15 -0700263
264 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
265 * Then tweak to be more precise when appropriate.
266 */
267 set_mask = 0;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200268 clr_mask = LM75_SHUTDOWN; /* continuous conversions */
269
Jean Delvare0cd2c722013-05-04 14:49:36 +0200270 switch (kind) {
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200271 case adt75:
272 clr_mask |= 1 << 5; /* not one-shot mode */
Jean Delvare0cd2c722013-05-04 14:49:36 +0200273 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700274 data->sample_time = MSEC_PER_SEC / 8;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200275 break;
276 case ds1775:
277 case ds75:
278 case stds75:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200279 clr_mask |= 3 << 5;
280 set_mask |= 2 << 5; /* 11-bit mode */
281 data->resolution = 11;
Guenter Roecke65365f2016-06-19 17:49:19 -0700282 data->sample_time = MSEC_PER_SEC;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200283 break;
Jagan Teki2e9a41b2018-12-06 02:44:22 +0530284 case stlm75:
285 data->resolution = 9;
286 data->sample_time = MSEC_PER_SEC / 5;
287 break;
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200288 case ds7505:
289 set_mask |= 3 << 5; /* 12-bit mode */
290 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700291 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200292 break;
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +0100293 case g751:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200294 case lm75:
295 case lm75a:
296 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700297 data->sample_time = MSEC_PER_SEC / 2;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200298 break;
Michael Thalmeier799fc602014-11-18 17:08:04 +0100299 case lm75b:
300 data->resolution = 11;
Guenter Roecke65365f2016-06-19 17:49:19 -0700301 data->sample_time = MSEC_PER_SEC / 4;
Michael Thalmeier799fc602014-11-18 17:08:04 +0100302 break;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200303 case max6625:
304 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700305 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200306 break;
307 case max6626:
308 data->resolution = 12;
309 data->resolution_limits = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700310 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200311 break;
Kun Yia54ca772018-09-11 13:25:58 -0700312 case max31725:
313 data->resolution = 16;
314 data->sample_time = MSEC_PER_SEC / 8;
315 break;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200316 case tcn75:
317 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700318 data->sample_time = MSEC_PER_SEC / 8;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200319 break;
320 case mcp980x:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200321 data->resolution_limits = 9;
322 /* fall through */
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200323 case tmp100:
324 case tmp101:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200325 set_mask |= 3 << 5; /* 12-bit mode */
326 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700327 data->sample_time = MSEC_PER_SEC;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200328 clr_mask |= 1 << 7; /* not one-shot mode */
329 break;
Frans Klaverc83959f2014-06-26 11:21:11 +0200330 case tmp112:
331 set_mask |= 3 << 5; /* 12-bit mode */
332 clr_mask |= 1 << 7; /* not one-shot mode */
333 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700334 data->sample_time = MSEC_PER_SEC / 4;
Frans Klaverc83959f2014-06-26 11:21:11 +0200335 break;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200336 case tmp105:
337 case tmp175:
338 case tmp275:
339 case tmp75:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200340 set_mask |= 3 << 5; /* 12-bit mode */
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200341 clr_mask |= 1 << 7; /* not one-shot mode */
Jean Delvare0cd2c722013-05-04 14:49:36 +0200342 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700343 data->sample_time = MSEC_PER_SEC / 2;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200344 break;
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100345 case tmp75b: /* not one-shot mode, Conversion rate 37Hz */
Iker Perez del Palomar Sustatxaa95a4f3f2019-08-01 08:53:24 +0100346 clr_mask |= 1 << 7 | 0x3 << 5;
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100347 data->resolution = 12;
348 data->sample_time = MSEC_PER_SEC / 37;
349 break;
Ben Gardner9c32e812015-10-07 21:55:20 -0500350 case tmp75c:
351 clr_mask |= 1 << 5; /* not one-shot mode */
352 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700353 data->sample_time = MSEC_PER_SEC / 4;
Ben Gardner9c32e812015-10-07 21:55:20 -0500354 break;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200355 }
David Brownell9ebd3d82008-05-03 19:33:15 -0700356
357 /* configure as specified */
Guenter Roeck38aefb42016-06-19 17:11:13 -0700358 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
David Brownell9ebd3d82008-05-03 19:33:15 -0700359 if (status < 0) {
Guenter Roeckd663ec42014-01-13 16:00:37 -0800360 dev_dbg(dev, "Can't read config? %d\n", status);
Guenter Roeck13ac7a02012-06-02 09:58:08 -0700361 return status;
David Brownell9ebd3d82008-05-03 19:33:15 -0700362 }
363 data->orig_conf = status;
364 new = status & ~clr_mask;
365 new |= set_mask;
366 if (status != new)
Guenter Roeck38aefb42016-06-19 17:11:13 -0700367 i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700368
Guenter Roeck90e2b542016-07-25 14:56:00 -0700369 err = devm_add_action_or_reset(dev, lm75_remove, data);
370 if (err)
371 return err;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700372
Guenter Roeckd663ec42014-01-13 16:00:37 -0800373 dev_dbg(dev, "Config %02x\n", new);
David Brownell9ebd3d82008-05-03 19:33:15 -0700374
Guenter Roeck08b02432016-06-19 19:15:05 -0700375 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
376 data, &lm75_chip_info,
377 NULL);
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700378 if (IS_ERR(hwmon_dev))
379 return PTR_ERR(hwmon_dev);
David Brownell9ebd3d82008-05-03 19:33:15 -0700380
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700381 dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
David Brownell9ebd3d82008-05-03 19:33:15 -0700382
383 return 0;
David Brownell9ebd3d82008-05-03 19:33:15 -0700384}
385
David Brownell9ebd3d82008-05-03 19:33:15 -0700386static const struct i2c_device_id lm75_ids[] = {
Michael Henneriche96f9d82011-10-13 04:43:31 -0400387 { "adt75", adt75, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700388 { "ds1775", ds1775, },
389 { "ds75", ds75, },
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200390 { "ds7505", ds7505, },
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +0100391 { "g751", g751, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700392 { "lm75", lm75, },
393 { "lm75a", lm75a, },
Michael Thalmeier799fc602014-11-18 17:08:04 +0100394 { "lm75b", lm75b, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700395 { "max6625", max6625, },
396 { "max6626", max6626, },
Kun Yia54ca772018-09-11 13:25:58 -0700397 { "max31725", max31725, },
398 { "max31726", max31725, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700399 { "mcp980x", mcp980x, },
400 { "stds75", stds75, },
Jagan Teki2e9a41b2018-12-06 02:44:22 +0530401 { "stlm75", stlm75, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700402 { "tcn75", tcn75, },
403 { "tmp100", tmp100, },
404 { "tmp101", tmp101, },
Shubhrajyoti Datta6d034052010-05-27 19:59:03 +0200405 { "tmp105", tmp105, },
Frans Klaverc83959f2014-06-26 11:21:11 +0200406 { "tmp112", tmp112, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700407 { "tmp175", tmp175, },
408 { "tmp275", tmp275, },
409 { "tmp75", tmp75, },
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100410 { "tmp75b", tmp75b, },
Ben Gardner9c32e812015-10-07 21:55:20 -0500411 { "tmp75c", tmp75c, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700412 { /* LIST END */ }
413};
414MODULE_DEVICE_TABLE(i2c, lm75_ids);
415
Guenter Roeckffa83e72019-04-04 06:40:00 -0700416static const struct of_device_id __maybe_unused lm75_of_match[] = {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300417 {
418 .compatible = "adi,adt75",
419 .data = (void *)adt75
420 },
421 {
422 .compatible = "dallas,ds1775",
423 .data = (void *)ds1775
424 },
425 {
426 .compatible = "dallas,ds75",
427 .data = (void *)ds75
428 },
429 {
430 .compatible = "dallas,ds7505",
431 .data = (void *)ds7505
432 },
433 {
434 .compatible = "gmt,g751",
435 .data = (void *)g751
436 },
437 {
438 .compatible = "national,lm75",
439 .data = (void *)lm75
440 },
441 {
442 .compatible = "national,lm75a",
443 .data = (void *)lm75a
444 },
445 {
446 .compatible = "national,lm75b",
447 .data = (void *)lm75b
448 },
449 {
450 .compatible = "maxim,max6625",
451 .data = (void *)max6625
452 },
453 {
454 .compatible = "maxim,max6626",
455 .data = (void *)max6626
456 },
457 {
Kun Yia54ca772018-09-11 13:25:58 -0700458 .compatible = "maxim,max31725",
459 .data = (void *)max31725
460 },
461 {
462 .compatible = "maxim,max31726",
463 .data = (void *)max31725
464 },
465 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300466 .compatible = "maxim,mcp980x",
467 .data = (void *)mcp980x
468 },
469 {
470 .compatible = "st,stds75",
471 .data = (void *)stds75
472 },
473 {
Jagan Teki2e9a41b2018-12-06 02:44:22 +0530474 .compatible = "st,stlm75",
475 .data = (void *)stlm75
476 },
477 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300478 .compatible = "microchip,tcn75",
479 .data = (void *)tcn75
480 },
481 {
482 .compatible = "ti,tmp100",
483 .data = (void *)tmp100
484 },
485 {
486 .compatible = "ti,tmp101",
487 .data = (void *)tmp101
488 },
489 {
490 .compatible = "ti,tmp105",
491 .data = (void *)tmp105
492 },
493 {
494 .compatible = "ti,tmp112",
495 .data = (void *)tmp112
496 },
497 {
498 .compatible = "ti,tmp175",
499 .data = (void *)tmp175
500 },
501 {
502 .compatible = "ti,tmp275",
503 .data = (void *)tmp275
504 },
505 {
506 .compatible = "ti,tmp75",
507 .data = (void *)tmp75
508 },
509 {
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100510 .compatible = "ti,tmp75b",
511 .data = (void *)tmp75b
512 },
513 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300514 .compatible = "ti,tmp75c",
515 .data = (void *)tmp75c
516 },
517 { },
518};
519MODULE_DEVICE_TABLE(of, lm75_of_match);
520
Len Sorensen05e82fe2011-03-21 17:59:36 +0100521#define LM75A_ID 0xA1
522
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200523/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100524static int lm75_detect(struct i2c_client *new_client,
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200525 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200527 struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int i;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100529 int conf, hyst, os;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100530 bool is_lm75a = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
533 I2C_FUNC_SMBUS_WORD_DATA))
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200534 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jean Delvare426343e2011-10-13 17:15:11 -0400536 /*
537 * Now, we do the remaining detection. There is no identification-
538 * dedicated register so we have to rely on several tricks:
539 * unused bits, registers cycling over 8-address boundaries,
540 * addresses 0x04-0x07 returning the last read value.
541 * The cycling+unused addresses combination is not tested,
542 * since it would significantly slow the detection down and would
543 * hardly add any value.
544 *
545 * The National Semiconductor LM75A is different than earlier
546 * LM75s. It has an ID byte of 0xaX (where X is the chip
547 * revision, with 1 being the only revision in existence) in
548 * register 7, and unused registers return 0xff rather than the
549 * last read value.
550 *
551 * Note that this function only detects the original National
552 * Semiconductor LM75 and the LM75A. Clones from other vendors
553 * aren't detected, on purpose, because they are typically never
554 * found on PC hardware. They are found on embedded designs where
555 * they can be instantiated explicitly so detection is not needed.
556 * The absence of identification registers on all these clones
557 * would make their exhaustive detection very difficult and weak,
558 * and odds are that the driver would bind to unsupported devices.
559 */
Len Sorensen05e82fe2011-03-21 17:59:36 +0100560
Jean Delvaree76f67b2011-03-21 17:59:36 +0100561 /* Unused bits */
Jean Delvare52df6442009-12-09 20:35:57 +0100562 conf = i2c_smbus_read_byte_data(new_client, 1);
Jean Delvaree76f67b2011-03-21 17:59:36 +0100563 if (conf & 0xe0)
564 return -ENODEV;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100565
566 /* First check for LM75A */
567 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
568 /* LM75A returns 0xff on unused registers so
569 just to be sure we check for that too. */
570 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
571 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
572 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
573 return -ENODEV;
574 is_lm75a = 1;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100575 hyst = i2c_smbus_read_byte_data(new_client, 2);
576 os = i2c_smbus_read_byte_data(new_client, 3);
Len Sorensen05e82fe2011-03-21 17:59:36 +0100577 } else { /* Traditional style LM75 detection */
578 /* Unused addresses */
Jean Delvaree76f67b2011-03-21 17:59:36 +0100579 hyst = i2c_smbus_read_byte_data(new_client, 2);
580 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
581 || i2c_smbus_read_byte_data(new_client, 5) != hyst
582 || i2c_smbus_read_byte_data(new_client, 6) != hyst
583 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
Len Sorensen05e82fe2011-03-21 17:59:36 +0100584 return -ENODEV;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100585 os = i2c_smbus_read_byte_data(new_client, 3);
586 if (i2c_smbus_read_byte_data(new_client, 4) != os
587 || i2c_smbus_read_byte_data(new_client, 5) != os
588 || i2c_smbus_read_byte_data(new_client, 6) != os
589 || i2c_smbus_read_byte_data(new_client, 7) != os)
Len Sorensen05e82fe2011-03-21 17:59:36 +0100590 return -ENODEV;
591 }
Guenter Roeck4ad40cc2014-12-04 09:58:15 -0800592 /*
593 * It is very unlikely that this is a LM75 if both
594 * hysteresis and temperature limit registers are 0.
595 */
596 if (hyst == 0 && os == 0)
597 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Jean Delvare52df6442009-12-09 20:35:57 +0100599 /* Addresses cycling */
Jean Delvaree76f67b2011-03-21 17:59:36 +0100600 for (i = 8; i <= 248; i += 40) {
Jean Delvare52df6442009-12-09 20:35:57 +0100601 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
Jean Delvaree76f67b2011-03-21 17:59:36 +0100602 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
603 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
Jean Delvare52df6442009-12-09 20:35:57 +0100604 return -ENODEV;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100605 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
606 != LM75A_ID)
607 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
Len Sorensen05e82fe2011-03-21 17:59:36 +0100610 strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200615#ifdef CONFIG_PM
616static int lm75_suspend(struct device *dev)
617{
618 int status;
619 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck38aefb42016-06-19 17:11:13 -0700620 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200621 if (status < 0) {
622 dev_dbg(&client->dev, "Can't read config? %d\n", status);
623 return status;
624 }
625 status = status | LM75_SHUTDOWN;
Guenter Roeck38aefb42016-06-19 17:11:13 -0700626 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200627 return 0;
628}
629
630static int lm75_resume(struct device *dev)
631{
632 int status;
633 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck38aefb42016-06-19 17:11:13 -0700634 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200635 if (status < 0) {
636 dev_dbg(&client->dev, "Can't read config? %d\n", status);
637 return status;
638 }
639 status = status & ~LM75_SHUTDOWN;
Guenter Roeck38aefb42016-06-19 17:11:13 -0700640 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200641 return 0;
642}
643
644static const struct dev_pm_ops lm75_dev_pm_ops = {
645 .suspend = lm75_suspend,
646 .resume = lm75_resume,
647};
648#define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
649#else
650#define LM75_DEV_PM_OPS NULL
651#endif /* CONFIG_PM */
652
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200653static struct i2c_driver lm75_driver = {
654 .class = I2C_CLASS_HWMON,
David Brownell01a52392008-04-21 12:10:53 -0700655 .driver = {
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200656 .name = "lm75",
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300657 .of_match_table = of_match_ptr(lm75_of_match),
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200658 .pm = LM75_DEV_PM_OPS,
David Brownell01a52392008-04-21 12:10:53 -0700659 },
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200660 .probe = lm75_probe,
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200661 .id_table = lm75_ids,
662 .detect = lm75_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100663 .address_list = normal_i2c,
David Brownell01a52392008-04-21 12:10:53 -0700664};
665
Axel Linf0967ee2012-01-20 15:38:18 +0800666module_i2c_driver(lm75_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
669MODULE_DESCRIPTION("LM75 driver");
670MODULE_LICENSE("GPL");