1cff37c9eSJean Delvare /* Texas Instruments TMP102 SMBus temperature sensor driver 2beb1b6bbSSteven King * 3cff37c9eSJean Delvare * Copyright (C) 2010 Steven King <sfking@fdwdc.com> 4beb1b6bbSSteven King * 5beb1b6bbSSteven King * This program is free software; you can redistribute it and/or modify 6beb1b6bbSSteven King * it under the terms of the GNU General Public License as published by 7beb1b6bbSSteven King * the Free Software Foundation; either version 2 of the License, or 8beb1b6bbSSteven King * (at your option) any later version. 9beb1b6bbSSteven King * 10beb1b6bbSSteven King * This program is distributed in the hope that it will be useful, 11beb1b6bbSSteven King * but WITHOUT ANY WARRANTY; without even the implied warranty of 12beb1b6bbSSteven King * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13beb1b6bbSSteven King * GNU General Public License for more details. 14beb1b6bbSSteven King */ 15beb1b6bbSSteven King 163d8f7a89SGuenter Roeck #include <linux/delay.h> 17beb1b6bbSSteven King #include <linux/module.h> 18beb1b6bbSSteven King #include <linux/init.h> 19beb1b6bbSSteven King #include <linux/slab.h> 20beb1b6bbSSteven King #include <linux/i2c.h> 21beb1b6bbSSteven King #include <linux/hwmon.h> 22beb1b6bbSSteven King #include <linux/hwmon-sysfs.h> 23beb1b6bbSSteven King #include <linux/err.h> 24beb1b6bbSSteven King #include <linux/mutex.h> 25cff37c9eSJean Delvare #include <linux/device.h> 26dcd8f392SJean Delvare #include <linux/jiffies.h> 27*28a340dbSGuenter Roeck #include <linux/regmap.h> 286a027523SEduardo Valentin #include <linux/thermal.h> 296a027523SEduardo Valentin #include <linux/of.h> 30beb1b6bbSSteven King 31beb1b6bbSSteven King #define DRIVER_NAME "tmp102" 32beb1b6bbSSteven King 33beb1b6bbSSteven King #define TMP102_TEMP_REG 0x00 34beb1b6bbSSteven King #define TMP102_CONF_REG 0x01 35beb1b6bbSSteven King /* note: these bit definitions are byte swapped */ 36beb1b6bbSSteven King #define TMP102_CONF_SD 0x0100 37beb1b6bbSSteven King #define TMP102_CONF_TM 0x0200 38beb1b6bbSSteven King #define TMP102_CONF_POL 0x0400 39beb1b6bbSSteven King #define TMP102_CONF_F0 0x0800 40beb1b6bbSSteven King #define TMP102_CONF_F1 0x1000 41beb1b6bbSSteven King #define TMP102_CONF_R0 0x2000 42beb1b6bbSSteven King #define TMP102_CONF_R1 0x4000 43beb1b6bbSSteven King #define TMP102_CONF_OS 0x8000 44beb1b6bbSSteven King #define TMP102_CONF_EM 0x0010 45beb1b6bbSSteven King #define TMP102_CONF_AL 0x0020 46beb1b6bbSSteven King #define TMP102_CONF_CR0 0x0040 47beb1b6bbSSteven King #define TMP102_CONF_CR1 0x0080 48beb1b6bbSSteven King #define TMP102_TLOW_REG 0x02 49beb1b6bbSSteven King #define TMP102_THIGH_REG 0x03 50beb1b6bbSSteven King 51a9f92ccfSGuenter Roeck #define TMP102_CONFREG_MASK (TMP102_CONF_SD | TMP102_CONF_TM | \ 52a9f92ccfSGuenter Roeck TMP102_CONF_POL | TMP102_CONF_F0 | \ 53a9f92ccfSGuenter Roeck TMP102_CONF_F1 | TMP102_CONF_OS | \ 54a9f92ccfSGuenter Roeck TMP102_CONF_EM | TMP102_CONF_AL | \ 55a9f92ccfSGuenter Roeck TMP102_CONF_CR0 | TMP102_CONF_CR1) 56a9f92ccfSGuenter Roeck 57a9f92ccfSGuenter Roeck #define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \ 58a9f92ccfSGuenter Roeck TMP102_CONF_CR0) 59a9f92ccfSGuenter Roeck #define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \ 60a9f92ccfSGuenter Roeck TMP102_CONF_CR1) 61a9f92ccfSGuenter Roeck 623d8f7a89SGuenter Roeck #define CONVERSION_TIME_MS 35 /* in milli-seconds */ 633d8f7a89SGuenter Roeck 64beb1b6bbSSteven King struct tmp102 { 65*28a340dbSGuenter Roeck struct regmap *regmap; 6638806bdaSJean Delvare u16 config_orig; 673d8f7a89SGuenter Roeck unsigned long ready_time; 68beb1b6bbSSteven King }; 69beb1b6bbSSteven King 70cff37c9eSJean Delvare /* convert left adjusted 13-bit TMP102 register value to milliCelsius */ 71cff37c9eSJean Delvare static inline int tmp102_reg_to_mC(s16 val) 72beb1b6bbSSteven King { 73cff37c9eSJean Delvare return ((val & ~0x01) * 1000) / 128; 74beb1b6bbSSteven King } 75beb1b6bbSSteven King 76cff37c9eSJean Delvare /* convert milliCelsius to left adjusted 13-bit TMP102 register value */ 77cff37c9eSJean Delvare static inline u16 tmp102_mC_to_reg(int val) 78beb1b6bbSSteven King { 79beb1b6bbSSteven King return (val * 128) / 1000; 80beb1b6bbSSteven King } 81beb1b6bbSSteven King 8217e8351aSSascha Hauer static int tmp102_read_temp(void *dev, int *temp) 836a027523SEduardo Valentin { 843d8f7a89SGuenter Roeck struct tmp102 *tmp102 = dev_get_drvdata(dev); 85*28a340dbSGuenter Roeck unsigned int reg; 86*28a340dbSGuenter Roeck int ret; 876a027523SEduardo Valentin 883d8f7a89SGuenter Roeck if (time_before(jiffies, tmp102->ready_time)) { 8900917b5cSNishanth Menon dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__); 9000917b5cSNishanth Menon return -EAGAIN; 9100917b5cSNishanth Menon } 9200917b5cSNishanth Menon 93*28a340dbSGuenter Roeck ret = regmap_read(tmp102->regmap, TMP102_TEMP_REG, ®); 94*28a340dbSGuenter Roeck if (ret < 0) 95*28a340dbSGuenter Roeck return ret; 963d8f7a89SGuenter Roeck 97*28a340dbSGuenter Roeck *temp = tmp102_reg_to_mC(reg); 986a027523SEduardo Valentin 996a027523SEduardo Valentin return 0; 1006a027523SEduardo Valentin } 1016a027523SEduardo Valentin 102beb1b6bbSSteven King static ssize_t tmp102_show_temp(struct device *dev, 103beb1b6bbSSteven King struct device_attribute *attr, 104beb1b6bbSSteven King char *buf) 105beb1b6bbSSteven King { 106beb1b6bbSSteven King struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 1073d8f7a89SGuenter Roeck struct tmp102 *tmp102 = dev_get_drvdata(dev); 108*28a340dbSGuenter Roeck int regaddr = sda->index; 109*28a340dbSGuenter Roeck unsigned int reg; 110*28a340dbSGuenter Roeck int err; 111beb1b6bbSSteven King 112*28a340dbSGuenter Roeck if (regaddr == TMP102_TEMP_REG && 113*28a340dbSGuenter Roeck time_before(jiffies, tmp102->ready_time)) 11400917b5cSNishanth Menon return -EAGAIN; 11500917b5cSNishanth Menon 116*28a340dbSGuenter Roeck err = regmap_read(tmp102->regmap, regaddr, ®); 117*28a340dbSGuenter Roeck if (err < 0) 118*28a340dbSGuenter Roeck return err; 1193d8f7a89SGuenter Roeck 120*28a340dbSGuenter Roeck return sprintf(buf, "%d\n", tmp102_reg_to_mC(reg)); 121beb1b6bbSSteven King } 122beb1b6bbSSteven King 123beb1b6bbSSteven King static ssize_t tmp102_set_temp(struct device *dev, 124beb1b6bbSSteven King struct device_attribute *attr, 125beb1b6bbSSteven King const char *buf, size_t count) 126beb1b6bbSSteven King { 127beb1b6bbSSteven King struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 128ad9beea4SGuenter Roeck struct tmp102 *tmp102 = dev_get_drvdata(dev); 129*28a340dbSGuenter Roeck int reg = sda->index; 130beb1b6bbSSteven King long val; 131*28a340dbSGuenter Roeck int err; 132beb1b6bbSSteven King 133179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val) < 0) 134beb1b6bbSSteven King return -EINVAL; 1352a844c14SGuenter Roeck val = clamp_val(val, -256000, 255000); 136cff37c9eSJean Delvare 137*28a340dbSGuenter Roeck err = regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(val)); 138*28a340dbSGuenter Roeck return err ? : count; 139beb1b6bbSSteven King } 140beb1b6bbSSteven King 141*28a340dbSGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL, 142*28a340dbSGuenter Roeck TMP102_TEMP_REG); 143beb1b6bbSSteven King 144beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp, 145*28a340dbSGuenter Roeck tmp102_set_temp, TMP102_TLOW_REG); 146beb1b6bbSSteven King 147beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, 148*28a340dbSGuenter Roeck tmp102_set_temp, TMP102_THIGH_REG); 149beb1b6bbSSteven King 150ad9beea4SGuenter Roeck static struct attribute *tmp102_attrs[] = { 151beb1b6bbSSteven King &sensor_dev_attr_temp1_input.dev_attr.attr, 152beb1b6bbSSteven King &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 153beb1b6bbSSteven King &sensor_dev_attr_temp1_max.dev_attr.attr, 154beb1b6bbSSteven King NULL 155beb1b6bbSSteven King }; 156ad9beea4SGuenter Roeck ATTRIBUTE_GROUPS(tmp102); 157beb1b6bbSSteven King 1582251aef6SEduardo Valentin static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = { 1592251aef6SEduardo Valentin .get_temp = tmp102_read_temp, 1602251aef6SEduardo Valentin }; 1612251aef6SEduardo Valentin 162b17ea1caSGuenter Roeck static void tmp102_restore_config(void *data) 163b17ea1caSGuenter Roeck { 164b17ea1caSGuenter Roeck struct tmp102 *tmp102 = data; 165b17ea1caSGuenter Roeck 166*28a340dbSGuenter Roeck regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig); 167b17ea1caSGuenter Roeck } 168b17ea1caSGuenter Roeck 169*28a340dbSGuenter Roeck static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg) 170*28a340dbSGuenter Roeck { 171*28a340dbSGuenter Roeck return reg != TMP102_TEMP_REG; 172*28a340dbSGuenter Roeck } 173*28a340dbSGuenter Roeck 174*28a340dbSGuenter Roeck static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg) 175*28a340dbSGuenter Roeck { 176*28a340dbSGuenter Roeck return reg == TMP102_TEMP_REG; 177*28a340dbSGuenter Roeck } 178*28a340dbSGuenter Roeck 179*28a340dbSGuenter Roeck static const struct regmap_config tmp102_regmap_config = { 180*28a340dbSGuenter Roeck .reg_bits = 8, 181*28a340dbSGuenter Roeck .val_bits = 16, 182*28a340dbSGuenter Roeck .max_register = TMP102_THIGH_REG, 183*28a340dbSGuenter Roeck .writeable_reg = tmp102_is_writeable_reg, 184*28a340dbSGuenter Roeck .volatile_reg = tmp102_is_volatile_reg, 185*28a340dbSGuenter Roeck .val_format_endian = REGMAP_ENDIAN_BIG, 186*28a340dbSGuenter Roeck .cache_type = REGCACHE_RBTREE, 187*28a340dbSGuenter Roeck .use_single_rw = true, 188*28a340dbSGuenter Roeck }; 189*28a340dbSGuenter Roeck 1906c931ae1SBill Pemberton static int tmp102_probe(struct i2c_client *client, 191beb1b6bbSSteven King const struct i2c_device_id *id) 192beb1b6bbSSteven King { 193fbd9af16SGuenter Roeck struct device *dev = &client->dev; 194ad9beea4SGuenter Roeck struct device *hwmon_dev; 195beb1b6bbSSteven King struct tmp102 *tmp102; 196*28a340dbSGuenter Roeck unsigned int regval; 197*28a340dbSGuenter Roeck int err; 198beb1b6bbSSteven King 199cff37c9eSJean Delvare if (!i2c_check_functionality(client->adapter, 200beb1b6bbSSteven King I2C_FUNC_SMBUS_WORD_DATA)) { 201fbd9af16SGuenter Roeck dev_err(dev, 202b55f3757SGuenter Roeck "adapter doesn't support SMBus word transactions\n"); 203beb1b6bbSSteven King return -ENODEV; 204beb1b6bbSSteven King } 205beb1b6bbSSteven King 206fbd9af16SGuenter Roeck tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL); 207f511a21fSGuenter Roeck if (!tmp102) 208beb1b6bbSSteven King return -ENOMEM; 209f511a21fSGuenter Roeck 210beb1b6bbSSteven King i2c_set_clientdata(client, tmp102); 211beb1b6bbSSteven King 212*28a340dbSGuenter Roeck tmp102->regmap = devm_regmap_init_i2c(client, &tmp102_regmap_config); 213*28a340dbSGuenter Roeck if (IS_ERR(tmp102->regmap)) 214*28a340dbSGuenter Roeck return PTR_ERR(tmp102->regmap); 215*28a340dbSGuenter Roeck 216*28a340dbSGuenter Roeck err = regmap_read(tmp102->regmap, TMP102_CONF_REG, ®val); 217*28a340dbSGuenter Roeck if (err < 0) { 218fbd9af16SGuenter Roeck dev_err(dev, "error reading config register\n"); 219*28a340dbSGuenter Roeck return err; 22038806bdaSJean Delvare } 221a9f92ccfSGuenter Roeck 222*28a340dbSGuenter Roeck if ((regval & ~TMP102_CONFREG_MASK) != 223a9f92ccfSGuenter Roeck (TMP102_CONF_R0 | TMP102_CONF_R1)) { 224a9f92ccfSGuenter Roeck dev_err(dev, "unexpected config register value\n"); 225a9f92ccfSGuenter Roeck return -ENODEV; 226a9f92ccfSGuenter Roeck } 227a9f92ccfSGuenter Roeck 228*28a340dbSGuenter Roeck tmp102->config_orig = regval; 229b17ea1caSGuenter Roeck 230b17ea1caSGuenter Roeck devm_add_action(dev, tmp102_restore_config, tmp102); 231b17ea1caSGuenter Roeck 232*28a340dbSGuenter Roeck regval &= ~TMP102_CONFIG_CLEAR; 233*28a340dbSGuenter Roeck regval |= TMP102_CONFIG_SET; 234a9f92ccfSGuenter Roeck 235*28a340dbSGuenter Roeck err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval); 236*28a340dbSGuenter Roeck if (err < 0) { 237fbd9af16SGuenter Roeck dev_err(dev, "error writing config register\n"); 238*28a340dbSGuenter Roeck return err; 239cff37c9eSJean Delvare } 2403d8f7a89SGuenter Roeck 2413d8f7a89SGuenter Roeck tmp102->ready_time = jiffies; 2423d8f7a89SGuenter Roeck if (tmp102->config_orig & TMP102_CONF_SD) { 2433d8f7a89SGuenter Roeck /* 2443d8f7a89SGuenter Roeck * Mark that we are not ready with data until the first 2453d8f7a89SGuenter Roeck * conversion is complete 2463d8f7a89SGuenter Roeck */ 2473d8f7a89SGuenter Roeck tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS); 2483d8f7a89SGuenter Roeck } 2493d8f7a89SGuenter Roeck 250b17ea1caSGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 251b17ea1caSGuenter Roeck tmp102, 252b17ea1caSGuenter Roeck tmp102_groups); 253ad9beea4SGuenter Roeck if (IS_ERR(hwmon_dev)) { 254ad9beea4SGuenter Roeck dev_dbg(dev, "unable to register hwmon device\n"); 255b17ea1caSGuenter Roeck return PTR_ERR(hwmon_dev); 256beb1b6bbSSteven King } 25751b77fd7SEduardo Valentin devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev, 2582251aef6SEduardo Valentin &tmp102_of_thermal_ops); 2596a027523SEduardo Valentin 260fbd9af16SGuenter Roeck dev_info(dev, "initialized\n"); 261beb1b6bbSSteven King 262beb1b6bbSSteven King return 0; 263beb1b6bbSSteven King } 264beb1b6bbSSteven King 265dd378b1bSGrygorii Strashko #ifdef CONFIG_PM_SLEEP 266beb1b6bbSSteven King static int tmp102_suspend(struct device *dev) 267beb1b6bbSSteven King { 268beb1b6bbSSteven King struct i2c_client *client = to_i2c_client(dev); 269*28a340dbSGuenter Roeck struct tmp102 *tmp102 = i2c_get_clientdata(client); 270beb1b6bbSSteven King 271*28a340dbSGuenter Roeck return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG, 272*28a340dbSGuenter Roeck TMP102_CONF_SD, TMP102_CONF_SD); 273beb1b6bbSSteven King } 274beb1b6bbSSteven King 275beb1b6bbSSteven King static int tmp102_resume(struct device *dev) 276beb1b6bbSSteven King { 277beb1b6bbSSteven King struct i2c_client *client = to_i2c_client(dev); 2783d8f7a89SGuenter Roeck struct tmp102 *tmp102 = i2c_get_clientdata(client); 279*28a340dbSGuenter Roeck int err; 280beb1b6bbSSteven King 281*28a340dbSGuenter Roeck err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG, 282*28a340dbSGuenter Roeck TMP102_CONF_SD, 0); 283beb1b6bbSSteven King 2843d8f7a89SGuenter Roeck tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS); 2853d8f7a89SGuenter Roeck 286*28a340dbSGuenter Roeck return err; 287beb1b6bbSSteven King } 288beb1b6bbSSteven King #endif /* CONFIG_PM */ 289beb1b6bbSSteven King 290dd378b1bSGrygorii Strashko static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume); 291dd378b1bSGrygorii Strashko 292beb1b6bbSSteven King static const struct i2c_device_id tmp102_id[] = { 293cff37c9eSJean Delvare { "tmp102", 0 }, 294beb1b6bbSSteven King { } 295beb1b6bbSSteven King }; 296cff37c9eSJean Delvare MODULE_DEVICE_TABLE(i2c, tmp102_id); 297beb1b6bbSSteven King 298beb1b6bbSSteven King static struct i2c_driver tmp102_driver = { 299beb1b6bbSSteven King .driver.name = DRIVER_NAME, 300dd378b1bSGrygorii Strashko .driver.pm = &tmp102_dev_pm_ops, 301beb1b6bbSSteven King .probe = tmp102_probe, 302beb1b6bbSSteven King .id_table = tmp102_id, 303beb1b6bbSSteven King }; 304beb1b6bbSSteven King 305f0967eeaSAxel Lin module_i2c_driver(tmp102_driver); 306beb1b6bbSSteven King 307beb1b6bbSSteven King MODULE_AUTHOR("Steven King <sfking@fdwdc.com>"); 308beb1b6bbSSteven King MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver"); 309beb1b6bbSSteven King MODULE_LICENSE("GPL"); 310