1 // SPDX-License-Identifier: GPL-2.0
3 * Hardware monitoring driver for Maxim MAX16508, MAX16600, MAX16601,
6 * Implementation notes:
8 * This chip series supports two rails, VCORE and VSA. Telemetry information
9 * for the two rails is reported in two subsequent I2C addresses. The driver
10 * instantiates a dummy I2C client at the second I2C address to report
11 * information for the VSA rail in a single instance of the driver.
12 * Telemetry for the VSA rail is reported to the PMBus core in PMBus page 2.
14 * The chip reports input current using two separate methods. The input current
15 * reported with the standard READ_IIN command is derived from the output
16 * current. The first method is reported to the PMBus core with PMBus page 0,
17 * the second method is reported with PMBus page 1.
19 * The chip supports reading per-phase temperatures and per-phase input/output
20 * currents for VCORE. Telemetry is reported in vendor specific registers.
21 * The driver translates the vendor specific register values to PMBus standard
22 * register values and reports per-phase information in PMBus page 0.
24 * Copyright 2019, 2020 Google LLC.
27 #include <linux/bits.h>
28 #include <linux/i2c.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
35 enum chips
{ max16508
, max16600
, max16601
, max16602
};
37 #define REG_DEFAULT_NUM_POP 0xc4
38 #define REG_SETPT_DVID 0xd1
39 #define DAC_10MV_MODE BIT(4)
40 #define REG_IOUT_AVG_PK 0xee
41 #define REG_IIN_SENSOR 0xf1
42 #define REG_TOTAL_INPUT_POWER 0xf2
43 #define REG_PHASE_ID 0xf3
44 #define CORE_RAIL_INDICATOR BIT(7)
45 #define REG_PHASE_REPORTING 0xf4
47 #define MAX16601_NUM_PHASES 8
49 struct max16601_data
{
51 struct pmbus_driver_info info
;
52 struct i2c_client
*vsa
;
56 #define to_max16601_data(x) container_of(x, struct max16601_data, info)
58 static int max16601_read_byte(struct i2c_client
*client
, int page
, int reg
)
60 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
61 struct max16601_data
*data
= to_max16601_data(info
);
64 if (page
== 2) /* VSA */
65 return i2c_smbus_read_byte_data(data
->vsa
, reg
);
71 static int max16601_read_word(struct i2c_client
*client
, int page
, int phase
,
74 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
75 struct max16601_data
*data
= to_max16601_data(info
);
76 u8 buf
[I2C_SMBUS_BLOCK_MAX
+ 1];
86 case PMBUS_READ_TEMPERATURE_1
:
87 ret
= i2c_smbus_write_byte_data(client
, REG_PHASE_ID
,
91 ret
= i2c_smbus_read_block_data(client
,
99 case PMBUS_READ_TEMPERATURE_1
:
100 return buf
[1] << 8 | buf
[0];
101 case PMBUS_READ_IOUT
:
102 return buf
[3] << 8 | buf
[2];
104 return buf
[5] << 8 | buf
[4];
110 case 1: /* VCORE, read IIN/PIN from sensor element */
113 return i2c_smbus_read_word_data(client
, REG_IIN_SENSOR
);
115 return i2c_smbus_read_word_data(client
,
116 REG_TOTAL_INPUT_POWER
);
123 case PMBUS_VIRT_READ_IOUT_MAX
:
124 ret
= i2c_smbus_read_word_data(data
->vsa
,
128 if (sign_extend32(ret
, 10) >
129 sign_extend32(data
->iout_avg_pkg
, 10))
130 data
->iout_avg_pkg
= ret
;
131 return data
->iout_avg_pkg
;
132 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
134 case PMBUS_IOUT_OC_FAULT_LIMIT
:
135 case PMBUS_IOUT_OC_WARN_LIMIT
:
136 case PMBUS_OT_FAULT_LIMIT
:
137 case PMBUS_OT_WARN_LIMIT
:
139 case PMBUS_READ_IOUT
:
140 case PMBUS_READ_TEMPERATURE_1
:
141 case PMBUS_STATUS_WORD
:
142 return i2c_smbus_read_word_data(data
->vsa
, reg
);
151 static int max16601_write_byte(struct i2c_client
*client
, int page
, u8 reg
)
153 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
154 struct max16601_data
*data
= to_max16601_data(info
);
157 if (reg
== PMBUS_CLEAR_FAULTS
)
158 return i2c_smbus_write_byte(data
->vsa
, reg
);
164 static int max16601_write_word(struct i2c_client
*client
, int page
, int reg
,
167 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
168 struct max16601_data
*data
= to_max16601_data(info
);
173 case 1: /* VCORE IIN/PIN from sensor element */
178 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
179 data
->iout_avg_pkg
= 0xfc00;
181 case PMBUS_IOUT_OC_FAULT_LIMIT
:
182 case PMBUS_IOUT_OC_WARN_LIMIT
:
183 case PMBUS_OT_FAULT_LIMIT
:
184 case PMBUS_OT_WARN_LIMIT
:
185 return i2c_smbus_write_word_data(data
->vsa
, reg
, value
);
192 static int max16601_identify(struct i2c_client
*client
,
193 struct pmbus_driver_info
*info
)
195 struct max16601_data
*data
= to_max16601_data(info
);
198 reg
= i2c_smbus_read_byte_data(client
, REG_SETPT_DVID
);
201 if (reg
& DAC_10MV_MODE
)
202 info
->vrm_version
[0] = vr13
;
204 info
->vrm_version
[0] = vr12
;
206 if (data
->id
!= max16600
&& data
->id
!= max16601
&& data
->id
!= max16602
)
209 reg
= i2c_smbus_read_byte_data(client
, REG_DEFAULT_NUM_POP
);
214 * If REG_DEFAULT_NUM_POP returns 0, we don't know how many phases
215 * are populated. Stick with the default in that case.
218 if (reg
&& reg
<= MAX16601_NUM_PHASES
)
219 info
->phases
[0] = reg
;
224 static struct pmbus_driver_info max16601_info
= {
226 .format
[PSC_VOLTAGE_IN
] = linear
,
227 .format
[PSC_VOLTAGE_OUT
] = vid
,
228 .format
[PSC_CURRENT_IN
] = linear
,
229 .format
[PSC_CURRENT_OUT
] = linear
,
230 .format
[PSC_TEMPERATURE
] = linear
,
231 .format
[PSC_POWER
] = linear
,
232 .func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_IIN
| PMBUS_HAVE_PIN
|
233 PMBUS_HAVE_STATUS_INPUT
|
234 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
|
235 PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
|
236 PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
|
237 PMBUS_HAVE_POUT
| PMBUS_PAGE_VIRTUAL
| PMBUS_PHASE_VIRTUAL
,
238 .func
[1] = PMBUS_HAVE_IIN
| PMBUS_HAVE_PIN
| PMBUS_PAGE_VIRTUAL
,
239 .func
[2] = PMBUS_HAVE_IIN
| PMBUS_HAVE_STATUS_INPUT
|
240 PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
|
241 PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
| PMBUS_PAGE_VIRTUAL
,
242 .phases
[0] = MAX16601_NUM_PHASES
,
243 .pfunc
[0] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_TEMP
,
244 .pfunc
[1] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
,
245 .pfunc
[2] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_TEMP
,
246 .pfunc
[3] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
,
247 .pfunc
[4] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_TEMP
,
248 .pfunc
[5] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
,
249 .pfunc
[6] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_TEMP
,
250 .pfunc
[7] = PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
,
251 .identify
= max16601_identify
,
252 .read_byte_data
= max16601_read_byte
,
253 .read_word_data
= max16601_read_word
,
254 .write_byte
= max16601_write_byte
,
255 .write_word_data
= max16601_write_word
,
258 static void max16601_remove(void *_data
)
260 struct max16601_data
*data
= _data
;
262 i2c_unregister_device(data
->vsa
);
265 static const struct i2c_device_id max16601_id
[] = {
266 {"max16508", max16508
},
267 {"max16600", max16600
},
268 {"max16601", max16601
},
269 {"max16602", max16602
},
272 MODULE_DEVICE_TABLE(i2c
, max16601_id
);
274 static int max16601_get_id(struct i2c_client
*client
)
276 struct device
*dev
= &client
->dev
;
277 u8 buf
[I2C_SMBUS_BLOCK_MAX
+ 1];
281 ret
= i2c_smbus_read_block_data(client
, PMBUS_IC_DEVICE_ID
, buf
);
282 if (ret
< 0 || ret
< 11)
286 * PMBUS_IC_DEVICE_ID is expected to return MAX1660[012]y.xx",
287 * "MAX16500y.xx".cdxxcccccccccc, or "MAX16508y.xx".
289 if (!strncmp(buf
, "MAX16500", 8) || !strncmp(buf
, "MAX16508", 8)) {
291 } else if (!strncmp(buf
, "MAX16600", 8)) {
293 } else if (!strncmp(buf
, "MAX16601", 8)) {
295 } else if (!strncmp(buf
, "MAX16602", 8)) {
299 dev_err(dev
, "Unsupported chip '%s'\n", buf
);
305 static int max16601_probe(struct i2c_client
*client
)
307 struct device
*dev
= &client
->dev
;
308 const struct i2c_device_id
*id
;
309 struct max16601_data
*data
;
312 if (!i2c_check_functionality(client
->adapter
,
313 I2C_FUNC_SMBUS_READ_BYTE_DATA
|
314 I2C_FUNC_SMBUS_READ_BLOCK_DATA
))
317 chip_id
= max16601_get_id(client
);
321 id
= i2c_match_id(max16601_id
, client
);
322 if (chip_id
!= id
->driver_data
)
323 dev_warn(&client
->dev
,
324 "Device mismatch: Configured %s (%d), detected %d\n",
325 id
->name
, (int) id
->driver_data
, chip_id
);
327 ret
= i2c_smbus_read_byte_data(client
, REG_PHASE_ID
);
330 if (!(ret
& CORE_RAIL_INDICATOR
)) {
332 "Driver must be instantiated on CORE rail I2C address\n");
336 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
341 data
->iout_avg_pkg
= 0xfc00;
342 data
->vsa
= i2c_new_dummy_device(client
->adapter
, client
->addr
+ 1);
343 if (IS_ERR(data
->vsa
)) {
344 dev_err(dev
, "Failed to register VSA client\n");
345 return PTR_ERR(data
->vsa
);
347 ret
= devm_add_action_or_reset(dev
, max16601_remove
, data
);
351 data
->info
= max16601_info
;
353 return pmbus_do_probe(client
, &data
->info
);
356 static struct i2c_driver max16601_driver
= {
360 .probe
= max16601_probe
,
361 .id_table
= max16601_id
,
364 module_i2c_driver(max16601_driver
);
366 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
367 MODULE_DESCRIPTION("PMBus driver for Maxim MAX16601");
368 MODULE_LICENSE("GPL v2");
369 MODULE_IMPORT_NS("PMBUS");