12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28991ebd9SSamuel Mendoza-Jonas /*
38991ebd9SSamuel Mendoza-Jonas * Hardware monitoring driver for IR35221
48991ebd9SSamuel Mendoza-Jonas *
58991ebd9SSamuel Mendoza-Jonas * Copyright (C) IBM Corporation 2017.
68991ebd9SSamuel Mendoza-Jonas */
78991ebd9SSamuel Mendoza-Jonas
88991ebd9SSamuel Mendoza-Jonas #include <linux/err.h>
98991ebd9SSamuel Mendoza-Jonas #include <linux/i2c.h>
108991ebd9SSamuel Mendoza-Jonas #include <linux/init.h>
118991ebd9SSamuel Mendoza-Jonas #include <linux/kernel.h>
128991ebd9SSamuel Mendoza-Jonas #include <linux/module.h>
138991ebd9SSamuel Mendoza-Jonas #include "pmbus.h"
148991ebd9SSamuel Mendoza-Jonas
158991ebd9SSamuel Mendoza-Jonas #define IR35221_MFR_VIN_PEAK 0xc5
168991ebd9SSamuel Mendoza-Jonas #define IR35221_MFR_VOUT_PEAK 0xc6
178991ebd9SSamuel Mendoza-Jonas #define IR35221_MFR_IOUT_PEAK 0xc7
188991ebd9SSamuel Mendoza-Jonas #define IR35221_MFR_TEMP_PEAK 0xc8
198991ebd9SSamuel Mendoza-Jonas #define IR35221_MFR_VIN_VALLEY 0xc9
208991ebd9SSamuel Mendoza-Jonas #define IR35221_MFR_VOUT_VALLEY 0xca
218991ebd9SSamuel Mendoza-Jonas #define IR35221_MFR_IOUT_VALLEY 0xcb
228991ebd9SSamuel Mendoza-Jonas #define IR35221_MFR_TEMP_VALLEY 0xcc
238991ebd9SSamuel Mendoza-Jonas
ir35221_read_word_data(struct i2c_client * client,int page,int phase,int reg)2443f33b6eSGuenter Roeck static int ir35221_read_word_data(struct i2c_client *client, int page,
2543f33b6eSGuenter Roeck int phase, int reg)
268991ebd9SSamuel Mendoza-Jonas {
278991ebd9SSamuel Mendoza-Jonas int ret;
288991ebd9SSamuel Mendoza-Jonas
298991ebd9SSamuel Mendoza-Jonas switch (reg) {
308991ebd9SSamuel Mendoza-Jonas case PMBUS_VIRT_READ_VIN_MAX:
3143f33b6eSGuenter Roeck ret = pmbus_read_word_data(client, page, phase,
3243f33b6eSGuenter Roeck IR35221_MFR_VIN_PEAK);
338991ebd9SSamuel Mendoza-Jonas break;
348991ebd9SSamuel Mendoza-Jonas case PMBUS_VIRT_READ_VOUT_MAX:
3543f33b6eSGuenter Roeck ret = pmbus_read_word_data(client, page, phase,
3643f33b6eSGuenter Roeck IR35221_MFR_VOUT_PEAK);
378991ebd9SSamuel Mendoza-Jonas break;
388991ebd9SSamuel Mendoza-Jonas case PMBUS_VIRT_READ_IOUT_MAX:
3943f33b6eSGuenter Roeck ret = pmbus_read_word_data(client, page, phase,
4043f33b6eSGuenter Roeck IR35221_MFR_IOUT_PEAK);
418991ebd9SSamuel Mendoza-Jonas break;
428991ebd9SSamuel Mendoza-Jonas case PMBUS_VIRT_READ_TEMP_MAX:
4343f33b6eSGuenter Roeck ret = pmbus_read_word_data(client, page, phase,
4443f33b6eSGuenter Roeck IR35221_MFR_TEMP_PEAK);
458991ebd9SSamuel Mendoza-Jonas break;
468991ebd9SSamuel Mendoza-Jonas case PMBUS_VIRT_READ_VIN_MIN:
4743f33b6eSGuenter Roeck ret = pmbus_read_word_data(client, page, phase,
488991ebd9SSamuel Mendoza-Jonas IR35221_MFR_VIN_VALLEY);
498991ebd9SSamuel Mendoza-Jonas break;
508991ebd9SSamuel Mendoza-Jonas case PMBUS_VIRT_READ_VOUT_MIN:
5143f33b6eSGuenter Roeck ret = pmbus_read_word_data(client, page, phase,
528991ebd9SSamuel Mendoza-Jonas IR35221_MFR_VOUT_VALLEY);
538991ebd9SSamuel Mendoza-Jonas break;
548991ebd9SSamuel Mendoza-Jonas case PMBUS_VIRT_READ_IOUT_MIN:
5543f33b6eSGuenter Roeck ret = pmbus_read_word_data(client, page, phase,
568991ebd9SSamuel Mendoza-Jonas IR35221_MFR_IOUT_VALLEY);
578991ebd9SSamuel Mendoza-Jonas break;
588991ebd9SSamuel Mendoza-Jonas case PMBUS_VIRT_READ_TEMP_MIN:
5943f33b6eSGuenter Roeck ret = pmbus_read_word_data(client, page, phase,
608991ebd9SSamuel Mendoza-Jonas IR35221_MFR_TEMP_VALLEY);
618991ebd9SSamuel Mendoza-Jonas break;
628991ebd9SSamuel Mendoza-Jonas default:
638991ebd9SSamuel Mendoza-Jonas ret = -ENODATA;
648991ebd9SSamuel Mendoza-Jonas break;
658991ebd9SSamuel Mendoza-Jonas }
668991ebd9SSamuel Mendoza-Jonas
678991ebd9SSamuel Mendoza-Jonas return ret;
688991ebd9SSamuel Mendoza-Jonas }
698991ebd9SSamuel Mendoza-Jonas
ir35221_probe(struct i2c_client * client)70dd431939SStephen Kitt static int ir35221_probe(struct i2c_client *client)
718991ebd9SSamuel Mendoza-Jonas {
728991ebd9SSamuel Mendoza-Jonas struct pmbus_driver_info *info;
738991ebd9SSamuel Mendoza-Jonas u8 buf[I2C_SMBUS_BLOCK_MAX];
748991ebd9SSamuel Mendoza-Jonas int ret;
758991ebd9SSamuel Mendoza-Jonas
768991ebd9SSamuel Mendoza-Jonas if (!i2c_check_functionality(client->adapter,
778991ebd9SSamuel Mendoza-Jonas I2C_FUNC_SMBUS_READ_BYTE_DATA
788991ebd9SSamuel Mendoza-Jonas | I2C_FUNC_SMBUS_READ_WORD_DATA
798991ebd9SSamuel Mendoza-Jonas | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
808991ebd9SSamuel Mendoza-Jonas return -ENODEV;
818991ebd9SSamuel Mendoza-Jonas
828991ebd9SSamuel Mendoza-Jonas ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
838991ebd9SSamuel Mendoza-Jonas if (ret < 0) {
848991ebd9SSamuel Mendoza-Jonas dev_err(&client->dev, "Failed to read PMBUS_MFR_ID\n");
858991ebd9SSamuel Mendoza-Jonas return ret;
868991ebd9SSamuel Mendoza-Jonas }
878991ebd9SSamuel Mendoza-Jonas if (ret != 2 || strncmp(buf, "RI", strlen("RI"))) {
888991ebd9SSamuel Mendoza-Jonas dev_err(&client->dev, "MFR_ID unrecognised\n");
898991ebd9SSamuel Mendoza-Jonas return -ENODEV;
908991ebd9SSamuel Mendoza-Jonas }
918991ebd9SSamuel Mendoza-Jonas
928991ebd9SSamuel Mendoza-Jonas ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
938991ebd9SSamuel Mendoza-Jonas if (ret < 0) {
948991ebd9SSamuel Mendoza-Jonas dev_err(&client->dev, "Failed to read PMBUS_MFR_MODEL\n");
958991ebd9SSamuel Mendoza-Jonas return ret;
968991ebd9SSamuel Mendoza-Jonas }
978991ebd9SSamuel Mendoza-Jonas if (ret != 2 || !(buf[0] == 0x6c && buf[1] == 0x00)) {
988991ebd9SSamuel Mendoza-Jonas dev_err(&client->dev, "MFR_MODEL unrecognised\n");
998991ebd9SSamuel Mendoza-Jonas return -ENODEV;
1008991ebd9SSamuel Mendoza-Jonas }
1018991ebd9SSamuel Mendoza-Jonas
1028991ebd9SSamuel Mendoza-Jonas info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info),
1038991ebd9SSamuel Mendoza-Jonas GFP_KERNEL);
1048991ebd9SSamuel Mendoza-Jonas if (!info)
1058991ebd9SSamuel Mendoza-Jonas return -ENOMEM;
1068991ebd9SSamuel Mendoza-Jonas
1078991ebd9SSamuel Mendoza-Jonas info->read_word_data = ir35221_read_word_data;
1088991ebd9SSamuel Mendoza-Jonas
1098991ebd9SSamuel Mendoza-Jonas info->pages = 2;
1108991ebd9SSamuel Mendoza-Jonas info->format[PSC_VOLTAGE_IN] = linear;
1118991ebd9SSamuel Mendoza-Jonas info->format[PSC_VOLTAGE_OUT] = linear;
1128991ebd9SSamuel Mendoza-Jonas info->format[PSC_CURRENT_IN] = linear;
1138991ebd9SSamuel Mendoza-Jonas info->format[PSC_CURRENT_OUT] = linear;
1148991ebd9SSamuel Mendoza-Jonas info->format[PSC_POWER] = linear;
1158991ebd9SSamuel Mendoza-Jonas info->format[PSC_TEMPERATURE] = linear;
1168991ebd9SSamuel Mendoza-Jonas
1178991ebd9SSamuel Mendoza-Jonas info->func[0] = PMBUS_HAVE_VIN
1188991ebd9SSamuel Mendoza-Jonas | PMBUS_HAVE_VOUT | PMBUS_HAVE_IIN
1198991ebd9SSamuel Mendoza-Jonas | PMBUS_HAVE_IOUT | PMBUS_HAVE_PIN
1208991ebd9SSamuel Mendoza-Jonas | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
1218991ebd9SSamuel Mendoza-Jonas | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
1228991ebd9SSamuel Mendoza-Jonas | PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP;
1238991ebd9SSamuel Mendoza-Jonas info->func[1] = info->func[0];
1248991ebd9SSamuel Mendoza-Jonas
125dd431939SStephen Kitt return pmbus_do_probe(client, info);
1268991ebd9SSamuel Mendoza-Jonas }
1278991ebd9SSamuel Mendoza-Jonas
1288991ebd9SSamuel Mendoza-Jonas static const struct i2c_device_id ir35221_id[] = {
1298991ebd9SSamuel Mendoza-Jonas {"ir35221", 0},
1308991ebd9SSamuel Mendoza-Jonas {}
1318991ebd9SSamuel Mendoza-Jonas };
1328991ebd9SSamuel Mendoza-Jonas
1338991ebd9SSamuel Mendoza-Jonas MODULE_DEVICE_TABLE(i2c, ir35221_id);
1348991ebd9SSamuel Mendoza-Jonas
1358991ebd9SSamuel Mendoza-Jonas static struct i2c_driver ir35221_driver = {
1368991ebd9SSamuel Mendoza-Jonas .driver = {
1378991ebd9SSamuel Mendoza-Jonas .name = "ir35221",
1388991ebd9SSamuel Mendoza-Jonas },
139*1975d167SUwe Kleine-König .probe = ir35221_probe,
1408991ebd9SSamuel Mendoza-Jonas .id_table = ir35221_id,
1418991ebd9SSamuel Mendoza-Jonas };
1428991ebd9SSamuel Mendoza-Jonas
1438991ebd9SSamuel Mendoza-Jonas module_i2c_driver(ir35221_driver);
1448991ebd9SSamuel Mendoza-Jonas
1458991ebd9SSamuel Mendoza-Jonas MODULE_AUTHOR("Samuel Mendoza-Jonas <sam@mendozajonas.com");
1468991ebd9SSamuel Mendoza-Jonas MODULE_DESCRIPTION("PMBus driver for IR35221");
1478991ebd9SSamuel Mendoza-Jonas MODULE_LICENSE("GPL");
148b94ca77eSGuenter Roeck MODULE_IMPORT_NS(PMBUS);
149