| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* Texas Instruments TMP102 SMBus temperature sensor driver |
| 3 |
* |
| 4 |
* Copyright (C) 2010 Steven King <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/delay.h> |
| 8 |
#include <linux/module.h> |
| 9 |
#include <linux/init.h> |
| 10 |
#include <linux/slab.h> |
| 11 |
#include <linux/i2c.h> |
| 12 |
#include <linux/hwmon.h> |
| 13 |
#include <linux/err.h> |
| 14 |
#include <linux/device.h> |
| 15 |
#include <linux/jiffies.h> |
| 16 |
#include <linux/regmap.h> |
| 17 |
#include <linux/regulator/consumer.h> |
| 18 |
#include <linux/of.h> |
| 19 |
|
| 20 |
#define DRIVER_NAME "tmp102" |
| 21 |
|
| 22 |
#define TMP102_TEMP_REG 0x00 |
| 23 |
#define TMP102_CONF_REG 0x01 |
| 24 |
/* note: these bit definitions are byte swapped */ |
| 25 |
#define TMP102_CONF_SD 0x0100 |
| 26 |
#define TMP102_CONF_TM 0x0200 |
| 27 |
#define TMP102_CONF_POL 0x0400 |
| 28 |
#define TMP102_CONF_F0 0x0800 |
| 29 |
#define TMP102_CONF_F1 0x1000 |
| 30 |
#define TMP102_CONF_R0 0x2000 |
| 31 |
#define TMP102_CONF_R1 0x4000 |
| 32 |
#define TMP102_CONF_OS 0x8000 |
| 33 |
#define TMP102_CONF_EM 0x0010 |
| 34 |
#define TMP102_CONF_AL 0x0020 |
| 35 |
#define TMP102_CONF_CR0 0x0040 |
| 36 |
#define TMP102_CONF_CR1 0x0080 |
| 37 |
#define TMP102_TLOW_REG 0x02 |
| 38 |
#define TMP102_THIGH_REG 0x03 |
| 39 |
|
| 40 |
#define TMP102_CONFREG_MASK (TMP102_CONF_SD | TMP102_CONF_TM | \ |
| 41 |
TMP102_CONF_POL | TMP102_CONF_F0 | \ |
| 42 |
TMP102_CONF_F1 | TMP102_CONF_OS | \ |
| 43 |
TMP102_CONF_EM | TMP102_CONF_AL | \ |
| 44 |
TMP102_CONF_CR0 | TMP102_CONF_CR1) |
| 45 |
|
| 46 |
#define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \ |
| 47 |
TMP102_CONF_CR0) |
| 48 |
#define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \ |
| 49 |
TMP102_CONF_CR1) |
| 50 |
|
| 51 |
#define CONVERSION_TIME_MS 35 /* in milli-seconds */ |
| 52 |
|
| 53 |
struct tmp102 { |
| 54 |
const char *label; |
| 55 |
struct regmap *regmap; |
| 56 |
u16 config_orig; |
| 57 |
unsigned long ready_time; |
| 58 |
}; |
| 59 |
|
| 60 |
/* convert left adjusted 13-bit TMP102 register value to milliCelsius */ |
| 61 |
static inline int tmp102_reg_to_mC(s16 val) |
| 62 |
{ |
| 63 |
return ((val & ~0x01) * 1000) / 128; |
| 64 |
} |
| 65 |
|
| 66 |
/* convert milliCelsius to left adjusted 13-bit TMP102 register value */ |
| 67 |
static inline u16 tmp102_mC_to_reg(int val) |
| 68 |
{ |
| 69 |
return (val * 128) / 1000; |
| 70 |
} |
| 71 |
|
| 72 |
static int tmp102_read_string(struct device *dev, enum hwmon_sensor_types type, |
| 73 |
u32 attr, int channel, const char **str) |
| 74 |
{ |
| 75 |
struct tmp102 *tmp102 = dev_get_drvdata(dev); |
| 76 |
|
| 77 |
*str = tmp102->label; |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
static int tmp102_read(struct device *dev, enum hwmon_sensor_types type, |
| 83 |
u32 attr, int channel, long *temp) |
| 84 |
{ |
| 85 |
struct tmp102 *tmp102 = dev_get_drvdata(dev); |
| 86 |
unsigned int regval; |
| 87 |
int err, reg; |
| 88 |
|
| 89 |
switch (attr) { |
| 90 |
case hwmon_temp_input: |
| 91 |
/* Is it too early to return a conversion ? */ |
| 92 |
if (time_before(jiffies, tmp102->ready_time)) { |
| 93 |
dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__); |
| 94 |
return -EAGAIN; |
| 95 |
} |
| 96 |
reg = TMP102_TEMP_REG; |
| 97 |
break; |
| 98 |
case hwmon_temp_max_hyst: |
| 99 |
reg = TMP102_TLOW_REG; |
| 100 |
break; |
| 101 |
case hwmon_temp_max: |
| 102 |
reg = TMP102_THIGH_REG; |
| 103 |
break; |
| 104 |
default: |
| 105 |
return -EOPNOTSUPP; |
| 106 |
} |
| 107 |
|
| 108 |
err = regmap_read(tmp102->regmap, reg, ®val); |
| 109 |
if (err < 0) |
| 110 |
return err; |
| 111 |
*temp = tmp102_reg_to_mC(regval); |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
static int tmp102_write(struct device *dev, enum hwmon_sensor_types type, |
| 117 |
u32 attr, int channel, long temp) |
| 118 |
{ |
| 119 |
struct tmp102 *tmp102 = dev_get_drvdata(dev); |
| 120 |
int reg; |
| 121 |
|
| 122 |
switch (attr) { |
| 123 |
case hwmon_temp_max_hyst: |
| 124 |
reg = TMP102_TLOW_REG; |
| 125 |
break; |
| 126 |
case hwmon_temp_max: |
| 127 |
reg = TMP102_THIGH_REG; |
| 128 |
break; |
| 129 |
default: |
| 130 |
return -EOPNOTSUPP; |
| 131 |
} |
| 132 |
|
| 133 |
temp = clamp_val(temp, -256000, 255000); |
| 134 |
return regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(temp)); |
| 135 |
} |
| 136 |
|
| 137 |
static umode_t tmp102_is_visible(const void *data, enum hwmon_sensor_types type, |
| 138 |
u32 attr, int channel) |
| 139 |
{ |
| 140 |
const struct tmp102 *tmp102 = data; |
| 141 |
|
| 142 |
if (type != hwmon_temp) |
| 143 |
return 0; |
| 144 |
|
| 145 |
switch (attr) { |
| 146 |
case hwmon_temp_input: |
| 147 |
return 0444; |
| 148 |
case hwmon_temp_label: |
| 149 |
if (tmp102->label) |
| 150 |
return 0444; |
| 151 |
return 0; |
| 152 |
case hwmon_temp_max_hyst: |
| 153 |
case hwmon_temp_max: |
| 154 |
return 0644; |
| 155 |
default: |
| 156 |
return 0; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static const struct hwmon_channel_info * const tmp102_info[] = { |
| 161 |
HWMON_CHANNEL_INFO(chip, |
| 162 |
HWMON_C_REGISTER_TZ), |
| 163 |
HWMON_CHANNEL_INFO(temp, |
| 164 |
HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MAX | HWMON_T_MAX_HYST), |
| 165 |
NULL |
| 166 |
}; |
| 167 |
|
| 168 |
static const struct hwmon_ops tmp102_hwmon_ops = { |
| 169 |
.is_visible = tmp102_is_visible, |
| 170 |
.read_string = tmp102_read_string, |
| 171 |
.read = tmp102_read, |
| 172 |
.write = tmp102_write, |
| 173 |
}; |
| 174 |
|
| 175 |
static const struct hwmon_chip_info tmp102_chip_info = { |
| 176 |
.ops = &tmp102_hwmon_ops, |
| 177 |
.info = tmp102_info, |
| 178 |
}; |
| 179 |
|
| 180 |
static void tmp102_restore_config(void *data) |
| 181 |
{ |
| 182 |
struct tmp102 *tmp102 = data; |
| 183 |
|
| 184 |
regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig); |
| 185 |
} |
| 186 |
|
| 187 |
static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg) |
| 188 |
{ |
| 189 |
return reg != TMP102_TEMP_REG; |
| 190 |
} |
| 191 |
|
| 192 |
static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg) |
| 193 |
{ |
| 194 |
return reg == TMP102_TEMP_REG; |
| 195 |
} |
| 196 |
|
| 197 |
static const struct regmap_config tmp102_regmap_config = { |
| 198 |
.reg_bits = 8, |
| 199 |
.val_bits = 16, |
| 200 |
.max_register = TMP102_THIGH_REG, |
| 201 |
.writeable_reg = tmp102_is_writeable_reg, |
| 202 |
.volatile_reg = tmp102_is_volatile_reg, |
| 203 |
.val_format_endian = REGMAP_ENDIAN_BIG, |
| 204 |
.cache_type = REGCACHE_MAPLE, |
| 205 |
.use_single_read = true, |
| 206 |
.use_single_write = true, |
| 207 |
}; |
| 208 |
|
| 209 |
static int tmp102_probe(struct i2c_client *client) |
| 210 |
{ |
| 211 |
struct device *dev = &client->dev; |
| 212 |
struct device *hwmon_dev; |
| 213 |
struct tmp102 *tmp102; |
| 214 |
unsigned int regval; |
| 215 |
int err; |
| 216 |
|
| 217 |
if (!i2c_check_functionality(client->adapter, |
| 218 |
I2C_FUNC_SMBUS_WORD_DATA)) { |
| 219 |
dev_err(dev, |
| 220 |
"adapter doesn't support SMBus word transactions\n"); |
| 221 |
return -ENODEV; |
| 222 |
} |
| 223 |
|
| 224 |
err = devm_regulator_get_enable_optional(dev, "vcc"); |
| 225 |
if (err < 0 && err != -ENODEV) |
| 226 |
return dev_err_probe(dev, err, "Failed to enable regulator\n"); |
| 227 |
|
| 228 |
tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL); |
| 229 |
if (!tmp102) |
| 230 |
return -ENOMEM; |
| 231 |
|
| 232 |
of_property_read_string(dev->of_node, "label", &tmp102->label); |
| 233 |
|
| 234 |
i2c_set_clientdata(client, tmp102); |
| 235 |
|
| 236 |
tmp102->regmap = devm_regmap_init_i2c(client, &tmp102_regmap_config); |
| 237 |
if (IS_ERR(tmp102->regmap)) |
| 238 |
return PTR_ERR(tmp102->regmap); |
| 239 |
|
| 240 |
err = regmap_read(tmp102->regmap, TMP102_CONF_REG, ®val); |
| 241 |
if (err < 0) { |
| 242 |
dev_err(dev, "error reading config register\n"); |
| 243 |
return err; |
| 244 |
} |
| 245 |
|
| 246 |
if ((regval & ~TMP102_CONFREG_MASK) != |
| 247 |
(TMP102_CONF_R0 | TMP102_CONF_R1)) { |
| 248 |
dev_err(dev, "unexpected config register value\n"); |
| 249 |
return -ENODEV; |
| 250 |
} |
| 251 |
|
| 252 |
tmp102->config_orig = regval; |
| 253 |
|
| 254 |
err = devm_add_action_or_reset(dev, tmp102_restore_config, tmp102); |
| 255 |
if (err) |
| 256 |
return err; |
| 257 |
|
| 258 |
regval &= ~TMP102_CONFIG_CLEAR; |
| 259 |
regval |= TMP102_CONFIG_SET; |
| 260 |
|
| 261 |
err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval); |
| 262 |
if (err < 0) { |
| 263 |
dev_err(dev, "error writing config register\n"); |
| 264 |
return err; |
| 265 |
} |
| 266 |
|
| 267 |
/* |
| 268 |
* Mark that we are not ready with data until the first |
| 269 |
* conversion is complete |
| 270 |
*/ |
| 271 |
tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS); |
| 272 |
|
| 273 |
hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, |
| 274 |
tmp102, |
| 275 |
&tmp102_chip_info, |
| 276 |
NULL); |
| 277 |
if (IS_ERR(hwmon_dev)) { |
| 278 |
dev_dbg(dev, "unable to register hwmon device\n"); |
| 279 |
return PTR_ERR(hwmon_dev); |
| 280 |
} |
| 281 |
dev_info(dev, "initialized\n"); |
| 282 |
|
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
static int tmp102_suspend(struct device *dev) |
| 287 |
{ |
| 288 |
struct i2c_client *client = to_i2c_client(dev); |
| 289 |
struct tmp102 *tmp102 = i2c_get_clientdata(client); |
| 290 |
|
| 291 |
return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG, |
| 292 |
TMP102_CONF_SD, TMP102_CONF_SD); |
| 293 |
} |
| 294 |
|
| 295 |
static int tmp102_resume(struct device *dev) |
| 296 |
{ |
| 297 |
struct i2c_client *client = to_i2c_client(dev); |
| 298 |
struct tmp102 *tmp102 = i2c_get_clientdata(client); |
| 299 |
int err; |
| 300 |
|
| 301 |
err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG, |
| 302 |
TMP102_CONF_SD, 0); |
| 303 |
|
| 304 |
tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS); |
| 305 |
|
| 306 |
return err; |
| 307 |
} |
| 308 |
|
| 309 |
static DEFINE_SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume); |
| 310 |
|
| 311 |
static const struct i2c_device_id tmp102_id[] = { |
| 312 |
{ "tmp102" }, |
| 313 |
{ } |
| 314 |
}; |
| 315 |
MODULE_DEVICE_TABLE(i2c, tmp102_id); |
| 316 |
|
| 317 |
static const struct of_device_id __maybe_unused tmp102_of_match[] = { |
| 318 |
{ .compatible = "ti,tmp102" }, |
| 319 |
{ }, |
| 320 |
}; |
| 321 |
MODULE_DEVICE_TABLE(of, tmp102_of_match); |
| 322 |
|
| 323 |
static struct i2c_driver tmp102_driver = { |
| 324 |
.driver.name = DRIVER_NAME, |
| 325 |
.driver.of_match_table = of_match_ptr(tmp102_of_match), |
| 326 |
.driver.pm = pm_sleep_ptr(&tmp102_dev_pm_ops), |
| 327 |
.probe = tmp102_probe, |
| 328 |
.id_table = tmp102_id, |
| 329 |
}; |
| 330 |
|
| 331 |
module_i2c_driver(tmp102_driver); |
| 332 |
|
| 333 |
MODULE_AUTHOR("Steven King <[email protected]>"); |
| 334 |
MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver"); |
| 335 |
MODULE_LICENSE("GPL"); |