106776077SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 24e233cbeSAdrien Demarez /* 34e233cbeSAdrien Demarez * LM73 Sensor driver 44e233cbeSAdrien Demarez * Based on LM75 54e233cbeSAdrien Demarez * 64e233cbeSAdrien Demarez * Copyright (C) 2007, CenoSYS (www.cenosys.com). 74e233cbeSAdrien Demarez * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu). 84e233cbeSAdrien Demarez * 94e233cbeSAdrien Demarez * Guillaume Ligneul <guillaume.ligneul@gmail.com> 104e233cbeSAdrien Demarez * Adrien Demarez <adrien.demarez@bolloretelecom.eu> 114e233cbeSAdrien Demarez * Jeremy Laine <jeremy.laine@bolloretelecom.eu> 122bf9233aSChris Verges * Chris Verges <kg4ysn@gmail.com> 134e233cbeSAdrien Demarez */ 144e233cbeSAdrien Demarez 154e233cbeSAdrien Demarez #include <linux/module.h> 164e233cbeSAdrien Demarez #include <linux/init.h> 174e233cbeSAdrien Demarez #include <linux/i2c.h> 184e233cbeSAdrien Demarez #include <linux/hwmon.h> 194e233cbeSAdrien Demarez #include <linux/hwmon-sysfs.h> 204e233cbeSAdrien Demarez #include <linux/err.h> 214e233cbeSAdrien Demarez 224e233cbeSAdrien Demarez 234e233cbeSAdrien Demarez /* Addresses scanned */ 244e233cbeSAdrien Demarez static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 254e233cbeSAdrien Demarez 0x4d, 0x4e, I2C_CLIENT_END }; 264e233cbeSAdrien Demarez 274e233cbeSAdrien Demarez /* LM73 registers */ 284e233cbeSAdrien Demarez #define LM73_REG_INPUT 0x00 294e233cbeSAdrien Demarez #define LM73_REG_CONF 0x01 304e233cbeSAdrien Demarez #define LM73_REG_MAX 0x02 314e233cbeSAdrien Demarez #define LM73_REG_MIN 0x03 324e233cbeSAdrien Demarez #define LM73_REG_CTRL 0x04 334e233cbeSAdrien Demarez #define LM73_REG_ID 0x07 344e233cbeSAdrien Demarez 3590f4102cSJean Delvare #define LM73_ID 0x9001 /* 0x0190, byte-swapped */ 364e233cbeSAdrien Demarez #define DRVNAME "lm73" 3791bba688SGuenter Roeck #define LM73_TEMP_MIN (-256000 / 250) 3891bba688SGuenter Roeck #define LM73_TEMP_MAX (255750 / 250) 394e233cbeSAdrien Demarez 408c14d126SChris Verges #define LM73_CTRL_RES_SHIFT 5 418c14d126SChris Verges #define LM73_CTRL_RES_MASK (BIT(5) | BIT(6)) 428c14d126SChris Verges #define LM73_CTRL_TO_MASK BIT(7) 434e233cbeSAdrien Demarez 442bf9233aSChris Verges #define LM73_CTRL_HI_SHIFT 2 452bf9233aSChris Verges #define LM73_CTRL_LO_SHIFT 1 462bf9233aSChris Verges 478c14d126SChris Verges static const unsigned short lm73_convrates[] = { 488c14d126SChris Verges 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */ 498c14d126SChris Verges 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */ 508c14d126SChris Verges 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */ 518c14d126SChris Verges 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */ 528c14d126SChris Verges }; 538c14d126SChris Verges 548c14d126SChris Verges struct lm73_data { 553710263fSGuenter Roeck struct i2c_client *client; 568c14d126SChris Verges struct mutex lock; 578c14d126SChris Verges u8 ctrl; /* control register value */ 588c14d126SChris Verges }; 598c14d126SChris Verges 608c14d126SChris Verges /*-----------------------------------------------------------------------*/ 614e233cbeSAdrien Demarez 620f875accSGuenter Roeck static ssize_t temp_store(struct device *dev, struct device_attribute *da, 634e233cbeSAdrien Demarez const char *buf, size_t count) 644e233cbeSAdrien Demarez { 654e233cbeSAdrien Demarez struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 663710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 674e233cbeSAdrien Demarez long temp; 684e233cbeSAdrien Demarez short value; 690602934fSChris Verges s32 err; 704e233cbeSAdrien Demarez 71179c4fdbSFrans Meulenbroeks int status = kstrtol(buf, 10, &temp); 724e233cbeSAdrien Demarez if (status < 0) 734e233cbeSAdrien Demarez return status; 744e233cbeSAdrien Demarez 754e233cbeSAdrien Demarez /* Write value */ 7691bba688SGuenter Roeck value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5; 773710263fSGuenter Roeck err = i2c_smbus_write_word_swapped(data->client, attr->index, value); 780602934fSChris Verges return (err < 0) ? err : count; 794e233cbeSAdrien Demarez } 804e233cbeSAdrien Demarez 810f875accSGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *da, 824e233cbeSAdrien Demarez char *buf) 834e233cbeSAdrien Demarez { 844e233cbeSAdrien Demarez struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 853710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 860602934fSChris Verges int temp; 870602934fSChris Verges 883710263fSGuenter Roeck s32 err = i2c_smbus_read_word_swapped(data->client, attr->index); 890602934fSChris Verges if (err < 0) 900602934fSChris Verges return err; 910602934fSChris Verges 924e233cbeSAdrien Demarez /* use integer division instead of equivalent right shift to 934e233cbeSAdrien Demarez guarantee arithmetic shift and preserve the sign */ 940602934fSChris Verges temp = (((s16) err) * 250) / 32; 954e6104b1Sye xingchen return sysfs_emit(buf, "%d\n", temp); 964e233cbeSAdrien Demarez } 974e233cbeSAdrien Demarez 980f875accSGuenter Roeck static ssize_t convrate_store(struct device *dev, struct device_attribute *da, 998c14d126SChris Verges const char *buf, size_t count) 1008c14d126SChris Verges { 1013710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 1028c14d126SChris Verges unsigned long convrate; 1038c14d126SChris Verges s32 err; 1048c14d126SChris Verges int res = 0; 1058c14d126SChris Verges 1068c14d126SChris Verges err = kstrtoul(buf, 10, &convrate); 1078c14d126SChris Verges if (err < 0) 1088c14d126SChris Verges return err; 1098c14d126SChris Verges 1108c14d126SChris Verges /* 1118c14d126SChris Verges * Convert the desired conversion rate into register bits. 1128c14d126SChris Verges * res is already initialized, and everything past the second-to-last 1138c14d126SChris Verges * value in the array is treated as belonging to the last value 1148c14d126SChris Verges * in the array. 1158c14d126SChris Verges */ 1168c14d126SChris Verges while (res < (ARRAY_SIZE(lm73_convrates) - 1) && 1178c14d126SChris Verges convrate > lm73_convrates[res]) 1188c14d126SChris Verges res++; 1198c14d126SChris Verges 1208c14d126SChris Verges mutex_lock(&data->lock); 1218c14d126SChris Verges data->ctrl &= LM73_CTRL_TO_MASK; 1228c14d126SChris Verges data->ctrl |= res << LM73_CTRL_RES_SHIFT; 1233710263fSGuenter Roeck err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL, 1243710263fSGuenter Roeck data->ctrl); 1258c14d126SChris Verges mutex_unlock(&data->lock); 1268c14d126SChris Verges 1278c14d126SChris Verges if (err < 0) 1288c14d126SChris Verges return err; 1298c14d126SChris Verges 1308c14d126SChris Verges return count; 1318c14d126SChris Verges } 1328c14d126SChris Verges 1330f875accSGuenter Roeck static ssize_t convrate_show(struct device *dev, struct device_attribute *da, 1348c14d126SChris Verges char *buf) 1358c14d126SChris Verges { 1363710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 1378c14d126SChris Verges int res; 1388c14d126SChris Verges 1398c14d126SChris Verges res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT; 1404e6104b1Sye xingchen return sysfs_emit(buf, "%hu\n", lm73_convrates[res]); 1418c14d126SChris Verges } 1424e233cbeSAdrien Demarez 1430f875accSGuenter Roeck static ssize_t maxmin_alarm_show(struct device *dev, 1442bf9233aSChris Verges struct device_attribute *da, char *buf) 1452bf9233aSChris Verges { 1462bf9233aSChris Verges struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 1473710263fSGuenter Roeck struct lm73_data *data = dev_get_drvdata(dev); 1482bf9233aSChris Verges s32 ctrl; 1492bf9233aSChris Verges 1502bf9233aSChris Verges mutex_lock(&data->lock); 1513710263fSGuenter Roeck ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL); 1522bf9233aSChris Verges if (ctrl < 0) 1532bf9233aSChris Verges goto abort; 1542bf9233aSChris Verges data->ctrl = ctrl; 1552bf9233aSChris Verges mutex_unlock(&data->lock); 1562bf9233aSChris Verges 1574e6104b1Sye xingchen return sysfs_emit(buf, "%d\n", (ctrl >> attr->index) & 1); 1582bf9233aSChris Verges 1592bf9233aSChris Verges abort: 1602bf9233aSChris Verges mutex_unlock(&data->lock); 1612bf9233aSChris Verges return ctrl; 1622bf9233aSChris Verges } 1632bf9233aSChris Verges 1644e233cbeSAdrien Demarez /*-----------------------------------------------------------------------*/ 1654e233cbeSAdrien Demarez 1664e233cbeSAdrien Demarez /* sysfs attributes for hwmon */ 1674e233cbeSAdrien Demarez 1680f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, LM73_REG_MAX); 1690f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, LM73_REG_MIN); 1700f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, LM73_REG_INPUT); 1710f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(update_interval, convrate, 0); 1720f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, maxmin_alarm, 1730f875accSGuenter Roeck LM73_CTRL_HI_SHIFT); 1740f875accSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, maxmin_alarm, 1750f875accSGuenter Roeck LM73_CTRL_LO_SHIFT); 1764e233cbeSAdrien Demarez 1773710263fSGuenter Roeck static struct attribute *lm73_attrs[] = { 1784e233cbeSAdrien Demarez &sensor_dev_attr_temp1_input.dev_attr.attr, 1794e233cbeSAdrien Demarez &sensor_dev_attr_temp1_max.dev_attr.attr, 1804e233cbeSAdrien Demarez &sensor_dev_attr_temp1_min.dev_attr.attr, 1818c14d126SChris Verges &sensor_dev_attr_update_interval.dev_attr.attr, 1822bf9233aSChris Verges &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 1832bf9233aSChris Verges &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 1844e233cbeSAdrien Demarez NULL 1854e233cbeSAdrien Demarez }; 1863710263fSGuenter Roeck ATTRIBUTE_GROUPS(lm73); 1874e233cbeSAdrien Demarez 1884e233cbeSAdrien Demarez /*-----------------------------------------------------------------------*/ 1894e233cbeSAdrien Demarez 1904e233cbeSAdrien Demarez /* device probe and removal */ 1914e233cbeSAdrien Demarez 1924e233cbeSAdrien Demarez static int 19391ed7c40SStephen Kitt lm73_probe(struct i2c_client *client) 1944e233cbeSAdrien Demarez { 1953710263fSGuenter Roeck struct device *dev = &client->dev; 1963710263fSGuenter Roeck struct device *hwmon_dev; 1978c14d126SChris Verges struct lm73_data *data; 1988c14d126SChris Verges int ctrl; 1998c14d126SChris Verges 2003710263fSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL); 2018c14d126SChris Verges if (!data) 2028c14d126SChris Verges return -ENOMEM; 2038c14d126SChris Verges 2043710263fSGuenter Roeck data->client = client; 2058c14d126SChris Verges mutex_init(&data->lock); 2068c14d126SChris Verges 2078c14d126SChris Verges ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL); 2088c14d126SChris Verges if (ctrl < 0) 2098c14d126SChris Verges return ctrl; 2108c14d126SChris Verges data->ctrl = ctrl; 2114e233cbeSAdrien Demarez 2123710263fSGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 2133710263fSGuenter Roeck data, lm73_groups); 2143710263fSGuenter Roeck if (IS_ERR(hwmon_dev)) 2153710263fSGuenter Roeck return PTR_ERR(hwmon_dev); 2164e233cbeSAdrien Demarez 2173710263fSGuenter Roeck dev_info(dev, "sensor '%s'\n", client->name); 2184e233cbeSAdrien Demarez 2194e233cbeSAdrien Demarez return 0; 2204e233cbeSAdrien Demarez } 2214e233cbeSAdrien Demarez 2224e233cbeSAdrien Demarez static const struct i2c_device_id lm73_ids[] = { 223*d8a66f36SUwe Kleine-König { "lm73" }, 2244e233cbeSAdrien Demarez { /* LIST END */ } 2254e233cbeSAdrien Demarez }; 2264e233cbeSAdrien Demarez MODULE_DEVICE_TABLE(i2c, lm73_ids); 2274e233cbeSAdrien Demarez 2284e233cbeSAdrien Demarez /* Return 0 if detection is successful, -ENODEV otherwise */ 229310ec792SJean Delvare static int lm73_detect(struct i2c_client *new_client, 2304e233cbeSAdrien Demarez struct i2c_board_info *info) 2314e233cbeSAdrien Demarez { 2324e233cbeSAdrien Demarez struct i2c_adapter *adapter = new_client->adapter; 23324d6e2a8SJean Delvare int id, ctrl, conf; 2344e233cbeSAdrien Demarez 2354e233cbeSAdrien Demarez if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 2364e233cbeSAdrien Demarez I2C_FUNC_SMBUS_WORD_DATA)) 2374e233cbeSAdrien Demarez return -ENODEV; 2384e233cbeSAdrien Demarez 23924d6e2a8SJean Delvare /* 24024d6e2a8SJean Delvare * Do as much detection as possible with byte reads first, as word 24124d6e2a8SJean Delvare * reads can confuse other devices. 24224d6e2a8SJean Delvare */ 24324d6e2a8SJean Delvare ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL); 24424d6e2a8SJean Delvare if (ctrl < 0 || (ctrl & 0x10)) 24524d6e2a8SJean Delvare return -ENODEV; 24624d6e2a8SJean Delvare 24724d6e2a8SJean Delvare conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF); 24824d6e2a8SJean Delvare if (conf < 0 || (conf & 0x0c)) 24924d6e2a8SJean Delvare return -ENODEV; 25024d6e2a8SJean Delvare 25124d6e2a8SJean Delvare id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID); 25224d6e2a8SJean Delvare if (id < 0 || id != (LM73_ID & 0xff)) 25324d6e2a8SJean Delvare return -ENODEV; 25424d6e2a8SJean Delvare 2554e233cbeSAdrien Demarez /* Check device ID */ 2564e233cbeSAdrien Demarez id = i2c_smbus_read_word_data(new_client, LM73_REG_ID); 25724d6e2a8SJean Delvare if (id < 0 || id != LM73_ID) 2584e233cbeSAdrien Demarez return -ENODEV; 2594e233cbeSAdrien Demarez 260f2f394dbSWolfram Sang strscpy(info->type, "lm73", I2C_NAME_SIZE); 2614e233cbeSAdrien Demarez 2624e233cbeSAdrien Demarez return 0; 2634e233cbeSAdrien Demarez } 2644e233cbeSAdrien Demarez 2650454e799SHenry Shen static const struct of_device_id lm73_of_match[] = { 2660454e799SHenry Shen { 2670454e799SHenry Shen .compatible = "ti,lm73", 2680454e799SHenry Shen }, 2690454e799SHenry Shen { }, 2700454e799SHenry Shen }; 2710454e799SHenry Shen 2720454e799SHenry Shen MODULE_DEVICE_TABLE(of, lm73_of_match); 2730454e799SHenry Shen 2744e233cbeSAdrien Demarez static struct i2c_driver lm73_driver = { 2754e233cbeSAdrien Demarez .class = I2C_CLASS_HWMON, 2764e233cbeSAdrien Demarez .driver = { 2774e233cbeSAdrien Demarez .name = "lm73", 2780454e799SHenry Shen .of_match_table = lm73_of_match, 2794e233cbeSAdrien Demarez }, 2801975d167SUwe Kleine-König .probe = lm73_probe, 2814e233cbeSAdrien Demarez .id_table = lm73_ids, 2824e233cbeSAdrien Demarez .detect = lm73_detect, 283c3813d6aSJean Delvare .address_list = normal_i2c, 2844e233cbeSAdrien Demarez }; 2854e233cbeSAdrien Demarez 286f0967eeaSAxel Lin module_i2c_driver(lm73_driver); 2874e233cbeSAdrien Demarez 2884e233cbeSAdrien Demarez MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>"); 2894e233cbeSAdrien Demarez MODULE_DESCRIPTION("LM73 driver"); 2904e233cbeSAdrien Demarez MODULE_LICENSE("GPL"); 291