xref: /linux/drivers/hwmon/pmbus/aps-379.c (revision 46576fa32908043975471bd26fe833a7d8015b35)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Hardware monitoring driver for Sony APS-379 Power Supplies
4  *
5  * Copyright 2026 Allied Telesis Labs
6  */
7 
8 #include <linux/i2c.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pmbus.h>
13 #include "pmbus.h"
14 
15 /*
16  * The VOUT format used by the chip is linear11, not linear16. Provide a hard
17  * coded VOUT_MODE that says VOUT is in linear mode with a fixed exponent of
18  * 2^-4.
19  */
20 #define APS_379_VOUT_MODE ((u8)(-4 & 0x1f))
21 
22 static int aps_379_read_byte_data(struct i2c_client *client, int page, int reg)
23 {
24 	switch (reg) {
25 	case PMBUS_VOUT_MODE:
26 		return APS_379_VOUT_MODE;
27 	default:
28 		return -ENODATA;
29 	}
30 }
31 
32 /*
33  * The APS-379 uses linear11 format instead of linear16. We've reported the exponent
34  * via the PMBUS_VOUT_MODE so we just return the mantissa here.
35  */
36 static int aps_379_read_vout(struct i2c_client *client)
37 {
38 	int ret;
39 
40 	ret = pmbus_read_word_data(client, 0, 0xff, PMBUS_READ_VOUT);
41 	if (ret < 0)
42 		return ret;
43 
44 	return clamp_val(sign_extend32(ret & 0x7ff, 10), 0, 0x3ff);
45 }
46 
47 static int aps_379_read_word_data(struct i2c_client *client, int page, int phase, int reg)
48 {
49 	switch (reg) {
50 	case PMBUS_VOUT_UV_WARN_LIMIT:
51 	case PMBUS_VOUT_OV_WARN_LIMIT:
52 	case PMBUS_VOUT_UV_FAULT_LIMIT:
53 	case PMBUS_VOUT_OV_FAULT_LIMIT:
54 	case PMBUS_IOUT_OC_WARN_LIMIT:
55 	case PMBUS_IOUT_UC_FAULT_LIMIT:
56 	case PMBUS_UT_WARN_LIMIT:
57 	case PMBUS_UT_FAULT_LIMIT:
58 	case PMBUS_OT_WARN_LIMIT:
59 	case PMBUS_OT_FAULT_LIMIT:
60 	case PMBUS_PIN_OP_WARN_LIMIT:
61 	case PMBUS_POUT_OP_WARN_LIMIT:
62 	case PMBUS_MFR_IIN_MAX:
63 	case PMBUS_MFR_PIN_MAX:
64 	case PMBUS_MFR_VOUT_MIN:
65 	case PMBUS_MFR_VOUT_MAX:
66 	case PMBUS_MFR_IOUT_MAX:
67 	case PMBUS_MFR_POUT_MAX:
68 	case PMBUS_MFR_MAX_TEMP_1:
69 		/* These commands return data but it is invalid/un-documented */
70 		return -ENXIO;
71 	case PMBUS_IOUT_OC_FAULT_LIMIT:
72 		/*
73 		 * The standard requires this to be a value in Amps but it's
74 		 * actually a percentage of the rated output (123A for
75 		 * 110-240Vac, 110A for 90-100Vac) which we don't know. Ignore
76 		 * it rather than guessing.
77 		 */
78 		return -ENXIO;
79 	case PMBUS_READ_VOUT:
80 		return aps_379_read_vout(client);
81 	default:
82 		return -ENODATA;
83 	}
84 }
85 
86 static struct pmbus_driver_info aps_379_info = {
87 	.pages = 1,
88 	.format[PSC_VOLTAGE_OUT] = linear,
89 	.format[PSC_CURRENT_OUT] = linear,
90 	.format[PSC_POWER] = linear,
91 	.format[PSC_TEMPERATURE] = linear,
92 	.format[PSC_FAN] = linear,
93 	.func[0] = PMBUS_HAVE_VOUT |
94 		PMBUS_HAVE_IOUT |
95 		PMBUS_HAVE_PIN | PMBUS_HAVE_POUT |
96 		PMBUS_HAVE_TEMP |
97 		PMBUS_HAVE_FAN12,
98 	.read_byte_data = aps_379_read_byte_data,
99 	.read_word_data = aps_379_read_word_data,
100 };
101 
102 static const struct i2c_device_id aps_379_id[] = {
103 	{ "aps-379", 0 },
104 	{},
105 };
106 MODULE_DEVICE_TABLE(i2c, aps_379_id);
107 
108 static int aps_379_probe(struct i2c_client *client)
109 {
110 	struct device *dev = &client->dev;
111 	u8 buf[I2C_SMBUS_BLOCK_MAX + 1] = { 0 };
112 	int ret;
113 
114 	if (!i2c_check_functionality(client->adapter,
115 				     I2C_FUNC_SMBUS_READ_BYTE_DATA
116 				     | I2C_FUNC_SMBUS_READ_WORD_DATA
117 				     | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
118 		return -ENODEV;
119 
120 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
121 	if (ret < 0) {
122 		dev_err(dev, "Failed to read Manufacturer Model\n");
123 		return ret;
124 	}
125 
126 	if (strncasecmp(buf, aps_379_id[0].name, strlen(aps_379_id[0].name)) != 0) {
127 		buf[ret] = '\0';
128 		dev_err(dev, "Unsupported Manufacturer Model '%s'\n", buf);
129 		return -ENODEV;
130 	}
131 
132 	return pmbus_do_probe(client, &aps_379_info);
133 }
134 
135 static const struct of_device_id __maybe_unused aps_379_of_match[] = {
136 	{ .compatible = "sony,aps-379" },
137 	{},
138 };
139 MODULE_DEVICE_TABLE(of, aps_379_of_match);
140 
141 static struct i2c_driver aps_379_driver = {
142 	.driver = {
143 		.name = "aps-379",
144 		.of_match_table = of_match_ptr(aps_379_of_match),
145 	},
146 	.probe = aps_379_probe,
147 	.id_table = aps_379_id,
148 };
149 
150 module_i2c_driver(aps_379_driver);
151 
152 MODULE_AUTHOR("Chris Packham");
153 MODULE_DESCRIPTION("PMBus driver for Sony APS-379");
154 MODULE_LICENSE("GPL");
155 MODULE_IMPORT_NS("PMBUS");
156