Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 1 | /* Texas Instruments TMP102 SMBus temperature sensor driver |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 2 | * |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 3 | * Copyright (C) 2010 Steven King <sfking@fdwdc.com> |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 16 | #include <linux/delay.h> |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/hwmon.h> |
| 22 | #include <linux/hwmon-sysfs.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/mutex.h> |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 25 | #include <linux/device.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 26 | #include <linux/jiffies.h> |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 27 | #include <linux/regmap.h> |
Eduardo Valentin | 6a02752 | 2013-07-16 14:57:51 -0400 | [diff] [blame] | 28 | #include <linux/thermal.h> |
| 29 | #include <linux/of.h> |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 30 | |
| 31 | #define DRIVER_NAME "tmp102" |
| 32 | |
| 33 | #define TMP102_TEMP_REG 0x00 |
| 34 | #define TMP102_CONF_REG 0x01 |
| 35 | /* note: these bit definitions are byte swapped */ |
| 36 | #define TMP102_CONF_SD 0x0100 |
| 37 | #define TMP102_CONF_TM 0x0200 |
| 38 | #define TMP102_CONF_POL 0x0400 |
| 39 | #define TMP102_CONF_F0 0x0800 |
| 40 | #define TMP102_CONF_F1 0x1000 |
| 41 | #define TMP102_CONF_R0 0x2000 |
| 42 | #define TMP102_CONF_R1 0x4000 |
| 43 | #define TMP102_CONF_OS 0x8000 |
| 44 | #define TMP102_CONF_EM 0x0010 |
| 45 | #define TMP102_CONF_AL 0x0020 |
| 46 | #define TMP102_CONF_CR0 0x0040 |
| 47 | #define TMP102_CONF_CR1 0x0080 |
| 48 | #define TMP102_TLOW_REG 0x02 |
| 49 | #define TMP102_THIGH_REG 0x03 |
| 50 | |
Guenter Roeck | a9f92ccf3 | 2016-06-22 10:01:57 -0700 | [diff] [blame] | 51 | #define TMP102_CONFREG_MASK (TMP102_CONF_SD | TMP102_CONF_TM | \ |
| 52 | TMP102_CONF_POL | TMP102_CONF_F0 | \ |
| 53 | TMP102_CONF_F1 | TMP102_CONF_OS | \ |
| 54 | TMP102_CONF_EM | TMP102_CONF_AL | \ |
| 55 | TMP102_CONF_CR0 | TMP102_CONF_CR1) |
| 56 | |
| 57 | #define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \ |
| 58 | TMP102_CONF_CR0) |
| 59 | #define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \ |
| 60 | TMP102_CONF_CR1) |
| 61 | |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 62 | #define CONVERSION_TIME_MS 35 /* in milli-seconds */ |
| 63 | |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 64 | struct tmp102 { |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 65 | struct regmap *regmap; |
Jean Delvare | 38806bd | 2010-05-27 19:58:59 +0200 | [diff] [blame] | 66 | u16 config_orig; |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 67 | unsigned long ready_time; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 70 | /* convert left adjusted 13-bit TMP102 register value to milliCelsius */ |
| 71 | static inline int tmp102_reg_to_mC(s16 val) |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 72 | { |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 73 | return ((val & ~0x01) * 1000) / 128; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 76 | /* convert milliCelsius to left adjusted 13-bit TMP102 register value */ |
| 77 | static inline u16 tmp102_mC_to_reg(int val) |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 78 | { |
| 79 | return (val * 128) / 1000; |
| 80 | } |
| 81 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 82 | static int tmp102_read_temp(void *dev, int *temp) |
Eduardo Valentin | 6a02752 | 2013-07-16 14:57:51 -0400 | [diff] [blame] | 83 | { |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 84 | struct tmp102 *tmp102 = dev_get_drvdata(dev); |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 85 | unsigned int reg; |
| 86 | int ret; |
Eduardo Valentin | 6a02752 | 2013-07-16 14:57:51 -0400 | [diff] [blame] | 87 | |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 88 | if (time_before(jiffies, tmp102->ready_time)) { |
Nishanth Menon | 00917b5 | 2015-12-01 10:10:21 -0600 | [diff] [blame] | 89 | dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__); |
| 90 | return -EAGAIN; |
| 91 | } |
| 92 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 93 | ret = regmap_read(tmp102->regmap, TMP102_TEMP_REG, ®); |
| 94 | if (ret < 0) |
| 95 | return ret; |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 96 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 97 | *temp = tmp102_reg_to_mC(reg); |
Eduardo Valentin | 6a02752 | 2013-07-16 14:57:51 -0400 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 102 | static ssize_t tmp102_show_temp(struct device *dev, |
| 103 | struct device_attribute *attr, |
| 104 | char *buf) |
| 105 | { |
| 106 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 107 | struct tmp102 *tmp102 = dev_get_drvdata(dev); |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 108 | int regaddr = sda->index; |
| 109 | unsigned int reg; |
| 110 | int err; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 111 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 112 | if (regaddr == TMP102_TEMP_REG && |
| 113 | time_before(jiffies, tmp102->ready_time)) |
Nishanth Menon | 00917b5 | 2015-12-01 10:10:21 -0600 | [diff] [blame] | 114 | return -EAGAIN; |
| 115 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 116 | err = regmap_read(tmp102->regmap, regaddr, ®); |
| 117 | if (err < 0) |
| 118 | return err; |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 119 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 120 | return sprintf(buf, "%d\n", tmp102_reg_to_mC(reg)); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static ssize_t tmp102_set_temp(struct device *dev, |
| 124 | struct device_attribute *attr, |
| 125 | const char *buf, size_t count) |
| 126 | { |
| 127 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | ad9beea | 2014-02-02 10:08:09 -0800 | [diff] [blame] | 128 | struct tmp102 *tmp102 = dev_get_drvdata(dev); |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 129 | int reg = sda->index; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 130 | long val; |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 131 | int err; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 132 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 133 | if (kstrtol(buf, 10, &val) < 0) |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 134 | return -EINVAL; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 135 | val = clamp_val(val, -256000, 255000); |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 136 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 137 | err = regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(val)); |
| 138 | return err ? : count; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 141 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL, |
| 142 | TMP102_TEMP_REG); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 143 | |
| 144 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp, |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 145 | tmp102_set_temp, TMP102_TLOW_REG); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 146 | |
| 147 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 148 | tmp102_set_temp, TMP102_THIGH_REG); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 149 | |
Guenter Roeck | ad9beea | 2014-02-02 10:08:09 -0800 | [diff] [blame] | 150 | static struct attribute *tmp102_attrs[] = { |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 151 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 152 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 153 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 154 | NULL |
| 155 | }; |
Guenter Roeck | ad9beea | 2014-02-02 10:08:09 -0800 | [diff] [blame] | 156 | ATTRIBUTE_GROUPS(tmp102); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 157 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 158 | static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = { |
| 159 | .get_temp = tmp102_read_temp, |
| 160 | }; |
| 161 | |
Guenter Roeck | b17ea1c | 2016-06-19 20:09:54 -0700 | [diff] [blame] | 162 | static void tmp102_restore_config(void *data) |
| 163 | { |
| 164 | struct tmp102 *tmp102 = data; |
Guenter Roeck | b17ea1c | 2016-06-19 20:09:54 -0700 | [diff] [blame] | 165 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 166 | regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig); |
Guenter Roeck | b17ea1c | 2016-06-19 20:09:54 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 169 | static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg) |
| 170 | { |
| 171 | return reg != TMP102_TEMP_REG; |
| 172 | } |
| 173 | |
| 174 | static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg) |
| 175 | { |
| 176 | return reg == TMP102_TEMP_REG; |
| 177 | } |
| 178 | |
| 179 | static const struct regmap_config tmp102_regmap_config = { |
| 180 | .reg_bits = 8, |
| 181 | .val_bits = 16, |
| 182 | .max_register = TMP102_THIGH_REG, |
| 183 | .writeable_reg = tmp102_is_writeable_reg, |
| 184 | .volatile_reg = tmp102_is_volatile_reg, |
| 185 | .val_format_endian = REGMAP_ENDIAN_BIG, |
| 186 | .cache_type = REGCACHE_RBTREE, |
| 187 | .use_single_rw = true, |
| 188 | }; |
| 189 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 190 | static int tmp102_probe(struct i2c_client *client, |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 191 | const struct i2c_device_id *id) |
| 192 | { |
Guenter Roeck | fbd9af1 | 2014-02-02 14:22:30 -0800 | [diff] [blame] | 193 | struct device *dev = &client->dev; |
Guenter Roeck | ad9beea | 2014-02-02 10:08:09 -0800 | [diff] [blame] | 194 | struct device *hwmon_dev; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 195 | struct tmp102 *tmp102; |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 196 | unsigned int regval; |
| 197 | int err; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 198 | |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 199 | if (!i2c_check_functionality(client->adapter, |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 200 | I2C_FUNC_SMBUS_WORD_DATA)) { |
Guenter Roeck | fbd9af1 | 2014-02-02 14:22:30 -0800 | [diff] [blame] | 201 | dev_err(dev, |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 202 | "adapter doesn't support SMBus word transactions\n"); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 203 | return -ENODEV; |
| 204 | } |
| 205 | |
Guenter Roeck | fbd9af1 | 2014-02-02 14:22:30 -0800 | [diff] [blame] | 206 | tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL); |
Guenter Roeck | f511a21 | 2012-06-02 11:35:52 -0700 | [diff] [blame] | 207 | if (!tmp102) |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 208 | return -ENOMEM; |
Guenter Roeck | f511a21 | 2012-06-02 11:35:52 -0700 | [diff] [blame] | 209 | |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 210 | i2c_set_clientdata(client, tmp102); |
| 211 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 212 | tmp102->regmap = devm_regmap_init_i2c(client, &tmp102_regmap_config); |
| 213 | if (IS_ERR(tmp102->regmap)) |
| 214 | return PTR_ERR(tmp102->regmap); |
| 215 | |
| 216 | err = regmap_read(tmp102->regmap, TMP102_CONF_REG, ®val); |
| 217 | if (err < 0) { |
Guenter Roeck | fbd9af1 | 2014-02-02 14:22:30 -0800 | [diff] [blame] | 218 | dev_err(dev, "error reading config register\n"); |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 219 | return err; |
Jean Delvare | 38806bd | 2010-05-27 19:58:59 +0200 | [diff] [blame] | 220 | } |
Guenter Roeck | a9f92ccf3 | 2016-06-22 10:01:57 -0700 | [diff] [blame] | 221 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 222 | if ((regval & ~TMP102_CONFREG_MASK) != |
Guenter Roeck | a9f92ccf3 | 2016-06-22 10:01:57 -0700 | [diff] [blame] | 223 | (TMP102_CONF_R0 | TMP102_CONF_R1)) { |
| 224 | dev_err(dev, "unexpected config register value\n"); |
| 225 | return -ENODEV; |
| 226 | } |
| 227 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 228 | tmp102->config_orig = regval; |
Guenter Roeck | b17ea1c | 2016-06-19 20:09:54 -0700 | [diff] [blame] | 229 | |
| 230 | devm_add_action(dev, tmp102_restore_config, tmp102); |
| 231 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 232 | regval &= ~TMP102_CONFIG_CLEAR; |
| 233 | regval |= TMP102_CONFIG_SET; |
Guenter Roeck | a9f92ccf3 | 2016-06-22 10:01:57 -0700 | [diff] [blame] | 234 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 235 | err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval); |
| 236 | if (err < 0) { |
Guenter Roeck | fbd9af1 | 2014-02-02 14:22:30 -0800 | [diff] [blame] | 237 | dev_err(dev, "error writing config register\n"); |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 238 | return err; |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 239 | } |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 240 | |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 241 | tmp102->ready_time = jiffies; |
| 242 | if (tmp102->config_orig & TMP102_CONF_SD) { |
| 243 | /* |
| 244 | * Mark that we are not ready with data until the first |
| 245 | * conversion is complete |
| 246 | */ |
| 247 | tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS); |
| 248 | } |
| 249 | |
Guenter Roeck | b17ea1c | 2016-06-19 20:09:54 -0700 | [diff] [blame] | 250 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 251 | tmp102, |
| 252 | tmp102_groups); |
Guenter Roeck | ad9beea | 2014-02-02 10:08:09 -0800 | [diff] [blame] | 253 | if (IS_ERR(hwmon_dev)) { |
| 254 | dev_dbg(dev, "unable to register hwmon device\n"); |
Guenter Roeck | b17ea1c | 2016-06-19 20:09:54 -0700 | [diff] [blame] | 255 | return PTR_ERR(hwmon_dev); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 256 | } |
Eduardo Valentin | 51b77fd | 2016-03-09 13:02:54 -0800 | [diff] [blame] | 257 | devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev, |
| 258 | &tmp102_of_thermal_ops); |
Eduardo Valentin | 6a02752 | 2013-07-16 14:57:51 -0400 | [diff] [blame] | 259 | |
Guenter Roeck | fbd9af1 | 2014-02-02 14:22:30 -0800 | [diff] [blame] | 260 | dev_info(dev, "initialized\n"); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 261 | |
| 262 | return 0; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Grygorii Strashko | dd378b1 | 2015-02-03 17:01:58 +0200 | [diff] [blame] | 265 | #ifdef CONFIG_PM_SLEEP |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 266 | static int tmp102_suspend(struct device *dev) |
| 267 | { |
| 268 | struct i2c_client *client = to_i2c_client(dev); |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 269 | struct tmp102 *tmp102 = i2c_get_clientdata(client); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 270 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 271 | return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG, |
| 272 | TMP102_CONF_SD, TMP102_CONF_SD); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int tmp102_resume(struct device *dev) |
| 276 | { |
| 277 | struct i2c_client *client = to_i2c_client(dev); |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 278 | struct tmp102 *tmp102 = i2c_get_clientdata(client); |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 279 | int err; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 280 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 281 | err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG, |
| 282 | TMP102_CONF_SD, 0); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 283 | |
Guenter Roeck | 3d8f7a8 | 2016-06-19 20:34:57 -0700 | [diff] [blame] | 284 | tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS); |
| 285 | |
Guenter Roeck | 28a340d | 2016-06-20 09:55:46 -0700 | [diff] [blame^] | 286 | return err; |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 287 | } |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 288 | #endif /* CONFIG_PM */ |
| 289 | |
Grygorii Strashko | dd378b1 | 2015-02-03 17:01:58 +0200 | [diff] [blame] | 290 | static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume); |
| 291 | |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 292 | static const struct i2c_device_id tmp102_id[] = { |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 293 | { "tmp102", 0 }, |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 294 | { } |
| 295 | }; |
Jean Delvare | cff37c9 | 2010-05-27 19:58:57 +0200 | [diff] [blame] | 296 | MODULE_DEVICE_TABLE(i2c, tmp102_id); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 297 | |
| 298 | static struct i2c_driver tmp102_driver = { |
| 299 | .driver.name = DRIVER_NAME, |
Grygorii Strashko | dd378b1 | 2015-02-03 17:01:58 +0200 | [diff] [blame] | 300 | .driver.pm = &tmp102_dev_pm_ops, |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 301 | .probe = tmp102_probe, |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 302 | .id_table = tmp102_id, |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 303 | }; |
| 304 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 305 | module_i2c_driver(tmp102_driver); |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 306 | |
Steven King | beb1b6b | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 307 | MODULE_AUTHOR("Steven King <sfking@fdwdc.com>"); |
| 308 | MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver"); |
| 309 | MODULE_LICENSE("GPL"); |