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>
11 * Chris Verges <kg4ysn@gmail.com>
13 * This software program is licensed subject to the GNU General Public License
14 * (GPL).Version 2,June 1991, available at
15 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/err.h>
26 /* Addresses scanned */
27 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
28 0x4d, 0x4e, I2C_CLIENT_END };
31 #define LM73_REG_INPUT 0x00
32 #define LM73_REG_CONF 0x01
33 #define LM73_REG_MAX 0x02
34 #define LM73_REG_MIN 0x03
35 #define LM73_REG_CTRL 0x04
36 #define LM73_REG_ID 0x07
38 #define LM73_ID 0x9001 /* 0x0190, byte-swapped */
39 #define DRVNAME "lm73"
40 #define LM73_TEMP_MIN (-256000 / 250)
41 #define LM73_TEMP_MAX (255750 / 250)
43 #define LM73_CTRL_RES_SHIFT 5
44 #define LM73_CTRL_RES_MASK (BIT(5) | BIT(6))
45 #define LM73_CTRL_TO_MASK BIT(7)
47 #define LM73_CTRL_HI_SHIFT 2
48 #define LM73_CTRL_LO_SHIFT 1
50 static const unsigned short lm73_convrates[] = {
51 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */
52 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */
53 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */
54 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */
58 struct i2c_client *client;
60 u8 ctrl; /* control register value */
63 /*-----------------------------------------------------------------------*/
65 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
66 const char *buf, size_t count)
68 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
69 struct lm73_data *data = dev_get_drvdata(dev);
74 int status = kstrtol(buf, 10, &temp);
79 value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
80 err = i2c_smbus_write_word_swapped(data->client, attr->index, value);
81 return (err < 0) ? err : count;
84 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
87 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
88 struct lm73_data *data = dev_get_drvdata(dev);
91 s32 err = i2c_smbus_read_word_swapped(data->client, attr->index);
95 /* use integer division instead of equivalent right shift to
96 guarantee arithmetic shift and preserve the sign */
97 temp = (((s16) err) * 250) / 32;
98 return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
101 static ssize_t set_convrate(struct device *dev, struct device_attribute *da,
102 const char *buf, size_t count)
104 struct lm73_data *data = dev_get_drvdata(dev);
105 unsigned long convrate;
109 err = kstrtoul(buf, 10, &convrate);
114 * Convert the desired conversion rate into register bits.
115 * res is already initialized, and everything past the second-to-last
116 * value in the array is treated as belonging to the last value
119 while (res < (ARRAY_SIZE(lm73_convrates) - 1) &&
120 convrate > lm73_convrates[res])
123 mutex_lock(&data->lock);
124 data->ctrl &= LM73_CTRL_TO_MASK;
125 data->ctrl |= res << LM73_CTRL_RES_SHIFT;
126 err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL,
128 mutex_unlock(&data->lock);
136 static ssize_t show_convrate(struct device *dev, struct device_attribute *da,
139 struct lm73_data *data = dev_get_drvdata(dev);
142 res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT;
143 return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]);
146 static ssize_t show_maxmin_alarm(struct device *dev,
147 struct device_attribute *da, char *buf)
149 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
150 struct lm73_data *data = dev_get_drvdata(dev);
153 mutex_lock(&data->lock);
154 ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL);
158 mutex_unlock(&data->lock);
160 return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1);
163 mutex_unlock(&data->lock);
167 /*-----------------------------------------------------------------------*/
169 /* sysfs attributes for hwmon */
171 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
172 show_temp, set_temp, LM73_REG_MAX);
173 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
174 show_temp, set_temp, LM73_REG_MIN);
175 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
176 show_temp, NULL, LM73_REG_INPUT);
177 static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
178 show_convrate, set_convrate, 0);
179 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
180 show_maxmin_alarm, NULL, LM73_CTRL_HI_SHIFT);
181 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
182 show_maxmin_alarm, NULL, LM73_CTRL_LO_SHIFT);
184 static struct attribute *lm73_attrs[] = {
185 &sensor_dev_attr_temp1_input.dev_attr.attr,
186 &sensor_dev_attr_temp1_max.dev_attr.attr,
187 &sensor_dev_attr_temp1_min.dev_attr.attr,
188 &sensor_dev_attr_update_interval.dev_attr.attr,
189 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
190 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
193 ATTRIBUTE_GROUPS(lm73);
195 /*-----------------------------------------------------------------------*/
197 /* device probe and removal */
200 lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
202 struct device *dev = &client->dev;
203 struct device *hwmon_dev;
204 struct lm73_data *data;
207 data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL);
211 data->client = client;
212 mutex_init(&data->lock);
214 ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
219 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
221 if (IS_ERR(hwmon_dev))
222 return PTR_ERR(hwmon_dev);
224 dev_info(dev, "sensor '%s'\n", client->name);
229 static const struct i2c_device_id lm73_ids[] = {
233 MODULE_DEVICE_TABLE(i2c, lm73_ids);
235 /* Return 0 if detection is successful, -ENODEV otherwise */
236 static int lm73_detect(struct i2c_client *new_client,
237 struct i2c_board_info *info)
239 struct i2c_adapter *adapter = new_client->adapter;
242 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
243 I2C_FUNC_SMBUS_WORD_DATA))
247 * Do as much detection as possible with byte reads first, as word
248 * reads can confuse other devices.
250 ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
251 if (ctrl < 0 || (ctrl & 0x10))
254 conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
255 if (conf < 0 || (conf & 0x0c))
258 id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
259 if (id < 0 || id != (LM73_ID & 0xff))
262 /* Check device ID */
263 id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
264 if (id < 0 || id != LM73_ID)
267 strlcpy(info->type, "lm73", I2C_NAME_SIZE);
272 static struct i2c_driver lm73_driver = {
273 .class = I2C_CLASS_HWMON,
278 .id_table = lm73_ids,
279 .detect = lm73_detect,
280 .address_list = normal_i2c,
283 module_i2c_driver(lm73_driver);
285 MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
286 MODULE_DESCRIPTION("LM73 driver");
287 MODULE_LICENSE("GPL");