xref: /openbmc/linux/drivers/hwmon/adm1029.c (revision e4af8ad0)
1 /*
2  * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3  *
4  * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
5  *
6  * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
7  *
8  * Give only processor, motherboard temperatures and fan tachs
9  * Very rare chip please let me know if you use it
10  *
11  * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
12  *
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation version 2 of the License
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  */
23 
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/hwmon.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33 
34 /*
35  * Addresses to scan
36  */
37 
38 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
39 						0x2e, 0x2f, I2C_CLIENT_END
40 };
41 
42 /*
43  * The ADM1029 registers
44  * Manufacturer ID is 0x41 for Analog Devices
45  */
46 
47 #define ADM1029_REG_MAN_ID			0x0D
48 #define ADM1029_REG_CHIP_ID			0x0E
49 #define ADM1029_REG_CONFIG			0x01
50 #define ADM1029_REG_NB_FAN_SUPPORT		0x02
51 
52 #define ADM1029_REG_TEMP_DEVICES_INSTALLED	0x06
53 
54 #define ADM1029_REG_LOCAL_TEMP			0xA0
55 #define ADM1029_REG_REMOTE1_TEMP		0xA1
56 #define ADM1029_REG_REMOTE2_TEMP		0xA2
57 
58 #define ADM1029_REG_LOCAL_TEMP_HIGH		0x90
59 #define ADM1029_REG_REMOTE1_TEMP_HIGH		0x91
60 #define ADM1029_REG_REMOTE2_TEMP_HIGH		0x92
61 
62 #define ADM1029_REG_LOCAL_TEMP_LOW		0x98
63 #define ADM1029_REG_REMOTE1_TEMP_LOW		0x99
64 #define ADM1029_REG_REMOTE2_TEMP_LOW		0x9A
65 
66 #define ADM1029_REG_FAN1			0x70
67 #define ADM1029_REG_FAN2			0x71
68 
69 #define ADM1029_REG_FAN1_MIN			0x78
70 #define ADM1029_REG_FAN2_MIN			0x79
71 
72 #define ADM1029_REG_FAN1_CONFIG			0x68
73 #define ADM1029_REG_FAN2_CONFIG			0x69
74 
75 #define TEMP_FROM_REG(val)	((val) * 1000)
76 
77 #define DIV_FROM_REG(val)	(1 << (((val) >> 6) - 1))
78 
79 /* Registers to be checked by adm1029_update_device() */
80 static const u8 ADM1029_REG_TEMP[] = {
81 	ADM1029_REG_LOCAL_TEMP,
82 	ADM1029_REG_REMOTE1_TEMP,
83 	ADM1029_REG_REMOTE2_TEMP,
84 	ADM1029_REG_LOCAL_TEMP_HIGH,
85 	ADM1029_REG_REMOTE1_TEMP_HIGH,
86 	ADM1029_REG_REMOTE2_TEMP_HIGH,
87 	ADM1029_REG_LOCAL_TEMP_LOW,
88 	ADM1029_REG_REMOTE1_TEMP_LOW,
89 	ADM1029_REG_REMOTE2_TEMP_LOW,
90 };
91 
92 static const u8 ADM1029_REG_FAN[] = {
93 	ADM1029_REG_FAN1,
94 	ADM1029_REG_FAN2,
95 	ADM1029_REG_FAN1_MIN,
96 	ADM1029_REG_FAN2_MIN,
97 };
98 
99 static const u8 ADM1029_REG_FAN_DIV[] = {
100 	ADM1029_REG_FAN1_CONFIG,
101 	ADM1029_REG_FAN2_CONFIG,
102 };
103 
104 /*
105  * Client data (each client gets its own)
106  */
107 
108 struct adm1029_data {
109 	struct i2c_client *client;
110 	struct mutex update_lock;
111 	char valid;		/* zero until following fields are valid */
112 	unsigned long last_updated;	/* in jiffies */
113 
114 	/* registers values, signed for temperature, unsigned for other stuff */
115 	s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
116 	u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
117 	u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
118 };
119 
120 /*
121  * function that update the status of the chips (temperature for example)
122  */
123 static struct adm1029_data *adm1029_update_device(struct device *dev)
124 {
125 	struct adm1029_data *data = dev_get_drvdata(dev);
126 	struct i2c_client *client = data->client;
127 
128 	mutex_lock(&data->update_lock);
129 	/*
130 	 * Use the "cache" Luke, don't recheck values
131 	 * if there are already checked not a long time later
132 	 */
133 	if (time_after(jiffies, data->last_updated + HZ * 2)
134 	 || !data->valid) {
135 		int nr;
136 
137 		dev_dbg(&client->dev, "Updating adm1029 data\n");
138 
139 		for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
140 			data->temp[nr] =
141 			    i2c_smbus_read_byte_data(client,
142 						     ADM1029_REG_TEMP[nr]);
143 		}
144 		for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
145 			data->fan[nr] =
146 			    i2c_smbus_read_byte_data(client,
147 						     ADM1029_REG_FAN[nr]);
148 		}
149 		for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
150 			data->fan_div[nr] =
151 			    i2c_smbus_read_byte_data(client,
152 						     ADM1029_REG_FAN_DIV[nr]);
153 		}
154 
155 		data->last_updated = jiffies;
156 		data->valid = 1;
157 	}
158 
159 	mutex_unlock(&data->update_lock);
160 
161 	return data;
162 }
163 
164 /*
165  * Sysfs stuff
166  */
167 
168 static ssize_t
169 show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
170 {
171 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
172 	struct adm1029_data *data = adm1029_update_device(dev);
173 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
174 }
175 
176 static ssize_t
177 show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
178 {
179 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
180 	struct adm1029_data *data = adm1029_update_device(dev);
181 	u16 val;
182 	if (data->fan[attr->index] == 0
183 	    || (data->fan_div[attr->index] & 0xC0) == 0
184 	    || data->fan[attr->index] == 255) {
185 		return sprintf(buf, "0\n");
186 	}
187 
188 	val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
189 	    / data->fan[attr->index];
190 	return sprintf(buf, "%d\n", val);
191 }
192 
193 static ssize_t
194 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
195 {
196 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
197 	struct adm1029_data *data = adm1029_update_device(dev);
198 	if ((data->fan_div[attr->index] & 0xC0) == 0)
199 		return sprintf(buf, "0\n");
200 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
201 }
202 
203 static ssize_t set_fan_div(struct device *dev,
204 	    struct device_attribute *devattr, const char *buf, size_t count)
205 {
206 	struct adm1029_data *data = dev_get_drvdata(dev);
207 	struct i2c_client *client = data->client;
208 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
209 	u8 reg;
210 	long val;
211 	int ret = kstrtol(buf, 10, &val);
212 	if (ret < 0)
213 		return ret;
214 
215 	mutex_lock(&data->update_lock);
216 
217 	/*Read actual config */
218 	reg = i2c_smbus_read_byte_data(client,
219 				       ADM1029_REG_FAN_DIV[attr->index]);
220 
221 	switch (val) {
222 	case 1:
223 		val = 1;
224 		break;
225 	case 2:
226 		val = 2;
227 		break;
228 	case 4:
229 		val = 3;
230 		break;
231 	default:
232 		mutex_unlock(&data->update_lock);
233 		dev_err(&client->dev,
234 			"fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
235 			val);
236 		return -EINVAL;
237 	}
238 	/* Update the value */
239 	reg = (reg & 0x3F) | (val << 6);
240 
241 	/* Update the cache */
242 	data->fan_div[attr->index] = reg;
243 
244 	/* Write value */
245 	i2c_smbus_write_byte_data(client,
246 				  ADM1029_REG_FAN_DIV[attr->index], reg);
247 	mutex_unlock(&data->update_lock);
248 
249 	return count;
250 }
251 
252 /*
253  * Access rights on sysfs. S_IRUGO: Is Readable by User, Group and Others
254  *			   S_IWUSR: Is Writable by User.
255  */
256 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
257 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
258 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
259 
260 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
261 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
262 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
263 
264 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
265 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
266 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
267 
268 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
269 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
270 
271 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
272 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
273 
274 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
275 			  show_fan_div, set_fan_div, 0);
276 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
277 			  show_fan_div, set_fan_div, 1);
278 
279 static struct attribute *adm1029_attrs[] = {
280 	&sensor_dev_attr_temp1_input.dev_attr.attr,
281 	&sensor_dev_attr_temp1_min.dev_attr.attr,
282 	&sensor_dev_attr_temp1_max.dev_attr.attr,
283 	&sensor_dev_attr_temp2_input.dev_attr.attr,
284 	&sensor_dev_attr_temp2_min.dev_attr.attr,
285 	&sensor_dev_attr_temp2_max.dev_attr.attr,
286 	&sensor_dev_attr_temp3_input.dev_attr.attr,
287 	&sensor_dev_attr_temp3_min.dev_attr.attr,
288 	&sensor_dev_attr_temp3_max.dev_attr.attr,
289 	&sensor_dev_attr_fan1_input.dev_attr.attr,
290 	&sensor_dev_attr_fan2_input.dev_attr.attr,
291 	&sensor_dev_attr_fan1_min.dev_attr.attr,
292 	&sensor_dev_attr_fan2_min.dev_attr.attr,
293 	&sensor_dev_attr_fan1_div.dev_attr.attr,
294 	&sensor_dev_attr_fan2_div.dev_attr.attr,
295 	NULL
296 };
297 
298 ATTRIBUTE_GROUPS(adm1029);
299 
300 /*
301  * Real code
302  */
303 
304 /* Return 0 if detection is successful, -ENODEV otherwise */
305 static int adm1029_detect(struct i2c_client *client,
306 			  struct i2c_board_info *info)
307 {
308 	struct i2c_adapter *adapter = client->adapter;
309 	u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
310 
311 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
312 		return -ENODEV;
313 
314 	/*
315 	 * ADM1029 doesn't have CHIP ID, check just MAN ID
316 	 * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
317 	 * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
318 	 * documented
319 	 */
320 
321 	man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
322 	chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
323 	temp_devices_installed = i2c_smbus_read_byte_data(client,
324 					ADM1029_REG_TEMP_DEVICES_INSTALLED);
325 	nb_fan_support = i2c_smbus_read_byte_data(client,
326 						ADM1029_REG_NB_FAN_SUPPORT);
327 	/* 0x41 is Analog Devices */
328 	if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01
329 	    || nb_fan_support != 0x03)
330 		return -ENODEV;
331 
332 	if ((chip_id & 0xF0) != 0x00) {
333 		/*
334 		 * There are no "official" CHIP ID, so actually
335 		 * we use Major/Minor revision for that
336 		 */
337 		pr_info("Unknown major revision %x, please let us know\n",
338 			chip_id);
339 		return -ENODEV;
340 	}
341 
342 	strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
343 
344 	return 0;
345 }
346 
347 static int adm1029_init_client(struct i2c_client *client)
348 {
349 	u8 config;
350 
351 	config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
352 	if ((config & 0x10) == 0) {
353 		i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
354 					  config | 0x10);
355 	}
356 	/* recheck config */
357 	config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
358 	if ((config & 0x10) == 0) {
359 		dev_err(&client->dev, "Initialization failed!\n");
360 		return 0;
361 	}
362 	return 1;
363 }
364 
365 static int adm1029_probe(struct i2c_client *client,
366 			 const struct i2c_device_id *id)
367 {
368 	struct device *dev = &client->dev;
369 	struct adm1029_data *data;
370 	struct device *hwmon_dev;
371 
372 	data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
373 	if (!data)
374 		return -ENOMEM;
375 
376 	data->client = client;
377 	mutex_init(&data->update_lock);
378 
379 	/*
380 	 * Initialize the ADM1029 chip
381 	 * Check config register
382 	 */
383 	if (adm1029_init_client(client) == 0)
384 		return -ENODEV;
385 
386 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
387 							   data,
388 							   adm1029_groups);
389 	return PTR_ERR_OR_ZERO(hwmon_dev);
390 }
391 
392 static const struct i2c_device_id adm1029_id[] = {
393 	{ "adm1029", 0 },
394 	{ }
395 };
396 MODULE_DEVICE_TABLE(i2c, adm1029_id);
397 
398 static struct i2c_driver adm1029_driver = {
399 	.class		= I2C_CLASS_HWMON,
400 	.driver = {
401 		.name = "adm1029",
402 	},
403 	.probe		= adm1029_probe,
404 	.id_table	= adm1029_id,
405 	.detect		= adm1029_detect,
406 	.address_list	= normal_i2c,
407 };
408 
409 module_i2c_driver(adm1029_driver);
410 
411 MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
412 MODULE_DESCRIPTION("adm1029 driver");
413 MODULE_LICENSE("GPL v2");
414