• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void emc2305_unset_tz(struct device *dev);
116 
emc2305_get_max_channel(const struct emc2305_data * data)117 static int emc2305_get_max_channel(const struct emc2305_data *data)
118 {
119 	return data->pwm_num;
120 }
121 
emc2305_get_cdev_idx(struct thermal_cooling_device * cdev)122 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
123 {
124 	struct emc2305_data *data = cdev->devdata;
125 	size_t len = strlen(cdev->type);
126 	int ret;
127 
128 	if (len <= 0)
129 		return -EINVAL;
130 
131 	/*
132 	 * Returns index of cooling device 0..4 in case of separate PWM setting.
133 	 * Zero index is used in case of one common PWM setting.
134 	 * If the mode is not set as pwm_separate, all PWMs are to be bound
135 	 * to the common thermal zone and should work at the same speed
136 	 * to perform cooling for the same thermal junction.
137 	 * Otherwise, return specific channel that will be used in bound
138 	 * related PWM to the thermal zone.
139 	 */
140 	if (!data->pwm_separate)
141 		return 0;
142 
143 	ret = cdev->type[len - 1];
144 	switch (ret) {
145 	case '1' ... '5':
146 		return ret - '1';
147 	default:
148 		break;
149 	}
150 	return -EINVAL;
151 }
152 
emc2305_get_cur_state(struct thermal_cooling_device * cdev,unsigned long * state)153 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
154 {
155 	int cdev_idx;
156 	struct emc2305_data *data = cdev->devdata;
157 
158 	cdev_idx = emc2305_get_cdev_idx(cdev);
159 	if (cdev_idx < 0)
160 		return cdev_idx;
161 
162 	*state = data->cdev_data[cdev_idx].cur_state;
163 	return 0;
164 }
165 
emc2305_get_max_state(struct thermal_cooling_device * cdev,unsigned long * state)166 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
167 {
168 	struct emc2305_data *data = cdev->devdata;
169 	*state = data->max_state;
170 	return 0;
171 }
172 
__emc2305_set_cur_state(struct emc2305_data * data,int cdev_idx,unsigned long state)173 static int __emc2305_set_cur_state(struct emc2305_data *data, int cdev_idx, unsigned long state)
174 {
175 	int ret;
176 	struct i2c_client *client = data->client;
177 	u8 val, i;
178 
179 	state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
180 
181 	val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX);
182 
183 	data->cdev_data[cdev_idx].cur_state = state;
184 	if (data->pwm_separate) {
185 		ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
186 		if (ret < 0)
187 			return ret;
188 	} else {
189 		/*
190 		 * Set the same PWM value in all channels
191 		 * if common PWM channel is used.
192 		 */
193 		for (i = 0; i < data->pwm_num; i++) {
194 			ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
195 			if (ret < 0)
196 				return ret;
197 		}
198 	}
199 
200 	return 0;
201 }
202 
emc2305_set_cur_state(struct thermal_cooling_device * cdev,unsigned long state)203 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
204 {
205 	int cdev_idx, ret;
206 	struct emc2305_data *data = cdev->devdata;
207 
208 	if (state > data->max_state)
209 		return -EINVAL;
210 
211 	cdev_idx =  emc2305_get_cdev_idx(cdev);
212 	if (cdev_idx < 0)
213 		return cdev_idx;
214 
215 	/* Save thermal state. */
216 	data->cdev_data[cdev_idx].last_thermal_state = state;
217 	ret = __emc2305_set_cur_state(data, cdev_idx, state);
218 	if (ret < 0)
219 		return ret;
220 
221 	return 0;
222 }
223 
224 static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
225 	.get_max_state = emc2305_get_max_state,
226 	.get_cur_state = emc2305_get_cur_state,
227 	.set_cur_state = emc2305_set_cur_state,
228 };
229 
emc2305_show_fault(struct device * dev,int channel)230 static int emc2305_show_fault(struct device *dev, int channel)
231 {
232 	struct emc2305_data *data = dev_get_drvdata(dev);
233 	struct i2c_client *client = data->client;
234 	int status_reg;
235 
236 	status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
237 	if (status_reg < 0)
238 		return status_reg;
239 
240 	return status_reg & (1 << channel) ? 1 : 0;
241 }
242 
emc2305_show_fan(struct device * dev,int channel)243 static int emc2305_show_fan(struct device *dev, int channel)
244 {
245 	struct emc2305_data *data = dev_get_drvdata(dev);
246 	struct i2c_client *client = data->client;
247 	int ret;
248 
249 	ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
250 	if (ret <= 0)
251 		return ret;
252 
253 	ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
254 	ret = EMC2305_RPM_FACTOR / ret;
255 	if (ret <= EMC2305_TACH_RANGE_MIN)
256 		return 0;
257 
258 	return ret * EMC2305_TACH_CNT_MULTIPLIER;
259 }
260 
emc2305_show_pwm(struct device * dev,int channel)261 static int emc2305_show_pwm(struct device *dev, int channel)
262 {
263 	struct emc2305_data *data = dev_get_drvdata(dev);
264 	struct i2c_client *client = data->client;
265 
266 	return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
267 }
268 
emc2305_set_pwm(struct device * dev,long val,int channel)269 static int emc2305_set_pwm(struct device *dev, long val, int channel)
270 {
271 	struct emc2305_data *data = dev_get_drvdata(dev);
272 	struct i2c_client *client = data->client;
273 	int ret;
274 
275 	if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX)
276 		return -EINVAL;
277 
278 	ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
279 	if (ret < 0)
280 		return ret;
281 	data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
282 								    EMC2305_FAN_MAX);
283 	return 0;
284 }
285 
emc2305_set_single_tz(struct device * dev,int idx)286 static int emc2305_set_single_tz(struct device *dev, int idx)
287 {
288 	struct emc2305_data *data = dev_get_drvdata(dev);
289 	long pwm;
290 	int i, cdev_idx, ret;
291 
292 	cdev_idx = (idx) ? idx - 1 : 0;
293 	pwm = data->pwm_min[cdev_idx];
294 
295 	data->cdev_data[cdev_idx].cdev =
296 		thermal_cooling_device_register(emc2305_fan_name[idx], data,
297 						&emc2305_cooling_ops);
298 
299 	if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
300 		dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
301 		return PTR_ERR(data->cdev_data[cdev_idx].cdev);
302 	}
303 
304 	if (data->cdev_data[cdev_idx].cur_state > 0)
305 		/* Update pwm when temperature is above trips */
306 		pwm = EMC2305_PWM_STATE2DUTY(data->cdev_data[cdev_idx].cur_state,
307 					     data->max_state, EMC2305_FAN_MAX);
308 
309 	/* Set minimal PWM speed. */
310 	if (data->pwm_separate) {
311 		ret = emc2305_set_pwm(dev, pwm, cdev_idx);
312 		if (ret < 0)
313 			return ret;
314 	} else {
315 		for (i = 0; i < data->pwm_num; i++) {
316 			ret = emc2305_set_pwm(dev, pwm, i);
317 			if (ret < 0)
318 				return ret;
319 		}
320 	}
321 	data->cdev_data[cdev_idx].cur_state =
322 		EMC2305_PWM_DUTY2STATE(pwm, data->max_state,
323 				       EMC2305_FAN_MAX);
324 	data->cdev_data[cdev_idx].last_hwmon_state =
325 		EMC2305_PWM_DUTY2STATE(pwm, data->max_state,
326 				       EMC2305_FAN_MAX);
327 	return 0;
328 }
329 
emc2305_set_tz(struct device * dev)330 static int emc2305_set_tz(struct device *dev)
331 {
332 	struct emc2305_data *data = dev_get_drvdata(dev);
333 	int i, ret;
334 
335 	if (!data->pwm_separate)
336 		return emc2305_set_single_tz(dev, 0);
337 
338 	for (i = 0; i < data->pwm_num; i++) {
339 		ret = emc2305_set_single_tz(dev, i + 1);
340 		if (ret)
341 			goto thermal_cooling_device_register_fail;
342 	}
343 	return 0;
344 
345 thermal_cooling_device_register_fail:
346 	emc2305_unset_tz(dev);
347 	return ret;
348 }
349 
emc2305_unset_tz(struct device * dev)350 static void emc2305_unset_tz(struct device *dev)
351 {
352 	struct emc2305_data *data = dev_get_drvdata(dev);
353 	int i;
354 
355 	/* Unregister cooling device. */
356 	for (i = 0; i < EMC2305_PWM_MAX; i++)
357 		if (data->cdev_data[i].cdev)
358 			thermal_cooling_device_unregister(data->cdev_data[i].cdev);
359 }
360 
361 static umode_t
emc2305_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)362 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
363 {
364 	int max_channel = emc2305_get_max_channel(data);
365 
366 	/* Don't show channels which are not physically connected. */
367 	if (channel >= max_channel)
368 		return 0;
369 	switch (type) {
370 	case hwmon_fan:
371 		switch (attr) {
372 		case hwmon_fan_input:
373 			return 0444;
374 		case hwmon_fan_fault:
375 			return 0444;
376 		default:
377 			break;
378 		}
379 		break;
380 	case hwmon_pwm:
381 		switch (attr) {
382 		case hwmon_pwm_input:
383 			return 0644;
384 		default:
385 			break;
386 		}
387 		break;
388 	default:
389 		break;
390 	}
391 
392 	return 0;
393 };
394 
395 static int
emc2305_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)396 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
397 {
398 	struct emc2305_data *data = dev_get_drvdata(dev);
399 	int cdev_idx;
400 
401 	switch (type) {
402 	case hwmon_pwm:
403 		switch (attr) {
404 		case hwmon_pwm_input:
405 			/* If thermal is configured - handle PWM limit setting. */
406 			if (IS_REACHABLE(CONFIG_THERMAL)) {
407 				if (data->pwm_separate)
408 					cdev_idx = channel;
409 				else
410 					cdev_idx = 0;
411 				data->cdev_data[cdev_idx].last_hwmon_state =
412 					EMC2305_PWM_DUTY2STATE(val, data->max_state,
413 							       EMC2305_FAN_MAX);
414 				/*
415 				 * Update PWM only in case requested state is not less than the
416 				 * last thermal state.
417 				 */
418 				if (data->cdev_data[cdev_idx].last_hwmon_state >=
419 				    data->cdev_data[cdev_idx].last_thermal_state)
420 					return __emc2305_set_cur_state(data, cdev_idx,
421 							data->cdev_data[cdev_idx].last_hwmon_state);
422 				return 0;
423 			}
424 			return emc2305_set_pwm(dev, val, channel);
425 		default:
426 			break;
427 		}
428 		break;
429 	default:
430 		break;
431 	}
432 
433 	return -EOPNOTSUPP;
434 };
435 
436 static int
emc2305_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)437 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
438 {
439 	int ret;
440 
441 	switch (type) {
442 	case hwmon_fan:
443 		switch (attr) {
444 		case hwmon_fan_input:
445 			ret = emc2305_show_fan(dev, channel);
446 			if (ret < 0)
447 				return ret;
448 			*val = ret;
449 			return 0;
450 		case hwmon_fan_fault:
451 			ret = emc2305_show_fault(dev, channel);
452 			if (ret < 0)
453 				return ret;
454 			*val = ret;
455 			return 0;
456 		default:
457 			break;
458 		}
459 		break;
460 	case hwmon_pwm:
461 		switch (attr) {
462 		case hwmon_pwm_input:
463 			ret = emc2305_show_pwm(dev, channel);
464 			if (ret < 0)
465 				return ret;
466 			*val = ret;
467 			return 0;
468 		default:
469 			break;
470 		}
471 		break;
472 	default:
473 		break;
474 	}
475 
476 	return -EOPNOTSUPP;
477 };
478 
479 static const struct hwmon_ops emc2305_ops = {
480 	.is_visible = emc2305_is_visible,
481 	.read = emc2305_read,
482 	.write = emc2305_write,
483 };
484 
485 static const struct hwmon_channel_info * const emc2305_info[] = {
486 	HWMON_CHANNEL_INFO(fan,
487 			   HWMON_F_INPUT | HWMON_F_FAULT,
488 			   HWMON_F_INPUT | HWMON_F_FAULT,
489 			   HWMON_F_INPUT | HWMON_F_FAULT,
490 			   HWMON_F_INPUT | HWMON_F_FAULT,
491 			   HWMON_F_INPUT | HWMON_F_FAULT),
492 	HWMON_CHANNEL_INFO(pwm,
493 			   HWMON_PWM_INPUT,
494 			   HWMON_PWM_INPUT,
495 			   HWMON_PWM_INPUT,
496 			   HWMON_PWM_INPUT,
497 			   HWMON_PWM_INPUT),
498 	NULL
499 };
500 
501 static const struct hwmon_chip_info emc2305_chip_info = {
502 	.ops = &emc2305_ops,
503 	.info = emc2305_info,
504 };
505 
emc2305_identify(struct device * dev)506 static int emc2305_identify(struct device *dev)
507 {
508 	struct i2c_client *client = to_i2c_client(dev);
509 	struct emc2305_data *data = i2c_get_clientdata(client);
510 	int ret;
511 
512 	ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
513 	if (ret < 0)
514 		return ret;
515 
516 	switch (ret) {
517 	case EMC2305:
518 		data->pwm_num = 5;
519 		break;
520 	case EMC2303:
521 		data->pwm_num = 3;
522 		break;
523 	case EMC2302:
524 		data->pwm_num = 2;
525 		break;
526 	case EMC2301:
527 		data->pwm_num = 1;
528 		break;
529 	default:
530 		return -ENODEV;
531 	}
532 
533 	return 0;
534 }
535 
emc2305_probe(struct i2c_client * client)536 static int emc2305_probe(struct i2c_client *client)
537 {
538 	struct i2c_adapter *adapter = client->adapter;
539 	struct device *dev = &client->dev;
540 	struct emc2305_data *data;
541 	struct emc2305_platform_data *pdata;
542 	int vendor;
543 	int ret;
544 	int i;
545 
546 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
547 		return -ENODEV;
548 
549 	vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
550 	if (vendor != EMC2305_VENDOR)
551 		return -ENODEV;
552 
553 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
554 	if (!data)
555 		return -ENOMEM;
556 
557 	i2c_set_clientdata(client, data);
558 	data->client = client;
559 
560 	ret = emc2305_identify(dev);
561 	if (ret)
562 		return ret;
563 
564 	pdata = dev_get_platdata(&client->dev);
565 	if (pdata) {
566 		if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE)
567 			return -EINVAL;
568 		data->max_state = pdata->max_state;
569 		/*
570 		 * Validate a number of active PWM channels. Note that
571 		 * configured number can be less than the actual maximum
572 		 * supported by the device.
573 		 */
574 		if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX)
575 			return -EINVAL;
576 		data->pwm_num = pdata->pwm_num;
577 		data->pwm_separate = pdata->pwm_separate;
578 		for (i = 0; i < EMC2305_PWM_MAX; i++)
579 			data->pwm_min[i] = pdata->pwm_min[i];
580 	} else {
581 		data->max_state = EMC2305_FAN_MAX_STATE;
582 		data->pwm_separate = false;
583 		for (i = 0; i < EMC2305_PWM_MAX; i++)
584 			data->pwm_min[i] = EMC2305_FAN_MIN;
585 	}
586 
587 	data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
588 							       &emc2305_chip_info, NULL);
589 	if (IS_ERR(data->hwmon_dev))
590 		return PTR_ERR(data->hwmon_dev);
591 
592 	if (IS_REACHABLE(CONFIG_THERMAL)) {
593 		ret = emc2305_set_tz(dev);
594 		if (ret != 0)
595 			return ret;
596 	}
597 
598 	for (i = 0; i < data->pwm_num; i++) {
599 		ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
600 						data->pwm_min[i]);
601 		if (ret < 0)
602 			return ret;
603 	}
604 
605 	return 0;
606 }
607 
emc2305_remove(struct i2c_client * client)608 static void emc2305_remove(struct i2c_client *client)
609 {
610 	struct device *dev = &client->dev;
611 
612 	if (IS_REACHABLE(CONFIG_THERMAL))
613 		emc2305_unset_tz(dev);
614 }
615 
616 static struct i2c_driver emc2305_driver = {
617 	.driver = {
618 		.name = "emc2305",
619 	},
620 	.probe = emc2305_probe,
621 	.remove	  = emc2305_remove,
622 	.id_table = emc2305_ids,
623 };
624 
625 module_i2c_driver(emc2305_driver);
626 
627 MODULE_AUTHOR("Nvidia");
628 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
629 MODULE_LICENSE("GPL");
630