blob: 0cd1447718c9368bc17f2a7ac7abce902290121e [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,
Daniel Mack557c7ff2019-07-11 14:45:04 +020039 pct2075,
David Brownell9ebd3d82008-05-03 19:33:15 -070040 stds75,
Jagan Teki2e9a41b2018-12-06 02:44:22 +053041 stlm75,
David Brownell9ebd3d82008-05-03 19:33:15 -070042 tcn75,
43 tmp100,
44 tmp101,
Shubhrajyoti Datta6d034052010-05-27 19:59:03 +020045 tmp105,
Frans Klaverc83959f2014-06-26 11:21:11 +020046 tmp112,
David Brownell9ebd3d82008-05-03 19:33:15 -070047 tmp175,
48 tmp275,
49 tmp75,
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +010050 tmp75b,
Ben Gardner9c32e812015-10-07 21:55:20 -050051 tmp75c,
David Brownell9ebd3d82008-05-03 19:33:15 -070052};
53
Jean Delvare8ff69ee2008-08-10 22:56:16 +020054/* Addresses scanned */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050055static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* The LM75 registers */
Guenter Roecke65365f2016-06-19 17:49:19 -070059#define LM75_REG_TEMP 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define LM75_REG_CONF 0x01
Guenter Roecke65365f2016-06-19 17:49:19 -070061#define LM75_REG_HYST 0x02
62#define LM75_REG_MAX 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* Each client has this additional data */
65struct lm75_data {
Guenter Roeckd663ec42014-01-13 16:00:37 -080066 struct i2c_client *client;
Guenter Roecke65365f2016-06-19 17:49:19 -070067 struct regmap *regmap;
David Brownell9ebd3d82008-05-03 19:33:15 -070068 u8 orig_conf;
Kun Yia54ca772018-09-11 13:25:58 -070069 u8 resolution; /* In bits, between 9 and 16 */
Jean Delvare87d06212013-05-04 14:49:36 +020070 u8 resolution_limits;
Guenter Roecke65365f2016-06-19 17:49:19 -070071 unsigned int sample_time; /* In ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
David Brownell01a52392008-04-21 12:10:53 -070074/*-----------------------------------------------------------------------*/
75
Eduardo Valentin22e73182013-07-16 14:54:55 -040076static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
77{
78 return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
79}
80
Guenter Roeck08b02432016-06-19 19:15:05 -070081static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
82 u32 attr, int channel, long *val)
Eduardo Valentin22e73182013-07-16 14:54:55 -040083{
Guenter Roecke65365f2016-06-19 17:49:19 -070084 struct lm75_data *data = dev_get_drvdata(dev);
Guenter Roeck08b02432016-06-19 19:15:05 -070085 unsigned int regval;
86 int err, reg;
Eduardo Valentin22e73182013-07-16 14:54:55 -040087
Guenter Roeck08b02432016-06-19 19:15:05 -070088 switch (type) {
89 case hwmon_chip:
90 switch (attr) {
91 case hwmon_chip_update_interval:
92 *val = data->sample_time;
Luis de Bethencourtccffe772018-01-17 18:24:48 +000093 break;
Guenter Roeck08b02432016-06-19 19:15:05 -070094 default:
95 return -EINVAL;
96 }
97 break;
98 case hwmon_temp:
99 switch (attr) {
100 case hwmon_temp_input:
101 reg = LM75_REG_TEMP;
102 break;
103 case hwmon_temp_max:
104 reg = LM75_REG_MAX;
105 break;
106 case hwmon_temp_max_hyst:
107 reg = LM75_REG_HYST;
108 break;
109 default:
110 return -EINVAL;
111 }
112 err = regmap_read(data->regmap, reg, &regval);
113 if (err < 0)
114 return err;
Eduardo Valentin22e73182013-07-16 14:54:55 -0400115
Guenter Roeck08b02432016-06-19 19:15:05 -0700116 *val = lm75_reg_to_mc(regval, data->resolution);
117 break;
118 default:
119 return -EINVAL;
120 }
Eduardo Valentin22e73182013-07-16 14:54:55 -0400121 return 0;
122}
123
Guenter Roeck08b02432016-06-19 19:15:05 -0700124static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
125 u32 attr, int channel, long temp)
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200126{
Guenter Roecke65365f2016-06-19 17:49:19 -0700127 struct lm75_data *data = dev_get_drvdata(dev);
Jean Delvare87d06212013-05-04 14:49:36 +0200128 u8 resolution;
Guenter Roeck08b02432016-06-19 19:15:05 -0700129 int reg;
Shubhrajyoti De3cd9522010-10-28 20:31:44 +0200130
Guenter Roeck08b02432016-06-19 19:15:05 -0700131 if (type != hwmon_temp)
132 return -EINVAL;
133
134 switch (attr) {
135 case hwmon_temp_max:
136 reg = LM75_REG_MAX;
137 break;
138 case hwmon_temp_max_hyst:
139 reg = LM75_REG_HYST;
140 break;
141 default:
142 return -EINVAL;
143 }
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200144
Jean Delvare87d06212013-05-04 14:49:36 +0200145 /*
146 * Resolution of limit registers is assumed to be the same as the
147 * temperature input register resolution unless given explicitly.
148 */
Guenter Roeck08b02432016-06-19 19:15:05 -0700149 if (data->resolution_limits)
Jean Delvare87d06212013-05-04 14:49:36 +0200150 resolution = data->resolution_limits;
151 else
152 resolution = data->resolution;
153
Jean Delvare87d06212013-05-04 14:49:36 +0200154 temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
Guenter Roecke65365f2016-06-19 17:49:19 -0700155 temp = DIV_ROUND_CLOSEST(temp << (resolution - 8),
156 1000) << (16 - resolution);
Guenter Roecke65365f2016-06-19 17:49:19 -0700157
Guenter Roeck7d82fcc2019-08-08 12:00:18 -0700158 return regmap_write(data->regmap, reg, (u16)temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Guenter Roeck08b02432016-06-19 19:15:05 -0700161static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
162 u32 attr, int channel)
Guenter Roeck5f7e5e22016-06-19 17:56:22 -0700163{
Guenter Roeck08b02432016-06-19 19:15:05 -0700164 switch (type) {
165 case hwmon_chip:
166 switch (attr) {
167 case hwmon_chip_update_interval:
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800168 return 0444;
Guenter Roeck08b02432016-06-19 19:15:05 -0700169 }
170 break;
171 case hwmon_temp:
172 switch (attr) {
173 case hwmon_temp_input:
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800174 return 0444;
Guenter Roeck08b02432016-06-19 19:15:05 -0700175 case hwmon_temp_max:
176 case hwmon_temp_max_hyst:
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800177 return 0644;
Guenter Roeck08b02432016-06-19 19:15:05 -0700178 }
179 break;
180 default:
181 break;
182 }
183 return 0;
Guenter Roeck5f7e5e22016-06-19 17:56:22 -0700184}
185
Guenter Roeck08b02432016-06-19 19:15:05 -0700186static const struct hwmon_channel_info *lm75_info[] = {
Guenter Roecke4f6fed12019-03-31 10:53:47 -0700187 HWMON_CHANNEL_INFO(chip,
188 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
189 HWMON_CHANNEL_INFO(temp,
190 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
Guenter Roeck08b02432016-06-19 19:15:05 -0700191 NULL
192};
193
194static const struct hwmon_ops lm75_hwmon_ops = {
195 .is_visible = lm75_is_visible,
196 .read = lm75_read,
197 .write = lm75_write,
198};
199
200static const struct hwmon_chip_info lm75_chip_info = {
201 .ops = &lm75_hwmon_ops,
202 .info = lm75_info,
203};
204
Guenter Roecke65365f2016-06-19 17:49:19 -0700205static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
206{
207 return reg != LM75_REG_TEMP;
208}
209
210static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
211{
212 return reg == LM75_REG_TEMP;
213}
214
215static const struct regmap_config lm75_regmap_config = {
216 .reg_bits = 8,
217 .val_bits = 16,
218 .max_register = LM75_REG_MAX,
219 .writeable_reg = lm75_is_writeable_reg,
220 .volatile_reg = lm75_is_volatile_reg,
221 .val_format_endian = REGMAP_ENDIAN_BIG,
222 .cache_type = REGCACHE_RBTREE,
David Frey1c96a2f2018-09-01 09:50:41 -0700223 .use_single_read = true,
224 .use_single_write = true,
Guenter Roecke65365f2016-06-19 17:49:19 -0700225};
226
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700227static void lm75_remove(void *data)
228{
229 struct lm75_data *lm75 = data;
230 struct i2c_client *client = lm75->client;
231
232 i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
233}
234
David Brownell9ebd3d82008-05-03 19:33:15 -0700235static int
236lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
237{
Guenter Roeckd663ec42014-01-13 16:00:37 -0800238 struct device *dev = &client->dev;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700239 struct device *hwmon_dev;
David Brownell9ebd3d82008-05-03 19:33:15 -0700240 struct lm75_data *data;
Guenter Roeck90e2b542016-07-25 14:56:00 -0700241 int status, err;
David Brownell9ebd3d82008-05-03 19:33:15 -0700242 u8 set_mask, clr_mask;
243 int new;
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300244 enum lm75_type kind;
245
246 if (client->dev.of_node)
247 kind = (enum lm75_type)of_device_get_match_data(&client->dev);
248 else
249 kind = id->driver_data;
David Brownell9ebd3d82008-05-03 19:33:15 -0700250
251 if (!i2c_check_functionality(client->adapter,
252 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
253 return -EIO;
254
Guenter Roeckd663ec42014-01-13 16:00:37 -0800255 data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
David Brownell9ebd3d82008-05-03 19:33:15 -0700256 if (!data)
257 return -ENOMEM;
258
Guenter Roeckd663ec42014-01-13 16:00:37 -0800259 data->client = client;
Guenter Roecke65365f2016-06-19 17:49:19 -0700260
261 data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
262 if (IS_ERR(data->regmap))
263 return PTR_ERR(data->regmap);
David Brownell9ebd3d82008-05-03 19:33:15 -0700264
265 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
266 * Then tweak to be more precise when appropriate.
267 */
268 set_mask = 0;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200269 clr_mask = LM75_SHUTDOWN; /* continuous conversions */
270
Jean Delvare0cd2c722013-05-04 14:49:36 +0200271 switch (kind) {
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200272 case adt75:
273 clr_mask |= 1 << 5; /* not one-shot mode */
Jean Delvare0cd2c722013-05-04 14:49:36 +0200274 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700275 data->sample_time = MSEC_PER_SEC / 8;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200276 break;
277 case ds1775:
278 case ds75:
279 case stds75:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200280 clr_mask |= 3 << 5;
281 set_mask |= 2 << 5; /* 11-bit mode */
282 data->resolution = 11;
Guenter Roecke65365f2016-06-19 17:49:19 -0700283 data->sample_time = MSEC_PER_SEC;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200284 break;
Jagan Teki2e9a41b2018-12-06 02:44:22 +0530285 case stlm75:
286 data->resolution = 9;
287 data->sample_time = MSEC_PER_SEC / 5;
288 break;
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200289 case ds7505:
290 set_mask |= 3 << 5; /* 12-bit mode */
291 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700292 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200293 break;
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +0100294 case g751:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200295 case lm75:
296 case lm75a:
297 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700298 data->sample_time = MSEC_PER_SEC / 2;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200299 break;
Michael Thalmeier799fc602014-11-18 17:08:04 +0100300 case lm75b:
301 data->resolution = 11;
Guenter Roecke65365f2016-06-19 17:49:19 -0700302 data->sample_time = MSEC_PER_SEC / 4;
Michael Thalmeier799fc602014-11-18 17:08:04 +0100303 break;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200304 case max6625:
305 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700306 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200307 break;
308 case max6626:
309 data->resolution = 12;
310 data->resolution_limits = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700311 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200312 break;
Kun Yia54ca772018-09-11 13:25:58 -0700313 case max31725:
314 data->resolution = 16;
315 data->sample_time = MSEC_PER_SEC / 8;
316 break;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200317 case tcn75:
318 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700319 data->sample_time = MSEC_PER_SEC / 8;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200320 break;
Daniel Mack557c7ff2019-07-11 14:45:04 +0200321 case pct2075:
322 data->resolution = 11;
323 data->sample_time = MSEC_PER_SEC / 10;
324 break;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200325 case mcp980x:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200326 data->resolution_limits = 9;
327 /* fall through */
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200328 case tmp100:
329 case tmp101:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200330 set_mask |= 3 << 5; /* 12-bit mode */
331 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700332 data->sample_time = MSEC_PER_SEC;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200333 clr_mask |= 1 << 7; /* not one-shot mode */
334 break;
Frans Klaverc83959f2014-06-26 11:21:11 +0200335 case tmp112:
336 set_mask |= 3 << 5; /* 12-bit mode */
337 clr_mask |= 1 << 7; /* not one-shot mode */
338 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700339 data->sample_time = MSEC_PER_SEC / 4;
Frans Klaverc83959f2014-06-26 11:21:11 +0200340 break;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200341 case tmp105:
342 case tmp175:
343 case tmp275:
344 case tmp75:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200345 set_mask |= 3 << 5; /* 12-bit mode */
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200346 clr_mask |= 1 << 7; /* not one-shot mode */
Jean Delvare0cd2c722013-05-04 14:49:36 +0200347 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700348 data->sample_time = MSEC_PER_SEC / 2;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200349 break;
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100350 case tmp75b: /* not one-shot mode, Conversion rate 37Hz */
Iker Perez del Palomar Sustatxaa95a4f3f2019-08-01 08:53:24 +0100351 clr_mask |= 1 << 7 | 0x3 << 5;
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100352 data->resolution = 12;
353 data->sample_time = MSEC_PER_SEC / 37;
354 break;
Ben Gardner9c32e812015-10-07 21:55:20 -0500355 case tmp75c:
356 clr_mask |= 1 << 5; /* not one-shot mode */
357 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700358 data->sample_time = MSEC_PER_SEC / 4;
Ben Gardner9c32e812015-10-07 21:55:20 -0500359 break;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200360 }
David Brownell9ebd3d82008-05-03 19:33:15 -0700361
362 /* configure as specified */
Guenter Roeck38aefb42016-06-19 17:11:13 -0700363 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
David Brownell9ebd3d82008-05-03 19:33:15 -0700364 if (status < 0) {
Guenter Roeckd663ec42014-01-13 16:00:37 -0800365 dev_dbg(dev, "Can't read config? %d\n", status);
Guenter Roeck13ac7a02012-06-02 09:58:08 -0700366 return status;
David Brownell9ebd3d82008-05-03 19:33:15 -0700367 }
368 data->orig_conf = status;
369 new = status & ~clr_mask;
370 new |= set_mask;
371 if (status != new)
Guenter Roeck38aefb42016-06-19 17:11:13 -0700372 i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700373
Guenter Roeck90e2b542016-07-25 14:56:00 -0700374 err = devm_add_action_or_reset(dev, lm75_remove, data);
375 if (err)
376 return err;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700377
Guenter Roeckd663ec42014-01-13 16:00:37 -0800378 dev_dbg(dev, "Config %02x\n", new);
David Brownell9ebd3d82008-05-03 19:33:15 -0700379
Guenter Roeck08b02432016-06-19 19:15:05 -0700380 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
381 data, &lm75_chip_info,
382 NULL);
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700383 if (IS_ERR(hwmon_dev))
384 return PTR_ERR(hwmon_dev);
David Brownell9ebd3d82008-05-03 19:33:15 -0700385
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700386 dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
David Brownell9ebd3d82008-05-03 19:33:15 -0700387
388 return 0;
David Brownell9ebd3d82008-05-03 19:33:15 -0700389}
390
David Brownell9ebd3d82008-05-03 19:33:15 -0700391static const struct i2c_device_id lm75_ids[] = {
Michael Henneriche96f9d82011-10-13 04:43:31 -0400392 { "adt75", adt75, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700393 { "ds1775", ds1775, },
394 { "ds75", ds75, },
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200395 { "ds7505", ds7505, },
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +0100396 { "g751", g751, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700397 { "lm75", lm75, },
398 { "lm75a", lm75a, },
Michael Thalmeier799fc602014-11-18 17:08:04 +0100399 { "lm75b", lm75b, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700400 { "max6625", max6625, },
401 { "max6626", max6626, },
Kun Yia54ca772018-09-11 13:25:58 -0700402 { "max31725", max31725, },
403 { "max31726", max31725, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700404 { "mcp980x", mcp980x, },
Daniel Mack557c7ff2019-07-11 14:45:04 +0200405 { "pct2075", pct2075, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700406 { "stds75", stds75, },
Jagan Teki2e9a41b2018-12-06 02:44:22 +0530407 { "stlm75", stlm75, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700408 { "tcn75", tcn75, },
409 { "tmp100", tmp100, },
410 { "tmp101", tmp101, },
Shubhrajyoti Datta6d034052010-05-27 19:59:03 +0200411 { "tmp105", tmp105, },
Frans Klaverc83959f2014-06-26 11:21:11 +0200412 { "tmp112", tmp112, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700413 { "tmp175", tmp175, },
414 { "tmp275", tmp275, },
415 { "tmp75", tmp75, },
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100416 { "tmp75b", tmp75b, },
Ben Gardner9c32e812015-10-07 21:55:20 -0500417 { "tmp75c", tmp75c, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700418 { /* LIST END */ }
419};
420MODULE_DEVICE_TABLE(i2c, lm75_ids);
421
Guenter Roeckffa83e72019-04-04 06:40:00 -0700422static const struct of_device_id __maybe_unused lm75_of_match[] = {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300423 {
424 .compatible = "adi,adt75",
425 .data = (void *)adt75
426 },
427 {
428 .compatible = "dallas,ds1775",
429 .data = (void *)ds1775
430 },
431 {
432 .compatible = "dallas,ds75",
433 .data = (void *)ds75
434 },
435 {
436 .compatible = "dallas,ds7505",
437 .data = (void *)ds7505
438 },
439 {
440 .compatible = "gmt,g751",
441 .data = (void *)g751
442 },
443 {
444 .compatible = "national,lm75",
445 .data = (void *)lm75
446 },
447 {
448 .compatible = "national,lm75a",
449 .data = (void *)lm75a
450 },
451 {
452 .compatible = "national,lm75b",
453 .data = (void *)lm75b
454 },
455 {
456 .compatible = "maxim,max6625",
457 .data = (void *)max6625
458 },
459 {
460 .compatible = "maxim,max6626",
461 .data = (void *)max6626
462 },
463 {
Kun Yia54ca772018-09-11 13:25:58 -0700464 .compatible = "maxim,max31725",
465 .data = (void *)max31725
466 },
467 {
468 .compatible = "maxim,max31726",
469 .data = (void *)max31725
470 },
471 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300472 .compatible = "maxim,mcp980x",
473 .data = (void *)mcp980x
474 },
475 {
Daniel Mack557c7ff2019-07-11 14:45:04 +0200476 .compatible = "nxp,pct2075",
477 .data = (void *)pct2075
478 },
479 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300480 .compatible = "st,stds75",
481 .data = (void *)stds75
482 },
483 {
Jagan Teki2e9a41b2018-12-06 02:44:22 +0530484 .compatible = "st,stlm75",
485 .data = (void *)stlm75
486 },
487 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300488 .compatible = "microchip,tcn75",
489 .data = (void *)tcn75
490 },
491 {
492 .compatible = "ti,tmp100",
493 .data = (void *)tmp100
494 },
495 {
496 .compatible = "ti,tmp101",
497 .data = (void *)tmp101
498 },
499 {
500 .compatible = "ti,tmp105",
501 .data = (void *)tmp105
502 },
503 {
504 .compatible = "ti,tmp112",
505 .data = (void *)tmp112
506 },
507 {
508 .compatible = "ti,tmp175",
509 .data = (void *)tmp175
510 },
511 {
512 .compatible = "ti,tmp275",
513 .data = (void *)tmp275
514 },
515 {
516 .compatible = "ti,tmp75",
517 .data = (void *)tmp75
518 },
519 {
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100520 .compatible = "ti,tmp75b",
521 .data = (void *)tmp75b
522 },
523 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300524 .compatible = "ti,tmp75c",
525 .data = (void *)tmp75c
526 },
527 { },
528};
529MODULE_DEVICE_TABLE(of, lm75_of_match);
530
Len Sorensen05e82fe2011-03-21 17:59:36 +0100531#define LM75A_ID 0xA1
532
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200533/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100534static int lm75_detect(struct i2c_client *new_client,
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200535 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200537 struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int i;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100539 int conf, hyst, os;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100540 bool is_lm75a = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
543 I2C_FUNC_SMBUS_WORD_DATA))
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200544 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Jean Delvare426343e2011-10-13 17:15:11 -0400546 /*
547 * Now, we do the remaining detection. There is no identification-
548 * dedicated register so we have to rely on several tricks:
549 * unused bits, registers cycling over 8-address boundaries,
550 * addresses 0x04-0x07 returning the last read value.
551 * The cycling+unused addresses combination is not tested,
552 * since it would significantly slow the detection down and would
553 * hardly add any value.
554 *
555 * The National Semiconductor LM75A is different than earlier
556 * LM75s. It has an ID byte of 0xaX (where X is the chip
557 * revision, with 1 being the only revision in existence) in
558 * register 7, and unused registers return 0xff rather than the
559 * last read value.
560 *
561 * Note that this function only detects the original National
562 * Semiconductor LM75 and the LM75A. Clones from other vendors
563 * aren't detected, on purpose, because they are typically never
564 * found on PC hardware. They are found on embedded designs where
565 * they can be instantiated explicitly so detection is not needed.
566 * The absence of identification registers on all these clones
567 * would make their exhaustive detection very difficult and weak,
568 * and odds are that the driver would bind to unsupported devices.
569 */
Len Sorensen05e82fe2011-03-21 17:59:36 +0100570
Jean Delvaree76f67b2011-03-21 17:59:36 +0100571 /* Unused bits */
Jean Delvare52df6442009-12-09 20:35:57 +0100572 conf = i2c_smbus_read_byte_data(new_client, 1);
Jean Delvaree76f67b2011-03-21 17:59:36 +0100573 if (conf & 0xe0)
574 return -ENODEV;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100575
576 /* First check for LM75A */
577 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
578 /* LM75A returns 0xff on unused registers so
579 just to be sure we check for that too. */
580 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
581 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
582 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
583 return -ENODEV;
584 is_lm75a = 1;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100585 hyst = i2c_smbus_read_byte_data(new_client, 2);
586 os = i2c_smbus_read_byte_data(new_client, 3);
Len Sorensen05e82fe2011-03-21 17:59:36 +0100587 } else { /* Traditional style LM75 detection */
588 /* Unused addresses */
Jean Delvaree76f67b2011-03-21 17:59:36 +0100589 hyst = i2c_smbus_read_byte_data(new_client, 2);
590 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
591 || i2c_smbus_read_byte_data(new_client, 5) != hyst
592 || i2c_smbus_read_byte_data(new_client, 6) != hyst
593 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
Len Sorensen05e82fe2011-03-21 17:59:36 +0100594 return -ENODEV;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100595 os = i2c_smbus_read_byte_data(new_client, 3);
596 if (i2c_smbus_read_byte_data(new_client, 4) != os
597 || i2c_smbus_read_byte_data(new_client, 5) != os
598 || i2c_smbus_read_byte_data(new_client, 6) != os
599 || i2c_smbus_read_byte_data(new_client, 7) != os)
Len Sorensen05e82fe2011-03-21 17:59:36 +0100600 return -ENODEV;
601 }
Guenter Roeck4ad40cc2014-12-04 09:58:15 -0800602 /*
603 * It is very unlikely that this is a LM75 if both
604 * hysteresis and temperature limit registers are 0.
605 */
606 if (hyst == 0 && os == 0)
607 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jean Delvare52df6442009-12-09 20:35:57 +0100609 /* Addresses cycling */
Jean Delvaree76f67b2011-03-21 17:59:36 +0100610 for (i = 8; i <= 248; i += 40) {
Jean Delvare52df6442009-12-09 20:35:57 +0100611 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
Jean Delvaree76f67b2011-03-21 17:59:36 +0100612 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
613 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
Jean Delvare52df6442009-12-09 20:35:57 +0100614 return -ENODEV;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100615 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
616 != LM75A_ID)
617 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
Len Sorensen05e82fe2011-03-21 17:59:36 +0100620 strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200625#ifdef CONFIG_PM
626static int lm75_suspend(struct device *dev)
627{
628 int status;
629 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck38aefb42016-06-19 17:11:13 -0700630 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200631 if (status < 0) {
632 dev_dbg(&client->dev, "Can't read config? %d\n", status);
633 return status;
634 }
635 status = status | LM75_SHUTDOWN;
Guenter Roeck38aefb42016-06-19 17:11:13 -0700636 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200637 return 0;
638}
639
640static int lm75_resume(struct device *dev)
641{
642 int status;
643 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck38aefb42016-06-19 17:11:13 -0700644 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200645 if (status < 0) {
646 dev_dbg(&client->dev, "Can't read config? %d\n", status);
647 return status;
648 }
649 status = status & ~LM75_SHUTDOWN;
Guenter Roeck38aefb42016-06-19 17:11:13 -0700650 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200651 return 0;
652}
653
654static const struct dev_pm_ops lm75_dev_pm_ops = {
655 .suspend = lm75_suspend,
656 .resume = lm75_resume,
657};
658#define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
659#else
660#define LM75_DEV_PM_OPS NULL
661#endif /* CONFIG_PM */
662
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200663static struct i2c_driver lm75_driver = {
664 .class = I2C_CLASS_HWMON,
David Brownell01a52392008-04-21 12:10:53 -0700665 .driver = {
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200666 .name = "lm75",
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300667 .of_match_table = of_match_ptr(lm75_of_match),
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200668 .pm = LM75_DEV_PM_OPS,
David Brownell01a52392008-04-21 12:10:53 -0700669 },
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200670 .probe = lm75_probe,
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200671 .id_table = lm75_ids,
672 .detect = lm75_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100673 .address_list = normal_i2c,
David Brownell01a52392008-04-21 12:10:53 -0700674};
675
Axel Linf0967ee2012-01-20 15:38:18 +0800676module_i2c_driver(lm75_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
679MODULE_DESCRIPTION("LM75 driver");
680MODULE_LICENSE("GPL");