Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Shubhrajyoti D | caaa0f3 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 3 | * lm75.c - Part of lm_sensors, Linux kernel modules for hardware |
| 4 | * monitoring |
| 5 | * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
Shubhrajyoti D | caaa0f3 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #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. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 13 | #include <linux/hwmon.h> |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 14 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 15 | #include <linux/err.h> |
Javier Martinez Canillas | e97a45f | 2017-02-24 10:13:02 -0300 | [diff] [blame] | 16 | #include <linux/of_device.h> |
Eduardo Valentin | 22e7318 | 2013-07-16 14:54:55 -0400 | [diff] [blame] | 17 | #include <linux/of.h> |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 18 | #include <linux/regmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "lm75.h" |
| 20 | |
| 21 | |
David Brownell | 01a5239 | 2008-04-21 12:10:53 -0700 | [diff] [blame] | 22 | /* |
| 23 | * This driver handles the LM75 and compatible digital temperature sensors. |
David Brownell | 01a5239 | 2008-04-21 12:10:53 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 26 | enum lm75_type { /* keep sorted in alphabetical order */ |
Michael Hennerich | e96f9d8 | 2011-10-13 04:43:31 -0400 | [diff] [blame] | 27 | adt75, |
Jean Delvare | 1f86df4 | 2009-12-14 21:17:26 +0100 | [diff] [blame] | 28 | ds1775, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 29 | ds75, |
Jean Delvare | 3fbc81e | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 30 | ds7505, |
Arnaud Ebalard | c98d6c6 | 2013-11-09 18:39:14 +0100 | [diff] [blame] | 31 | g751, |
Jean Delvare | 1f86df4 | 2009-12-14 21:17:26 +0100 | [diff] [blame] | 32 | lm75, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 33 | lm75a, |
Michael Thalmeier | 799fc60 | 2014-11-18 17:08:04 +0100 | [diff] [blame] | 34 | lm75b, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 35 | max6625, |
| 36 | max6626, |
Kun Yi | a54ca77 | 2018-09-11 13:25:58 -0700 | [diff] [blame] | 37 | max31725, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 38 | mcp980x, |
Daniel Mack | 557c7ff | 2019-07-11 14:45:04 +0200 | [diff] [blame^] | 39 | pct2075, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 40 | stds75, |
Jagan Teki | 2e9a41b | 2018-12-06 02:44:22 +0530 | [diff] [blame] | 41 | stlm75, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 42 | tcn75, |
| 43 | tmp100, |
| 44 | tmp101, |
Shubhrajyoti Datta | 6d03405 | 2010-05-27 19:59:03 +0200 | [diff] [blame] | 45 | tmp105, |
Frans Klaver | c83959f | 2014-06-26 11:21:11 +0200 | [diff] [blame] | 46 | tmp112, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 47 | tmp175, |
| 48 | tmp275, |
| 49 | tmp75, |
Iker Perez del Palomar Sustatxa | 39abe9d | 2019-05-03 17:15:00 +0100 | [diff] [blame] | 50 | tmp75b, |
Ben Gardner | 9c32e81 | 2015-10-07 21:55:20 -0500 | [diff] [blame] | 51 | tmp75c, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Jean Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 54 | /* Addresses scanned */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 55 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* The LM75 registers */ |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 59 | #define LM75_REG_TEMP 0x00 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #define LM75_REG_CONF 0x01 |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 61 | #define LM75_REG_HYST 0x02 |
| 62 | #define LM75_REG_MAX 0x03 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* Each client has this additional data */ |
| 65 | struct lm75_data { |
Guenter Roeck | d663ec4 | 2014-01-13 16:00:37 -0800 | [diff] [blame] | 66 | struct i2c_client *client; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 67 | struct regmap *regmap; |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 68 | u8 orig_conf; |
Kun Yi | a54ca77 | 2018-09-11 13:25:58 -0700 | [diff] [blame] | 69 | u8 resolution; /* In bits, between 9 and 16 */ |
Jean Delvare | 87d0621 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 70 | u8 resolution_limits; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 71 | unsigned int sample_time; /* In ms */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
David Brownell | 01a5239 | 2008-04-21 12:10:53 -0700 | [diff] [blame] | 74 | /*-----------------------------------------------------------------------*/ |
| 75 | |
Eduardo Valentin | 22e7318 | 2013-07-16 14:54:55 -0400 | [diff] [blame] | 76 | static inline long lm75_reg_to_mc(s16 temp, u8 resolution) |
| 77 | { |
| 78 | return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8); |
| 79 | } |
| 80 | |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 81 | static int lm75_read(struct device *dev, enum hwmon_sensor_types type, |
| 82 | u32 attr, int channel, long *val) |
Eduardo Valentin | 22e7318 | 2013-07-16 14:54:55 -0400 | [diff] [blame] | 83 | { |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 84 | struct lm75_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 85 | unsigned int regval; |
| 86 | int err, reg; |
Eduardo Valentin | 22e7318 | 2013-07-16 14:54:55 -0400 | [diff] [blame] | 87 | |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 88 | switch (type) { |
| 89 | case hwmon_chip: |
| 90 | switch (attr) { |
| 91 | case hwmon_chip_update_interval: |
| 92 | *val = data->sample_time; |
Luis de Bethencourt | ccffe77 | 2018-01-17 18:24:48 +0000 | [diff] [blame] | 93 | break; |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 94 | 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, ®val); |
| 113 | if (err < 0) |
| 114 | return err; |
Eduardo Valentin | 22e7318 | 2013-07-16 14:54:55 -0400 | [diff] [blame] | 115 | |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 116 | *val = lm75_reg_to_mc(regval, data->resolution); |
| 117 | break; |
| 118 | default: |
| 119 | return -EINVAL; |
| 120 | } |
Eduardo Valentin | 22e7318 | 2013-07-16 14:54:55 -0400 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 124 | static int lm75_write(struct device *dev, enum hwmon_sensor_types type, |
| 125 | u32 attr, int channel, long temp) |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 126 | { |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 127 | struct lm75_data *data = dev_get_drvdata(dev); |
Jean Delvare | 87d0621 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 128 | u8 resolution; |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 129 | int reg; |
Shubhrajyoti D | e3cd952 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 130 | |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 131 | 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 Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 144 | |
Jean Delvare | 87d0621 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 145 | /* |
| 146 | * Resolution of limit registers is assumed to be the same as the |
| 147 | * temperature input register resolution unless given explicitly. |
| 148 | */ |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 149 | if (data->resolution_limits) |
Jean Delvare | 87d0621 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 150 | resolution = data->resolution_limits; |
| 151 | else |
| 152 | resolution = data->resolution; |
| 153 | |
Jean Delvare | 87d0621 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 154 | temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX); |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 155 | temp = DIV_ROUND_CLOSEST(temp << (resolution - 8), |
| 156 | 1000) << (16 - resolution); |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 157 | |
Guenter Roeck | 7d82fcc | 2019-08-08 12:00:18 -0700 | [diff] [blame] | 158 | return regmap_write(data->regmap, reg, (u16)temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 161 | static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type, |
| 162 | u32 attr, int channel) |
Guenter Roeck | 5f7e5e2 | 2016-06-19 17:56:22 -0700 | [diff] [blame] | 163 | { |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 164 | switch (type) { |
| 165 | case hwmon_chip: |
| 166 | switch (attr) { |
| 167 | case hwmon_chip_update_interval: |
Guenter Roeck | e6ab6e0 | 2018-12-10 14:02:11 -0800 | [diff] [blame] | 168 | return 0444; |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 169 | } |
| 170 | break; |
| 171 | case hwmon_temp: |
| 172 | switch (attr) { |
| 173 | case hwmon_temp_input: |
Guenter Roeck | e6ab6e0 | 2018-12-10 14:02:11 -0800 | [diff] [blame] | 174 | return 0444; |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 175 | case hwmon_temp_max: |
| 176 | case hwmon_temp_max_hyst: |
Guenter Roeck | e6ab6e0 | 2018-12-10 14:02:11 -0800 | [diff] [blame] | 177 | return 0644; |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 178 | } |
| 179 | break; |
| 180 | default: |
| 181 | break; |
| 182 | } |
| 183 | return 0; |
Guenter Roeck | 5f7e5e2 | 2016-06-19 17:56:22 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 186 | static const struct hwmon_channel_info *lm75_info[] = { |
Guenter Roeck | e4f6fed1 | 2019-03-31 10:53:47 -0700 | [diff] [blame] | 187 | 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 Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 191 | NULL |
| 192 | }; |
| 193 | |
| 194 | static const struct hwmon_ops lm75_hwmon_ops = { |
| 195 | .is_visible = lm75_is_visible, |
| 196 | .read = lm75_read, |
| 197 | .write = lm75_write, |
| 198 | }; |
| 199 | |
| 200 | static const struct hwmon_chip_info lm75_chip_info = { |
| 201 | .ops = &lm75_hwmon_ops, |
| 202 | .info = lm75_info, |
| 203 | }; |
| 204 | |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 205 | static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg) |
| 206 | { |
| 207 | return reg != LM75_REG_TEMP; |
| 208 | } |
| 209 | |
| 210 | static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg) |
| 211 | { |
| 212 | return reg == LM75_REG_TEMP; |
| 213 | } |
| 214 | |
| 215 | static 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 Frey | 1c96a2f | 2018-09-01 09:50:41 -0700 | [diff] [blame] | 223 | .use_single_read = true, |
| 224 | .use_single_write = true, |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
Guenter Roeck | 9e37d3e | 2016-06-19 17:06:48 -0700 | [diff] [blame] | 227 | static 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 Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 235 | static int |
| 236 | lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 237 | { |
Guenter Roeck | d663ec4 | 2014-01-13 16:00:37 -0800 | [diff] [blame] | 238 | struct device *dev = &client->dev; |
Guenter Roeck | 9e37d3e | 2016-06-19 17:06:48 -0700 | [diff] [blame] | 239 | struct device *hwmon_dev; |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 240 | struct lm75_data *data; |
Guenter Roeck | 90e2b54 | 2016-07-25 14:56:00 -0700 | [diff] [blame] | 241 | int status, err; |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 242 | u8 set_mask, clr_mask; |
| 243 | int new; |
Javier Martinez Canillas | e97a45f | 2017-02-24 10:13:02 -0300 | [diff] [blame] | 244 | 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 Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 250 | |
| 251 | if (!i2c_check_functionality(client->adapter, |
| 252 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) |
| 253 | return -EIO; |
| 254 | |
Guenter Roeck | d663ec4 | 2014-01-13 16:00:37 -0800 | [diff] [blame] | 255 | data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL); |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 256 | if (!data) |
| 257 | return -ENOMEM; |
| 258 | |
Guenter Roeck | d663ec4 | 2014-01-13 16:00:37 -0800 | [diff] [blame] | 259 | data->client = client; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 260 | |
| 261 | data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config); |
| 262 | if (IS_ERR(data->regmap)) |
| 263 | return PTR_ERR(data->regmap); |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 264 | |
| 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 Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 269 | clr_mask = LM75_SHUTDOWN; /* continuous conversions */ |
| 270 | |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 271 | switch (kind) { |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 272 | case adt75: |
| 273 | clr_mask |= 1 << 5; /* not one-shot mode */ |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 274 | data->resolution = 12; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 275 | data->sample_time = MSEC_PER_SEC / 8; |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 276 | break; |
| 277 | case ds1775: |
| 278 | case ds75: |
| 279 | case stds75: |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 280 | clr_mask |= 3 << 5; |
| 281 | set_mask |= 2 << 5; /* 11-bit mode */ |
| 282 | data->resolution = 11; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 283 | data->sample_time = MSEC_PER_SEC; |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 284 | break; |
Jagan Teki | 2e9a41b | 2018-12-06 02:44:22 +0530 | [diff] [blame] | 285 | case stlm75: |
| 286 | data->resolution = 9; |
| 287 | data->sample_time = MSEC_PER_SEC / 5; |
| 288 | break; |
Jean Delvare | 3fbc81e | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 289 | case ds7505: |
| 290 | set_mask |= 3 << 5; /* 12-bit mode */ |
| 291 | data->resolution = 12; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 292 | data->sample_time = MSEC_PER_SEC / 4; |
Jean Delvare | 3fbc81e | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 293 | break; |
Arnaud Ebalard | c98d6c6 | 2013-11-09 18:39:14 +0100 | [diff] [blame] | 294 | case g751: |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 295 | case lm75: |
| 296 | case lm75a: |
| 297 | data->resolution = 9; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 298 | data->sample_time = MSEC_PER_SEC / 2; |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 299 | break; |
Michael Thalmeier | 799fc60 | 2014-11-18 17:08:04 +0100 | [diff] [blame] | 300 | case lm75b: |
| 301 | data->resolution = 11; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 302 | data->sample_time = MSEC_PER_SEC / 4; |
Michael Thalmeier | 799fc60 | 2014-11-18 17:08:04 +0100 | [diff] [blame] | 303 | break; |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 304 | case max6625: |
| 305 | data->resolution = 9; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 306 | data->sample_time = MSEC_PER_SEC / 4; |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 307 | break; |
| 308 | case max6626: |
| 309 | data->resolution = 12; |
| 310 | data->resolution_limits = 9; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 311 | data->sample_time = MSEC_PER_SEC / 4; |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 312 | break; |
Kun Yi | a54ca77 | 2018-09-11 13:25:58 -0700 | [diff] [blame] | 313 | case max31725: |
| 314 | data->resolution = 16; |
| 315 | data->sample_time = MSEC_PER_SEC / 8; |
| 316 | break; |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 317 | case tcn75: |
| 318 | data->resolution = 9; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 319 | data->sample_time = MSEC_PER_SEC / 8; |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 320 | break; |
Daniel Mack | 557c7ff | 2019-07-11 14:45:04 +0200 | [diff] [blame^] | 321 | case pct2075: |
| 322 | data->resolution = 11; |
| 323 | data->sample_time = MSEC_PER_SEC / 10; |
| 324 | break; |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 325 | case mcp980x: |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 326 | data->resolution_limits = 9; |
| 327 | /* fall through */ |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 328 | case tmp100: |
| 329 | case tmp101: |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 330 | set_mask |= 3 << 5; /* 12-bit mode */ |
| 331 | data->resolution = 12; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 332 | data->sample_time = MSEC_PER_SEC; |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 333 | clr_mask |= 1 << 7; /* not one-shot mode */ |
| 334 | break; |
Frans Klaver | c83959f | 2014-06-26 11:21:11 +0200 | [diff] [blame] | 335 | case tmp112: |
| 336 | set_mask |= 3 << 5; /* 12-bit mode */ |
| 337 | clr_mask |= 1 << 7; /* not one-shot mode */ |
| 338 | data->resolution = 12; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 339 | data->sample_time = MSEC_PER_SEC / 4; |
Frans Klaver | c83959f | 2014-06-26 11:21:11 +0200 | [diff] [blame] | 340 | break; |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 341 | case tmp105: |
| 342 | case tmp175: |
| 343 | case tmp275: |
| 344 | case tmp75: |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 345 | set_mask |= 3 << 5; /* 12-bit mode */ |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 346 | clr_mask |= 1 << 7; /* not one-shot mode */ |
Jean Delvare | 0cd2c72 | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 347 | data->resolution = 12; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 348 | data->sample_time = MSEC_PER_SEC / 2; |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 349 | break; |
Iker Perez del Palomar Sustatxa | 39abe9d | 2019-05-03 17:15:00 +0100 | [diff] [blame] | 350 | case tmp75b: /* not one-shot mode, Conversion rate 37Hz */ |
Iker Perez del Palomar Sustatxa | a95a4f3f | 2019-08-01 08:53:24 +0100 | [diff] [blame] | 351 | clr_mask |= 1 << 7 | 0x3 << 5; |
Iker Perez del Palomar Sustatxa | 39abe9d | 2019-05-03 17:15:00 +0100 | [diff] [blame] | 352 | data->resolution = 12; |
| 353 | data->sample_time = MSEC_PER_SEC / 37; |
| 354 | break; |
Ben Gardner | 9c32e81 | 2015-10-07 21:55:20 -0500 | [diff] [blame] | 355 | case tmp75c: |
| 356 | clr_mask |= 1 << 5; /* not one-shot mode */ |
| 357 | data->resolution = 12; |
Guenter Roeck | e65365f | 2016-06-19 17:49:19 -0700 | [diff] [blame] | 358 | data->sample_time = MSEC_PER_SEC / 4; |
Ben Gardner | 9c32e81 | 2015-10-07 21:55:20 -0500 | [diff] [blame] | 359 | break; |
Jean Delvare | 8a5c5cc | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 360 | } |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 361 | |
| 362 | /* configure as specified */ |
Guenter Roeck | 38aefb4 | 2016-06-19 17:11:13 -0700 | [diff] [blame] | 363 | status = i2c_smbus_read_byte_data(client, LM75_REG_CONF); |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 364 | if (status < 0) { |
Guenter Roeck | d663ec4 | 2014-01-13 16:00:37 -0800 | [diff] [blame] | 365 | dev_dbg(dev, "Can't read config? %d\n", status); |
Guenter Roeck | 13ac7a0 | 2012-06-02 09:58:08 -0700 | [diff] [blame] | 366 | return status; |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 367 | } |
| 368 | data->orig_conf = status; |
| 369 | new = status & ~clr_mask; |
| 370 | new |= set_mask; |
| 371 | if (status != new) |
Guenter Roeck | 38aefb4 | 2016-06-19 17:11:13 -0700 | [diff] [blame] | 372 | i2c_smbus_write_byte_data(client, LM75_REG_CONF, new); |
Guenter Roeck | 9e37d3e | 2016-06-19 17:06:48 -0700 | [diff] [blame] | 373 | |
Guenter Roeck | 90e2b54 | 2016-07-25 14:56:00 -0700 | [diff] [blame] | 374 | err = devm_add_action_or_reset(dev, lm75_remove, data); |
| 375 | if (err) |
| 376 | return err; |
Guenter Roeck | 9e37d3e | 2016-06-19 17:06:48 -0700 | [diff] [blame] | 377 | |
Guenter Roeck | d663ec4 | 2014-01-13 16:00:37 -0800 | [diff] [blame] | 378 | dev_dbg(dev, "Config %02x\n", new); |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 379 | |
Guenter Roeck | 08b0243 | 2016-06-19 19:15:05 -0700 | [diff] [blame] | 380 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, |
| 381 | data, &lm75_chip_info, |
| 382 | NULL); |
Guenter Roeck | 9e37d3e | 2016-06-19 17:06:48 -0700 | [diff] [blame] | 383 | if (IS_ERR(hwmon_dev)) |
| 384 | return PTR_ERR(hwmon_dev); |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 385 | |
Guenter Roeck | 9e37d3e | 2016-06-19 17:06:48 -0700 | [diff] [blame] | 386 | dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name); |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 387 | |
| 388 | return 0; |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 389 | } |
| 390 | |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 391 | static const struct i2c_device_id lm75_ids[] = { |
Michael Hennerich | e96f9d8 | 2011-10-13 04:43:31 -0400 | [diff] [blame] | 392 | { "adt75", adt75, }, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 393 | { "ds1775", ds1775, }, |
| 394 | { "ds75", ds75, }, |
Jean Delvare | 3fbc81e | 2013-05-04 14:49:36 +0200 | [diff] [blame] | 395 | { "ds7505", ds7505, }, |
Arnaud Ebalard | c98d6c6 | 2013-11-09 18:39:14 +0100 | [diff] [blame] | 396 | { "g751", g751, }, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 397 | { "lm75", lm75, }, |
| 398 | { "lm75a", lm75a, }, |
Michael Thalmeier | 799fc60 | 2014-11-18 17:08:04 +0100 | [diff] [blame] | 399 | { "lm75b", lm75b, }, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 400 | { "max6625", max6625, }, |
| 401 | { "max6626", max6626, }, |
Kun Yi | a54ca77 | 2018-09-11 13:25:58 -0700 | [diff] [blame] | 402 | { "max31725", max31725, }, |
| 403 | { "max31726", max31725, }, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 404 | { "mcp980x", mcp980x, }, |
Daniel Mack | 557c7ff | 2019-07-11 14:45:04 +0200 | [diff] [blame^] | 405 | { "pct2075", pct2075, }, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 406 | { "stds75", stds75, }, |
Jagan Teki | 2e9a41b | 2018-12-06 02:44:22 +0530 | [diff] [blame] | 407 | { "stlm75", stlm75, }, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 408 | { "tcn75", tcn75, }, |
| 409 | { "tmp100", tmp100, }, |
| 410 | { "tmp101", tmp101, }, |
Shubhrajyoti Datta | 6d03405 | 2010-05-27 19:59:03 +0200 | [diff] [blame] | 411 | { "tmp105", tmp105, }, |
Frans Klaver | c83959f | 2014-06-26 11:21:11 +0200 | [diff] [blame] | 412 | { "tmp112", tmp112, }, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 413 | { "tmp175", tmp175, }, |
| 414 | { "tmp275", tmp275, }, |
| 415 | { "tmp75", tmp75, }, |
Iker Perez del Palomar Sustatxa | 39abe9d | 2019-05-03 17:15:00 +0100 | [diff] [blame] | 416 | { "tmp75b", tmp75b, }, |
Ben Gardner | 9c32e81 | 2015-10-07 21:55:20 -0500 | [diff] [blame] | 417 | { "tmp75c", tmp75c, }, |
David Brownell | 9ebd3d8 | 2008-05-03 19:33:15 -0700 | [diff] [blame] | 418 | { /* LIST END */ } |
| 419 | }; |
| 420 | MODULE_DEVICE_TABLE(i2c, lm75_ids); |
| 421 | |
Guenter Roeck | ffa83e7 | 2019-04-04 06:40:00 -0700 | [diff] [blame] | 422 | static const struct of_device_id __maybe_unused lm75_of_match[] = { |
Javier Martinez Canillas | e97a45f | 2017-02-24 10:13:02 -0300 | [diff] [blame] | 423 | { |
| 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 Yi | a54ca77 | 2018-09-11 13:25:58 -0700 | [diff] [blame] | 464 | .compatible = "maxim,max31725", |
| 465 | .data = (void *)max31725 |
| 466 | }, |
| 467 | { |
| 468 | .compatible = "maxim,max31726", |
| 469 | .data = (void *)max31725 |
| 470 | }, |
| 471 | { |
Javier Martinez Canillas | e97a45f | 2017-02-24 10:13:02 -0300 | [diff] [blame] | 472 | .compatible = "maxim,mcp980x", |
| 473 | .data = (void *)mcp980x |
| 474 | }, |
| 475 | { |
Daniel Mack | 557c7ff | 2019-07-11 14:45:04 +0200 | [diff] [blame^] | 476 | .compatible = "nxp,pct2075", |
| 477 | .data = (void *)pct2075 |
| 478 | }, |
| 479 | { |
Javier Martinez Canillas | e97a45f | 2017-02-24 10:13:02 -0300 | [diff] [blame] | 480 | .compatible = "st,stds75", |
| 481 | .data = (void *)stds75 |
| 482 | }, |
| 483 | { |
Jagan Teki | 2e9a41b | 2018-12-06 02:44:22 +0530 | [diff] [blame] | 484 | .compatible = "st,stlm75", |
| 485 | .data = (void *)stlm75 |
| 486 | }, |
| 487 | { |
Javier Martinez Canillas | e97a45f | 2017-02-24 10:13:02 -0300 | [diff] [blame] | 488 | .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 Sustatxa | 39abe9d | 2019-05-03 17:15:00 +0100 | [diff] [blame] | 520 | .compatible = "ti,tmp75b", |
| 521 | .data = (void *)tmp75b |
| 522 | }, |
| 523 | { |
Javier Martinez Canillas | e97a45f | 2017-02-24 10:13:02 -0300 | [diff] [blame] | 524 | .compatible = "ti,tmp75c", |
| 525 | .data = (void *)tmp75c |
| 526 | }, |
| 527 | { }, |
| 528 | }; |
| 529 | MODULE_DEVICE_TABLE(of, lm75_of_match); |
| 530 | |
Len Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 531 | #define LM75A_ID 0xA1 |
| 532 | |
Jean Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 533 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 534 | static int lm75_detect(struct i2c_client *new_client, |
Jean Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 535 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
Jean Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 537 | struct i2c_adapter *adapter = new_client->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | int i; |
Jean Delvare | e76f67b | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 539 | int conf, hyst, os; |
Len Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 540 | bool is_lm75a = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 543 | I2C_FUNC_SMBUS_WORD_DATA)) |
Jean Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 544 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Jean Delvare | 426343e | 2011-10-13 17:15:11 -0400 | [diff] [blame] | 546 | /* |
| 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 Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 570 | |
Jean Delvare | e76f67b | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 571 | /* Unused bits */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 572 | conf = i2c_smbus_read_byte_data(new_client, 1); |
Jean Delvare | e76f67b | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 573 | if (conf & 0xe0) |
| 574 | return -ENODEV; |
Len Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 575 | |
| 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 Delvare | e76f67b | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 585 | hyst = i2c_smbus_read_byte_data(new_client, 2); |
| 586 | os = i2c_smbus_read_byte_data(new_client, 3); |
Len Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 587 | } else { /* Traditional style LM75 detection */ |
| 588 | /* Unused addresses */ |
Jean Delvare | e76f67b | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 589 | 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 Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 594 | return -ENODEV; |
Jean Delvare | e76f67b | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 595 | 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 Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 600 | return -ENODEV; |
| 601 | } |
Guenter Roeck | 4ad40cc | 2014-12-04 09:58:15 -0800 | [diff] [blame] | 602 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 609 | /* Addresses cycling */ |
Jean Delvare | e76f67b | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 610 | for (i = 8; i <= 248; i += 40) { |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 611 | if (i2c_smbus_read_byte_data(new_client, i + 1) != conf |
Jean Delvare | e76f67b | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 612 | || i2c_smbus_read_byte_data(new_client, i + 2) != hyst |
| 613 | || i2c_smbus_read_byte_data(new_client, i + 3) != os) |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 614 | return -ENODEV; |
Len Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 615 | if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7) |
| 616 | != LM75A_ID) |
| 617 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Len Sorensen | 05e82fe | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 620 | strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE); |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Shubhrajyoti Datta | 9914518 | 2010-08-14 21:08:50 +0200 | [diff] [blame] | 625 | #ifdef CONFIG_PM |
| 626 | static int lm75_suspend(struct device *dev) |
| 627 | { |
| 628 | int status; |
| 629 | struct i2c_client *client = to_i2c_client(dev); |
Guenter Roeck | 38aefb4 | 2016-06-19 17:11:13 -0700 | [diff] [blame] | 630 | status = i2c_smbus_read_byte_data(client, LM75_REG_CONF); |
Shubhrajyoti Datta | 9914518 | 2010-08-14 21:08:50 +0200 | [diff] [blame] | 631 | 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 Roeck | 38aefb4 | 2016-06-19 17:11:13 -0700 | [diff] [blame] | 636 | i2c_smbus_write_byte_data(client, LM75_REG_CONF, status); |
Shubhrajyoti Datta | 9914518 | 2010-08-14 21:08:50 +0200 | [diff] [blame] | 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int lm75_resume(struct device *dev) |
| 641 | { |
| 642 | int status; |
| 643 | struct i2c_client *client = to_i2c_client(dev); |
Guenter Roeck | 38aefb4 | 2016-06-19 17:11:13 -0700 | [diff] [blame] | 644 | status = i2c_smbus_read_byte_data(client, LM75_REG_CONF); |
Shubhrajyoti Datta | 9914518 | 2010-08-14 21:08:50 +0200 | [diff] [blame] | 645 | 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 Roeck | 38aefb4 | 2016-06-19 17:11:13 -0700 | [diff] [blame] | 650 | i2c_smbus_write_byte_data(client, LM75_REG_CONF, status); |
Shubhrajyoti Datta | 9914518 | 2010-08-14 21:08:50 +0200 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static 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 Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 663 | static struct i2c_driver lm75_driver = { |
| 664 | .class = I2C_CLASS_HWMON, |
David Brownell | 01a5239 | 2008-04-21 12:10:53 -0700 | [diff] [blame] | 665 | .driver = { |
Jean Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 666 | .name = "lm75", |
Javier Martinez Canillas | e97a45f | 2017-02-24 10:13:02 -0300 | [diff] [blame] | 667 | .of_match_table = of_match_ptr(lm75_of_match), |
Shubhrajyoti Datta | 9914518 | 2010-08-14 21:08:50 +0200 | [diff] [blame] | 668 | .pm = LM75_DEV_PM_OPS, |
David Brownell | 01a5239 | 2008-04-21 12:10:53 -0700 | [diff] [blame] | 669 | }, |
Jean Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 670 | .probe = lm75_probe, |
Jean Delvare | 8ff69ee | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 671 | .id_table = lm75_ids, |
| 672 | .detect = lm75_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 673 | .address_list = normal_i2c, |
David Brownell | 01a5239 | 2008-04-21 12:10:53 -0700 | [diff] [blame] | 674 | }; |
| 675 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 676 | module_i2c_driver(lm75_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); |
| 679 | MODULE_DESCRIPTION("LM75 driver"); |
| 680 | MODULE_LICENSE("GPL"); |