xref: /linux/drivers/hwmon/pmbus/max20830.c (revision 9611c0ce215a66770ccbe5c126bf57ba8c31bcad)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Hardware monitoring driver for Analog Devices MAX20830
4  *
5  * Copyright (C) 2026 Analog Devices, Inc.
6  */
7 
8 #include <linux/errno.h>
9 #include <linux/i2c.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/string.h>
13 #include "pmbus.h"
14 
15 #define MAX20830_IC_DEVICE_ID_LENGTH	9
16 
17 static struct pmbus_driver_info max20830_info = {
18 	.pages = 1,
19 	.format[PSC_VOLTAGE_IN] = linear,
20 	.format[PSC_VOLTAGE_OUT] = linear,
21 	.format[PSC_CURRENT_OUT] = linear,
22 	.format[PSC_TEMPERATURE] = linear,
23 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
24 		PMBUS_HAVE_TEMP |
25 		PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
26 		PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
27 };
28 
29 static int max20830_probe(struct i2c_client *client)
30 {
31 	u8 buf[I2C_SMBUS_BLOCK_MAX + 1] = {};
32 	int ret;
33 
34 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA) &&
35 	    !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
36 		return -ENODEV;
37 
38 	/*
39 	 * Use i2c_smbus_read_block_data() if supported, otherwise fall back
40 	 * to i2c_smbus_read_i2c_block_data() to support I2C controllers
41 	 * which do not support SMBus block reads.
42 	 */
43 	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
44 		/* Reads 9 Data bytes from MAX20830 */
45 		ret = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, buf);
46 		if (ret < 0)
47 			return dev_err_probe(&client->dev, ret,
48 					     "Failed to read IC_DEVICE_ID\n");
49 	} else {
50 		/* Reads 1 length byte + 9 Data bytes from MAX20830 */
51 		ret = i2c_smbus_read_i2c_block_data(client, PMBUS_IC_DEVICE_ID,
52 						    MAX20830_IC_DEVICE_ID_LENGTH + 1,
53 						    buf);
54 		if (ret < 0)
55 			return dev_err_probe(&client->dev, ret,
56 					     "Failed to read IC_DEVICE_ID\n");
57 		/*
58 		 * Moves data forward, removing the length byte, this is to
59 		 * match the format of i2c_smbus_read_block_data().
60 		 * Also adjust return value to reflect length byte removal.
61 		 */
62 		memmove(buf, buf + 1, MAX20830_IC_DEVICE_ID_LENGTH);
63 		ret = ret - 1;
64 	}
65 
66 	/*
67 	 * MAX20830 IC_DEVICE_ID sends string data "MAX20830\0".
68 	 * Return value should at least be 9 bytes of data.
69 	 */
70 	if (ret < MAX20830_IC_DEVICE_ID_LENGTH)
71 		return dev_err_probe(&client->dev, -ENODEV,
72 				     "IC_DEVICE_ID too short: expected at least 9 bytes, got %d\n",
73 				     ret);
74 
75 	/* 9 bytes of data, buf[0]-buf[7] = "MAX20830", buf[8] = '\0' */
76 	buf[MAX20830_IC_DEVICE_ID_LENGTH - 1] = '\0';
77 	if (strncmp(buf, "MAX20830", MAX20830_IC_DEVICE_ID_LENGTH - 1))
78 		return dev_err_probe(&client->dev, -ENODEV,
79 				     "Unsupported device: '%s'\n", buf);
80 
81 	return pmbus_do_probe(client, &max20830_info);
82 }
83 
84 static const struct i2c_device_id max20830_id[] = {
85 	{"max20830"},
86 	{ }
87 };
88 MODULE_DEVICE_TABLE(i2c, max20830_id);
89 
90 static const struct of_device_id max20830_of_match[] = {
91 	{ .compatible = "adi,max20830" },
92 	{ }
93 };
94 MODULE_DEVICE_TABLE(of, max20830_of_match);
95 
96 static struct i2c_driver max20830_driver = {
97 	.driver = {
98 		.name = "max20830",
99 		.of_match_table = max20830_of_match,
100 	},
101 	.probe = max20830_probe,
102 	.id_table = max20830_id,
103 };
104 
105 module_i2c_driver(max20830_driver);
106 
107 MODULE_AUTHOR("Alexis Czezar Torreno <alexisczezar.torreno@analog.com>");
108 MODULE_DESCRIPTION("PMBus driver for Analog Devices MAX20830");
109 MODULE_LICENSE("GPL");
110 MODULE_IMPORT_NS("PMBUS");
111