1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for Maxim MAX15301
5 * Copyright (c) 2021 Flextronics International Sweden AB
7 * Even though the specification does not specifically mention it,
8 * extensive empirical testing has revealed that auto-detection of
9 * limit-registers will fail in a random fashion unless the delay
10 * parameter is set to above about 80us. The default delay is set
11 * to 100us to include some safety margin.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/ktime.h>
21 #include <linux/delay.h>
22 #include <linux/pmbus.h>
25 static const struct i2c_device_id max15301_id[] = {
30 MODULE_DEVICE_TABLE(i2c, max15301_id);
32 struct max15301_data {
34 ktime_t access; /* Chip access time */
35 int delay; /* Delay between chip accesses in us */
36 struct pmbus_driver_info info;
39 #define to_max15301_data(x) container_of(x, struct max15301_data, info)
41 #define MAX15301_WAIT_TIME 100 /* us */
43 static ushort delay = MAX15301_WAIT_TIME;
44 module_param(delay, ushort, 0644);
45 MODULE_PARM_DESC(delay, "Delay between chip accesses in us");
47 static struct max15301_data max15301_data = {
50 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
51 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
52 | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
53 | PMBUS_HAVE_STATUS_TEMP
54 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
58 /* This chip needs a delay between accesses */
59 static inline void max15301_wait(const struct max15301_data *data)
62 s64 delta = ktime_us_delta(ktime_get(), data->access);
64 if (delta < data->delay)
65 udelay(data->delay - delta);
69 static int max15301_read_word_data(struct i2c_client *client, int page,
72 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
73 struct max15301_data *data = to_max15301_data(info);
79 if (reg >= PMBUS_VIRT_BASE)
83 ret = pmbus_read_word_data(client, page, phase, reg);
84 data->access = ktime_get();
89 static int max15301_read_byte_data(struct i2c_client *client, int page, int reg)
91 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
92 struct max15301_data *data = to_max15301_data(info);
99 ret = pmbus_read_byte_data(client, page, reg);
100 data->access = ktime_get();
105 static int max15301_write_word_data(struct i2c_client *client, int page, int reg,
108 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
109 struct max15301_data *data = to_max15301_data(info);
115 if (reg >= PMBUS_VIRT_BASE)
119 ret = pmbus_write_word_data(client, page, reg, word);
120 data->access = ktime_get();
125 static int max15301_write_byte(struct i2c_client *client, int page, u8 value)
127 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
128 struct max15301_data *data = to_max15301_data(info);
135 ret = pmbus_write_byte(client, page, value);
136 data->access = ktime_get();
141 static int max15301_probe(struct i2c_client *client)
144 u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
145 const struct i2c_device_id *mid;
146 struct pmbus_driver_info *info = &max15301_data.info;
148 if (!i2c_check_functionality(client->adapter,
149 I2C_FUNC_SMBUS_READ_BYTE_DATA
150 | I2C_FUNC_SMBUS_BLOCK_DATA))
153 status = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, device_id);
155 dev_err(&client->dev, "Failed to read Device Id\n");
158 for (mid = max15301_id; mid->name[0]; mid++) {
159 if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
163 dev_err(&client->dev, "Unsupported device\n");
167 max15301_data.delay = delay;
169 info->read_byte_data = max15301_read_byte_data;
170 info->read_word_data = max15301_read_word_data;
171 info->write_byte = max15301_write_byte;
172 info->write_word_data = max15301_write_word_data;
174 return pmbus_do_probe(client, info);
177 static struct i2c_driver max15301_driver = {
181 .probe = max15301_probe,
182 .id_table = max15301_id,
185 module_i2c_driver(max15301_driver);
188 MODULE_DESCRIPTION("PMBus driver for Maxim MAX15301");
189 MODULE_LICENSE("GPL");
190 MODULE_IMPORT_NS(PMBUS);