1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for MPS MP5990 Hot-Swap Controller
4 */
5
6 #include <linux/i2c.h>
7 #include <linux/module.h>
8 #include <linux/of_device.h>
9 #include "pmbus.h"
10
11 #define MP5990_EFUSE_CFG (0xC4)
12 #define MP5990_VOUT_FORMAT BIT(9)
13
14 struct mp5990_data {
15 struct pmbus_driver_info info;
16 u8 vout_mode;
17 u8 vout_linear_exponent;
18 };
19
20 #define to_mp5990_data(x) container_of(x, struct mp5990_data, info)
21
mp5990_read_byte_data(struct i2c_client * client,int page,int reg)22 static int mp5990_read_byte_data(struct i2c_client *client, int page, int reg)
23 {
24 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
25 struct mp5990_data *data = to_mp5990_data(info);
26
27 switch (reg) {
28 case PMBUS_VOUT_MODE:
29 if (data->vout_mode == linear) {
30 /*
31 * The VOUT format is linear11, not linear16.
32 * We should enforce the VOUT in linear mode and
33 * return exponent value.
34 */
35 return data->vout_linear_exponent;
36 }
37
38 /*
39 * The datasheet does not support the VOUT command,
40 * but the device responds with a default value of 0x17.
41 * In the standard, 0x17 represents linear mode. However,
42 * Therefore, we should enforce the VOUT in the direct format
43 * when format use default direct mode.
44 */
45 return PB_VOUT_MODE_DIRECT;
46
47 default:
48 return -ENODATA;
49 }
50 }
51
mp5990_read_word_data(struct i2c_client * client,int page,int phase,int reg)52 static int mp5990_read_word_data(struct i2c_client *client, int page,
53 int phase, int reg)
54 {
55 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
56 struct mp5990_data *data = to_mp5990_data(info);
57 int ret;
58 s32 mantissa;
59
60 switch (reg) {
61 case PMBUS_READ_VOUT:
62 ret = pmbus_read_word_data(client, page, phase, reg);
63 if (ret < 0)
64 return ret;
65 /*
66 * Because the VOUT mode is linear11 and not linear16,
67 * we disregard bits[15:11] and report based on the VOUT mode.
68 */
69 if (data->vout_mode == linear) {
70 mantissa = ((s16)((ret & 0x7ff) << 5)) >> 5;
71 ret = mantissa;
72 }
73 break;
74 default:
75 return -ENODATA;
76 }
77
78 return ret;
79 }
80
81 static struct pmbus_driver_info mp5990_info = {
82 .pages = 1,
83 .format[PSC_VOLTAGE_IN] = direct,
84 .format[PSC_VOLTAGE_OUT] = direct,
85 .format[PSC_CURRENT_OUT] = direct,
86 .format[PSC_POWER] = direct,
87 .format[PSC_TEMPERATURE] = direct,
88 .m[PSC_VOLTAGE_IN] = 32,
89 .b[PSC_VOLTAGE_IN] = 0,
90 .R[PSC_VOLTAGE_IN] = 0,
91 .m[PSC_VOLTAGE_OUT] = 32,
92 .b[PSC_VOLTAGE_OUT] = 0,
93 .R[PSC_VOLTAGE_OUT] = 0,
94 .m[PSC_CURRENT_OUT] = 16,
95 .b[PSC_CURRENT_OUT] = 0,
96 .R[PSC_CURRENT_OUT] = 0,
97 .m[PSC_POWER] = 1,
98 .b[PSC_POWER] = 0,
99 .R[PSC_POWER] = 0,
100 .m[PSC_TEMPERATURE] = 1,
101 .b[PSC_TEMPERATURE] = 0,
102 .R[PSC_TEMPERATURE] = 0,
103 .func[0] =
104 PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_PIN |
105 PMBUS_HAVE_TEMP | PMBUS_HAVE_IOUT |
106 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
107 .read_byte_data = mp5990_read_byte_data,
108 .read_word_data = mp5990_read_word_data,
109 };
110
mp5990_probe(struct i2c_client * client)111 static int mp5990_probe(struct i2c_client *client)
112 {
113 struct pmbus_driver_info *info;
114 struct mp5990_data *data;
115 int ret;
116
117 data = devm_kzalloc(&client->dev, sizeof(struct mp5990_data),
118 GFP_KERNEL);
119 if (!data)
120 return -ENOMEM;
121
122 memcpy(&data->info, &mp5990_info, sizeof(*info));
123 info = &data->info;
124
125 /* Read Vout Config */
126 ret = i2c_smbus_read_word_data(client, MP5990_EFUSE_CFG);
127 if (ret < 0) {
128 dev_err(&client->dev, "Can't get vout mode.");
129 return ret;
130 }
131
132 /*
133 * EFUSE_CFG (0xC4) bit9=1 is linear mode, bit=0 is direct mode.
134 */
135 if (ret & MP5990_VOUT_FORMAT) {
136 data->vout_mode = linear;
137 data->info.format[PSC_VOLTAGE_IN] = linear;
138 data->info.format[PSC_VOLTAGE_OUT] = linear;
139 data->info.format[PSC_CURRENT_OUT] = linear;
140 data->info.format[PSC_POWER] = linear;
141 ret = i2c_smbus_read_word_data(client, PMBUS_READ_VOUT);
142 if (ret < 0) {
143 dev_err(&client->dev, "Can't get vout exponent.");
144 return ret;
145 }
146 data->vout_linear_exponent = (u8)((ret >> 11) & 0x1f);
147 } else {
148 data->vout_mode = direct;
149 }
150 return pmbus_do_probe(client, info);
151 }
152
153 static const struct of_device_id mp5990_of_match[] = {
154 { .compatible = "mps,mp5990" },
155 {}
156 };
157
158 static const struct i2c_device_id mp5990_id[] = {
159 {"mp5990", 0},
160 { }
161 };
162 MODULE_DEVICE_TABLE(i2c, mp5990_id);
163
164 static struct i2c_driver mp5990_driver = {
165 .driver = {
166 .name = "mp5990",
167 .of_match_table = mp5990_of_match,
168 },
169 .probe = mp5990_probe,
170 .id_table = mp5990_id,
171 };
172 module_i2c_driver(mp5990_driver);
173
174 MODULE_AUTHOR("Peter Yin <peter.yin@quantatw.com>");
175 MODULE_DESCRIPTION("PMBus driver for MP5990 HSC");
176 MODULE_LICENSE("GPL");
177 MODULE_IMPORT_NS(PMBUS);
178