xref: /linux/drivers/hwmon/lm92.c (revision fcb49571b4d7a993190d4dc40331ac5c7350d2dd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lm92 - Hardware monitoring driver
4  * Copyright (C) 2005-2008  Jean Delvare <jdelvare@suse.de>
5  *
6  * Based on the lm90 driver, with some ideas taken from the lm_sensors
7  * lm92 driver as well.
8  *
9  * The LM92 is a sensor chip made by National Semiconductor. It reports
10  * its own temperature with a 0.0625 deg resolution and a 0.33 deg
11  * accuracy. Complete datasheet can be obtained from National's website
12  * at:
13  *   http://www.national.com/pf/LM/LM92.html
14  *
15  * This driver also supports the MAX6635 sensor chip made by Maxim.
16  * This chip is compatible with the LM92, but has a lesser accuracy
17  * (1.0 deg). Complete datasheet can be obtained from Maxim's website
18  * at:
19  *   http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3074
20  *
21  * Since the LM92 was the first chipset supported by this driver, most
22  * comments will refer to this chipset, but are actually general and
23  * concern all supported chipsets, unless mentioned otherwise.
24  *
25  * Support could easily be added for the National Semiconductor LM76
26  * and Maxim MAX6633 and MAX6634 chips, which are mostly compatible
27  * with the LM92.
28  */
29 
30 #include <linux/err.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/i2c.h>
34 #include <linux/init.h>
35 #include <linux/jiffies.h>
36 #include <linux/module.h>
37 #include <linux/mutex.h>
38 #include <linux/slab.h>
39 
40 /*
41  * The LM92 and MAX6635 have 2 two-state pins for address selection,
42  * resulting in 4 possible addresses.
43  */
44 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
45 						I2C_CLIENT_END };
46 /* The LM92 registers */
47 #define LM92_REG_CONFIG			0x01 /* 8-bit, RW */
48 #define LM92_REG_TEMP			0x00 /* 16-bit, RO */
49 #define LM92_REG_TEMP_HYST		0x02 /* 16-bit, RW */
50 #define LM92_REG_TEMP_CRIT		0x03 /* 16-bit, RW */
51 #define LM92_REG_TEMP_LOW		0x04 /* 16-bit, RW */
52 #define LM92_REG_TEMP_HIGH		0x05 /* 16-bit, RW */
53 #define LM92_REG_MAN_ID			0x07 /* 16-bit, RO, LM92 only */
54 
55 /*
56  * The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
57  * left-justified in 16-bit registers. No rounding is done, with such
58  * a resolution it's just not worth it. Note that the MAX6635 doesn't
59  * make use of the 4 lower bits for limits (i.e. effective resolution
60  * for limits is 1 degree Celsius).
61  */
62 static inline int TEMP_FROM_REG(s16 reg)
63 {
64 	return reg / 8 * 625 / 10;
65 }
66 
67 static inline s16 TEMP_TO_REG(long val, int resolution)
68 {
69 	val = clamp_val(val, -60000, 160000);
70 	return DIV_ROUND_CLOSEST(val << (resolution - 9), 1000) << (16 - resolution);
71 }
72 
73 /* Alarm flags are stored in the 3 LSB of the temperature register */
74 static inline u8 ALARMS_FROM_REG(s16 reg)
75 {
76 	return reg & 0x0007;
77 }
78 
79 enum temp_index {
80 	t_input,
81 	t_crit,
82 	t_min,
83 	t_max,
84 	t_hyst,
85 	t_num_regs
86 };
87 
88 static const u8 regs[t_num_regs] = {
89 	[t_input] = LM92_REG_TEMP,
90 	[t_crit] = LM92_REG_TEMP_CRIT,
91 	[t_min] = LM92_REG_TEMP_LOW,
92 	[t_max] = LM92_REG_TEMP_HIGH,
93 	[t_hyst] = LM92_REG_TEMP_HYST,
94 };
95 
96 /* Client data (each client gets its own) */
97 struct lm92_data {
98 	struct i2c_client *client;
99 	struct mutex update_lock;
100 	int resolution;
101 	bool valid; /* false until following fields are valid */
102 	unsigned long last_updated; /* in jiffies */
103 
104 	/* registers values */
105 	s16 temp[t_num_regs];	/* index with enum temp_index */
106 };
107 
108 /*
109  * Sysfs attributes and callback functions
110  */
111 
112 static struct lm92_data *lm92_update_device(struct device *dev)
113 {
114 	struct lm92_data *data = dev_get_drvdata(dev);
115 	struct i2c_client *client = data->client;
116 	int i;
117 
118 	mutex_lock(&data->update_lock);
119 
120 	if (time_after(jiffies, data->last_updated + HZ) ||
121 	    !data->valid) {
122 		dev_dbg(&client->dev, "Updating lm92 data\n");
123 		for (i = 0; i < t_num_regs; i++) {
124 			data->temp[i] =
125 				i2c_smbus_read_word_swapped(client, regs[i]);
126 		}
127 		data->last_updated = jiffies;
128 		data->valid = true;
129 	}
130 
131 	mutex_unlock(&data->update_lock);
132 
133 	return data;
134 }
135 
136 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
137 			 char *buf)
138 {
139 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
140 	struct lm92_data *data = lm92_update_device(dev);
141 
142 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
143 }
144 
145 static ssize_t temp_store(struct device *dev,
146 			  struct device_attribute *devattr, const char *buf,
147 			  size_t count)
148 {
149 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
150 	struct lm92_data *data = dev_get_drvdata(dev);
151 	struct i2c_client *client = data->client;
152 	int nr = attr->index;
153 	long val;
154 	int err;
155 
156 	err = kstrtol(buf, 10, &val);
157 	if (err)
158 		return err;
159 
160 	mutex_lock(&data->update_lock);
161 	data->temp[nr] = TEMP_TO_REG(val, data->resolution);
162 	i2c_smbus_write_word_swapped(client, regs[nr], data->temp[nr]);
163 	mutex_unlock(&data->update_lock);
164 	return count;
165 }
166 
167 static ssize_t temp_hyst_show(struct device *dev,
168 			      struct device_attribute *devattr, char *buf)
169 {
170 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
171 	struct lm92_data *data = lm92_update_device(dev);
172 
173 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])
174 		       - TEMP_FROM_REG(data->temp[t_hyst]));
175 }
176 
177 static ssize_t temp1_min_hyst_show(struct device *dev,
178 				   struct device_attribute *attr, char *buf)
179 {
180 	struct lm92_data *data = lm92_update_device(dev);
181 
182 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[t_min])
183 		       + TEMP_FROM_REG(data->temp[t_hyst]));
184 }
185 
186 static ssize_t temp_hyst_store(struct device *dev,
187 			       struct device_attribute *devattr,
188 			       const char *buf, size_t count)
189 {
190 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
191 	struct lm92_data *data = dev_get_drvdata(dev);
192 	struct i2c_client *client = data->client;
193 	long val;
194 	int err;
195 
196 	err = kstrtol(buf, 10, &val);
197 	if (err)
198 		return err;
199 
200 	val = clamp_val(val, -120000, 220000);
201 	mutex_lock(&data->update_lock);
202 	data->temp[t_hyst] =
203 		TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val,
204 			    data->resolution);
205 	i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
206 				     data->temp[t_hyst]);
207 	mutex_unlock(&data->update_lock);
208 	return count;
209 }
210 
211 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
212 			   char *buf)
213 {
214 	struct lm92_data *data = lm92_update_device(dev);
215 
216 	return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp[t_input]));
217 }
218 
219 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
220 			  char *buf)
221 {
222 	int bitnr = to_sensor_dev_attr(attr)->index;
223 	struct lm92_data *data = lm92_update_device(dev);
224 	return sprintf(buf, "%d\n", (data->temp[t_input] >> bitnr) & 1);
225 }
226 
227 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
228 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_crit);
229 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_hyst, t_crit);
230 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, t_min);
231 static DEVICE_ATTR_RO(temp1_min_hyst);
232 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_max);
233 static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, temp_hyst, t_max);
234 static DEVICE_ATTR_RO(alarms);
235 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 2);
236 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 0);
237 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
238 
239 /*
240  * Detection and registration
241  */
242 
243 static void lm92_init_client(struct i2c_client *client)
244 {
245 	u8 config;
246 
247 	/* Start the conversions if needed */
248 	config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
249 	if (config & 0x01)
250 		i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
251 					  config & 0xFE);
252 }
253 
254 static struct attribute *lm92_attrs[] = {
255 	&sensor_dev_attr_temp1_input.dev_attr.attr,
256 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
257 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
258 	&sensor_dev_attr_temp1_min.dev_attr.attr,
259 	&dev_attr_temp1_min_hyst.attr,
260 	&sensor_dev_attr_temp1_max.dev_attr.attr,
261 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
262 	&dev_attr_alarms.attr,
263 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
264 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
265 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
266 	NULL
267 };
268 ATTRIBUTE_GROUPS(lm92);
269 
270 /* Return 0 if detection is successful, -ENODEV otherwise */
271 static int lm92_detect(struct i2c_client *new_client,
272 		       struct i2c_board_info *info)
273 {
274 	struct i2c_adapter *adapter = new_client->adapter;
275 	u8 config_addr = LM92_REG_CONFIG;
276 	u8 man_id_addr = LM92_REG_MAN_ID;
277 	int i, regval;
278 
279 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
280 					    | I2C_FUNC_SMBUS_WORD_DATA))
281 		return -ENODEV;
282 
283 	/*
284 	 * Register values repeat with multiples of 8.
285 	 * Read twice to improve detection accuracy.
286 	 */
287 	for (i = 0; i < 2; i++) {
288 		regval = i2c_smbus_read_word_data(new_client, man_id_addr);
289 		if (regval != 0x0180)
290 			return -ENODEV;
291 		regval = i2c_smbus_read_byte_data(new_client, config_addr);
292 		if (regval < 0 || (regval & 0xe0))
293 			return -ENODEV;
294 		config_addr += 8;
295 		man_id_addr += 8;
296 	}
297 
298 	strscpy(info->type, "lm92", I2C_NAME_SIZE);
299 
300 	return 0;
301 }
302 
303 static int lm92_probe(struct i2c_client *new_client)
304 {
305 	struct device *hwmon_dev;
306 	struct lm92_data *data;
307 
308 	data = devm_kzalloc(&new_client->dev, sizeof(struct lm92_data),
309 			    GFP_KERNEL);
310 	if (!data)
311 		return -ENOMEM;
312 
313 	data->client = new_client;
314 	data->resolution = (unsigned long)i2c_get_match_data(new_client);
315 	mutex_init(&data->update_lock);
316 
317 	/* Initialize the chipset */
318 	lm92_init_client(new_client);
319 
320 	hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
321 							   new_client->name,
322 							   data, lm92_groups);
323 	return PTR_ERR_OR_ZERO(hwmon_dev);
324 }
325 
326 /*
327  * Module and driver stuff
328  */
329 
330 /* .driver_data is limit register resolution */
331 static const struct i2c_device_id lm92_id[] = {
332 	{ "lm92", 13 },
333 	{ "max6635", 9 },
334 	{ }
335 };
336 MODULE_DEVICE_TABLE(i2c, lm92_id);
337 
338 static struct i2c_driver lm92_driver = {
339 	.class		= I2C_CLASS_HWMON,
340 	.driver = {
341 		.name	= "lm92",
342 	},
343 	.probe		= lm92_probe,
344 	.id_table	= lm92_id,
345 	.detect		= lm92_detect,
346 	.address_list	= normal_i2c,
347 };
348 
349 module_i2c_driver(lm92_driver);
350 
351 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
352 MODULE_DESCRIPTION("LM92/MAX6635 driver");
353 MODULE_LICENSE("GPL");
354