1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Hardware monitoring driver for EMC2305 fan controller
4  *
5  * Copyright (C) 2022 Nvidia Technologies Ltd.
6  */
7 
8 #include <linux/err.h>
9 #include <linux/hwmon.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/platform_data/emc2305.h>
13 #include <linux/thermal.h>
14 
15 #define EMC2305_REG_DRIVE_FAIL_STATUS	0x27
16 #define EMC2305_REG_VENDOR		0xfe
17 #define EMC2305_FAN_MAX			0xff
18 #define EMC2305_FAN_MIN			0x00
19 #define EMC2305_FAN_MAX_STATE		10
20 #define EMC2305_DEVICE			0x34
21 #define EMC2305_VENDOR			0x5d
22 #define EMC2305_REG_PRODUCT_ID		0xfd
23 #define EMC2305_TACH_REGS_UNUSE_BITS	3
24 #define EMC2305_TACH_CNT_MULTIPLIER	0x02
25 #define EMC2305_TACH_RANGE_MIN		480
26 
27 #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
28 	DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max))
29 #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
30 	DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state))
31 
32 /*
33  * Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
34  * equal (poles * 2 + 1).
35  */
36 #define EMC2305_RPM_FACTOR		3932160
37 
38 #define EMC2305_REG_FAN_DRIVE(n)	(0x30 + 0x10 * (n))
39 #define EMC2305_REG_FAN_MIN_DRIVE(n)	(0x38 + 0x10 * (n))
40 #define EMC2305_REG_FAN_TACH(n)		(0x3e + 0x10 * (n))
41 
42 enum emc230x_product_id {
43 	EMC2305 = 0x34,
44 	EMC2303 = 0x35,
45 	EMC2302 = 0x36,
46 	EMC2301 = 0x37,
47 };
48 
49 static const struct i2c_device_id emc2305_ids[] = {
50 	{ "emc2305" },
51 	{ "emc2303" },
52 	{ "emc2302" },
53 	{ "emc2301" },
54 	{ }
55 };
56 MODULE_DEVICE_TABLE(i2c, emc2305_ids);
57 
58 /**
59  * struct emc2305_cdev_data - device-specific cooling device state
60  * @cdev: cooling device
61  * @cur_state: cooling current state
62  * @last_hwmon_state: last cooling state updated by hwmon subsystem
63  * @last_thermal_state: last cooling state updated by thermal subsystem
64  *
65  * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
66  * speed feature. The purpose of this feature is to provides ability to limit fan speed
67  * according to some system wise considerations, like absence of some replaceable units (PSU or
68  * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
69  * some other factors which indirectly impacts system's airflow
70  * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
71  * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
72  * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
73  * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
74  * attribute.
75  * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
76  * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
77  * speed will be just stored with no PWM update.
78  */
79 struct emc2305_cdev_data {
80 	struct thermal_cooling_device *cdev;
81 	unsigned int cur_state;
82 	unsigned long last_hwmon_state;
83 	unsigned long last_thermal_state;
84 };
85 
86 /**
87  * struct emc2305_data - device-specific data
88  * @client: i2c client
89  * @hwmon_dev: hwmon device
90  * @max_state: maximum cooling state of the cooling device
91  * @pwm_num: number of PWM channels
92  * @pwm_separate: separate PWM settings for every channel
93  * @pwm_min: array of minimum PWM per channel
94  * @cdev_data: array of cooling devices data
95  */
96 struct emc2305_data {
97 	struct i2c_client *client;
98 	struct device *hwmon_dev;
99 	u8 max_state;
100 	u8 pwm_num;
101 	bool pwm_separate;
102 	u8 pwm_min[EMC2305_PWM_MAX];
103 	struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
104 };
105 
106 static char *emc2305_fan_name[] = {
107 	"emc2305_fan",
108 	"emc2305_fan1",
109 	"emc2305_fan2",
110 	"emc2305_fan3",
111 	"emc2305_fan4",
112 	"emc2305_fan5",
113 };
114 
115 static int emc2305_get_max_channel(const struct emc2305_data *data)
116 {
117 	return data->pwm_num;
118 }
119 
120 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
121 {
122 	struct emc2305_data *data = cdev->devdata;
123 	size_t len = strlen(cdev->type);
124 	int ret;
125 
126 	if (len <= 0)
127 		return -EINVAL;
128 
129 	/*
130 	 * Returns index of cooling device 0..4 in case of separate PWM setting.
131 	 * Zero index is used in case of one common PWM setting.
132 	 * If the mode is not set as pwm_separate, all PWMs are to be bound
133 	 * to the common thermal zone and should work at the same speed
134 	 * to perform cooling for the same thermal junction.
135 	 * Otherwise, return specific channel that will be used in bound
136 	 * related PWM to the thermal zone.
137 	 */
138 	if (!data->pwm_separate)
139 		return 0;
140 
141 	ret = cdev->type[len - 1];
142 	switch (ret) {
143 	case '1' ... '5':
144 		return ret - '1';
145 	default:
146 		break;
147 	}
148 	return -EINVAL;
149 }
150 
151 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
152 {
153 	int cdev_idx;
154 	struct emc2305_data *data = cdev->devdata;
155 
156 	cdev_idx = emc2305_get_cdev_idx(cdev);
157 	if (cdev_idx < 0)
158 		return cdev_idx;
159 
160 	*state = data->cdev_data[cdev_idx].cur_state;
161 	return 0;
162 }
163 
164 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
165 {
166 	struct emc2305_data *data = cdev->devdata;
167 	*state = data->max_state;
168 	return 0;
169 }
170 
171 static int __emc2305_set_cur_state(struct emc2305_data *data, int cdev_idx, unsigned long state)
172 {
173 	int ret;
174 	struct i2c_client *client = data->client;
175 	u8 val, i;
176 
177 	state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
178 
179 	val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX);
180 
181 	data->cdev_data[cdev_idx].cur_state = state;
182 	if (data->pwm_separate) {
183 		ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
184 		if (ret < 0)
185 			return ret;
186 	} else {
187 		/*
188 		 * Set the same PWM value in all channels
189 		 * if common PWM channel is used.
190 		 */
191 		for (i = 0; i < data->pwm_num; i++) {
192 			ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
193 			if (ret < 0)
194 				return ret;
195 		}
196 	}
197 
198 	return 0;
199 }
200 
201 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
202 {
203 	int cdev_idx, ret;
204 	struct emc2305_data *data = cdev->devdata;
205 
206 	if (state > data->max_state)
207 		return -EINVAL;
208 
209 	cdev_idx =  emc2305_get_cdev_idx(cdev);
210 	if (cdev_idx < 0)
211 		return cdev_idx;
212 
213 	/* Save thermal state. */
214 	data->cdev_data[cdev_idx].last_thermal_state = state;
215 	ret = __emc2305_set_cur_state(data, cdev_idx, state);
216 	if (ret < 0)
217 		return ret;
218 
219 	return 0;
220 }
221 
222 static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
223 	.get_max_state = emc2305_get_max_state,
224 	.get_cur_state = emc2305_get_cur_state,
225 	.set_cur_state = emc2305_set_cur_state,
226 };
227 
228 static int emc2305_show_fault(struct device *dev, int channel)
229 {
230 	struct emc2305_data *data = dev_get_drvdata(dev);
231 	struct i2c_client *client = data->client;
232 	int status_reg;
233 
234 	status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
235 	if (status_reg < 0)
236 		return status_reg;
237 
238 	return status_reg & (1 << channel) ? 1 : 0;
239 }
240 
241 static int emc2305_show_fan(struct device *dev, int channel)
242 {
243 	struct emc2305_data *data = dev_get_drvdata(dev);
244 	struct i2c_client *client = data->client;
245 	int ret;
246 
247 	ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
248 	if (ret <= 0)
249 		return ret;
250 
251 	ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
252 	ret = EMC2305_RPM_FACTOR / ret;
253 	if (ret <= EMC2305_TACH_RANGE_MIN)
254 		return 0;
255 
256 	return ret * EMC2305_TACH_CNT_MULTIPLIER;
257 }
258 
259 static int emc2305_show_pwm(struct device *dev, int channel)
260 {
261 	struct emc2305_data *data = dev_get_drvdata(dev);
262 	struct i2c_client *client = data->client;
263 
264 	return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
265 }
266 
267 static int emc2305_set_pwm(struct device *dev, long val, int channel)
268 {
269 	struct emc2305_data *data = dev_get_drvdata(dev);
270 	struct i2c_client *client = data->client;
271 	int ret;
272 
273 	if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX)
274 		return -EINVAL;
275 
276 	ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
277 	if (ret < 0)
278 		return ret;
279 	data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
280 								    EMC2305_FAN_MAX);
281 	return 0;
282 }
283 
284 static int emc2305_set_single_tz(struct device *dev, int idx)
285 {
286 	struct emc2305_data *data = dev_get_drvdata(dev);
287 	long pwm;
288 	int i, cdev_idx, ret;
289 
290 	cdev_idx = (idx) ? idx - 1 : 0;
291 	pwm = data->pwm_min[cdev_idx];
292 
293 	data->cdev_data[cdev_idx].cdev =
294 		devm_thermal_of_cooling_device_register(dev, dev->of_node,
295 							emc2305_fan_name[idx], data,
296 							&emc2305_cooling_ops);
297 
298 	if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
299 		dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
300 		return PTR_ERR(data->cdev_data[cdev_idx].cdev);
301 	}
302 	/* Set minimal PWM speed. */
303 	if (data->pwm_separate) {
304 		ret = emc2305_set_pwm(dev, pwm, cdev_idx);
305 		if (ret < 0)
306 			return ret;
307 	} else {
308 		for (i = 0; i < data->pwm_num; i++) {
309 			ret = emc2305_set_pwm(dev, pwm, i);
310 			if (ret < 0)
311 				return ret;
312 		}
313 	}
314 	data->cdev_data[cdev_idx].cur_state =
315 		EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state,
316 				       EMC2305_FAN_MAX);
317 	data->cdev_data[cdev_idx].last_hwmon_state =
318 		EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state,
319 				       EMC2305_FAN_MAX);
320 	return 0;
321 }
322 
323 static int emc2305_set_tz(struct device *dev)
324 {
325 	struct emc2305_data *data = dev_get_drvdata(dev);
326 	int i, ret;
327 
328 	if (!data->pwm_separate)
329 		return emc2305_set_single_tz(dev, 0);
330 
331 	for (i = 0; i < data->pwm_num; i++) {
332 		ret = emc2305_set_single_tz(dev, i + 1);
333 		if (ret)
334 			return ret;
335 	}
336 	return 0;
337 }
338 
339 static umode_t
340 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
341 {
342 	int max_channel = emc2305_get_max_channel(data);
343 
344 	/* Don't show channels which are not physically connected. */
345 	if (channel >= max_channel)
346 		return 0;
347 	switch (type) {
348 	case hwmon_fan:
349 		switch (attr) {
350 		case hwmon_fan_input:
351 			return 0444;
352 		case hwmon_fan_fault:
353 			return 0444;
354 		default:
355 			break;
356 		}
357 		break;
358 	case hwmon_pwm:
359 		switch (attr) {
360 		case hwmon_pwm_input:
361 			return 0644;
362 		default:
363 			break;
364 		}
365 		break;
366 	default:
367 		break;
368 	}
369 
370 	return 0;
371 };
372 
373 static int
374 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
375 {
376 	struct emc2305_data *data = dev_get_drvdata(dev);
377 	int cdev_idx;
378 
379 	switch (type) {
380 	case hwmon_pwm:
381 		switch (attr) {
382 		case hwmon_pwm_input:
383 			/* If thermal is configured - handle PWM limit setting. */
384 			if (IS_REACHABLE(CONFIG_THERMAL)) {
385 				if (data->pwm_separate)
386 					cdev_idx = channel;
387 				else
388 					cdev_idx = 0;
389 				data->cdev_data[cdev_idx].last_hwmon_state =
390 					EMC2305_PWM_DUTY2STATE(val, data->max_state,
391 							       EMC2305_FAN_MAX);
392 				/*
393 				 * Update PWM only in case requested state is not less than the
394 				 * last thermal state.
395 				 */
396 				if (data->cdev_data[cdev_idx].last_hwmon_state >=
397 				    data->cdev_data[cdev_idx].last_thermal_state)
398 					return __emc2305_set_cur_state(data, cdev_idx,
399 							data->cdev_data[cdev_idx].last_hwmon_state);
400 				return 0;
401 			}
402 			return emc2305_set_pwm(dev, val, channel);
403 		default:
404 			break;
405 		}
406 		break;
407 	default:
408 		break;
409 	}
410 
411 	return -EOPNOTSUPP;
412 };
413 
414 static int
415 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
416 {
417 	int ret;
418 
419 	switch (type) {
420 	case hwmon_fan:
421 		switch (attr) {
422 		case hwmon_fan_input:
423 			ret = emc2305_show_fan(dev, channel);
424 			if (ret < 0)
425 				return ret;
426 			*val = ret;
427 			return 0;
428 		case hwmon_fan_fault:
429 			ret = emc2305_show_fault(dev, channel);
430 			if (ret < 0)
431 				return ret;
432 			*val = ret;
433 			return 0;
434 		default:
435 			break;
436 		}
437 		break;
438 	case hwmon_pwm:
439 		switch (attr) {
440 		case hwmon_pwm_input:
441 			ret = emc2305_show_pwm(dev, channel);
442 			if (ret < 0)
443 				return ret;
444 			*val = ret;
445 			return 0;
446 		default:
447 			break;
448 		}
449 		break;
450 	default:
451 		break;
452 	}
453 
454 	return -EOPNOTSUPP;
455 };
456 
457 static const struct hwmon_ops emc2305_ops = {
458 	.is_visible = emc2305_is_visible,
459 	.read = emc2305_read,
460 	.write = emc2305_write,
461 };
462 
463 static const struct hwmon_channel_info * const emc2305_info[] = {
464 	HWMON_CHANNEL_INFO(fan,
465 			   HWMON_F_INPUT | HWMON_F_FAULT,
466 			   HWMON_F_INPUT | HWMON_F_FAULT,
467 			   HWMON_F_INPUT | HWMON_F_FAULT,
468 			   HWMON_F_INPUT | HWMON_F_FAULT,
469 			   HWMON_F_INPUT | HWMON_F_FAULT),
470 	HWMON_CHANNEL_INFO(pwm,
471 			   HWMON_PWM_INPUT,
472 			   HWMON_PWM_INPUT,
473 			   HWMON_PWM_INPUT,
474 			   HWMON_PWM_INPUT,
475 			   HWMON_PWM_INPUT),
476 	NULL
477 };
478 
479 static const struct hwmon_chip_info emc2305_chip_info = {
480 	.ops = &emc2305_ops,
481 	.info = emc2305_info,
482 };
483 
484 static int emc2305_identify(struct device *dev)
485 {
486 	struct i2c_client *client = to_i2c_client(dev);
487 	struct emc2305_data *data = i2c_get_clientdata(client);
488 	int ret;
489 
490 	ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
491 	if (ret < 0)
492 		return ret;
493 
494 	switch (ret) {
495 	case EMC2305:
496 		data->pwm_num = 5;
497 		break;
498 	case EMC2303:
499 		data->pwm_num = 3;
500 		break;
501 	case EMC2302:
502 		data->pwm_num = 2;
503 		break;
504 	case EMC2301:
505 		data->pwm_num = 1;
506 		break;
507 	default:
508 		return -ENODEV;
509 	}
510 
511 	return 0;
512 }
513 
514 static int emc2305_probe(struct i2c_client *client)
515 {
516 	struct i2c_adapter *adapter = client->adapter;
517 	struct device *dev = &client->dev;
518 	struct emc2305_data *data;
519 	struct emc2305_platform_data *pdata;
520 	int vendor;
521 	int ret;
522 	int i;
523 
524 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
525 		return -ENODEV;
526 
527 	vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
528 	if (vendor != EMC2305_VENDOR)
529 		return -ENODEV;
530 
531 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
532 	if (!data)
533 		return -ENOMEM;
534 
535 	i2c_set_clientdata(client, data);
536 	data->client = client;
537 
538 	ret = emc2305_identify(dev);
539 	if (ret)
540 		return ret;
541 
542 	pdata = dev_get_platdata(&client->dev);
543 	if (pdata) {
544 		if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE)
545 			return -EINVAL;
546 		data->max_state = pdata->max_state;
547 		/*
548 		 * Validate a number of active PWM channels. Note that
549 		 * configured number can be less than the actual maximum
550 		 * supported by the device.
551 		 */
552 		if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX)
553 			return -EINVAL;
554 		data->pwm_num = pdata->pwm_num;
555 		data->pwm_separate = pdata->pwm_separate;
556 		for (i = 0; i < EMC2305_PWM_MAX; i++)
557 			data->pwm_min[i] = pdata->pwm_min[i];
558 	} else {
559 		data->max_state = EMC2305_FAN_MAX_STATE;
560 		data->pwm_separate = false;
561 		for (i = 0; i < EMC2305_PWM_MAX; i++)
562 			data->pwm_min[i] = EMC2305_FAN_MIN;
563 	}
564 
565 	data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
566 							       &emc2305_chip_info, NULL);
567 	if (IS_ERR(data->hwmon_dev))
568 		return PTR_ERR(data->hwmon_dev);
569 
570 	if (IS_REACHABLE(CONFIG_THERMAL)) {
571 		ret = emc2305_set_tz(dev);
572 		if (ret != 0)
573 			return ret;
574 	}
575 
576 	for (i = 0; i < data->pwm_num; i++) {
577 		ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
578 						data->pwm_min[i]);
579 		if (ret < 0)
580 			return ret;
581 	}
582 
583 	return 0;
584 }
585 
586 static const struct of_device_id of_emc2305_match_table[] = {
587 	{ .compatible = "microchip,emc2305", },
588 	{},
589 };
590 MODULE_DEVICE_TABLE(of, of_emc2305_match_table);
591 
592 static struct i2c_driver emc2305_driver = {
593 	.driver = {
594 		.name = "emc2305",
595 		.of_match_table = of_emc2305_match_table,
596 	},
597 	.probe = emc2305_probe,
598 	.id_table = emc2305_ids,
599 };
600 
601 module_i2c_driver(emc2305_driver);
602 
603 MODULE_AUTHOR("Nvidia");
604 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
605 MODULE_LICENSE("GPL");
606