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 
17 static int ahe50dc_fan_read_word_data(struct i2c_client *client, int page, int phase, int reg)
18 {
19 	/* temp1 in (virtual) page 1 is remapped to mfr-specific temp4 */
20 	if (page == 1) {
21 		if (reg == PMBUS_READ_TEMPERATURE_1)
22 			return i2c_smbus_read_word_data(client, AHE50DC_PMBUS_READ_TEMP4);
23 		return -EOPNOTSUPP;
24 	}
25 
26 	/*
27 	 * There's a fairly limited set of commands this device actually
28 	 * supports, so here we block attempts to read anything else (which
29 	 * return 0xffff and would cause confusion elsewhere).
30 	 */
31 	switch (reg) {
32 	case PMBUS_STATUS_WORD:
33 	case PMBUS_FAN_COMMAND_1:
34 	case PMBUS_FAN_COMMAND_2:
35 	case PMBUS_FAN_COMMAND_3:
36 	case PMBUS_FAN_COMMAND_4:
37 	case PMBUS_STATUS_FAN_12:
38 	case PMBUS_STATUS_FAN_34:
39 	case PMBUS_READ_VIN:
40 	case PMBUS_READ_TEMPERATURE_1:
41 	case PMBUS_READ_TEMPERATURE_2:
42 	case PMBUS_READ_TEMPERATURE_3:
43 	case PMBUS_READ_FAN_SPEED_1:
44 	case PMBUS_READ_FAN_SPEED_2:
45 	case PMBUS_READ_FAN_SPEED_3:
46 	case PMBUS_READ_FAN_SPEED_4:
47 		return -ENODATA;
48 	default:
49 		return -EOPNOTSUPP;
50 	}
51 }
52 
53 static struct pmbus_driver_info ahe50dc_fan_info = {
54 	.pages = 2,
55 	.format[PSC_FAN] = direct,
56 	.format[PSC_TEMPERATURE] = direct,
57 	.format[PSC_VOLTAGE_IN] = direct,
58 	.m[PSC_FAN] = 1,
59 	.b[PSC_FAN] = 0,
60 	.R[PSC_FAN] = 0,
61 	.m[PSC_TEMPERATURE] = 1,
62 	.b[PSC_TEMPERATURE] = 0,
63 	.R[PSC_TEMPERATURE] = 1,
64 	.m[PSC_VOLTAGE_IN] = 1,
65 	.b[PSC_VOLTAGE_IN] = 0,
66 	.R[PSC_VOLTAGE_IN] = 3,
67 	.func[0] = PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
68 		PMBUS_HAVE_VIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_FAN34 |
69 		PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_STATUS_FAN34 | PMBUS_PAGE_VIRTUAL,
70 	.func[1] = PMBUS_HAVE_TEMP | PMBUS_PAGE_VIRTUAL,
71 	.read_word_data = ahe50dc_fan_read_word_data,
72 };
73 
74 /*
75  * CAPABILITY returns 0xff, which appears to be this device's way indicating
76  * it doesn't support something (and if we enable I2C_CLIENT_PEC on seeing bit
77  * 7 being set it generates bad PECs, so let's not go there).
78  */
79 static struct pmbus_platform_data ahe50dc_fan_data = {
80 	.flags = PMBUS_NO_CAPABILITY,
81 };
82 
83 static int ahe50dc_fan_probe(struct i2c_client *client)
84 {
85 	client->dev.platform_data = &ahe50dc_fan_data;
86 	return pmbus_do_probe(client, &ahe50dc_fan_info);
87 }
88 
89 static const struct i2c_device_id ahe50dc_fan_id[] = {
90 	{ "ahe50dc_fan" },
91 	{ }
92 };
93 MODULE_DEVICE_TABLE(i2c, ahe50dc_fan_id);
94 
95 static const struct of_device_id __maybe_unused ahe50dc_fan_of_match[] = {
96 	{ .compatible = "delta,ahe50dc-fan" },
97 	{ }
98 };
99 MODULE_DEVICE_TABLE(of, ahe50dc_fan_of_match);
100 
101 static struct i2c_driver ahe50dc_fan_driver = {
102 	.driver = {
103 		   .name = "ahe50dc_fan",
104 		   .of_match_table = of_match_ptr(ahe50dc_fan_of_match),
105 	},
106 	.probe_new = ahe50dc_fan_probe,
107 	.id_table = ahe50dc_fan_id,
108 };
109 module_i2c_driver(ahe50dc_fan_driver);
110 
111 MODULE_AUTHOR("Zev Weiss <zev@bewilderbeest.net>");
112 MODULE_DESCRIPTION("Driver for Delta AHE-50DC power shelf fan control module");
113 MODULE_LICENSE("GPL");
114 MODULE_IMPORT_NS(PMBUS);
115