11e406332SErik Rosen // SPDX-License-Identifier: GPL-2.0-or-later 21e406332SErik Rosen /* 31e406332SErik Rosen * Hardware monitoring driver for Maxim MAX15301 41e406332SErik Rosen * 51e406332SErik Rosen * Copyright (c) 2021 Flextronics International Sweden AB 61e406332SErik Rosen * 71e406332SErik Rosen * Even though the specification does not specifically mention it, 81e406332SErik Rosen * extensive empirical testing has revealed that auto-detection of 91e406332SErik Rosen * limit-registers will fail in a random fashion unless the delay 101e406332SErik Rosen * parameter is set to above about 80us. The default delay is set 111e406332SErik Rosen * to 100us to include some safety margin. 121e406332SErik Rosen */ 131e406332SErik Rosen 141e406332SErik Rosen #include <linux/kernel.h> 151e406332SErik Rosen #include <linux/module.h> 161e406332SErik Rosen #include <linux/init.h> 171e406332SErik Rosen #include <linux/err.h> 181e406332SErik Rosen #include <linux/slab.h> 191e406332SErik Rosen #include <linux/i2c.h> 201e406332SErik Rosen #include <linux/ktime.h> 211e406332SErik Rosen #include <linux/delay.h> 221e406332SErik Rosen #include <linux/pmbus.h> 231e406332SErik Rosen #include "pmbus.h" 241e406332SErik Rosen 251e406332SErik Rosen static const struct i2c_device_id max15301_id[] = { 26d8a66f36SUwe Kleine-König { "bmr461" }, 27*2fa36597SUwe Kleine-König { "max15301" }, 281e406332SErik Rosen {} 291e406332SErik Rosen }; 301e406332SErik Rosen MODULE_DEVICE_TABLE(i2c, max15301_id); 311e406332SErik Rosen 321e406332SErik Rosen struct max15301_data { 331e406332SErik Rosen int id; 341e406332SErik Rosen ktime_t access; /* Chip access time */ 351e406332SErik Rosen int delay; /* Delay between chip accesses in us */ 361e406332SErik Rosen struct pmbus_driver_info info; 371e406332SErik Rosen }; 381e406332SErik Rosen 391e406332SErik Rosen #define to_max15301_data(x) container_of(x, struct max15301_data, info) 401e406332SErik Rosen 411e406332SErik Rosen #define MAX15301_WAIT_TIME 100 /* us */ 421e406332SErik Rosen 431e406332SErik Rosen static ushort delay = MAX15301_WAIT_TIME; 441e406332SErik Rosen module_param(delay, ushort, 0644); 451e406332SErik Rosen MODULE_PARM_DESC(delay, "Delay between chip accesses in us"); 461e406332SErik Rosen 471e406332SErik Rosen static struct max15301_data max15301_data = { 481e406332SErik Rosen .info = { 491e406332SErik Rosen .pages = 1, 501e406332SErik Rosen .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT 511e406332SErik Rosen | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT 521e406332SErik Rosen | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 531e406332SErik Rosen | PMBUS_HAVE_STATUS_TEMP 541e406332SErik Rosen | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT, 551e406332SErik Rosen } 561e406332SErik Rosen }; 571e406332SErik Rosen 581e406332SErik Rosen /* This chip needs a delay between accesses */ 591e406332SErik Rosen static inline void max15301_wait(const struct max15301_data *data) 601e406332SErik Rosen { 611e406332SErik Rosen if (data->delay) { 621e406332SErik Rosen s64 delta = ktime_us_delta(ktime_get(), data->access); 631e406332SErik Rosen 641e406332SErik Rosen if (delta < data->delay) 651e406332SErik Rosen udelay(data->delay - delta); 661e406332SErik Rosen } 671e406332SErik Rosen } 681e406332SErik Rosen 691e406332SErik Rosen static int max15301_read_word_data(struct i2c_client *client, int page, 701e406332SErik Rosen int phase, int reg) 711e406332SErik Rosen { 721e406332SErik Rosen const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 731e406332SErik Rosen struct max15301_data *data = to_max15301_data(info); 741e406332SErik Rosen int ret; 751e406332SErik Rosen 761e406332SErik Rosen if (page > 0) 771e406332SErik Rosen return -ENXIO; 781e406332SErik Rosen 791e406332SErik Rosen if (reg >= PMBUS_VIRT_BASE) 801e406332SErik Rosen return -ENXIO; 811e406332SErik Rosen 821e406332SErik Rosen max15301_wait(data); 831e406332SErik Rosen ret = pmbus_read_word_data(client, page, phase, reg); 841e406332SErik Rosen data->access = ktime_get(); 851e406332SErik Rosen 861e406332SErik Rosen return ret; 871e406332SErik Rosen } 881e406332SErik Rosen 891e406332SErik Rosen static int max15301_read_byte_data(struct i2c_client *client, int page, int reg) 901e406332SErik Rosen { 911e406332SErik Rosen const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 921e406332SErik Rosen struct max15301_data *data = to_max15301_data(info); 931e406332SErik Rosen int ret; 941e406332SErik Rosen 951e406332SErik Rosen if (page > 0) 961e406332SErik Rosen return -ENXIO; 971e406332SErik Rosen 981e406332SErik Rosen max15301_wait(data); 991e406332SErik Rosen ret = pmbus_read_byte_data(client, page, reg); 1001e406332SErik Rosen data->access = ktime_get(); 1011e406332SErik Rosen 1021e406332SErik Rosen return ret; 1031e406332SErik Rosen } 1041e406332SErik Rosen 1051e406332SErik Rosen static int max15301_write_word_data(struct i2c_client *client, int page, int reg, 1061e406332SErik Rosen u16 word) 1071e406332SErik Rosen { 1081e406332SErik Rosen const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 1091e406332SErik Rosen struct max15301_data *data = to_max15301_data(info); 1101e406332SErik Rosen int ret; 1111e406332SErik Rosen 1121e406332SErik Rosen if (page > 0) 1131e406332SErik Rosen return -ENXIO; 1141e406332SErik Rosen 1151e406332SErik Rosen if (reg >= PMBUS_VIRT_BASE) 1161e406332SErik Rosen return -ENXIO; 1171e406332SErik Rosen 1181e406332SErik Rosen max15301_wait(data); 1191e406332SErik Rosen ret = pmbus_write_word_data(client, page, reg, word); 1201e406332SErik Rosen data->access = ktime_get(); 1211e406332SErik Rosen 1221e406332SErik Rosen return ret; 1231e406332SErik Rosen } 1241e406332SErik Rosen 1251e406332SErik Rosen static int max15301_write_byte(struct i2c_client *client, int page, u8 value) 1261e406332SErik Rosen { 1271e406332SErik Rosen const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 1281e406332SErik Rosen struct max15301_data *data = to_max15301_data(info); 1291e406332SErik Rosen int ret; 1301e406332SErik Rosen 1311e406332SErik Rosen if (page > 0) 1321e406332SErik Rosen return -ENXIO; 1331e406332SErik Rosen 1341e406332SErik Rosen max15301_wait(data); 1351e406332SErik Rosen ret = pmbus_write_byte(client, page, value); 1361e406332SErik Rosen data->access = ktime_get(); 1371e406332SErik Rosen 1381e406332SErik Rosen return ret; 1391e406332SErik Rosen } 1401e406332SErik Rosen 1411e406332SErik Rosen static int max15301_probe(struct i2c_client *client) 1421e406332SErik Rosen { 1431e406332SErik Rosen int status; 1441e406332SErik Rosen u8 device_id[I2C_SMBUS_BLOCK_MAX + 1]; 1451e406332SErik Rosen const struct i2c_device_id *mid; 1461e406332SErik Rosen struct pmbus_driver_info *info = &max15301_data.info; 1471e406332SErik Rosen 1481e406332SErik Rosen if (!i2c_check_functionality(client->adapter, 1491e406332SErik Rosen I2C_FUNC_SMBUS_READ_BYTE_DATA 1501e406332SErik Rosen | I2C_FUNC_SMBUS_BLOCK_DATA)) 1511e406332SErik Rosen return -ENODEV; 1521e406332SErik Rosen 1531e406332SErik Rosen status = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, device_id); 1541e406332SErik Rosen if (status < 0) { 1551e406332SErik Rosen dev_err(&client->dev, "Failed to read Device Id\n"); 1561e406332SErik Rosen return status; 1571e406332SErik Rosen } 1581e406332SErik Rosen for (mid = max15301_id; mid->name[0]; mid++) { 1591e406332SErik Rosen if (!strncasecmp(mid->name, device_id, strlen(mid->name))) 1601e406332SErik Rosen break; 1611e406332SErik Rosen } 1621e406332SErik Rosen if (!mid->name[0]) { 1631e406332SErik Rosen dev_err(&client->dev, "Unsupported device\n"); 1641e406332SErik Rosen return -ENODEV; 1651e406332SErik Rosen } 1661e406332SErik Rosen 1671e406332SErik Rosen max15301_data.delay = delay; 1681e406332SErik Rosen 1691e406332SErik Rosen info->read_byte_data = max15301_read_byte_data; 1701e406332SErik Rosen info->read_word_data = max15301_read_word_data; 1711e406332SErik Rosen info->write_byte = max15301_write_byte; 1721e406332SErik Rosen info->write_word_data = max15301_write_word_data; 1731e406332SErik Rosen 1741e406332SErik Rosen return pmbus_do_probe(client, info); 1751e406332SErik Rosen } 1761e406332SErik Rosen 1771e406332SErik Rosen static struct i2c_driver max15301_driver = { 1781e406332SErik Rosen .driver = { 1791e406332SErik Rosen .name = "max15301", 1801e406332SErik Rosen }, 1811975d167SUwe Kleine-König .probe = max15301_probe, 1821e406332SErik Rosen .id_table = max15301_id, 1831e406332SErik Rosen }; 1841e406332SErik Rosen 1851e406332SErik Rosen module_i2c_driver(max15301_driver); 1861e406332SErik Rosen 1871e406332SErik Rosen MODULE_AUTHOR("Erik Rosen <erik.rosen@metormote.com>"); 1881e406332SErik Rosen MODULE_DESCRIPTION("PMBus driver for Maxim MAX15301"); 1891e406332SErik Rosen MODULE_LICENSE("GPL"); 190b94ca77eSGuenter Roeck MODULE_IMPORT_NS(PMBUS); 191