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 { 54ad9beea4SGuenter Roeck struct i2c_client *client; 55beb1b6bbSSteven King struct device *hwmon_dev; 566a027523SEduardo Valentin struct thermal_zone_device *tz; 57beb1b6bbSSteven King struct mutex lock; 5838806bdaSJean Delvare u16 config_orig; 59beb1b6bbSSteven King unsigned long last_update; 60beb1b6bbSSteven King int temp[3]; 61beb1b6bbSSteven King }; 62beb1b6bbSSteven King 63cff37c9eSJean Delvare /* convert left adjusted 13-bit TMP102 register value to milliCelsius */ 64cff37c9eSJean Delvare static inline int tmp102_reg_to_mC(s16 val) 65beb1b6bbSSteven King { 66cff37c9eSJean Delvare return ((val & ~0x01) * 1000) / 128; 67beb1b6bbSSteven King } 68beb1b6bbSSteven King 69cff37c9eSJean Delvare /* convert milliCelsius to left adjusted 13-bit TMP102 register value */ 70cff37c9eSJean Delvare static inline u16 tmp102_mC_to_reg(int val) 71beb1b6bbSSteven King { 72beb1b6bbSSteven King return (val * 128) / 1000; 73beb1b6bbSSteven King } 74beb1b6bbSSteven King 75beb1b6bbSSteven King static const u8 tmp102_reg[] = { 76beb1b6bbSSteven King TMP102_TEMP_REG, 77beb1b6bbSSteven King TMP102_TLOW_REG, 78beb1b6bbSSteven King TMP102_THIGH_REG, 79beb1b6bbSSteven King }; 80beb1b6bbSSteven King 81ad9beea4SGuenter Roeck static struct tmp102 *tmp102_update_device(struct device *dev) 82beb1b6bbSSteven King { 83ad9beea4SGuenter Roeck struct tmp102 *tmp102 = dev_get_drvdata(dev); 84ad9beea4SGuenter Roeck struct i2c_client *client = tmp102->client; 85beb1b6bbSSteven King 86beb1b6bbSSteven King mutex_lock(&tmp102->lock); 87cff37c9eSJean Delvare if (time_after(jiffies, tmp102->last_update + HZ / 3)) { 88beb1b6bbSSteven King int i; 89beb1b6bbSSteven King for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) { 9090f4102cSJean Delvare int status = i2c_smbus_read_word_swapped(client, 9190f4102cSJean Delvare tmp102_reg[i]); 92beb1b6bbSSteven King if (status > -1) 93beb1b6bbSSteven King tmp102->temp[i] = tmp102_reg_to_mC(status); 94beb1b6bbSSteven King } 95beb1b6bbSSteven King tmp102->last_update = jiffies; 96beb1b6bbSSteven King } 97beb1b6bbSSteven King mutex_unlock(&tmp102->lock); 98beb1b6bbSSteven King return tmp102; 99beb1b6bbSSteven King } 100beb1b6bbSSteven King 1016a027523SEduardo Valentin static int tmp102_read_temp(void *dev, long *temp) 1026a027523SEduardo Valentin { 103ad9beea4SGuenter Roeck struct tmp102 *tmp102 = tmp102_update_device(dev); 1046a027523SEduardo Valentin 1056a027523SEduardo Valentin *temp = tmp102->temp[0]; 1066a027523SEduardo Valentin 1076a027523SEduardo Valentin return 0; 1086a027523SEduardo Valentin } 1096a027523SEduardo Valentin 110beb1b6bbSSteven King static ssize_t tmp102_show_temp(struct device *dev, 111beb1b6bbSSteven King struct device_attribute *attr, 112beb1b6bbSSteven King char *buf) 113beb1b6bbSSteven King { 114beb1b6bbSSteven King struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 115ad9beea4SGuenter Roeck struct tmp102 *tmp102 = tmp102_update_device(dev); 116beb1b6bbSSteven King 117beb1b6bbSSteven King return sprintf(buf, "%d\n", tmp102->temp[sda->index]); 118beb1b6bbSSteven King } 119beb1b6bbSSteven King 120beb1b6bbSSteven King static ssize_t tmp102_set_temp(struct device *dev, 121beb1b6bbSSteven King struct device_attribute *attr, 122beb1b6bbSSteven King const char *buf, size_t count) 123beb1b6bbSSteven King { 124beb1b6bbSSteven King struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 125ad9beea4SGuenter Roeck struct tmp102 *tmp102 = dev_get_drvdata(dev); 126ad9beea4SGuenter Roeck struct i2c_client *client = tmp102->client; 127beb1b6bbSSteven King long val; 128cff37c9eSJean Delvare int status; 129beb1b6bbSSteven King 130179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val) < 0) 131beb1b6bbSSteven King return -EINVAL; 1322a844c14SGuenter Roeck val = clamp_val(val, -256000, 255000); 133cff37c9eSJean Delvare 134beb1b6bbSSteven King mutex_lock(&tmp102->lock); 135beb1b6bbSSteven King tmp102->temp[sda->index] = val; 13690f4102cSJean Delvare status = i2c_smbus_write_word_swapped(client, tmp102_reg[sda->index], 137beb1b6bbSSteven King tmp102_mC_to_reg(val)); 138beb1b6bbSSteven King mutex_unlock(&tmp102->lock); 139beb1b6bbSSteven King return status ? : count; 140beb1b6bbSSteven King } 141beb1b6bbSSteven King 142beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0); 143beb1b6bbSSteven King 144beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp, 145beb1b6bbSSteven King tmp102_set_temp, 1); 146beb1b6bbSSteven King 147beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, 148beb1b6bbSSteven King tmp102_set_temp, 2); 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 158beb1b6bbSSteven King #define TMP102_CONFIG (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1) 159beb1b6bbSSteven King #define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL) 160beb1b6bbSSteven King 161*2251aef6SEduardo Valentin static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = { 162*2251aef6SEduardo Valentin .get_temp = tmp102_read_temp, 163*2251aef6SEduardo Valentin }; 164*2251aef6SEduardo Valentin 1656c931ae1SBill Pemberton static int tmp102_probe(struct i2c_client *client, 166beb1b6bbSSteven King const struct i2c_device_id *id) 167beb1b6bbSSteven King { 168fbd9af16SGuenter Roeck struct device *dev = &client->dev; 169ad9beea4SGuenter Roeck struct device *hwmon_dev; 170beb1b6bbSSteven King struct tmp102 *tmp102; 171beb1b6bbSSteven King int status; 172beb1b6bbSSteven King 173cff37c9eSJean Delvare if (!i2c_check_functionality(client->adapter, 174beb1b6bbSSteven King I2C_FUNC_SMBUS_WORD_DATA)) { 175fbd9af16SGuenter Roeck dev_err(dev, 176b55f3757SGuenter Roeck "adapter doesn't support SMBus word transactions\n"); 177beb1b6bbSSteven King return -ENODEV; 178beb1b6bbSSteven King } 179beb1b6bbSSteven King 180fbd9af16SGuenter Roeck tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL); 181f511a21fSGuenter Roeck if (!tmp102) 182beb1b6bbSSteven King return -ENOMEM; 183f511a21fSGuenter Roeck 184beb1b6bbSSteven King i2c_set_clientdata(client, tmp102); 185ad9beea4SGuenter Roeck tmp102->client = client; 186beb1b6bbSSteven King 18790f4102cSJean Delvare status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 18838806bdaSJean Delvare if (status < 0) { 189fbd9af16SGuenter Roeck dev_err(dev, "error reading config register\n"); 190f511a21fSGuenter Roeck return status; 19138806bdaSJean Delvare } 19238806bdaSJean Delvare tmp102->config_orig = status; 19390f4102cSJean Delvare status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, 19490f4102cSJean Delvare TMP102_CONFIG); 195cff37c9eSJean Delvare if (status < 0) { 196fbd9af16SGuenter Roeck dev_err(dev, "error writing config register\n"); 19738806bdaSJean Delvare goto fail_restore_config; 198cff37c9eSJean Delvare } 19990f4102cSJean Delvare status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 200beb1b6bbSSteven King if (status < 0) { 201fbd9af16SGuenter Roeck dev_err(dev, "error reading config register\n"); 20238806bdaSJean Delvare goto fail_restore_config; 203beb1b6bbSSteven King } 204beb1b6bbSSteven King status &= ~TMP102_CONFIG_RD_ONLY; 205beb1b6bbSSteven King if (status != TMP102_CONFIG) { 206fbd9af16SGuenter Roeck dev_err(dev, "config settings did not stick\n"); 207cff37c9eSJean Delvare status = -ENODEV; 20838806bdaSJean Delvare goto fail_restore_config; 209beb1b6bbSSteven King } 210beb1b6bbSSteven King tmp102->last_update = jiffies - HZ; 211beb1b6bbSSteven King mutex_init(&tmp102->lock); 212beb1b6bbSSteven King 213ad9beea4SGuenter Roeck hwmon_dev = hwmon_device_register_with_groups(dev, client->name, 214ad9beea4SGuenter Roeck tmp102, tmp102_groups); 215ad9beea4SGuenter Roeck if (IS_ERR(hwmon_dev)) { 216ad9beea4SGuenter Roeck dev_dbg(dev, "unable to register hwmon device\n"); 217ad9beea4SGuenter Roeck status = PTR_ERR(hwmon_dev); 21838806bdaSJean Delvare goto fail_restore_config; 219beb1b6bbSSteven King } 220ad9beea4SGuenter Roeck tmp102->hwmon_dev = hwmon_dev; 221ad9beea4SGuenter Roeck tmp102->tz = thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev, 222*2251aef6SEduardo Valentin &tmp102_of_thermal_ops); 2236a027523SEduardo Valentin if (IS_ERR(tmp102->tz)) 2246a027523SEduardo Valentin tmp102->tz = NULL; 2256a027523SEduardo Valentin 226fbd9af16SGuenter Roeck dev_info(dev, "initialized\n"); 227beb1b6bbSSteven King 228beb1b6bbSSteven King return 0; 22938806bdaSJean Delvare 23038806bdaSJean Delvare fail_restore_config: 23190f4102cSJean Delvare i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, 23290f4102cSJean Delvare tmp102->config_orig); 233cff37c9eSJean Delvare return status; 234beb1b6bbSSteven King } 235beb1b6bbSSteven King 236281dfd0bSBill Pemberton static int tmp102_remove(struct i2c_client *client) 237beb1b6bbSSteven King { 238beb1b6bbSSteven King struct tmp102 *tmp102 = i2c_get_clientdata(client); 239beb1b6bbSSteven King 240ad9beea4SGuenter Roeck thermal_zone_of_sensor_unregister(tmp102->hwmon_dev, tmp102->tz); 241beb1b6bbSSteven King hwmon_device_unregister(tmp102->hwmon_dev); 24238806bdaSJean Delvare 24338806bdaSJean Delvare /* Stop monitoring if device was stopped originally */ 24438806bdaSJean Delvare if (tmp102->config_orig & TMP102_CONF_SD) { 24538806bdaSJean Delvare int config; 24638806bdaSJean Delvare 24790f4102cSJean Delvare config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 24838806bdaSJean Delvare if (config >= 0) 24990f4102cSJean Delvare i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, 25038806bdaSJean Delvare config | TMP102_CONF_SD); 25138806bdaSJean Delvare } 25238806bdaSJean Delvare 253beb1b6bbSSteven King return 0; 254beb1b6bbSSteven King } 255beb1b6bbSSteven King 256beb1b6bbSSteven King #ifdef CONFIG_PM 257beb1b6bbSSteven King static int tmp102_suspend(struct device *dev) 258beb1b6bbSSteven King { 259beb1b6bbSSteven King struct i2c_client *client = to_i2c_client(dev); 2608d4dee98SJean Delvare int config; 261beb1b6bbSSteven King 26290f4102cSJean Delvare config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 2638d4dee98SJean Delvare if (config < 0) 2648d4dee98SJean Delvare return config; 265beb1b6bbSSteven King 2668d4dee98SJean Delvare config |= TMP102_CONF_SD; 26790f4102cSJean Delvare return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config); 268beb1b6bbSSteven King } 269beb1b6bbSSteven King 270beb1b6bbSSteven King static int tmp102_resume(struct device *dev) 271beb1b6bbSSteven King { 272beb1b6bbSSteven King struct i2c_client *client = to_i2c_client(dev); 2738d4dee98SJean Delvare int config; 274beb1b6bbSSteven King 27590f4102cSJean Delvare config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 2768d4dee98SJean Delvare if (config < 0) 2778d4dee98SJean Delvare return config; 278beb1b6bbSSteven King 2798d4dee98SJean Delvare config &= ~TMP102_CONF_SD; 28090f4102cSJean Delvare return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config); 281beb1b6bbSSteven King } 282beb1b6bbSSteven King 283beb1b6bbSSteven King static const struct dev_pm_ops tmp102_dev_pm_ops = { 284beb1b6bbSSteven King .suspend = tmp102_suspend, 285beb1b6bbSSteven King .resume = tmp102_resume, 286beb1b6bbSSteven King }; 287beb1b6bbSSteven King 288beb1b6bbSSteven King #define TMP102_DEV_PM_OPS (&tmp102_dev_pm_ops) 289beb1b6bbSSteven King #else 290beb1b6bbSSteven King #define TMP102_DEV_PM_OPS NULL 291beb1b6bbSSteven King #endif /* CONFIG_PM */ 292beb1b6bbSSteven King 293beb1b6bbSSteven King static const struct i2c_device_id tmp102_id[] = { 294cff37c9eSJean Delvare { "tmp102", 0 }, 295beb1b6bbSSteven King { } 296beb1b6bbSSteven King }; 297cff37c9eSJean Delvare MODULE_DEVICE_TABLE(i2c, tmp102_id); 298beb1b6bbSSteven King 299beb1b6bbSSteven King static struct i2c_driver tmp102_driver = { 300beb1b6bbSSteven King .driver.name = DRIVER_NAME, 301beb1b6bbSSteven King .driver.pm = TMP102_DEV_PM_OPS, 302beb1b6bbSSteven King .probe = tmp102_probe, 3039e5e9b7aSBill Pemberton .remove = tmp102_remove, 304beb1b6bbSSteven King .id_table = tmp102_id, 305beb1b6bbSSteven King }; 306beb1b6bbSSteven King 307f0967eeaSAxel Lin module_i2c_driver(tmp102_driver); 308beb1b6bbSSteven King 309beb1b6bbSSteven King MODULE_AUTHOR("Steven King <sfking@fdwdc.com>"); 310beb1b6bbSSteven King MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver"); 311beb1b6bbSSteven King MODULE_LICENSE("GPL"); 312