1 // SPDX-License-Identifier: GPL-2.0
3 * Hardware monitoring driver for Maxim MAX16601
5 * Implementation notes:
7 * Ths chip supports two rails, VCORE and VSA. Telemetry information for the
8 * two rails is reported in two subsequent I2C addresses. The driver
9 * instantiates a dummy I2C client at the second I2C address to report
10 * information for the VSA rail in a single instance of the driver.
11 * Telemetry for the VSA rail is reported to the PMBus core in PMBus page 2.
13 * The chip reports input current using two separate methods. The input current
14 * reported with the standard READ_IIN command is derived from the output
15 * current. The first method is reported to the PMBus core with PMBus page 0,
16 * the second method is reported with PMBus page 1.
18 * The chip supports reading per-phase temperatures and per-phase input/output
19 * currents for VCORE. Telemetry is reported in vendor specific registers.
20 * The driver translates the vendor specific register values to PMBus standard
21 * register values and reports per-phase information in PMBus page 0.
23 * Copyright 2019, 2020 Google LLC.
26 #include <linux/bits.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
34 #define REG_SETPT_DVID 0xd1
35 #define DAC_10MV_MODE BIT(4)
36 #define REG_IOUT_AVG_PK 0xee
37 #define REG_IIN_SENSOR 0xf1
38 #define REG_TOTAL_INPUT_POWER 0xf2
39 #define REG_PHASE_ID 0xf3
40 #define CORE_RAIL_INDICATOR BIT(7)
41 #define REG_PHASE_REPORTING 0xf4
43 struct max16601_data
{
44 struct pmbus_driver_info info
;
45 struct i2c_client
*vsa
;
49 #define to_max16601_data(x) container_of(x, struct max16601_data, info)
51 static int max16601_read_byte(struct i2c_client
*client
, int page
, int reg
)
53 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
54 struct max16601_data
*data
= to_max16601_data(info
);
57 if (page
== 2) /* VSA */
58 return i2c_smbus_read_byte_data(data
->vsa
, reg
);
64 static int max16601_read_word(struct i2c_client
*client
, int page
, int phase
,
67 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
68 struct max16601_data
*data
= to_max16601_data(info
);
69 u8 buf
[I2C_SMBUS_BLOCK_MAX
+ 1];
79 case PMBUS_READ_TEMPERATURE_1
:
80 ret
= i2c_smbus_write_byte_data(client
, REG_PHASE_ID
,
84 ret
= i2c_smbus_read_block_data(client
,
92 case PMBUS_READ_TEMPERATURE_1
:
93 return buf
[1] << 8 | buf
[0];
95 return buf
[3] << 8 | buf
[2];
97 return buf
[5] << 8 | buf
[4];
103 case 1: /* VCORE, read IIN/PIN from sensor element */
106 return i2c_smbus_read_word_data(client
, REG_IIN_SENSOR
);
108 return i2c_smbus_read_word_data(client
,
109 REG_TOTAL_INPUT_POWER
);
116 case PMBUS_VIRT_READ_IOUT_MAX
:
117 ret
= i2c_smbus_read_word_data(data
->vsa
,
121 if (sign_extend32(ret
, 10) >
122 sign_extend32(data
->iout_avg_pkg
, 10))
123 data
->iout_avg_pkg
= ret
;
124 return data
->iout_avg_pkg
;
125 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
127 case PMBUS_IOUT_OC_FAULT_LIMIT
:
128 case PMBUS_IOUT_OC_WARN_LIMIT
:
129 case PMBUS_OT_FAULT_LIMIT
:
130 case PMBUS_OT_WARN_LIMIT
:
132 case PMBUS_READ_IOUT
:
133 case PMBUS_READ_TEMPERATURE_1
:
134 case PMBUS_STATUS_WORD
:
135 return i2c_smbus_read_word_data(data
->vsa
, reg
);
144 static int max16601_write_byte(struct i2c_client
*client
, int page
, u8 reg
)
146 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
147 struct max16601_data
*data
= to_max16601_data(info
);
150 if (reg
== PMBUS_CLEAR_FAULTS
)
151 return i2c_smbus_write_byte(data
->vsa
, reg
);
157 static int max16601_write_word(struct i2c_client
*client
, int page
, int reg
,
160 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
161 struct max16601_data
*data
= to_max16601_data(info
);
166 case 1: /* VCORE IIN/PIN from sensor element */
171 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
172 data
->iout_avg_pkg
= 0xfc00;
174 case PMBUS_IOUT_OC_FAULT_LIMIT
:
175 case PMBUS_IOUT_OC_WARN_LIMIT
:
176 case PMBUS_OT_FAULT_LIMIT
:
177 case PMBUS_OT_WARN_LIMIT
:
178 return i2c_smbus_write_word_data(data
->vsa
, reg
, value
);
185 static int max16601_identify(struct i2c_client
*client
,
186 struct pmbus_driver_info
*info
)
190 reg
= i2c_smbus_read_byte_data(client
, REG_SETPT_DVID
);
193 if (reg
& DAC_10MV_MODE
)
194 info
->vrm_version
[0] = vr13
;
196 info
->vrm_version
[0] = vr12
;
201 static struct pmbus_driver_info max16601_info
= {
203 .format
[PSC_VOLTAGE_IN
] = linear
,
204 .format
[PSC_VOLTAGE_OUT
] = vid
,
205 .format
[PSC_CURRENT_IN
] = linear
,
206 .format
[PSC_CURRENT_OUT
] = linear
,
207 .format
[PSC_TEMPERATURE
] = linear
,
208 .format
[PSC_POWER
] = linear
,
209 .func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_IIN
| PMBUS_HAVE_PIN
|
210 PMBUS_HAVE_STATUS_INPUT
|
211 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
|
212 PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
|
213 PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
|
214 PMBUS_HAVE_POUT
| PMBUS_PAGE_VIRTUAL
| PMBUS_PHASE_VIRTUAL
,
215 .func
[1] = PMBUS_HAVE_IIN
| PMBUS_HAVE_PIN
| PMBUS_PAGE_VIRTUAL
,
216 .func
[2] = PMBUS_HAVE_IIN
| PMBUS_HAVE_STATUS_INPUT
|
217 PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
|
218 PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
| PMBUS_PAGE_VIRTUAL
,
220 .pfunc
[0] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_TEMP
,
221 .pfunc
[1] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
,
222 .pfunc
[2] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_TEMP
,
223 .pfunc
[3] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
,
224 .pfunc
[4] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_TEMP
,
225 .pfunc
[5] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
,
226 .pfunc
[6] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_TEMP
,
227 .pfunc
[7] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
,
228 .identify
= max16601_identify
,
229 .read_byte_data
= max16601_read_byte
,
230 .read_word_data
= max16601_read_word
,
231 .write_byte
= max16601_write_byte
,
232 .write_word_data
= max16601_write_word
,
235 static void max16601_remove(void *_data
)
237 struct max16601_data
*data
= _data
;
239 i2c_unregister_device(data
->vsa
);
242 static int max16601_probe(struct i2c_client
*client
)
244 struct device
*dev
= &client
->dev
;
245 u8 buf
[I2C_SMBUS_BLOCK_MAX
+ 1];
246 struct max16601_data
*data
;
249 if (!i2c_check_functionality(client
->adapter
,
250 I2C_FUNC_SMBUS_READ_BYTE_DATA
|
251 I2C_FUNC_SMBUS_READ_BLOCK_DATA
))
254 ret
= i2c_smbus_read_block_data(client
, PMBUS_IC_DEVICE_ID
, buf
);
258 /* PMBUS_IC_DEVICE_ID is expected to return "MAX16601y.xx" */
259 if (ret
< 11 || strncmp(buf
, "MAX16601", 8)) {
261 dev_err(dev
, "Unsupported chip '%s'\n", buf
);
265 ret
= i2c_smbus_read_byte_data(client
, REG_PHASE_ID
);
268 if (!(ret
& CORE_RAIL_INDICATOR
)) {
270 "Driver must be instantiated on CORE rail I2C address\n");
274 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
278 data
->iout_avg_pkg
= 0xfc00;
279 data
->vsa
= i2c_new_dummy_device(client
->adapter
, client
->addr
+ 1);
280 if (IS_ERR(data
->vsa
)) {
281 dev_err(dev
, "Failed to register VSA client\n");
282 return PTR_ERR(data
->vsa
);
284 ret
= devm_add_action_or_reset(dev
, max16601_remove
, data
);
288 data
->info
= max16601_info
;
290 return pmbus_do_probe(client
, &data
->info
);
293 static const struct i2c_device_id max16601_id
[] = {
298 MODULE_DEVICE_TABLE(i2c
, max16601_id
);
300 static struct i2c_driver max16601_driver
= {
304 .probe_new
= max16601_probe
,
305 .id_table
= max16601_id
,
308 module_i2c_driver(max16601_driver
);
310 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
311 MODULE_DESCRIPTION("PMBus driver for Maxim MAX16601");
312 MODULE_LICENSE("GPL v2");