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" },
272fa36597SUwe 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 struct pmbus_driver_info info;
351e406332SErik Rosen };
361e406332SErik Rosen
371e406332SErik Rosen #define to_max15301_data(x) container_of(x, struct max15301_data, info)
381e406332SErik Rosen
391e406332SErik Rosen #define MAX15301_WAIT_TIME 100 /* us */
401e406332SErik Rosen
411e406332SErik Rosen static ushort delay = MAX15301_WAIT_TIME;
421e406332SErik Rosen module_param(delay, ushort, 0644);
431e406332SErik Rosen MODULE_PARM_DESC(delay, "Delay between chip accesses in us");
441e406332SErik Rosen
451e406332SErik Rosen static struct max15301_data max15301_data = {
461e406332SErik Rosen .info = {
471e406332SErik Rosen .pages = 1,
481e406332SErik Rosen .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
491e406332SErik Rosen | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
501e406332SErik Rosen | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
511e406332SErik Rosen | PMBUS_HAVE_STATUS_TEMP
521e406332SErik Rosen | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
531e406332SErik Rosen }
541e406332SErik Rosen };
551e406332SErik Rosen
max15301_probe(struct i2c_client * client)561e406332SErik Rosen static int max15301_probe(struct i2c_client *client)
571e406332SErik Rosen {
581e406332SErik Rosen int status;
591e406332SErik Rosen u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
601e406332SErik Rosen const struct i2c_device_id *mid;
611e406332SErik Rosen struct pmbus_driver_info *info = &max15301_data.info;
621e406332SErik Rosen
631e406332SErik Rosen if (!i2c_check_functionality(client->adapter,
641e406332SErik Rosen I2C_FUNC_SMBUS_READ_BYTE_DATA
651e406332SErik Rosen | I2C_FUNC_SMBUS_BLOCK_DATA))
661e406332SErik Rosen return -ENODEV;
671e406332SErik Rosen
681e406332SErik Rosen status = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, device_id);
691e406332SErik Rosen if (status < 0) {
701e406332SErik Rosen dev_err(&client->dev, "Failed to read Device Id\n");
711e406332SErik Rosen return status;
721e406332SErik Rosen }
731e406332SErik Rosen for (mid = max15301_id; mid->name[0]; mid++) {
741e406332SErik Rosen if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
751e406332SErik Rosen break;
761e406332SErik Rosen }
771e406332SErik Rosen if (!mid->name[0]) {
781e406332SErik Rosen dev_err(&client->dev, "Unsupported device\n");
791e406332SErik Rosen return -ENODEV;
801e406332SErik Rosen }
811e406332SErik Rosen
82*d83219e9SPatrick Rudolph info->access_delay = delay;
831e406332SErik Rosen
841e406332SErik Rosen return pmbus_do_probe(client, info);
851e406332SErik Rosen }
861e406332SErik Rosen
871e406332SErik Rosen static struct i2c_driver max15301_driver = {
881e406332SErik Rosen .driver = {
891e406332SErik Rosen .name = "max15301",
901e406332SErik Rosen },
911975d167SUwe Kleine-König .probe = max15301_probe,
921e406332SErik Rosen .id_table = max15301_id,
931e406332SErik Rosen };
941e406332SErik Rosen
951e406332SErik Rosen module_i2c_driver(max15301_driver);
961e406332SErik Rosen
971e406332SErik Rosen MODULE_AUTHOR("Erik Rosen <erik.rosen@metormote.com>");
981e406332SErik Rosen MODULE_DESCRIPTION("PMBus driver for Maxim MAX15301");
991e406332SErik Rosen MODULE_LICENSE("GPL");
100b94ca77eSGuenter Roeck MODULE_IMPORT_NS(PMBUS);
101