14e233cbeSAdrien Demarez /* 24e233cbeSAdrien Demarez * LM73 Sensor driver 34e233cbeSAdrien Demarez * Based on LM75 44e233cbeSAdrien Demarez * 54e233cbeSAdrien Demarez * Copyright (C) 2007, CenoSYS (www.cenosys.com). 64e233cbeSAdrien Demarez * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu). 74e233cbeSAdrien Demarez * 84e233cbeSAdrien Demarez * Guillaume Ligneul <guillaume.ligneul@gmail.com> 94e233cbeSAdrien Demarez * Adrien Demarez <adrien.demarez@bolloretelecom.eu> 104e233cbeSAdrien Demarez * Jeremy Laine <jeremy.laine@bolloretelecom.eu> 112bf9233aSChris Verges * Chris Verges <kg4ysn@gmail.com> 124e233cbeSAdrien Demarez * 134e233cbeSAdrien Demarez * This software program is licensed subject to the GNU General Public License 144e233cbeSAdrien Demarez * (GPL).Version 2,June 1991, available at 154e233cbeSAdrien Demarez * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 164e233cbeSAdrien Demarez */ 174e233cbeSAdrien Demarez 184e233cbeSAdrien Demarez #include <linux/module.h> 194e233cbeSAdrien Demarez #include <linux/init.h> 204e233cbeSAdrien Demarez #include <linux/i2c.h> 214e233cbeSAdrien Demarez #include <linux/hwmon.h> 224e233cbeSAdrien Demarez #include <linux/hwmon-sysfs.h> 234e233cbeSAdrien Demarez #include <linux/err.h> 244e233cbeSAdrien Demarez 254e233cbeSAdrien Demarez 264e233cbeSAdrien Demarez /* Addresses scanned */ 274e233cbeSAdrien Demarez static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 284e233cbeSAdrien Demarez 0x4d, 0x4e, I2C_CLIENT_END }; 294e233cbeSAdrien Demarez 304e233cbeSAdrien Demarez /* LM73 registers */ 314e233cbeSAdrien Demarez #define LM73_REG_INPUT 0x00 324e233cbeSAdrien Demarez #define LM73_REG_CONF 0x01 334e233cbeSAdrien Demarez #define LM73_REG_MAX 0x02 344e233cbeSAdrien Demarez #define LM73_REG_MIN 0x03 354e233cbeSAdrien Demarez #define LM73_REG_CTRL 0x04 364e233cbeSAdrien Demarez #define LM73_REG_ID 0x07 374e233cbeSAdrien Demarez 3890f4102cSJean Delvare #define LM73_ID 0x9001 /* 0x0190, byte-swapped */ 394e233cbeSAdrien Demarez #define DRVNAME "lm73" 4091bba688SGuenter Roeck #define LM73_TEMP_MIN (-256000 / 250) 4191bba688SGuenter Roeck #define LM73_TEMP_MAX (255750 / 250) 424e233cbeSAdrien Demarez 438c14d126SChris Verges #define LM73_CTRL_RES_SHIFT 5 448c14d126SChris Verges #define LM73_CTRL_RES_MASK (BIT(5) | BIT(6)) 458c14d126SChris Verges #define LM73_CTRL_TO_MASK BIT(7) 464e233cbeSAdrien Demarez 472bf9233aSChris Verges #define LM73_CTRL_HI_SHIFT 2 482bf9233aSChris Verges #define LM73_CTRL_LO_SHIFT 1 492bf9233aSChris Verges 508c14d126SChris Verges static const unsigned short lm73_convrates[] = { 518c14d126SChris Verges 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */ 528c14d126SChris Verges 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */ 538c14d126SChris Verges 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */ 548c14d126SChris Verges 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */ 558c14d126SChris Verges }; 568c14d126SChris Verges 578c14d126SChris Verges struct lm73_data { 583710263fSGuenter Roeck struct i2c_client *client; 598c14d126SChris Verges struct mutex lock; 608c14d126SChris Verges u8 ctrl; /* control register value */ 618c14d126SChris Verges }; 628c14d126SChris Verges 638c14d126SChris Verges /*-----------------------------------------------------------------------*/ 644e233cbeSAdrien Demarez 65*0f875accSGuenter Roeck static ssize_t temp_store(struct device *dev, struct device_attribute *da, 664e233cbeSAdrien Demarez const char *buf, size_t count) 674e233cbeSAdrien Demarez { 684e233cbeSAdrien Demarez struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 693710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 704e233cbeSAdrien Demarez long temp; 714e233cbeSAdrien Demarez short value; 720602934fSChris Verges s32 err; 734e233cbeSAdrien Demarez 74179c4fdbSFrans Meulenbroeks int status = kstrtol(buf, 10, &temp); 754e233cbeSAdrien Demarez if (status < 0) 764e233cbeSAdrien Demarez return status; 774e233cbeSAdrien Demarez 784e233cbeSAdrien Demarez /* Write value */ 7991bba688SGuenter Roeck value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5; 803710263fSGuenter Roeck err = i2c_smbus_write_word_swapped(data->client, attr->index, value); 810602934fSChris Verges return (err < 0) ? err : count; 824e233cbeSAdrien Demarez } 834e233cbeSAdrien Demarez 84*0f875accSGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *da, 854e233cbeSAdrien Demarez char *buf) 864e233cbeSAdrien Demarez { 874e233cbeSAdrien Demarez struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 883710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 890602934fSChris Verges int temp; 900602934fSChris Verges 913710263fSGuenter Roeck s32 err = i2c_smbus_read_word_swapped(data->client, attr->index); 920602934fSChris Verges if (err < 0) 930602934fSChris Verges return err; 940602934fSChris Verges 954e233cbeSAdrien Demarez /* use integer division instead of equivalent right shift to 964e233cbeSAdrien Demarez guarantee arithmetic shift and preserve the sign */ 970602934fSChris Verges temp = (((s16) err) * 250) / 32; 980602934fSChris Verges return scnprintf(buf, PAGE_SIZE, "%d\n", temp); 994e233cbeSAdrien Demarez } 1004e233cbeSAdrien Demarez 101*0f875accSGuenter Roeck static ssize_t convrate_store(struct device *dev, struct device_attribute *da, 1028c14d126SChris Verges const char *buf, size_t count) 1038c14d126SChris Verges { 1043710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 1058c14d126SChris Verges unsigned long convrate; 1068c14d126SChris Verges s32 err; 1078c14d126SChris Verges int res = 0; 1088c14d126SChris Verges 1098c14d126SChris Verges err = kstrtoul(buf, 10, &convrate); 1108c14d126SChris Verges if (err < 0) 1118c14d126SChris Verges return err; 1128c14d126SChris Verges 1138c14d126SChris Verges /* 1148c14d126SChris Verges * Convert the desired conversion rate into register bits. 1158c14d126SChris Verges * res is already initialized, and everything past the second-to-last 1168c14d126SChris Verges * value in the array is treated as belonging to the last value 1178c14d126SChris Verges * in the array. 1188c14d126SChris Verges */ 1198c14d126SChris Verges while (res < (ARRAY_SIZE(lm73_convrates) - 1) && 1208c14d126SChris Verges convrate > lm73_convrates[res]) 1218c14d126SChris Verges res++; 1228c14d126SChris Verges 1238c14d126SChris Verges mutex_lock(&data->lock); 1248c14d126SChris Verges data->ctrl &= LM73_CTRL_TO_MASK; 1258c14d126SChris Verges data->ctrl |= res << LM73_CTRL_RES_SHIFT; 1263710263fSGuenter Roeck err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL, 1273710263fSGuenter Roeck data->ctrl); 1288c14d126SChris Verges mutex_unlock(&data->lock); 1298c14d126SChris Verges 1308c14d126SChris Verges if (err < 0) 1318c14d126SChris Verges return err; 1328c14d126SChris Verges 1338c14d126SChris Verges return count; 1348c14d126SChris Verges } 1358c14d126SChris Verges 136*0f875accSGuenter Roeck static ssize_t convrate_show(struct device *dev, struct device_attribute *da, 1378c14d126SChris Verges char *buf) 1388c14d126SChris Verges { 1393710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 1408c14d126SChris Verges int res; 1418c14d126SChris Verges 1428c14d126SChris Verges res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT; 1438c14d126SChris Verges return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]); 1448c14d126SChris Verges } 1454e233cbeSAdrien Demarez 146*0f875accSGuenter Roeck static ssize_t maxmin_alarm_show(struct device *dev, 1472bf9233aSChris Verges struct device_attribute *da, char *buf) 1482bf9233aSChris Verges { 1492bf9233aSChris Verges struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 1503710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 1512bf9233aSChris Verges s32 ctrl; 1522bf9233aSChris Verges 1532bf9233aSChris Verges mutex_lock(&data->lock); 1543710263fSGuenter Roeck ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL); 1552bf9233aSChris Verges if (ctrl < 0) 1562bf9233aSChris Verges goto abort; 1572bf9233aSChris Verges data->ctrl = ctrl; 1582bf9233aSChris Verges mutex_unlock(&data->lock); 1592bf9233aSChris Verges 1602bf9233aSChris Verges return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1); 1612bf9233aSChris Verges 1622bf9233aSChris Verges abort: 1632bf9233aSChris Verges mutex_unlock(&data->lock); 1642bf9233aSChris Verges return ctrl; 1652bf9233aSChris Verges } 1662bf9233aSChris Verges 1674e233cbeSAdrien Demarez /*-----------------------------------------------------------------------*/ 1684e233cbeSAdrien Demarez 1694e233cbeSAdrien Demarez /* sysfs attributes for hwmon */ 1704e233cbeSAdrien Demarez 171*0f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, LM73_REG_MAX); 172*0f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, LM73_REG_MIN); 173*0f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, LM73_REG_INPUT); 174*0f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(update_interval, convrate, 0); 175*0f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, maxmin_alarm, 176*0f875accSGuenter Roeck LM73_CTRL_HI_SHIFT); 177*0f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, maxmin_alarm, 178*0f875accSGuenter Roeck LM73_CTRL_LO_SHIFT); 1794e233cbeSAdrien Demarez 1803710263fSGuenter Roeck static struct attribute *lm73_attrs[] = { 1814e233cbeSAdrien Demarez &sensor_dev_attr_temp1_input.dev_attr.attr, 1824e233cbeSAdrien Demarez &sensor_dev_attr_temp1_max.dev_attr.attr, 1834e233cbeSAdrien Demarez &sensor_dev_attr_temp1_min.dev_attr.attr, 1848c14d126SChris Verges &sensor_dev_attr_update_interval.dev_attr.attr, 1852bf9233aSChris Verges &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 1862bf9233aSChris Verges &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 1874e233cbeSAdrien Demarez NULL 1884e233cbeSAdrien Demarez }; 1893710263fSGuenter Roeck ATTRIBUTE_GROUPS(lm73); 1904e233cbeSAdrien Demarez 1914e233cbeSAdrien Demarez /*-----------------------------------------------------------------------*/ 1924e233cbeSAdrien Demarez 1934e233cbeSAdrien Demarez /* device probe and removal */ 1944e233cbeSAdrien Demarez 1954e233cbeSAdrien Demarez static int 1964e233cbeSAdrien Demarez lm73_probe(struct i2c_client *client, const struct i2c_device_id *id) 1974e233cbeSAdrien Demarez { 1983710263fSGuenter Roeck struct device *dev = &client->dev; 1993710263fSGuenter Roeck struct device *hwmon_dev; 2008c14d126SChris Verges struct lm73_data *data; 2018c14d126SChris Verges int ctrl; 2028c14d126SChris Verges 2033710263fSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL); 2048c14d126SChris Verges if (!data) 2058c14d126SChris Verges return -ENOMEM; 2068c14d126SChris Verges 2073710263fSGuenter Roeck data->client = client; 2088c14d126SChris Verges mutex_init(&data->lock); 2098c14d126SChris Verges 2108c14d126SChris Verges ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL); 2118c14d126SChris Verges if (ctrl < 0) 2128c14d126SChris Verges return ctrl; 2138c14d126SChris Verges data->ctrl = ctrl; 2144e233cbeSAdrien Demarez 2153710263fSGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 2163710263fSGuenter Roeck data, lm73_groups); 2173710263fSGuenter Roeck if (IS_ERR(hwmon_dev)) 2183710263fSGuenter Roeck return PTR_ERR(hwmon_dev); 2194e233cbeSAdrien Demarez 2203710263fSGuenter Roeck dev_info(dev, "sensor '%s'\n", client->name); 2214e233cbeSAdrien Demarez 2224e233cbeSAdrien Demarez return 0; 2234e233cbeSAdrien Demarez } 2244e233cbeSAdrien Demarez 2254e233cbeSAdrien Demarez static const struct i2c_device_id lm73_ids[] = { 2261f86df49SJean Delvare { "lm73", 0 }, 2274e233cbeSAdrien Demarez { /* LIST END */ } 2284e233cbeSAdrien Demarez }; 2294e233cbeSAdrien Demarez MODULE_DEVICE_TABLE(i2c, lm73_ids); 2304e233cbeSAdrien Demarez 2314e233cbeSAdrien Demarez /* Return 0 if detection is successful, -ENODEV otherwise */ 232310ec792SJean Delvare static int lm73_detect(struct i2c_client *new_client, 2334e233cbeSAdrien Demarez struct i2c_board_info *info) 2344e233cbeSAdrien Demarez { 2354e233cbeSAdrien Demarez struct i2c_adapter *adapter = new_client->adapter; 23624d6e2a8SJean Delvare int id, ctrl, conf; 2374e233cbeSAdrien Demarez 2384e233cbeSAdrien Demarez if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 2394e233cbeSAdrien Demarez I2C_FUNC_SMBUS_WORD_DATA)) 2404e233cbeSAdrien Demarez return -ENODEV; 2414e233cbeSAdrien Demarez 24224d6e2a8SJean Delvare /* 24324d6e2a8SJean Delvare * Do as much detection as possible with byte reads first, as word 24424d6e2a8SJean Delvare * reads can confuse other devices. 24524d6e2a8SJean Delvare */ 24624d6e2a8SJean Delvare ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL); 24724d6e2a8SJean Delvare if (ctrl < 0 || (ctrl & 0x10)) 24824d6e2a8SJean Delvare return -ENODEV; 24924d6e2a8SJean Delvare 25024d6e2a8SJean Delvare conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF); 25124d6e2a8SJean Delvare if (conf < 0 || (conf & 0x0c)) 25224d6e2a8SJean Delvare return -ENODEV; 25324d6e2a8SJean Delvare 25424d6e2a8SJean Delvare id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID); 25524d6e2a8SJean Delvare if (id < 0 || id != (LM73_ID & 0xff)) 25624d6e2a8SJean Delvare return -ENODEV; 25724d6e2a8SJean Delvare 2584e233cbeSAdrien Demarez /* Check device ID */ 2594e233cbeSAdrien Demarez id = i2c_smbus_read_word_data(new_client, LM73_REG_ID); 26024d6e2a8SJean Delvare if (id < 0 || id != LM73_ID) 2614e233cbeSAdrien Demarez return -ENODEV; 2624e233cbeSAdrien Demarez 2634e233cbeSAdrien Demarez strlcpy(info->type, "lm73", I2C_NAME_SIZE); 2644e233cbeSAdrien Demarez 2654e233cbeSAdrien Demarez return 0; 2664e233cbeSAdrien Demarez } 2674e233cbeSAdrien Demarez 2684e233cbeSAdrien Demarez static struct i2c_driver lm73_driver = { 2694e233cbeSAdrien Demarez .class = I2C_CLASS_HWMON, 2704e233cbeSAdrien Demarez .driver = { 2714e233cbeSAdrien Demarez .name = "lm73", 2724e233cbeSAdrien Demarez }, 2734e233cbeSAdrien Demarez .probe = lm73_probe, 2744e233cbeSAdrien Demarez .id_table = lm73_ids, 2754e233cbeSAdrien Demarez .detect = lm73_detect, 276c3813d6aSJean Delvare .address_list = normal_i2c, 2774e233cbeSAdrien Demarez }; 2784e233cbeSAdrien Demarez 279f0967eeaSAxel Lin module_i2c_driver(lm73_driver); 2804e233cbeSAdrien Demarez 2814e233cbeSAdrien Demarez MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>"); 2824e233cbeSAdrien Demarez MODULE_DESCRIPTION("LM73 driver"); 2834e233cbeSAdrien Demarez MODULE_LICENSE("GPL"); 284