1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Hardware monitoring driver for Analog Devices LT3074
4 *
5 * Copyright (C) 2025 Analog Devices, Inc.
6 */
7 #include <linux/err.h>
8 #include <linux/i2c.h>
9 #include <linux/module.h>
10
11 #include "pmbus.h"
12
13 #define LT3074_MFR_READ_VBIAS 0xc6
14 #define LT3074_MFR_BIAS_OV_WARN_LIMIT 0xc7
15 #define LT3074_MFR_BIAS_UV_WARN_LIMIT 0xc8
16 #define LT3074_MFR_SPECIAL_ID 0xe7
17
18 #define LT3074_SPECIAL_ID_VALUE 0x1c1d
19
20 static const struct regulator_desc __maybe_unused lt3074_reg_desc[] = {
21 PMBUS_REGULATOR_ONE("regulator"),
22 };
23
lt3074_read_word_data(struct i2c_client * client,int page,int phase,int reg)24 static int lt3074_read_word_data(struct i2c_client *client, int page,
25 int phase, int reg)
26 {
27 switch (reg) {
28 case PMBUS_VIRT_READ_VMON:
29 return pmbus_read_word_data(client, page, phase,
30 LT3074_MFR_READ_VBIAS);
31 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
32 return pmbus_read_word_data(client, page, phase,
33 LT3074_MFR_BIAS_UV_WARN_LIMIT);
34 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
35 return pmbus_read_word_data(client, page, phase,
36 LT3074_MFR_BIAS_OV_WARN_LIMIT);
37 default:
38 return -ENODATA;
39 }
40 }
41
lt3074_write_word_data(struct i2c_client * client,int page,int reg,u16 word)42 static int lt3074_write_word_data(struct i2c_client *client, int page,
43 int reg, u16 word)
44 {
45 switch (reg) {
46 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
47 return pmbus_write_word_data(client, 0,
48 LT3074_MFR_BIAS_UV_WARN_LIMIT,
49 word);
50 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
51 return pmbus_write_word_data(client, 0,
52 LT3074_MFR_BIAS_OV_WARN_LIMIT,
53 word);
54 default:
55 return -ENODATA;
56 }
57 }
58
59 static struct pmbus_driver_info lt3074_info = {
60 .pages = 1,
61 .format[PSC_VOLTAGE_IN] = linear,
62 .format[PSC_VOLTAGE_OUT] = linear,
63 .format[PSC_CURRENT_OUT] = linear,
64 .format[PSC_TEMPERATURE] = linear,
65 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
66 PMBUS_HAVE_TEMP | PMBUS_HAVE_VMON |
67 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
68 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
69 .read_word_data = lt3074_read_word_data,
70 .write_word_data = lt3074_write_word_data,
71 #if IS_ENABLED(CONFIG_SENSORS_LT3074_REGULATOR)
72 .num_regulators = 1,
73 .reg_desc = lt3074_reg_desc,
74 #endif
75 };
76
lt3074_probe(struct i2c_client * client)77 static int lt3074_probe(struct i2c_client *client)
78 {
79 int ret;
80 struct device *dev = &client->dev;
81
82 if (!i2c_check_functionality(client->adapter,
83 I2C_FUNC_SMBUS_READ_WORD_DATA))
84 return -ENODEV;
85
86 ret = i2c_smbus_read_word_data(client, LT3074_MFR_SPECIAL_ID);
87 if (ret < 0)
88 return dev_err_probe(dev, ret, "Failed to read ID\n");
89
90 if (ret != LT3074_SPECIAL_ID_VALUE)
91 return dev_err_probe(dev, -ENODEV, "ID mismatch\n");
92
93 return pmbus_do_probe(client, <3074_info);
94 }
95
96 static const struct i2c_device_id lt3074_id[] = {
97 { .name = "lt3074" },
98 { }
99 };
100 MODULE_DEVICE_TABLE(i2c, lt3074_id);
101
102 static const struct of_device_id __maybe_unused lt3074_of_match[] = {
103 { .compatible = "adi,lt3074" },
104 {}
105 };
106 MODULE_DEVICE_TABLE(of, lt3074_of_match);
107
108 static struct i2c_driver lt3074_driver = {
109 .driver = {
110 .name = "lt3074",
111 .of_match_table = of_match_ptr(lt3074_of_match),
112 },
113 .probe = lt3074_probe,
114 .id_table = lt3074_id,
115 };
116 module_i2c_driver(lt3074_driver);
117
118 MODULE_AUTHOR("Cedric Encarnacion <cedricjustine.encarnacion@analog.com>");
119 MODULE_DESCRIPTION("PMBus driver for Analog Devices LT3074");
120 MODULE_LICENSE("GPL");
121 MODULE_IMPORT_NS("PMBUS");
122