1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Delta AHE-50DC power shelf fan control module driver
4  *
5  * Copyright 2021 Zev Weiss <zev@bewilderbeest.net>
6  */
7 
8 #include <linux/i2c.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/pmbus.h>
12 
13 #include "pmbus.h"
14 
15 #define AHE50DC_PMBUS_READ_TEMP4 0xd0
16 
ahe50dc_fan_write_byte(struct i2c_client * client,int page,u8 value)17 static int ahe50dc_fan_write_byte(struct i2c_client *client, int page, u8 value)
18 {
19 	/*
20 	 * The CLEAR_FAULTS operation seems to sometimes (unpredictably, perhaps
21 	 * 5% of the time or so) trigger a problematic phenomenon in which the
22 	 * fan speeds surge momentarily and at least some (perhaps all?) of the
23 	 * system's power outputs experience a glitch.
24 	 *
25 	 * However, according to Delta it should be OK to simply not send any
26 	 * CLEAR_FAULTS commands (the device doesn't seem to be capable of
27 	 * reporting any faults anyway), so just blackhole them unconditionally.
28 	 */
29 	return value == PMBUS_CLEAR_FAULTS ? -EOPNOTSUPP : -ENODATA;
30 }
31 
ahe50dc_fan_read_word_data(struct i2c_client * client,int page,int phase,int reg)32 static int ahe50dc_fan_read_word_data(struct i2c_client *client, int page, int phase, int reg)
33 {
34 	/* temp1 in (virtual) page 1 is remapped to mfr-specific temp4 */
35 	if (page == 1) {
36 		if (reg == PMBUS_READ_TEMPERATURE_1)
37 			return i2c_smbus_read_word_data(client, AHE50DC_PMBUS_READ_TEMP4);
38 		return -EOPNOTSUPP;
39 	}
40 
41 	/*
42 	 * There's a fairly limited set of commands this device actually
43 	 * supports, so here we block attempts to read anything else (which
44 	 * return 0xffff and would cause confusion elsewhere).
45 	 */
46 	switch (reg) {
47 	case PMBUS_STATUS_WORD:
48 	case PMBUS_FAN_COMMAND_1:
49 	case PMBUS_FAN_COMMAND_2:
50 	case PMBUS_FAN_COMMAND_3:
51 	case PMBUS_FAN_COMMAND_4:
52 	case PMBUS_STATUS_FAN_12:
53 	case PMBUS_STATUS_FAN_34:
54 	case PMBUS_READ_VIN:
55 	case PMBUS_READ_TEMPERATURE_1:
56 	case PMBUS_READ_TEMPERATURE_2:
57 	case PMBUS_READ_TEMPERATURE_3:
58 	case PMBUS_READ_FAN_SPEED_1:
59 	case PMBUS_READ_FAN_SPEED_2:
60 	case PMBUS_READ_FAN_SPEED_3:
61 	case PMBUS_READ_FAN_SPEED_4:
62 		return -ENODATA;
63 	default:
64 		return -EOPNOTSUPP;
65 	}
66 }
67 
68 static struct pmbus_driver_info ahe50dc_fan_info = {
69 	.pages = 2,
70 	.format[PSC_FAN] = direct,
71 	.format[PSC_TEMPERATURE] = direct,
72 	.format[PSC_VOLTAGE_IN] = direct,
73 	.m[PSC_FAN] = 1,
74 	.b[PSC_FAN] = 0,
75 	.R[PSC_FAN] = 0,
76 	.m[PSC_TEMPERATURE] = 1,
77 	.b[PSC_TEMPERATURE] = 0,
78 	.R[PSC_TEMPERATURE] = 1,
79 	.m[PSC_VOLTAGE_IN] = 1,
80 	.b[PSC_VOLTAGE_IN] = 0,
81 	.R[PSC_VOLTAGE_IN] = 3,
82 	.func[0] = PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
83 		PMBUS_HAVE_VIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_FAN34 |
84 		PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_STATUS_FAN34 | PMBUS_PAGE_VIRTUAL,
85 	.func[1] = PMBUS_HAVE_TEMP | PMBUS_PAGE_VIRTUAL,
86 	.write_byte = ahe50dc_fan_write_byte,
87 	.read_word_data = ahe50dc_fan_read_word_data,
88 };
89 
90 /*
91  * CAPABILITY returns 0xff, which appears to be this device's way indicating
92  * it doesn't support something (and if we enable I2C_CLIENT_PEC on seeing bit
93  * 7 being set it generates bad PECs, so let's not go there).
94  */
95 static struct pmbus_platform_data ahe50dc_fan_data = {
96 	.flags = PMBUS_NO_CAPABILITY,
97 };
98 
ahe50dc_fan_probe(struct i2c_client * client)99 static int ahe50dc_fan_probe(struct i2c_client *client)
100 {
101 	client->dev.platform_data = &ahe50dc_fan_data;
102 	return pmbus_do_probe(client, &ahe50dc_fan_info);
103 }
104 
105 static const struct i2c_device_id ahe50dc_fan_id[] = {
106 	{ "ahe50dc_fan" },
107 	{ }
108 };
109 MODULE_DEVICE_TABLE(i2c, ahe50dc_fan_id);
110 
111 static const struct of_device_id __maybe_unused ahe50dc_fan_of_match[] = {
112 	{ .compatible = "delta,ahe50dc-fan" },
113 	{ }
114 };
115 MODULE_DEVICE_TABLE(of, ahe50dc_fan_of_match);
116 
117 static struct i2c_driver ahe50dc_fan_driver = {
118 	.driver = {
119 		   .name = "ahe50dc_fan",
120 		   .of_match_table = of_match_ptr(ahe50dc_fan_of_match),
121 	},
122 	.probe_new = ahe50dc_fan_probe,
123 	.id_table = ahe50dc_fan_id,
124 };
125 module_i2c_driver(ahe50dc_fan_driver);
126 
127 MODULE_AUTHOR("Zev Weiss <zev@bewilderbeest.net>");
128 MODULE_DESCRIPTION("Driver for Delta AHE-50DC power shelf fan control module");
129 MODULE_LICENSE("GPL");
130 MODULE_IMPORT_NS(PMBUS);
131