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 * You should have received a copy of the GNU General Public License 16beb1b6bbSSteven King * along with this program; if not, write to the Free Software 17beb1b6bbSSteven King * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA 18beb1b6bbSSteven King */ 19beb1b6bbSSteven King 20beb1b6bbSSteven King #include <linux/module.h> 21beb1b6bbSSteven King #include <linux/init.h> 22beb1b6bbSSteven King #include <linux/slab.h> 23beb1b6bbSSteven King #include <linux/i2c.h> 24beb1b6bbSSteven King #include <linux/hwmon.h> 25beb1b6bbSSteven King #include <linux/hwmon-sysfs.h> 26beb1b6bbSSteven King #include <linux/err.h> 27beb1b6bbSSteven King #include <linux/mutex.h> 28cff37c9eSJean Delvare #include <linux/device.h> 29dcd8f392SJean Delvare #include <linux/jiffies.h> 306a027523SEduardo Valentin #include <linux/thermal.h> 316a027523SEduardo Valentin #include <linux/of.h> 32beb1b6bbSSteven King 33beb1b6bbSSteven King #define DRIVER_NAME "tmp102" 34beb1b6bbSSteven King 35beb1b6bbSSteven King #define TMP102_TEMP_REG 0x00 36beb1b6bbSSteven King #define TMP102_CONF_REG 0x01 37beb1b6bbSSteven King /* note: these bit definitions are byte swapped */ 38beb1b6bbSSteven King #define TMP102_CONF_SD 0x0100 39beb1b6bbSSteven King #define TMP102_CONF_TM 0x0200 40beb1b6bbSSteven King #define TMP102_CONF_POL 0x0400 41beb1b6bbSSteven King #define TMP102_CONF_F0 0x0800 42beb1b6bbSSteven King #define TMP102_CONF_F1 0x1000 43beb1b6bbSSteven King #define TMP102_CONF_R0 0x2000 44beb1b6bbSSteven King #define TMP102_CONF_R1 0x4000 45beb1b6bbSSteven King #define TMP102_CONF_OS 0x8000 46beb1b6bbSSteven King #define TMP102_CONF_EM 0x0010 47beb1b6bbSSteven King #define TMP102_CONF_AL 0x0020 48beb1b6bbSSteven King #define TMP102_CONF_CR0 0x0040 49beb1b6bbSSteven King #define TMP102_CONF_CR1 0x0080 50beb1b6bbSSteven King #define TMP102_TLOW_REG 0x02 51beb1b6bbSSteven King #define TMP102_THIGH_REG 0x03 52beb1b6bbSSteven King 53beb1b6bbSSteven King struct tmp102 { 54beb1b6bbSSteven King struct device *hwmon_dev; 556a027523SEduardo Valentin struct thermal_zone_device *tz; 56beb1b6bbSSteven King struct mutex lock; 5738806bdaSJean Delvare u16 config_orig; 58beb1b6bbSSteven King unsigned long last_update; 59beb1b6bbSSteven King int temp[3]; 60beb1b6bbSSteven King }; 61beb1b6bbSSteven King 62cff37c9eSJean Delvare /* convert left adjusted 13-bit TMP102 register value to milliCelsius */ 63cff37c9eSJean Delvare static inline int tmp102_reg_to_mC(s16 val) 64beb1b6bbSSteven King { 65cff37c9eSJean Delvare return ((val & ~0x01) * 1000) / 128; 66beb1b6bbSSteven King } 67beb1b6bbSSteven King 68cff37c9eSJean Delvare /* convert milliCelsius to left adjusted 13-bit TMP102 register value */ 69cff37c9eSJean Delvare static inline u16 tmp102_mC_to_reg(int val) 70beb1b6bbSSteven King { 71beb1b6bbSSteven King return (val * 128) / 1000; 72beb1b6bbSSteven King } 73beb1b6bbSSteven King 74beb1b6bbSSteven King static const u8 tmp102_reg[] = { 75beb1b6bbSSteven King TMP102_TEMP_REG, 76beb1b6bbSSteven King TMP102_TLOW_REG, 77beb1b6bbSSteven King TMP102_THIGH_REG, 78beb1b6bbSSteven King }; 79beb1b6bbSSteven King 80beb1b6bbSSteven King static struct tmp102 *tmp102_update_device(struct i2c_client *client) 81beb1b6bbSSteven King { 82beb1b6bbSSteven King struct tmp102 *tmp102 = i2c_get_clientdata(client); 83beb1b6bbSSteven King 84beb1b6bbSSteven King mutex_lock(&tmp102->lock); 85cff37c9eSJean Delvare if (time_after(jiffies, tmp102->last_update + HZ / 3)) { 86beb1b6bbSSteven King int i; 87beb1b6bbSSteven King for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) { 8890f4102cSJean Delvare int status = i2c_smbus_read_word_swapped(client, 8990f4102cSJean Delvare tmp102_reg[i]); 90beb1b6bbSSteven King if (status > -1) 91beb1b6bbSSteven King tmp102->temp[i] = tmp102_reg_to_mC(status); 92beb1b6bbSSteven King } 93beb1b6bbSSteven King tmp102->last_update = jiffies; 94beb1b6bbSSteven King } 95beb1b6bbSSteven King mutex_unlock(&tmp102->lock); 96beb1b6bbSSteven King return tmp102; 97beb1b6bbSSteven King } 98beb1b6bbSSteven King 996a027523SEduardo Valentin static int tmp102_read_temp(void *dev, long *temp) 1006a027523SEduardo Valentin { 1016a027523SEduardo Valentin struct tmp102 *tmp102 = tmp102_update_device(to_i2c_client(dev)); 1026a027523SEduardo Valentin 1036a027523SEduardo Valentin *temp = tmp102->temp[0]; 1046a027523SEduardo Valentin 1056a027523SEduardo Valentin return 0; 1066a027523SEduardo Valentin } 1076a027523SEduardo Valentin 108beb1b6bbSSteven King static ssize_t tmp102_show_temp(struct device *dev, 109beb1b6bbSSteven King struct device_attribute *attr, 110beb1b6bbSSteven King char *buf) 111beb1b6bbSSteven King { 112beb1b6bbSSteven King struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 113beb1b6bbSSteven King struct tmp102 *tmp102 = tmp102_update_device(to_i2c_client(dev)); 114beb1b6bbSSteven King 115beb1b6bbSSteven King return sprintf(buf, "%d\n", tmp102->temp[sda->index]); 116beb1b6bbSSteven King } 117beb1b6bbSSteven King 118beb1b6bbSSteven King static ssize_t tmp102_set_temp(struct device *dev, 119beb1b6bbSSteven King struct device_attribute *attr, 120beb1b6bbSSteven King const char *buf, size_t count) 121beb1b6bbSSteven King { 122beb1b6bbSSteven King struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 123beb1b6bbSSteven King struct i2c_client *client = to_i2c_client(dev); 124beb1b6bbSSteven King struct tmp102 *tmp102 = i2c_get_clientdata(client); 125beb1b6bbSSteven King long val; 126cff37c9eSJean Delvare int status; 127beb1b6bbSSteven King 128179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val) < 0) 129beb1b6bbSSteven King return -EINVAL; 1302a844c14SGuenter Roeck val = clamp_val(val, -256000, 255000); 131cff37c9eSJean Delvare 132beb1b6bbSSteven King mutex_lock(&tmp102->lock); 133beb1b6bbSSteven King tmp102->temp[sda->index] = val; 13490f4102cSJean Delvare status = i2c_smbus_write_word_swapped(client, tmp102_reg[sda->index], 135beb1b6bbSSteven King tmp102_mC_to_reg(val)); 136beb1b6bbSSteven King mutex_unlock(&tmp102->lock); 137beb1b6bbSSteven King return status ? : count; 138beb1b6bbSSteven King } 139beb1b6bbSSteven King 140beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0); 141beb1b6bbSSteven King 142beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp, 143beb1b6bbSSteven King tmp102_set_temp, 1); 144beb1b6bbSSteven King 145beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, 146beb1b6bbSSteven King tmp102_set_temp, 2); 147beb1b6bbSSteven King 148beb1b6bbSSteven King static struct attribute *tmp102_attributes[] = { 149beb1b6bbSSteven King &sensor_dev_attr_temp1_input.dev_attr.attr, 150beb1b6bbSSteven King &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 151beb1b6bbSSteven King &sensor_dev_attr_temp1_max.dev_attr.attr, 152beb1b6bbSSteven King NULL 153beb1b6bbSSteven King }; 154beb1b6bbSSteven King 155beb1b6bbSSteven King static const struct attribute_group tmp102_attr_group = { 156beb1b6bbSSteven King .attrs = tmp102_attributes, 157beb1b6bbSSteven King }; 158beb1b6bbSSteven King 159beb1b6bbSSteven King #define TMP102_CONFIG (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1) 160beb1b6bbSSteven King #define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL) 161beb1b6bbSSteven King 1626c931ae1SBill Pemberton static int tmp102_probe(struct i2c_client *client, 163beb1b6bbSSteven King const struct i2c_device_id *id) 164beb1b6bbSSteven King { 165*fbd9af16SGuenter Roeck struct device *dev = &client->dev; 166beb1b6bbSSteven King struct tmp102 *tmp102; 167beb1b6bbSSteven King int status; 168beb1b6bbSSteven King 169cff37c9eSJean Delvare if (!i2c_check_functionality(client->adapter, 170beb1b6bbSSteven King I2C_FUNC_SMBUS_WORD_DATA)) { 171*fbd9af16SGuenter Roeck dev_err(dev, 172b55f3757SGuenter Roeck "adapter doesn't support SMBus word transactions\n"); 173beb1b6bbSSteven King return -ENODEV; 174beb1b6bbSSteven King } 175beb1b6bbSSteven King 176*fbd9af16SGuenter Roeck tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL); 177f511a21fSGuenter Roeck if (!tmp102) 178beb1b6bbSSteven King return -ENOMEM; 179f511a21fSGuenter Roeck 180beb1b6bbSSteven King i2c_set_clientdata(client, tmp102); 181beb1b6bbSSteven King 18290f4102cSJean Delvare status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 18338806bdaSJean Delvare if (status < 0) { 184*fbd9af16SGuenter Roeck dev_err(dev, "error reading config register\n"); 185f511a21fSGuenter Roeck return status; 18638806bdaSJean Delvare } 18738806bdaSJean Delvare tmp102->config_orig = status; 18890f4102cSJean Delvare status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, 18990f4102cSJean Delvare TMP102_CONFIG); 190cff37c9eSJean Delvare if (status < 0) { 191*fbd9af16SGuenter Roeck dev_err(dev, "error writing config register\n"); 19238806bdaSJean Delvare goto fail_restore_config; 193cff37c9eSJean Delvare } 19490f4102cSJean Delvare status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 195beb1b6bbSSteven King if (status < 0) { 196*fbd9af16SGuenter Roeck dev_err(dev, "error reading config register\n"); 19738806bdaSJean Delvare goto fail_restore_config; 198beb1b6bbSSteven King } 199beb1b6bbSSteven King status &= ~TMP102_CONFIG_RD_ONLY; 200beb1b6bbSSteven King if (status != TMP102_CONFIG) { 201*fbd9af16SGuenter Roeck dev_err(dev, "config settings did not stick\n"); 202cff37c9eSJean Delvare status = -ENODEV; 20338806bdaSJean Delvare goto fail_restore_config; 204beb1b6bbSSteven King } 205beb1b6bbSSteven King tmp102->last_update = jiffies - HZ; 206beb1b6bbSSteven King mutex_init(&tmp102->lock); 207beb1b6bbSSteven King 208*fbd9af16SGuenter Roeck status = sysfs_create_group(&dev->kobj, &tmp102_attr_group); 209beb1b6bbSSteven King if (status) { 210*fbd9af16SGuenter Roeck dev_dbg(dev, "could not create sysfs files\n"); 21138806bdaSJean Delvare goto fail_restore_config; 212beb1b6bbSSteven King } 213*fbd9af16SGuenter Roeck tmp102->hwmon_dev = hwmon_device_register(dev); 214beb1b6bbSSteven King if (IS_ERR(tmp102->hwmon_dev)) { 215*fbd9af16SGuenter Roeck dev_dbg(dev, "unable to register hwmon device\n"); 216beb1b6bbSSteven King status = PTR_ERR(tmp102->hwmon_dev); 21738806bdaSJean Delvare goto fail_remove_sysfs; 218beb1b6bbSSteven King } 219beb1b6bbSSteven King 220*fbd9af16SGuenter Roeck tmp102->tz = thermal_zone_of_sensor_register(dev, 0, dev, 2216a027523SEduardo Valentin tmp102_read_temp, NULL); 2226a027523SEduardo Valentin if (IS_ERR(tmp102->tz)) 2236a027523SEduardo Valentin tmp102->tz = NULL; 2246a027523SEduardo Valentin 225*fbd9af16SGuenter Roeck dev_info(dev, "initialized\n"); 226beb1b6bbSSteven King 227beb1b6bbSSteven King return 0; 22838806bdaSJean Delvare 22938806bdaSJean Delvare fail_remove_sysfs: 230*fbd9af16SGuenter Roeck sysfs_remove_group(&dev->kobj, &tmp102_attr_group); 23138806bdaSJean Delvare fail_restore_config: 23290f4102cSJean Delvare i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, 23390f4102cSJean Delvare tmp102->config_orig); 234cff37c9eSJean Delvare return status; 235beb1b6bbSSteven King } 236beb1b6bbSSteven King 237281dfd0bSBill Pemberton static int tmp102_remove(struct i2c_client *client) 238beb1b6bbSSteven King { 239beb1b6bbSSteven King struct tmp102 *tmp102 = i2c_get_clientdata(client); 240beb1b6bbSSteven King 2416a027523SEduardo Valentin thermal_zone_of_sensor_unregister(&client->dev, tmp102->tz); 242beb1b6bbSSteven King hwmon_device_unregister(tmp102->hwmon_dev); 243beb1b6bbSSteven King sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group); 24438806bdaSJean Delvare 24538806bdaSJean Delvare /* Stop monitoring if device was stopped originally */ 24638806bdaSJean Delvare if (tmp102->config_orig & TMP102_CONF_SD) { 24738806bdaSJean Delvare int config; 24838806bdaSJean Delvare 24990f4102cSJean Delvare config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 25038806bdaSJean Delvare if (config >= 0) 25190f4102cSJean Delvare i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, 25238806bdaSJean Delvare config | TMP102_CONF_SD); 25338806bdaSJean Delvare } 25438806bdaSJean Delvare 255beb1b6bbSSteven King return 0; 256beb1b6bbSSteven King } 257beb1b6bbSSteven King 258beb1b6bbSSteven King #ifdef CONFIG_PM 259beb1b6bbSSteven King static int tmp102_suspend(struct device *dev) 260beb1b6bbSSteven King { 261beb1b6bbSSteven King struct i2c_client *client = to_i2c_client(dev); 2628d4dee98SJean Delvare int config; 263beb1b6bbSSteven King 26490f4102cSJean Delvare config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 2658d4dee98SJean Delvare if (config < 0) 2668d4dee98SJean Delvare return config; 267beb1b6bbSSteven King 2688d4dee98SJean Delvare config |= TMP102_CONF_SD; 26990f4102cSJean Delvare return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config); 270beb1b6bbSSteven King } 271beb1b6bbSSteven King 272beb1b6bbSSteven King static int tmp102_resume(struct device *dev) 273beb1b6bbSSteven King { 274beb1b6bbSSteven King struct i2c_client *client = to_i2c_client(dev); 2758d4dee98SJean Delvare int config; 276beb1b6bbSSteven King 27790f4102cSJean Delvare config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 2788d4dee98SJean Delvare if (config < 0) 2798d4dee98SJean Delvare return config; 280beb1b6bbSSteven King 2818d4dee98SJean Delvare config &= ~TMP102_CONF_SD; 28290f4102cSJean Delvare return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config); 283beb1b6bbSSteven King } 284beb1b6bbSSteven King 285beb1b6bbSSteven King static const struct dev_pm_ops tmp102_dev_pm_ops = { 286beb1b6bbSSteven King .suspend = tmp102_suspend, 287beb1b6bbSSteven King .resume = tmp102_resume, 288beb1b6bbSSteven King }; 289beb1b6bbSSteven King 290beb1b6bbSSteven King #define TMP102_DEV_PM_OPS (&tmp102_dev_pm_ops) 291beb1b6bbSSteven King #else 292beb1b6bbSSteven King #define TMP102_DEV_PM_OPS NULL 293beb1b6bbSSteven King #endif /* CONFIG_PM */ 294beb1b6bbSSteven King 295beb1b6bbSSteven King static const struct i2c_device_id tmp102_id[] = { 296cff37c9eSJean Delvare { "tmp102", 0 }, 297beb1b6bbSSteven King { } 298beb1b6bbSSteven King }; 299cff37c9eSJean Delvare MODULE_DEVICE_TABLE(i2c, tmp102_id); 300beb1b6bbSSteven King 301beb1b6bbSSteven King static struct i2c_driver tmp102_driver = { 302beb1b6bbSSteven King .driver.name = DRIVER_NAME, 303beb1b6bbSSteven King .driver.pm = TMP102_DEV_PM_OPS, 304beb1b6bbSSteven King .probe = tmp102_probe, 3059e5e9b7aSBill Pemberton .remove = tmp102_remove, 306beb1b6bbSSteven King .id_table = tmp102_id, 307beb1b6bbSSteven King }; 308beb1b6bbSSteven King 309f0967eeaSAxel Lin module_i2c_driver(tmp102_driver); 310beb1b6bbSSteven King 311beb1b6bbSSteven King MODULE_AUTHOR("Steven King <sfking@fdwdc.com>"); 312beb1b6bbSSteven King MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver"); 313beb1b6bbSSteven King MODULE_LICENSE("GPL"); 314