xref: /openbmc/linux/drivers/hwmon/max31790.c (revision 6abeae2a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
4  *             monitoring.
5  *
6  * (C) 2015 by Il Han <corone.il.han@gmail.com>
7  */
8 
9 #include <linux/err.h>
10 #include <linux/hwmon.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/jiffies.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 
17 /* MAX31790 registers */
18 #define MAX31790_REG_GLOBAL_CONFIG	0x00
19 #define MAX31790_REG_FAN_CONFIG(ch)	(0x02 + (ch))
20 #define MAX31790_REG_FAN_DYNAMICS(ch)	(0x08 + (ch))
21 #define MAX31790_REG_FAN_FAULT_STATUS2	0x10
22 #define MAX31790_REG_FAN_FAULT_STATUS1	0x11
23 #define MAX31790_REG_TACH_COUNT(ch)	(0x18 + (ch) * 2)
24 #define MAX31790_REG_PWM_DUTY_CYCLE(ch)	(0x30 + (ch) * 2)
25 #define MAX31790_REG_PWMOUT(ch)		(0x40 + (ch) * 2)
26 #define MAX31790_REG_TARGET_COUNT(ch)	(0x50 + (ch) * 2)
27 
28 /* Fan Config register bits */
29 #define MAX31790_FAN_CFG_RPM_MODE	0x80
30 #define MAX31790_FAN_CFG_TACH_INPUT_EN	0x08
31 #define MAX31790_FAN_CFG_TACH_INPUT	0x01
32 
33 /* Fan Dynamics register bits */
34 #define MAX31790_FAN_DYN_SR_SHIFT	5
35 #define MAX31790_FAN_DYN_SR_MASK	0xE0
36 #define SR_FROM_REG(reg)		(((reg) & MAX31790_FAN_DYN_SR_MASK) \
37 					 >> MAX31790_FAN_DYN_SR_SHIFT)
38 
39 #define FAN_RPM_MIN			120
40 #define FAN_RPM_MAX			7864320
41 
42 #define RPM_FROM_REG(reg, sr)		(((reg) >> 4) ? \
43 					 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
44 					 FAN_RPM_MAX)
45 #define RPM_TO_REG(rpm, sr)		((60 * (sr) * 8192) / ((rpm) * 2))
46 
47 #define NR_CHANNEL			6
48 
49 /*
50  * Client data (each client gets its own)
51  */
52 struct max31790_data {
53 	struct i2c_client *client;
54 	struct mutex update_lock;
55 	bool valid; /* zero until following fields are valid */
56 	unsigned long last_updated; /* in jiffies */
57 
58 	/* register values */
59 	u8 fan_config[NR_CHANNEL];
60 	u8 fan_dynamics[NR_CHANNEL];
61 	u16 fault_status;
62 	u16 tach[NR_CHANNEL * 2];
63 	u16 pwm[NR_CHANNEL];
64 	u16 target_count[NR_CHANNEL];
65 };
66 
67 static struct max31790_data *max31790_update_device(struct device *dev)
68 {
69 	struct max31790_data *data = dev_get_drvdata(dev);
70 	struct i2c_client *client = data->client;
71 	struct max31790_data *ret = data;
72 	int i;
73 	int rv;
74 
75 	mutex_lock(&data->update_lock);
76 
77 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
78 		rv = i2c_smbus_read_byte_data(client,
79 				MAX31790_REG_FAN_FAULT_STATUS1);
80 		if (rv < 0)
81 			goto abort;
82 		data->fault_status = rv & 0x3F;
83 
84 		rv = i2c_smbus_read_byte_data(client,
85 				MAX31790_REG_FAN_FAULT_STATUS2);
86 		if (rv < 0)
87 			goto abort;
88 		data->fault_status |= (rv & 0x3F) << 6;
89 
90 		for (i = 0; i < NR_CHANNEL; i++) {
91 			rv = i2c_smbus_read_word_swapped(client,
92 					MAX31790_REG_TACH_COUNT(i));
93 			if (rv < 0)
94 				goto abort;
95 			data->tach[i] = rv;
96 
97 			if (data->fan_config[i]
98 			    & MAX31790_FAN_CFG_TACH_INPUT) {
99 				rv = i2c_smbus_read_word_swapped(client,
100 					MAX31790_REG_TACH_COUNT(NR_CHANNEL
101 								+ i));
102 				if (rv < 0)
103 					goto abort;
104 				data->tach[NR_CHANNEL + i] = rv;
105 			} else {
106 				rv = i2c_smbus_read_word_swapped(client,
107 						MAX31790_REG_PWMOUT(i));
108 				if (rv < 0)
109 					goto abort;
110 				data->pwm[i] = rv;
111 
112 				rv = i2c_smbus_read_word_swapped(client,
113 						MAX31790_REG_TARGET_COUNT(i));
114 				if (rv < 0)
115 					goto abort;
116 				data->target_count[i] = rv;
117 			}
118 		}
119 
120 		data->last_updated = jiffies;
121 		data->valid = true;
122 	}
123 	goto done;
124 
125 abort:
126 	data->valid = false;
127 	ret = ERR_PTR(rv);
128 
129 done:
130 	mutex_unlock(&data->update_lock);
131 
132 	return ret;
133 }
134 
135 static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
136 
137 static u8 get_tach_period(u8 fan_dynamics)
138 {
139 	return tach_period[SR_FROM_REG(fan_dynamics)];
140 }
141 
142 static u8 bits_for_tach_period(int rpm)
143 {
144 	u8 bits;
145 
146 	if (rpm < 500)
147 		bits = 0x0;
148 	else if (rpm < 1000)
149 		bits = 0x1;
150 	else if (rpm < 2000)
151 		bits = 0x2;
152 	else if (rpm < 4000)
153 		bits = 0x3;
154 	else if (rpm < 8000)
155 		bits = 0x4;
156 	else
157 		bits = 0x5;
158 
159 	return bits;
160 }
161 
162 static int max31790_read_fan(struct device *dev, u32 attr, int channel,
163 			     long *val)
164 {
165 	struct max31790_data *data = max31790_update_device(dev);
166 	int sr, rpm;
167 
168 	if (IS_ERR(data))
169 		return PTR_ERR(data);
170 
171 	switch (attr) {
172 	case hwmon_fan_input:
173 		sr = get_tach_period(data->fan_dynamics[channel]);
174 		rpm = RPM_FROM_REG(data->tach[channel], sr);
175 		*val = rpm;
176 		return 0;
177 	case hwmon_fan_target:
178 		sr = get_tach_period(data->fan_dynamics[channel]);
179 		rpm = RPM_FROM_REG(data->target_count[channel], sr);
180 		*val = rpm;
181 		return 0;
182 	case hwmon_fan_fault:
183 		*val = !!(data->fault_status & (1 << channel));
184 		return 0;
185 	default:
186 		return -EOPNOTSUPP;
187 	}
188 }
189 
190 static int max31790_write_fan(struct device *dev, u32 attr, int channel,
191 			      long val)
192 {
193 	struct max31790_data *data = dev_get_drvdata(dev);
194 	struct i2c_client *client = data->client;
195 	int target_count;
196 	int err = 0;
197 	u8 bits;
198 	int sr;
199 
200 	mutex_lock(&data->update_lock);
201 
202 	switch (attr) {
203 	case hwmon_fan_target:
204 		val = clamp_val(val, FAN_RPM_MIN, FAN_RPM_MAX);
205 		bits = bits_for_tach_period(val);
206 		data->fan_dynamics[channel] =
207 			((data->fan_dynamics[channel] &
208 			  ~MAX31790_FAN_DYN_SR_MASK) |
209 			 (bits << MAX31790_FAN_DYN_SR_SHIFT));
210 		err = i2c_smbus_write_byte_data(client,
211 					MAX31790_REG_FAN_DYNAMICS(channel),
212 					data->fan_dynamics[channel]);
213 		if (err < 0)
214 			break;
215 
216 		sr = get_tach_period(data->fan_dynamics[channel]);
217 		target_count = RPM_TO_REG(val, sr);
218 		target_count = clamp_val(target_count, 0x1, 0x7FF);
219 
220 		data->target_count[channel] = target_count << 5;
221 
222 		err = i2c_smbus_write_word_swapped(client,
223 					MAX31790_REG_TARGET_COUNT(channel),
224 					data->target_count[channel]);
225 		break;
226 	default:
227 		err = -EOPNOTSUPP;
228 		break;
229 	}
230 
231 	mutex_unlock(&data->update_lock);
232 
233 	return err;
234 }
235 
236 static umode_t max31790_fan_is_visible(const void *_data, u32 attr, int channel)
237 {
238 	const struct max31790_data *data = _data;
239 	u8 fan_config = data->fan_config[channel % NR_CHANNEL];
240 
241 	switch (attr) {
242 	case hwmon_fan_input:
243 	case hwmon_fan_fault:
244 		if (channel < NR_CHANNEL ||
245 		    (fan_config & MAX31790_FAN_CFG_TACH_INPUT))
246 			return 0444;
247 		return 0;
248 	case hwmon_fan_target:
249 		if (channel < NR_CHANNEL &&
250 		    !(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
251 			return 0644;
252 		return 0;
253 	default:
254 		return 0;
255 	}
256 }
257 
258 static int max31790_read_pwm(struct device *dev, u32 attr, int channel,
259 			     long *val)
260 {
261 	struct max31790_data *data = max31790_update_device(dev);
262 	u8 fan_config;
263 
264 	if (IS_ERR(data))
265 		return PTR_ERR(data);
266 
267 	fan_config = data->fan_config[channel];
268 
269 	switch (attr) {
270 	case hwmon_pwm_input:
271 		*val = data->pwm[channel] >> 8;
272 		return 0;
273 	case hwmon_pwm_enable:
274 		if (fan_config & MAX31790_FAN_CFG_RPM_MODE)
275 			*val = 2;
276 		else if (fan_config & MAX31790_FAN_CFG_TACH_INPUT_EN)
277 			*val = 1;
278 		else
279 			*val = 0;
280 		return 0;
281 	default:
282 		return -EOPNOTSUPP;
283 	}
284 }
285 
286 static int max31790_write_pwm(struct device *dev, u32 attr, int channel,
287 			      long val)
288 {
289 	struct max31790_data *data = dev_get_drvdata(dev);
290 	struct i2c_client *client = data->client;
291 	u8 fan_config;
292 	int err = 0;
293 
294 	mutex_lock(&data->update_lock);
295 
296 	switch (attr) {
297 	case hwmon_pwm_input:
298 		if (val < 0 || val > 255) {
299 			err = -EINVAL;
300 			break;
301 		}
302 		data->pwm[channel] = val << 8;
303 		err = i2c_smbus_write_word_swapped(client,
304 						   MAX31790_REG_PWMOUT(channel),
305 						   data->pwm[channel]);
306 		break;
307 	case hwmon_pwm_enable:
308 		fan_config = data->fan_config[channel];
309 		if (val == 0) {
310 			fan_config &= ~(MAX31790_FAN_CFG_TACH_INPUT_EN |
311 					MAX31790_FAN_CFG_RPM_MODE);
312 		} else if (val == 1) {
313 			fan_config = (fan_config |
314 				      MAX31790_FAN_CFG_TACH_INPUT_EN) &
315 				     ~MAX31790_FAN_CFG_RPM_MODE;
316 		} else if (val == 2) {
317 			fan_config |= MAX31790_FAN_CFG_TACH_INPUT_EN |
318 				      MAX31790_FAN_CFG_RPM_MODE;
319 		} else {
320 			err = -EINVAL;
321 			break;
322 		}
323 		data->fan_config[channel] = fan_config;
324 		err = i2c_smbus_write_byte_data(client,
325 					MAX31790_REG_FAN_CONFIG(channel),
326 					fan_config);
327 		break;
328 	default:
329 		err = -EOPNOTSUPP;
330 		break;
331 	}
332 
333 	mutex_unlock(&data->update_lock);
334 
335 	return err;
336 }
337 
338 static umode_t max31790_pwm_is_visible(const void *_data, u32 attr, int channel)
339 {
340 	const struct max31790_data *data = _data;
341 	u8 fan_config = data->fan_config[channel];
342 
343 	switch (attr) {
344 	case hwmon_pwm_input:
345 	case hwmon_pwm_enable:
346 		if (!(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
347 			return 0644;
348 		return 0;
349 	default:
350 		return 0;
351 	}
352 }
353 
354 static int max31790_read(struct device *dev, enum hwmon_sensor_types type,
355 			 u32 attr, int channel, long *val)
356 {
357 	switch (type) {
358 	case hwmon_fan:
359 		return max31790_read_fan(dev, attr, channel, val);
360 	case hwmon_pwm:
361 		return max31790_read_pwm(dev, attr, channel, val);
362 	default:
363 		return -EOPNOTSUPP;
364 	}
365 }
366 
367 static int max31790_write(struct device *dev, enum hwmon_sensor_types type,
368 			  u32 attr, int channel, long val)
369 {
370 	switch (type) {
371 	case hwmon_fan:
372 		return max31790_write_fan(dev, attr, channel, val);
373 	case hwmon_pwm:
374 		return max31790_write_pwm(dev, attr, channel, val);
375 	default:
376 		return -EOPNOTSUPP;
377 	}
378 }
379 
380 static umode_t max31790_is_visible(const void *data,
381 				   enum hwmon_sensor_types type,
382 				   u32 attr, int channel)
383 {
384 	switch (type) {
385 	case hwmon_fan:
386 		return max31790_fan_is_visible(data, attr, channel);
387 	case hwmon_pwm:
388 		return max31790_pwm_is_visible(data, attr, channel);
389 	default:
390 		return 0;
391 	}
392 }
393 
394 static const struct hwmon_channel_info *max31790_info[] = {
395 	HWMON_CHANNEL_INFO(fan,
396 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
397 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
398 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
399 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
400 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
401 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
402 			   HWMON_F_INPUT | HWMON_F_FAULT,
403 			   HWMON_F_INPUT | HWMON_F_FAULT,
404 			   HWMON_F_INPUT | HWMON_F_FAULT,
405 			   HWMON_F_INPUT | HWMON_F_FAULT,
406 			   HWMON_F_INPUT | HWMON_F_FAULT,
407 			   HWMON_F_INPUT | HWMON_F_FAULT),
408 	HWMON_CHANNEL_INFO(pwm,
409 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
410 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
411 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
412 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
413 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
414 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
415 	NULL
416 };
417 
418 static const struct hwmon_ops max31790_hwmon_ops = {
419 	.is_visible = max31790_is_visible,
420 	.read = max31790_read,
421 	.write = max31790_write,
422 };
423 
424 static const struct hwmon_chip_info max31790_chip_info = {
425 	.ops = &max31790_hwmon_ops,
426 	.info = max31790_info,
427 };
428 
429 static int max31790_init_client(struct i2c_client *client,
430 				struct max31790_data *data)
431 {
432 	int i, rv;
433 
434 	for (i = 0; i < NR_CHANNEL; i++) {
435 		rv = i2c_smbus_read_byte_data(client,
436 				MAX31790_REG_FAN_CONFIG(i));
437 		if (rv < 0)
438 			return rv;
439 		data->fan_config[i] = rv;
440 
441 		rv = i2c_smbus_read_byte_data(client,
442 				MAX31790_REG_FAN_DYNAMICS(i));
443 		if (rv < 0)
444 			return rv;
445 		data->fan_dynamics[i] = rv;
446 	}
447 
448 	return 0;
449 }
450 
451 static int max31790_probe(struct i2c_client *client)
452 {
453 	struct i2c_adapter *adapter = client->adapter;
454 	struct device *dev = &client->dev;
455 	struct max31790_data *data;
456 	struct device *hwmon_dev;
457 	int err;
458 
459 	if (!i2c_check_functionality(adapter,
460 			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
461 		return -ENODEV;
462 
463 	data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL);
464 	if (!data)
465 		return -ENOMEM;
466 
467 	data->client = client;
468 	mutex_init(&data->update_lock);
469 
470 	/*
471 	 * Initialize the max31790 chip
472 	 */
473 	err = max31790_init_client(client, data);
474 	if (err)
475 		return err;
476 
477 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
478 							 data,
479 							 &max31790_chip_info,
480 							 NULL);
481 
482 	return PTR_ERR_OR_ZERO(hwmon_dev);
483 }
484 
485 static const struct i2c_device_id max31790_id[] = {
486 	{ "max31790", 0 },
487 	{ }
488 };
489 MODULE_DEVICE_TABLE(i2c, max31790_id);
490 
491 static struct i2c_driver max31790_driver = {
492 	.class		= I2C_CLASS_HWMON,
493 	.probe_new	= max31790_probe,
494 	.driver = {
495 		.name	= "max31790",
496 	},
497 	.id_table	= max31790_id,
498 };
499 
500 module_i2c_driver(max31790_driver);
501 
502 MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>");
503 MODULE_DESCRIPTION("MAX31790 sensor driver");
504 MODULE_LICENSE("GPL");
505