5 * Copyright (C) 2007, CenoSYS (www.cenosys.com).
6 * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
8 * Guillaume Ligneul <guillaume.ligneul@gmail.com>
9 * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
10 * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
12 * This software program is licensed subject to the GNU General Public License
13 * (GPL).Version 2,June 1991, available at
14 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/i2c.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/err.h>
25 /* Addresses scanned */
26 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
27 0x4d, 0x4e, I2C_CLIENT_END };
30 #define LM73_REG_INPUT 0x00
31 #define LM73_REG_CONF 0x01
32 #define LM73_REG_MAX 0x02
33 #define LM73_REG_MIN 0x03
34 #define LM73_REG_CTRL 0x04
35 #define LM73_REG_ID 0x07
37 #define LM73_ID 0x9001 /* or 0x190 after a swab16() */
38 #define DRVNAME "lm73"
39 #define LM73_TEMP_MIN (-40)
40 #define LM73_TEMP_MAX 150
42 /*-----------------------------------------------------------------------*/
45 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
46 const char *buf, size_t count)
48 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
49 struct i2c_client *client = to_i2c_client(dev);
53 int status = strict_strtol(buf, 10, &temp);
58 value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4),
59 (LM73_TEMP_MAX*4)) << 5;
60 i2c_smbus_write_word_data(client, attr->index, swab16(value));
64 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
67 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
68 struct i2c_client *client = to_i2c_client(dev);
69 /* use integer division instead of equivalent right shift to
70 guarantee arithmetic shift and preserve the sign */
71 int temp = ((s16) (swab16(i2c_smbus_read_word_data(client,
72 attr->index)))*250) / 32;
73 return sprintf(buf, "%d\n", temp);
77 /*-----------------------------------------------------------------------*/
79 /* sysfs attributes for hwmon */
81 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
82 show_temp, set_temp, LM73_REG_MAX);
83 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
84 show_temp, set_temp, LM73_REG_MIN);
85 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
86 show_temp, NULL, LM73_REG_INPUT);
89 static struct attribute *lm73_attributes[] = {
90 &sensor_dev_attr_temp1_input.dev_attr.attr,
91 &sensor_dev_attr_temp1_max.dev_attr.attr,
92 &sensor_dev_attr_temp1_min.dev_attr.attr,
97 static const struct attribute_group lm73_group = {
98 .attrs = lm73_attributes,
101 /*-----------------------------------------------------------------------*/
103 /* device probe and removal */
106 lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
108 struct device *hwmon_dev;
111 /* Register sysfs hooks */
112 status = sysfs_create_group(&client->dev.kobj, &lm73_group);
116 hwmon_dev = hwmon_device_register(&client->dev);
117 if (IS_ERR(hwmon_dev)) {
118 status = PTR_ERR(hwmon_dev);
121 i2c_set_clientdata(client, hwmon_dev);
123 dev_info(&client->dev, "%s: sensor '%s'\n",
124 dev_name(hwmon_dev), client->name);
129 sysfs_remove_group(&client->dev.kobj, &lm73_group);
133 static int lm73_remove(struct i2c_client *client)
135 struct device *hwmon_dev = i2c_get_clientdata(client);
137 hwmon_device_unregister(hwmon_dev);
138 sysfs_remove_group(&client->dev.kobj, &lm73_group);
139 i2c_set_clientdata(client, NULL);
143 static const struct i2c_device_id lm73_ids[] = {
147 MODULE_DEVICE_TABLE(i2c, lm73_ids);
149 /* Return 0 if detection is successful, -ENODEV otherwise */
150 static int lm73_detect(struct i2c_client *new_client,
151 struct i2c_board_info *info)
153 struct i2c_adapter *adapter = new_client->adapter;
157 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
158 I2C_FUNC_SMBUS_WORD_DATA))
161 /* Check device ID */
162 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
163 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
164 if ((id != LM73_ID) || (ctrl & 0x10))
167 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
172 static struct i2c_driver lm73_driver = {
173 .class = I2C_CLASS_HWMON,
178 .remove = lm73_remove,
179 .id_table = lm73_ids,
180 .detect = lm73_detect,
181 .address_list = normal_i2c,
186 static int __init sensors_lm73_init(void)
188 return i2c_add_driver(&lm73_driver);
191 static void __exit sensors_lm73_exit(void)
193 i2c_del_driver(&lm73_driver);
196 MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
197 MODULE_DESCRIPTION("LM73 driver");
198 MODULE_LICENSE("GPL");
200 module_init(sensors_lm73_init);
201 module_exit(sensors_lm73_exit);