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> 114e233cbeSAdrien Demarez * 124e233cbeSAdrien Demarez * This software program is licensed subject to the GNU General Public License 134e233cbeSAdrien Demarez * (GPL).Version 2,June 1991, available at 144e233cbeSAdrien Demarez * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 154e233cbeSAdrien Demarez */ 164e233cbeSAdrien Demarez 174e233cbeSAdrien Demarez #include <linux/module.h> 184e233cbeSAdrien Demarez #include <linux/init.h> 194e233cbeSAdrien Demarez #include <linux/slab.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 384e233cbeSAdrien Demarez #define LM73_ID 0x9001 /* or 0x190 after a swab16() */ 394e233cbeSAdrien Demarez #define DRVNAME "lm73" 404e233cbeSAdrien Demarez #define LM73_TEMP_MIN (-40) 414e233cbeSAdrien Demarez #define LM73_TEMP_MAX 150 424e233cbeSAdrien Demarez 434e233cbeSAdrien Demarez /*-----------------------------------------------------------------------*/ 444e233cbeSAdrien Demarez 454e233cbeSAdrien Demarez 464e233cbeSAdrien Demarez static ssize_t set_temp(struct device *dev, struct device_attribute *da, 474e233cbeSAdrien Demarez const char *buf, size_t count) 484e233cbeSAdrien Demarez { 494e233cbeSAdrien Demarez struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 504e233cbeSAdrien Demarez struct i2c_client *client = to_i2c_client(dev); 514e233cbeSAdrien Demarez long temp; 524e233cbeSAdrien Demarez short value; 534e233cbeSAdrien Demarez 544e233cbeSAdrien Demarez int status = strict_strtol(buf, 10, &temp); 554e233cbeSAdrien Demarez if (status < 0) 564e233cbeSAdrien Demarez return status; 574e233cbeSAdrien Demarez 584e233cbeSAdrien Demarez /* Write value */ 594e233cbeSAdrien Demarez value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4), 604e233cbeSAdrien Demarez (LM73_TEMP_MAX*4)) << 5; 614e233cbeSAdrien Demarez i2c_smbus_write_word_data(client, attr->index, swab16(value)); 624e233cbeSAdrien Demarez return count; 634e233cbeSAdrien Demarez } 644e233cbeSAdrien Demarez 654e233cbeSAdrien Demarez static ssize_t show_temp(struct device *dev, struct device_attribute *da, 664e233cbeSAdrien Demarez char *buf) 674e233cbeSAdrien Demarez { 684e233cbeSAdrien Demarez struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 694e233cbeSAdrien Demarez struct i2c_client *client = to_i2c_client(dev); 704e233cbeSAdrien Demarez /* use integer division instead of equivalent right shift to 714e233cbeSAdrien Demarez guarantee arithmetic shift and preserve the sign */ 724e233cbeSAdrien Demarez int temp = ((s16) (swab16(i2c_smbus_read_word_data(client, 734e233cbeSAdrien Demarez attr->index)))*250) / 32; 744e233cbeSAdrien Demarez return sprintf(buf, "%d\n", temp); 754e233cbeSAdrien Demarez } 764e233cbeSAdrien Demarez 774e233cbeSAdrien Demarez 784e233cbeSAdrien Demarez /*-----------------------------------------------------------------------*/ 794e233cbeSAdrien Demarez 804e233cbeSAdrien Demarez /* sysfs attributes for hwmon */ 814e233cbeSAdrien Demarez 824e233cbeSAdrien Demarez static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, 834e233cbeSAdrien Demarez show_temp, set_temp, LM73_REG_MAX); 844e233cbeSAdrien Demarez static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, 854e233cbeSAdrien Demarez show_temp, set_temp, LM73_REG_MIN); 864e233cbeSAdrien Demarez static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, 874e233cbeSAdrien Demarez show_temp, NULL, LM73_REG_INPUT); 884e233cbeSAdrien Demarez 894e233cbeSAdrien Demarez 904e233cbeSAdrien Demarez static struct attribute *lm73_attributes[] = { 914e233cbeSAdrien Demarez &sensor_dev_attr_temp1_input.dev_attr.attr, 924e233cbeSAdrien Demarez &sensor_dev_attr_temp1_max.dev_attr.attr, 934e233cbeSAdrien Demarez &sensor_dev_attr_temp1_min.dev_attr.attr, 944e233cbeSAdrien Demarez 954e233cbeSAdrien Demarez NULL 964e233cbeSAdrien Demarez }; 974e233cbeSAdrien Demarez 984e233cbeSAdrien Demarez static const struct attribute_group lm73_group = { 994e233cbeSAdrien Demarez .attrs = lm73_attributes, 1004e233cbeSAdrien Demarez }; 1014e233cbeSAdrien Demarez 1024e233cbeSAdrien Demarez /*-----------------------------------------------------------------------*/ 1034e233cbeSAdrien Demarez 1044e233cbeSAdrien Demarez /* device probe and removal */ 1054e233cbeSAdrien Demarez 1064e233cbeSAdrien Demarez static int 1074e233cbeSAdrien Demarez lm73_probe(struct i2c_client *client, const struct i2c_device_id *id) 1084e233cbeSAdrien Demarez { 1094e233cbeSAdrien Demarez struct device *hwmon_dev; 1104e233cbeSAdrien Demarez int status; 1114e233cbeSAdrien Demarez 1124e233cbeSAdrien Demarez /* Register sysfs hooks */ 1134e233cbeSAdrien Demarez status = sysfs_create_group(&client->dev.kobj, &lm73_group); 1144e233cbeSAdrien Demarez if (status) 1154e233cbeSAdrien Demarez return status; 1164e233cbeSAdrien Demarez 1174e233cbeSAdrien Demarez hwmon_dev = hwmon_device_register(&client->dev); 1184e233cbeSAdrien Demarez if (IS_ERR(hwmon_dev)) { 1194e233cbeSAdrien Demarez status = PTR_ERR(hwmon_dev); 1204e233cbeSAdrien Demarez goto exit_remove; 1214e233cbeSAdrien Demarez } 1224e233cbeSAdrien Demarez i2c_set_clientdata(client, hwmon_dev); 1234e233cbeSAdrien Demarez 1244e233cbeSAdrien Demarez dev_info(&client->dev, "%s: sensor '%s'\n", 1254e233cbeSAdrien Demarez dev_name(hwmon_dev), client->name); 1264e233cbeSAdrien Demarez 1274e233cbeSAdrien Demarez return 0; 1284e233cbeSAdrien Demarez 1294e233cbeSAdrien Demarez exit_remove: 1304e233cbeSAdrien Demarez sysfs_remove_group(&client->dev.kobj, &lm73_group); 1314e233cbeSAdrien Demarez return status; 1324e233cbeSAdrien Demarez } 1334e233cbeSAdrien Demarez 1344e233cbeSAdrien Demarez static int lm73_remove(struct i2c_client *client) 1354e233cbeSAdrien Demarez { 1364e233cbeSAdrien Demarez struct device *hwmon_dev = i2c_get_clientdata(client); 1374e233cbeSAdrien Demarez 1384e233cbeSAdrien Demarez hwmon_device_unregister(hwmon_dev); 1394e233cbeSAdrien Demarez sysfs_remove_group(&client->dev.kobj, &lm73_group); 1404e233cbeSAdrien Demarez i2c_set_clientdata(client, NULL); 1414e233cbeSAdrien Demarez return 0; 1424e233cbeSAdrien Demarez } 1434e233cbeSAdrien Demarez 1444e233cbeSAdrien Demarez static const struct i2c_device_id lm73_ids[] = { 145*1f86df49SJean Delvare { "lm73", 0 }, 1464e233cbeSAdrien Demarez { /* LIST END */ } 1474e233cbeSAdrien Demarez }; 1484e233cbeSAdrien Demarez MODULE_DEVICE_TABLE(i2c, lm73_ids); 1494e233cbeSAdrien Demarez 1504e233cbeSAdrien Demarez /* Return 0 if detection is successful, -ENODEV otherwise */ 151310ec792SJean Delvare static int lm73_detect(struct i2c_client *new_client, 1524e233cbeSAdrien Demarez struct i2c_board_info *info) 1534e233cbeSAdrien Demarez { 1544e233cbeSAdrien Demarez struct i2c_adapter *adapter = new_client->adapter; 1554e233cbeSAdrien Demarez u16 id; 1564e233cbeSAdrien Demarez u8 ctrl; 1574e233cbeSAdrien Demarez 1584e233cbeSAdrien Demarez if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 1594e233cbeSAdrien Demarez I2C_FUNC_SMBUS_WORD_DATA)) 1604e233cbeSAdrien Demarez return -ENODEV; 1614e233cbeSAdrien Demarez 1624e233cbeSAdrien Demarez /* Check device ID */ 1634e233cbeSAdrien Demarez id = i2c_smbus_read_word_data(new_client, LM73_REG_ID); 1644e233cbeSAdrien Demarez ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL); 1654e233cbeSAdrien Demarez if ((id != LM73_ID) || (ctrl & 0x10)) 1664e233cbeSAdrien Demarez return -ENODEV; 1674e233cbeSAdrien Demarez 1684e233cbeSAdrien Demarez strlcpy(info->type, "lm73", I2C_NAME_SIZE); 1694e233cbeSAdrien Demarez 1704e233cbeSAdrien Demarez return 0; 1714e233cbeSAdrien Demarez } 1724e233cbeSAdrien Demarez 1734e233cbeSAdrien Demarez static struct i2c_driver lm73_driver = { 1744e233cbeSAdrien Demarez .class = I2C_CLASS_HWMON, 1754e233cbeSAdrien Demarez .driver = { 1764e233cbeSAdrien Demarez .name = "lm73", 1774e233cbeSAdrien Demarez }, 1784e233cbeSAdrien Demarez .probe = lm73_probe, 1794e233cbeSAdrien Demarez .remove = lm73_remove, 1804e233cbeSAdrien Demarez .id_table = lm73_ids, 1814e233cbeSAdrien Demarez .detect = lm73_detect, 182c3813d6aSJean Delvare .address_list = normal_i2c, 1834e233cbeSAdrien Demarez }; 1844e233cbeSAdrien Demarez 1854e233cbeSAdrien Demarez /* module glue */ 1864e233cbeSAdrien Demarez 1874e233cbeSAdrien Demarez static int __init sensors_lm73_init(void) 1884e233cbeSAdrien Demarez { 1894e233cbeSAdrien Demarez return i2c_add_driver(&lm73_driver); 1904e233cbeSAdrien Demarez } 1914e233cbeSAdrien Demarez 1924e233cbeSAdrien Demarez static void __exit sensors_lm73_exit(void) 1934e233cbeSAdrien Demarez { 1944e233cbeSAdrien Demarez i2c_del_driver(&lm73_driver); 1954e233cbeSAdrien Demarez } 1964e233cbeSAdrien Demarez 1974e233cbeSAdrien Demarez MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>"); 1984e233cbeSAdrien Demarez MODULE_DESCRIPTION("LM73 driver"); 1994e233cbeSAdrien Demarez MODULE_LICENSE("GPL"); 2004e233cbeSAdrien Demarez 2014e233cbeSAdrien Demarez module_init(sensors_lm73_init); 2024e233cbeSAdrien Demarez module_exit(sensors_lm73_exit); 203