| 1 | |
| 2 | |
| 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/property.h>
|
| 19 |
|
| 20 | #define DRIVER_NAME "tmp102"
|
| 21 |
|
| 22 | #define TMP102_TEMP_REG 0x00
|
| 23 | #define TMP102_CONF_REG 0x01
|
| 24 |
|
| 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
|
| 52 |
|
| 53 | #define NUM_SAMPLE_TIMES 4
|
| 54 | #define DEFAULT_SAMPLE_TIME_MS 250
|
| 55 | static const unsigned int *sample_times = (const unsigned int []){ 125, 250, 1000, 4000 };
|
| 56 |
|
| 57 | struct tmp102 {
|
| 58 | const char *label;
|
| 59 | struct regmap *regmap;
|
| 60 | u16 config_orig;
|
| 61 | unsigned long ready_time;
|
| 62 | u16 sample_time;
|
| 63 | };
|
| 64 |
|
| 65 |
|
| 66 | static inline int tmp102_reg_to_mC(s16 val)
|
| 67 | {
|
| 68 | return ((val & ~0x01) * 1000) / 128;
|
| 69 | }
|
| 70 |
|
| 71 |
|
| 72 | static inline u16 tmp102_mC_to_reg(int val)
|
| 73 | {
|
| 74 | return (val * 128) / 1000;
|
| 75 | }
|
| 76 |
|
| 77 | static int tmp102_read_string(struct device *dev, enum hwmon_sensor_types type,
|
| 78 | u32 attr, int channel, const char **str)
|
| 79 | {
|
| 80 | struct tmp102 *tmp102 = dev_get_drvdata(dev);
|
| 81 |
|
| 82 | *str = tmp102->label;
|
| 83 |
|
| 84 | return 0;
|
| 85 | }
|
| 86 |
|
| 87 | static int tmp102_read_chip(struct device *dev, u32 attr, long *val)
|
| 88 | {
|
| 89 | struct tmp102 *tmp102 = dev_get_drvdata(dev);
|
| 90 |
|
| 91 | switch (attr) {
|
| 92 | case hwmon_chip_update_interval:
|
| 93 | *val = tmp102->sample_time;
|
| 94 | return 0;
|
| 95 | default:
|
| 96 | return -EOPNOTSUPP;
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | static int tmp102_read_temp(struct device *dev, u32 attr, long *val)
|
| 101 | {
|
| 102 | struct tmp102 *tmp102 = dev_get_drvdata(dev);
|
| 103 | unsigned int regval;
|
| 104 | int err, reg;
|
| 105 |
|
| 106 | switch (attr) {
|
| 107 | case hwmon_temp_input:
|
| 108 |
|
| 109 | if (time_before(jiffies, tmp102->ready_time)) {
|
| 110 | dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
|
| 111 | return -EAGAIN;
|
| 112 | }
|
| 113 | reg = TMP102_TEMP_REG;
|
| 114 | break;
|
| 115 | case hwmon_temp_max_hyst:
|
| 116 | reg = TMP102_TLOW_REG;
|
| 117 | break;
|
| 118 | case hwmon_temp_max:
|
| 119 | reg = TMP102_THIGH_REG;
|
| 120 | break;
|
| 121 | default:
|
| 122 | return -EOPNOTSUPP;
|
| 123 | }
|
| 124 |
|
| 125 | err = regmap_read(tmp102->regmap, reg, ®val);
|
| 126 | if (err < 0)
|
| 127 | return err;
|
| 128 |
|
| 129 | *val = tmp102_reg_to_mC(regval);
|
| 130 |
|
| 131 | return 0;
|
| 132 | }
|
| 133 |
|
| 134 | static int tmp102_read(struct device *dev, enum hwmon_sensor_types type,
|
| 135 | u32 attr, int channel, long *val)
|
| 136 | {
|
| 137 | switch (type) {
|
| 138 | case hwmon_chip:
|
| 139 | return tmp102_read_chip(dev, attr, val);
|
| 140 | case hwmon_temp:
|
| 141 | return tmp102_read_temp(dev, attr, val);
|
| 142 | default:
|
| 143 | return -EOPNOTSUPP;
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | static int tmp102_update_interval(struct device *dev, long val)
|
| 148 | {
|
| 149 | struct tmp102 *tmp102 = dev_get_drvdata(dev);
|
| 150 | u8 index;
|
| 151 | s32 err;
|
| 152 |
|
| 153 | index = find_closest(val, sample_times, NUM_SAMPLE_TIMES);
|
| 154 |
|
| 155 | err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
|
| 156 | (TMP102_CONF_CR1 | TMP102_CONF_CR0), (3 - index) << 6);
|
| 157 | if (err < 0)
|
| 158 | return err;
|
| 159 | tmp102->sample_time = sample_times[index];
|
| 160 |
|
| 161 | return 0;
|
| 162 | }
|
| 163 |
|
| 164 | static int tmp102_write_chip(struct device *dev, u32 attr, long val)
|
| 165 | {
|
| 166 | switch (attr) {
|
| 167 | case hwmon_chip_update_interval:
|
| 168 | return tmp102_update_interval(dev, val);
|
| 169 | default:
|
| 170 | return -EOPNOTSUPP;
|
| 171 | }
|
| 172 | return 0;
|
| 173 | }
|
| 174 |
|
| 175 | static int tmp102_write_temp(struct device *dev, u32 attr, long val)
|
| 176 | {
|
| 177 | struct tmp102 *tmp102 = dev_get_drvdata(dev);
|
| 178 | int reg;
|
| 179 |
|
| 180 | switch (attr) {
|
| 181 | case hwmon_temp_max_hyst:
|
| 182 | reg = TMP102_TLOW_REG;
|
| 183 | break;
|
| 184 | case hwmon_temp_max:
|
| 185 | reg = TMP102_THIGH_REG;
|
| 186 | break;
|
| 187 | default:
|
| 188 | return -EOPNOTSUPP;
|
| 189 | }
|
| 190 |
|
| 191 | val = clamp_val(val, -256000, 255000);
|
| 192 | return regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(val));
|
| 193 | }
|
| 194 |
|
| 195 | static int tmp102_write(struct device *dev, enum hwmon_sensor_types type,
|
| 196 | u32 attr, int channel, long val)
|
| 197 | {
|
| 198 | switch (type) {
|
| 199 | case hwmon_chip:
|
| 200 | return tmp102_write_chip(dev, attr, val);
|
| 201 | case hwmon_temp:
|
| 202 | return tmp102_write_temp(dev, attr, val);
|
| 203 | default:
|
| 204 | return -EOPNOTSUPP;
|
| 205 | }
|
| 206 | return 0;
|
| 207 | }
|
| 208 |
|
| 209 | static umode_t tmp102_is_visible(const void *data, enum hwmon_sensor_types type,
|
| 210 | u32 attr, int channel)
|
| 211 | {
|
| 212 | const struct tmp102 *tmp102 = data;
|
| 213 |
|
| 214 | switch (type) {
|
| 215 | case hwmon_chip:
|
| 216 | switch (attr) {
|
| 217 | case hwmon_chip_update_interval:
|
| 218 | return 0644;
|
| 219 | default:
|
| 220 | break;
|
| 221 | }
|
| 222 | break;
|
| 223 | case hwmon_temp:
|
| 224 | switch (attr) {
|
| 225 | case hwmon_temp_input:
|
| 226 | return 0444;
|
| 227 | case hwmon_temp_label:
|
| 228 | if (tmp102->label)
|
| 229 | return 0444;
|
| 230 | return 0;
|
| 231 | case hwmon_temp_max_hyst:
|
| 232 | case hwmon_temp_max:
|
| 233 | return 0644;
|
| 234 | default:
|
| 235 | break;
|
| 236 | }
|
| 237 | break;
|
| 238 | default:
|
| 239 | break;
|
| 240 | }
|
| 241 | return 0;
|
| 242 | }
|
| 243 |
|
| 244 | static const struct hwmon_channel_info * const tmp102_info[] = {
|
| 245 | HWMON_CHANNEL_INFO(chip,
|
| 246 | HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
|
| 247 | HWMON_CHANNEL_INFO(temp,
|
| 248 | HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MAX | HWMON_T_MAX_HYST),
|
| 249 | NULL
|
| 250 | };
|
| 251 |
|
| 252 | static const struct hwmon_ops tmp102_hwmon_ops = {
|
| 253 | .is_visible = tmp102_is_visible,
|
| 254 | .read_string = tmp102_read_string,
|
| 255 | .read = tmp102_read,
|
| 256 | .write = tmp102_write,
|
| 257 | };
|
| 258 |
|
| 259 | static const struct hwmon_chip_info tmp102_chip_info = {
|
| 260 | .ops = &tmp102_hwmon_ops,
|
| 261 | .info = tmp102_info,
|
| 262 | };
|
| 263 |
|
| 264 | static void tmp102_restore_config(void *data)
|
| 265 | {
|
| 266 | struct tmp102 *tmp102 = data;
|
| 267 |
|
| 268 | regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig);
|
| 269 | }
|
| 270 |
|
| 271 | static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg)
|
| 272 | {
|
| 273 | return reg != TMP102_TEMP_REG;
|
| 274 | }
|
| 275 |
|
| 276 | static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg)
|
| 277 | {
|
| 278 | return reg == TMP102_TEMP_REG;
|
| 279 | }
|
| 280 |
|
| 281 | static const struct regmap_config tmp102_regmap_config = {
|
| 282 | .reg_bits = 8,
|
| 283 | .val_bits = 16,
|
| 284 | .max_register = TMP102_THIGH_REG,
|
| 285 | .writeable_reg = tmp102_is_writeable_reg,
|
| 286 | .volatile_reg = tmp102_is_volatile_reg,
|
| 287 | .val_format_endian = REGMAP_ENDIAN_BIG,
|
| 288 | .cache_type = REGCACHE_MAPLE,
|
| 289 | .use_single_read = true,
|
| 290 | .use_single_write = true,
|
| 291 | };
|
| 292 |
|
| 293 | static int tmp102_probe(struct i2c_client *client)
|
| 294 | {
|
| 295 | struct device *dev = &client->dev;
|
| 296 | struct device *hwmon_dev;
|
| 297 | struct tmp102 *tmp102;
|
| 298 | unsigned int regval;
|
| 299 | int err;
|
| 300 |
|
| 301 | if (!i2c_check_functionality(client->adapter,
|
| 302 | I2C_FUNC_SMBUS_WORD_DATA)) {
|
| 303 | dev_err(dev,
|
| 304 | "adapter doesn't support SMBus word transactions\n");
|
| 305 | return -ENODEV;
|
| 306 | }
|
| 307 |
|
| 308 | err = devm_regulator_get_enable_optional(dev, "vcc");
|
| 309 | if (err < 0 && err != -ENODEV)
|
| 310 | return dev_err_probe(dev, err, "Failed to enable regulator\n");
|
| 311 |
|
| 312 | tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
|
| 313 | if (!tmp102)
|
| 314 | return -ENOMEM;
|
| 315 |
|
| 316 | device_property_read_string(dev, "label", &tmp102->label);
|
| 317 |
|
| 318 | i2c_set_clientdata(client, tmp102);
|
| 319 |
|
| 320 | tmp102->regmap = devm_regmap_init_i2c(client, &tmp102_regmap_config);
|
| 321 | if (IS_ERR(tmp102->regmap))
|
| 322 | return PTR_ERR(tmp102->regmap);
|
| 323 |
|
| 324 | tmp102->sample_time = DEFAULT_SAMPLE_TIME_MS;
|
| 325 |
|
| 326 | err = regmap_read(tmp102->regmap, TMP102_CONF_REG, ®val);
|
| 327 | if (err < 0) {
|
| 328 | dev_err(dev, "error reading config register\n");
|
| 329 | return err;
|
| 330 | }
|
| 331 |
|
| 332 | if ((regval & ~TMP102_CONFREG_MASK) !=
|
| 333 | (TMP102_CONF_R0 | TMP102_CONF_R1)) {
|
| 334 | dev_err(dev, "unexpected config register value\n");
|
| 335 | return -ENODEV;
|
| 336 | }
|
| 337 |
|
| 338 | tmp102->config_orig = regval;
|
| 339 |
|
| 340 | err = devm_add_action_or_reset(dev, tmp102_restore_config, tmp102);
|
| 341 | if (err)
|
| 342 | return err;
|
| 343 |
|
| 344 | regval &= ~TMP102_CONFIG_CLEAR;
|
| 345 | regval |= TMP102_CONFIG_SET;
|
| 346 |
|
| 347 | err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval);
|
| 348 | if (err < 0) {
|
| 349 | dev_err(dev, "error writing config register\n");
|
| 350 | return err;
|
| 351 | }
|
| 352 |
|
| 353 | |
| 354 | * Mark that we are not ready with data until the first
|
| 355 | * conversion is complete
|
| 356 |
|
| 357 | tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
|
| 358 |
|
| 359 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
|
| 360 | tmp102,
|
| 361 | &tmp102_chip_info,
|
| 362 | NULL);
|
| 363 | if (IS_ERR(hwmon_dev)) {
|
| 364 | dev_dbg(dev, "unable to register hwmon device\n");
|
| 365 | return PTR_ERR(hwmon_dev);
|
| 366 | }
|
| 367 | dev_info(dev, "initialized\n");
|
| 368 |
|
| 369 | return 0;
|
| 370 | }
|
| 371 |
|
| 372 | static int tmp102_suspend(struct device *dev)
|
| 373 | {
|
| 374 | struct i2c_client *client = to_i2c_client(dev);
|
| 375 | struct tmp102 *tmp102 = i2c_get_clientdata(client);
|
| 376 |
|
| 377 | return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
|
| 378 | TMP102_CONF_SD, TMP102_CONF_SD);
|
| 379 | }
|
| 380 |
|
| 381 | static int tmp102_resume(struct device *dev)
|
| 382 | {
|
| 383 | struct i2c_client *client = to_i2c_client(dev);
|
| 384 | struct tmp102 *tmp102 = i2c_get_clientdata(client);
|
| 385 | int err;
|
| 386 |
|
| 387 | err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
|
| 388 | TMP102_CONF_SD, 0);
|
| 389 |
|
| 390 | tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
|
| 391 |
|
| 392 | return err;
|
| 393 | }
|
| 394 |
|
| 395 | static DEFINE_SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
|
| 396 |
|
| 397 | static const struct i2c_device_id tmp102_id[] = {
|
| 398 | { .name = "tmp102" },
|
| 399 | { .name = "tmp110" },
|
| 400 | { .name = "tmp113" },
|
| 401 | { }
|
| 402 | };
|
| 403 | MODULE_DEVICE_TABLE(i2c, tmp102_id);
|
| 404 |
|
| 405 | static const struct of_device_id tmp102_of_match[] = {
|
| 406 | { .compatible = "ti,tmp102" },
|
| 407 | { },
|
| 408 | };
|
| 409 | MODULE_DEVICE_TABLE(of, tmp102_of_match);
|
| 410 |
|
| 411 | static struct i2c_driver tmp102_driver = {
|
| 412 | .driver.name = DRIVER_NAME,
|
| 413 | .driver.of_match_table = tmp102_of_match,
|
| 414 | .driver.pm = pm_sleep_ptr(&tmp102_dev_pm_ops),
|
| 415 | .probe = tmp102_probe,
|
| 416 | .id_table = tmp102_id,
|
| 417 | };
|
| 418 |
|
| 419 | module_i2c_driver(tmp102_driver);
|
| 420 |
|
| 421 | MODULE_AUTHOR("Steven King <[email protected]>");
|
| 422 | MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
|
| 423 | MODULE_LICENSE("GPL");
|