blob: fc31669a86ba807665ffba97df3987da7fa1c606 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Shubhrajyoti Dcaaa0f32010-10-28 20:31:44 +02002 * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.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 Delvare9ca8e40c82007-05-08 17:22:01 +020027#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040028#include <linux/err.h>
Eduardo Valentin22e73182013-07-16 14:54:55 -040029#include <linux/of.h>
Guenter Roecke65365f2016-06-19 17:49:19 -070030#include <linux/regmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "lm75.h"
32
33
David Brownell01a52392008-04-21 12:10:53 -070034/*
35 * This driver handles the LM75 and compatible digital temperature sensors.
David Brownell01a52392008-04-21 12:10:53 -070036 */
37
David Brownell9ebd3d82008-05-03 19:33:15 -070038enum lm75_type { /* keep sorted in alphabetical order */
Michael Henneriche96f9d82011-10-13 04:43:31 -040039 adt75,
Jean Delvare1f86df42009-12-14 21:17:26 +010040 ds1775,
David Brownell9ebd3d82008-05-03 19:33:15 -070041 ds75,
Jean Delvare3fbc81e2013-05-04 14:49:36 +020042 ds7505,
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +010043 g751,
Jean Delvare1f86df42009-12-14 21:17:26 +010044 lm75,
David Brownell9ebd3d82008-05-03 19:33:15 -070045 lm75a,
Michael Thalmeier799fc602014-11-18 17:08:04 +010046 lm75b,
David Brownell9ebd3d82008-05-03 19:33:15 -070047 max6625,
48 max6626,
49 mcp980x,
50 stds75,
51 tcn75,
52 tmp100,
53 tmp101,
Shubhrajyoti Datta6d034052010-05-27 19:59:03 +020054 tmp105,
Frans Klaverc83959f2014-06-26 11:21:11 +020055 tmp112,
David Brownell9ebd3d82008-05-03 19:33:15 -070056 tmp175,
57 tmp275,
58 tmp75,
Ben Gardner9c32e812015-10-07 21:55:20 -050059 tmp75c,
David Brownell9ebd3d82008-05-03 19:33:15 -070060};
61
Jean Delvare8ff69ee2008-08-10 22:56:16 +020062/* Addresses scanned */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050063static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* The LM75 registers */
Guenter Roecke65365f2016-06-19 17:49:19 -070068#define LM75_REG_TEMP 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define LM75_REG_CONF 0x01
Guenter Roecke65365f2016-06-19 17:49:19 -070070#define LM75_REG_HYST 0x02
71#define LM75_REG_MAX 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* Each client has this additional data */
74struct lm75_data {
Guenter Roeckd663ec42014-01-13 16:00:37 -080075 struct i2c_client *client;
Guenter Roecke65365f2016-06-19 17:49:19 -070076 struct regmap *regmap;
David Brownell9ebd3d82008-05-03 19:33:15 -070077 u8 orig_conf;
Jean Delvare87d06212013-05-04 14:49:36 +020078 u8 resolution; /* In bits, between 9 and 12 */
79 u8 resolution_limits;
Guenter Roecke65365f2016-06-19 17:49:19 -070080 unsigned int sample_time; /* In ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
David Brownell01a52392008-04-21 12:10:53 -070083/*-----------------------------------------------------------------------*/
84
Eduardo Valentin22e73182013-07-16 14:54:55 -040085static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
86{
87 return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
88}
89
Guenter Roeck08b02432016-06-19 19:15:05 -070090static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
91 u32 attr, int channel, long *val)
Eduardo Valentin22e73182013-07-16 14:54:55 -040092{
Guenter Roecke65365f2016-06-19 17:49:19 -070093 struct lm75_data *data = dev_get_drvdata(dev);
Guenter Roeck08b02432016-06-19 19:15:05 -070094 unsigned int regval;
95 int err, reg;
Eduardo Valentin22e73182013-07-16 14:54:55 -040096
Guenter Roeck08b02432016-06-19 19:15:05 -070097 switch (type) {
98 case hwmon_chip:
99 switch (attr) {
100 case hwmon_chip_update_interval:
101 *val = data->sample_time;
102 break;;
103 default:
104 return -EINVAL;
105 }
106 break;
107 case hwmon_temp:
108 switch (attr) {
109 case hwmon_temp_input:
110 reg = LM75_REG_TEMP;
111 break;
112 case hwmon_temp_max:
113 reg = LM75_REG_MAX;
114 break;
115 case hwmon_temp_max_hyst:
116 reg = LM75_REG_HYST;
117 break;
118 default:
119 return -EINVAL;
120 }
121 err = regmap_read(data->regmap, reg, &regval);
122 if (err < 0)
123 return err;
Eduardo Valentin22e73182013-07-16 14:54:55 -0400124
Guenter Roeck08b02432016-06-19 19:15:05 -0700125 *val = lm75_reg_to_mc(regval, data->resolution);
126 break;
127 default:
128 return -EINVAL;
129 }
Eduardo Valentin22e73182013-07-16 14:54:55 -0400130 return 0;
131}
132
Guenter Roeck08b02432016-06-19 19:15:05 -0700133static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
134 u32 attr, int channel, long temp)
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200135{
Guenter Roecke65365f2016-06-19 17:49:19 -0700136 struct lm75_data *data = dev_get_drvdata(dev);
Jean Delvare87d06212013-05-04 14:49:36 +0200137 u8 resolution;
Guenter Roeck08b02432016-06-19 19:15:05 -0700138 int reg;
Shubhrajyoti De3cd9522010-10-28 20:31:44 +0200139
Guenter Roeck08b02432016-06-19 19:15:05 -0700140 if (type != hwmon_temp)
141 return -EINVAL;
142
143 switch (attr) {
144 case hwmon_temp_max:
145 reg = LM75_REG_MAX;
146 break;
147 case hwmon_temp_max_hyst:
148 reg = LM75_REG_HYST;
149 break;
150 default:
151 return -EINVAL;
152 }
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200153
Jean Delvare87d06212013-05-04 14:49:36 +0200154 /*
155 * Resolution of limit registers is assumed to be the same as the
156 * temperature input register resolution unless given explicitly.
157 */
Guenter Roeck08b02432016-06-19 19:15:05 -0700158 if (data->resolution_limits)
Jean Delvare87d06212013-05-04 14:49:36 +0200159 resolution = data->resolution_limits;
160 else
161 resolution = data->resolution;
162
Jean Delvare87d06212013-05-04 14:49:36 +0200163 temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
Guenter Roecke65365f2016-06-19 17:49:19 -0700164 temp = DIV_ROUND_CLOSEST(temp << (resolution - 8),
165 1000) << (16 - resolution);
Guenter Roecke65365f2016-06-19 17:49:19 -0700166
Guenter Roeck738ac442019-08-08 12:00:18 -0700167 return regmap_write(data->regmap, reg, (u16)temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Guenter Roeck08b02432016-06-19 19:15:05 -0700170static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
171 u32 attr, int channel)
Guenter Roeck5f7e5e22016-06-19 17:56:22 -0700172{
Guenter Roeck08b02432016-06-19 19:15:05 -0700173 switch (type) {
174 case hwmon_chip:
175 switch (attr) {
176 case hwmon_chip_update_interval:
177 return S_IRUGO;
178 }
179 break;
180 case hwmon_temp:
181 switch (attr) {
182 case hwmon_temp_input:
183 return S_IRUGO;
184 case hwmon_temp_max:
185 case hwmon_temp_max_hyst:
186 return S_IRUGO | S_IWUSR;
187 }
188 break;
189 default:
190 break;
191 }
192 return 0;
Guenter Roeck5f7e5e22016-06-19 17:56:22 -0700193}
194
David Brownell01a52392008-04-21 12:10:53 -0700195/*-----------------------------------------------------------------------*/
196
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200197/* device probe and removal */
David Brownell9ebd3d82008-05-03 19:33:15 -0700198
Guenter Roeck08b02432016-06-19 19:15:05 -0700199/* chip configuration */
200
201static const u32 lm75_chip_config[] = {
202 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
203 0
204};
205
206static const struct hwmon_channel_info lm75_chip = {
207 .type = hwmon_chip,
208 .config = lm75_chip_config,
209};
210
211static const u32 lm75_temp_config[] = {
212 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
213 0
214};
215
216static const struct hwmon_channel_info lm75_temp = {
217 .type = hwmon_temp,
218 .config = lm75_temp_config,
219};
220
221static const struct hwmon_channel_info *lm75_info[] = {
222 &lm75_chip,
223 &lm75_temp,
224 NULL
225};
226
227static const struct hwmon_ops lm75_hwmon_ops = {
228 .is_visible = lm75_is_visible,
229 .read = lm75_read,
230 .write = lm75_write,
231};
232
233static const struct hwmon_chip_info lm75_chip_info = {
234 .ops = &lm75_hwmon_ops,
235 .info = lm75_info,
236};
237
Guenter Roecke65365f2016-06-19 17:49:19 -0700238static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
239{
240 return reg != LM75_REG_TEMP;
241}
242
243static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
244{
245 return reg == LM75_REG_TEMP;
246}
247
248static const struct regmap_config lm75_regmap_config = {
249 .reg_bits = 8,
250 .val_bits = 16,
251 .max_register = LM75_REG_MAX,
252 .writeable_reg = lm75_is_writeable_reg,
253 .volatile_reg = lm75_is_volatile_reg,
254 .val_format_endian = REGMAP_ENDIAN_BIG,
255 .cache_type = REGCACHE_RBTREE,
256 .use_single_rw = true,
257};
258
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700259static void lm75_remove(void *data)
260{
261 struct lm75_data *lm75 = data;
262 struct i2c_client *client = lm75->client;
263
264 i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
265}
266
David Brownell9ebd3d82008-05-03 19:33:15 -0700267static int
268lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
269{
Guenter Roeckd663ec42014-01-13 16:00:37 -0800270 struct device *dev = &client->dev;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700271 struct device *hwmon_dev;
David Brownell9ebd3d82008-05-03 19:33:15 -0700272 struct lm75_data *data;
Guenter Roeck90e2b542016-07-25 14:56:00 -0700273 int status, err;
David Brownell9ebd3d82008-05-03 19:33:15 -0700274 u8 set_mask, clr_mask;
275 int new;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200276 enum lm75_type kind = id->driver_data;
David Brownell9ebd3d82008-05-03 19:33:15 -0700277
278 if (!i2c_check_functionality(client->adapter,
279 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
280 return -EIO;
281
Guenter Roeckd663ec42014-01-13 16:00:37 -0800282 data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
David Brownell9ebd3d82008-05-03 19:33:15 -0700283 if (!data)
284 return -ENOMEM;
285
Guenter Roeckd663ec42014-01-13 16:00:37 -0800286 data->client = client;
Guenter Roecke65365f2016-06-19 17:49:19 -0700287
288 data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
289 if (IS_ERR(data->regmap))
290 return PTR_ERR(data->regmap);
David Brownell9ebd3d82008-05-03 19:33:15 -0700291
292 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
293 * Then tweak to be more precise when appropriate.
294 */
295 set_mask = 0;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200296 clr_mask = LM75_SHUTDOWN; /* continuous conversions */
297
Jean Delvare0cd2c722013-05-04 14:49:36 +0200298 switch (kind) {
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200299 case adt75:
300 clr_mask |= 1 << 5; /* not one-shot mode */
Jean Delvare0cd2c722013-05-04 14:49:36 +0200301 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700302 data->sample_time = MSEC_PER_SEC / 8;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200303 break;
304 case ds1775:
305 case ds75:
306 case stds75:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200307 clr_mask |= 3 << 5;
308 set_mask |= 2 << 5; /* 11-bit mode */
309 data->resolution = 11;
Guenter Roecke65365f2016-06-19 17:49:19 -0700310 data->sample_time = MSEC_PER_SEC;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200311 break;
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200312 case ds7505:
313 set_mask |= 3 << 5; /* 12-bit mode */
314 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700315 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200316 break;
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +0100317 case g751:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200318 case lm75:
319 case lm75a:
320 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700321 data->sample_time = MSEC_PER_SEC / 2;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200322 break;
Michael Thalmeier799fc602014-11-18 17:08:04 +0100323 case lm75b:
324 data->resolution = 11;
Guenter Roecke65365f2016-06-19 17:49:19 -0700325 data->sample_time = MSEC_PER_SEC / 4;
Michael Thalmeier799fc602014-11-18 17:08:04 +0100326 break;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200327 case max6625:
328 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700329 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200330 break;
331 case max6626:
332 data->resolution = 12;
333 data->resolution_limits = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700334 data->sample_time = MSEC_PER_SEC / 4;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200335 break;
336 case tcn75:
337 data->resolution = 9;
Guenter Roecke65365f2016-06-19 17:49:19 -0700338 data->sample_time = MSEC_PER_SEC / 8;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200339 break;
340 case mcp980x:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200341 data->resolution_limits = 9;
342 /* fall through */
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200343 case tmp100:
344 case tmp101:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200345 set_mask |= 3 << 5; /* 12-bit mode */
346 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700347 data->sample_time = MSEC_PER_SEC;
Jean Delvare0cd2c722013-05-04 14:49:36 +0200348 clr_mask |= 1 << 7; /* not one-shot mode */
349 break;
Frans Klaverc83959f2014-06-26 11:21:11 +0200350 case tmp112:
351 set_mask |= 3 << 5; /* 12-bit mode */
352 clr_mask |= 1 << 7; /* not one-shot mode */
353 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700354 data->sample_time = MSEC_PER_SEC / 4;
Frans Klaverc83959f2014-06-26 11:21:11 +0200355 break;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200356 case tmp105:
357 case tmp175:
358 case tmp275:
359 case tmp75:
Jean Delvare0cd2c722013-05-04 14:49:36 +0200360 set_mask |= 3 << 5; /* 12-bit mode */
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200361 clr_mask |= 1 << 7; /* not one-shot mode */
Jean Delvare0cd2c722013-05-04 14:49:36 +0200362 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700363 data->sample_time = MSEC_PER_SEC / 2;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200364 break;
Ben Gardner9c32e812015-10-07 21:55:20 -0500365 case tmp75c:
366 clr_mask |= 1 << 5; /* not one-shot mode */
367 data->resolution = 12;
Guenter Roecke65365f2016-06-19 17:49:19 -0700368 data->sample_time = MSEC_PER_SEC / 4;
Ben Gardner9c32e812015-10-07 21:55:20 -0500369 break;
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200370 }
David Brownell9ebd3d82008-05-03 19:33:15 -0700371
372 /* configure as specified */
Guenter Roeck38aefb42016-06-19 17:11:13 -0700373 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
David Brownell9ebd3d82008-05-03 19:33:15 -0700374 if (status < 0) {
Guenter Roeckd663ec42014-01-13 16:00:37 -0800375 dev_dbg(dev, "Can't read config? %d\n", status);
Guenter Roeck13ac7a02012-06-02 09:58:08 -0700376 return status;
David Brownell9ebd3d82008-05-03 19:33:15 -0700377 }
378 data->orig_conf = status;
379 new = status & ~clr_mask;
380 new |= set_mask;
381 if (status != new)
Guenter Roeck38aefb42016-06-19 17:11:13 -0700382 i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700383
Guenter Roeck90e2b542016-07-25 14:56:00 -0700384 err = devm_add_action_or_reset(dev, lm75_remove, data);
385 if (err)
386 return err;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700387
Guenter Roeckd663ec42014-01-13 16:00:37 -0800388 dev_dbg(dev, "Config %02x\n", new);
David Brownell9ebd3d82008-05-03 19:33:15 -0700389
Guenter Roeck08b02432016-06-19 19:15:05 -0700390 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
391 data, &lm75_chip_info,
392 NULL);
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700393 if (IS_ERR(hwmon_dev))
394 return PTR_ERR(hwmon_dev);
David Brownell9ebd3d82008-05-03 19:33:15 -0700395
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700396 dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
David Brownell9ebd3d82008-05-03 19:33:15 -0700397
398 return 0;
David Brownell9ebd3d82008-05-03 19:33:15 -0700399}
400
David Brownell9ebd3d82008-05-03 19:33:15 -0700401static const struct i2c_device_id lm75_ids[] = {
Michael Henneriche96f9d82011-10-13 04:43:31 -0400402 { "adt75", adt75, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700403 { "ds1775", ds1775, },
404 { "ds75", ds75, },
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200405 { "ds7505", ds7505, },
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +0100406 { "g751", g751, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700407 { "lm75", lm75, },
408 { "lm75a", lm75a, },
Michael Thalmeier799fc602014-11-18 17:08:04 +0100409 { "lm75b", lm75b, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700410 { "max6625", max6625, },
411 { "max6626", max6626, },
412 { "mcp980x", mcp980x, },
413 { "stds75", stds75, },
414 { "tcn75", tcn75, },
415 { "tmp100", tmp100, },
416 { "tmp101", tmp101, },
Shubhrajyoti Datta6d034052010-05-27 19:59:03 +0200417 { "tmp105", tmp105, },
Frans Klaverc83959f2014-06-26 11:21:11 +0200418 { "tmp112", tmp112, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700419 { "tmp175", tmp175, },
420 { "tmp275", tmp275, },
421 { "tmp75", tmp75, },
Ben Gardner9c32e812015-10-07 21:55:20 -0500422 { "tmp75c", tmp75c, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700423 { /* LIST END */ }
424};
425MODULE_DEVICE_TABLE(i2c, lm75_ids);
426
Len Sorensen05e82fe2011-03-21 17:59:36 +0100427#define LM75A_ID 0xA1
428
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200429/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100430static int lm75_detect(struct i2c_client *new_client,
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200431 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200433 struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int i;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100435 int conf, hyst, os;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100436 bool is_lm75a = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
439 I2C_FUNC_SMBUS_WORD_DATA))
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200440 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jean Delvare426343e2011-10-13 17:15:11 -0400442 /*
443 * Now, we do the remaining detection. There is no identification-
444 * dedicated register so we have to rely on several tricks:
445 * unused bits, registers cycling over 8-address boundaries,
446 * addresses 0x04-0x07 returning the last read value.
447 * The cycling+unused addresses combination is not tested,
448 * since it would significantly slow the detection down and would
449 * hardly add any value.
450 *
451 * The National Semiconductor LM75A is different than earlier
452 * LM75s. It has an ID byte of 0xaX (where X is the chip
453 * revision, with 1 being the only revision in existence) in
454 * register 7, and unused registers return 0xff rather than the
455 * last read value.
456 *
457 * Note that this function only detects the original National
458 * Semiconductor LM75 and the LM75A. Clones from other vendors
459 * aren't detected, on purpose, because they are typically never
460 * found on PC hardware. They are found on embedded designs where
461 * they can be instantiated explicitly so detection is not needed.
462 * The absence of identification registers on all these clones
463 * would make their exhaustive detection very difficult and weak,
464 * and odds are that the driver would bind to unsupported devices.
465 */
Len Sorensen05e82fe2011-03-21 17:59:36 +0100466
Jean Delvaree76f67b2011-03-21 17:59:36 +0100467 /* Unused bits */
Jean Delvare52df6442009-12-09 20:35:57 +0100468 conf = i2c_smbus_read_byte_data(new_client, 1);
Jean Delvaree76f67b2011-03-21 17:59:36 +0100469 if (conf & 0xe0)
470 return -ENODEV;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100471
472 /* First check for LM75A */
473 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
474 /* LM75A returns 0xff on unused registers so
475 just to be sure we check for that too. */
476 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
477 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
478 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
479 return -ENODEV;
480 is_lm75a = 1;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100481 hyst = i2c_smbus_read_byte_data(new_client, 2);
482 os = i2c_smbus_read_byte_data(new_client, 3);
Len Sorensen05e82fe2011-03-21 17:59:36 +0100483 } else { /* Traditional style LM75 detection */
484 /* Unused addresses */
Jean Delvaree76f67b2011-03-21 17:59:36 +0100485 hyst = i2c_smbus_read_byte_data(new_client, 2);
486 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
487 || i2c_smbus_read_byte_data(new_client, 5) != hyst
488 || i2c_smbus_read_byte_data(new_client, 6) != hyst
489 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
Len Sorensen05e82fe2011-03-21 17:59:36 +0100490 return -ENODEV;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100491 os = i2c_smbus_read_byte_data(new_client, 3);
492 if (i2c_smbus_read_byte_data(new_client, 4) != os
493 || i2c_smbus_read_byte_data(new_client, 5) != os
494 || i2c_smbus_read_byte_data(new_client, 6) != os
495 || i2c_smbus_read_byte_data(new_client, 7) != os)
Len Sorensen05e82fe2011-03-21 17:59:36 +0100496 return -ENODEV;
497 }
Guenter Roeck4ad40cc2014-12-04 09:58:15 -0800498 /*
499 * It is very unlikely that this is a LM75 if both
500 * hysteresis and temperature limit registers are 0.
501 */
502 if (hyst == 0 && os == 0)
503 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Jean Delvare52df6442009-12-09 20:35:57 +0100505 /* Addresses cycling */
Jean Delvaree76f67b2011-03-21 17:59:36 +0100506 for (i = 8; i <= 248; i += 40) {
Jean Delvare52df6442009-12-09 20:35:57 +0100507 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
Jean Delvaree76f67b2011-03-21 17:59:36 +0100508 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
509 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
Jean Delvare52df6442009-12-09 20:35:57 +0100510 return -ENODEV;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100511 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
512 != LM75A_ID)
513 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
Len Sorensen05e82fe2011-03-21 17:59:36 +0100516 strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200521#ifdef CONFIG_PM
522static int lm75_suspend(struct device *dev)
523{
524 int status;
525 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck38aefb42016-06-19 17:11:13 -0700526 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200527 if (status < 0) {
528 dev_dbg(&client->dev, "Can't read config? %d\n", status);
529 return status;
530 }
531 status = status | LM75_SHUTDOWN;
Guenter Roeck38aefb42016-06-19 17:11:13 -0700532 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200533 return 0;
534}
535
536static int lm75_resume(struct device *dev)
537{
538 int status;
539 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck38aefb42016-06-19 17:11:13 -0700540 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200541 if (status < 0) {
542 dev_dbg(&client->dev, "Can't read config? %d\n", status);
543 return status;
544 }
545 status = status & ~LM75_SHUTDOWN;
Guenter Roeck38aefb42016-06-19 17:11:13 -0700546 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200547 return 0;
548}
549
550static const struct dev_pm_ops lm75_dev_pm_ops = {
551 .suspend = lm75_suspend,
552 .resume = lm75_resume,
553};
554#define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
555#else
556#define LM75_DEV_PM_OPS NULL
557#endif /* CONFIG_PM */
558
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200559static struct i2c_driver lm75_driver = {
560 .class = I2C_CLASS_HWMON,
David Brownell01a52392008-04-21 12:10:53 -0700561 .driver = {
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200562 .name = "lm75",
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200563 .pm = LM75_DEV_PM_OPS,
David Brownell01a52392008-04-21 12:10:53 -0700564 },
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200565 .probe = lm75_probe,
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200566 .id_table = lm75_ids,
567 .detect = lm75_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100568 .address_list = normal_i2c,
David Brownell01a52392008-04-21 12:10:53 -0700569};
570
Axel Linf0967ee2012-01-20 15:38:18 +0800571module_i2c_driver(lm75_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
574MODULE_DESCRIPTION("LM75 driver");
575MODULE_LICENSE("GPL");