blob: a1b4260e75b2ce1ecee7068c72fe3b47dd40d9ce [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Guenter Roeck442aba72011-01-26 20:09:02 -08002/*
3 * Hardware monitoring driver for PMBus devices
4 *
5 * Copyright (c) 2010, 2011 Ericsson AB.
Guenter Roeck442aba72011-01-26 20:09:02 -08006 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/err.h>
12#include <linux/slab.h>
13#include <linux/mutex.h>
14#include <linux/i2c.h>
Wolfram Sang4ba1bb12017-05-21 22:34:43 +020015#include <linux/pmbus.h>
Guenter Roeck442aba72011-01-26 20:09:02 -080016#include "pmbus.h"
17
Xiaoting Liu6f4a46f2019-01-10 10:18:20 +080018struct pmbus_device_info {
19 int pages;
20 u32 flags;
21};
22
Stephen Kittdd431932020-08-08 23:00:04 +020023static const struct i2c_device_id pmbus_id[];
24
Guenter Roeck442aba72011-01-26 20:09:02 -080025/*
26 * Find sensor groups and status registers on each page.
27 */
28static void pmbus_find_sensor_groups(struct i2c_client *client,
29 struct pmbus_driver_info *info)
30{
31 int page;
32
33 /* Sensors detected on page 0 only */
34 if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
35 info->func[0] |= PMBUS_HAVE_VIN;
36 if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
37 info->func[0] |= PMBUS_HAVE_VCAP;
38 if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
39 info->func[0] |= PMBUS_HAVE_IIN;
40 if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
41 info->func[0] |= PMBUS_HAVE_PIN;
42 if (info->func[0]
43 && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
44 info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
Guenter Roeck81ae6812011-06-30 06:54:05 -070045 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
46 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
Guenter Roeck442aba72011-01-26 20:09:02 -080047 info->func[0] |= PMBUS_HAVE_FAN12;
48 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
49 info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
50 }
Guenter Roeck81ae6812011-06-30 06:54:05 -070051 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
52 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
Guenter Roeck442aba72011-01-26 20:09:02 -080053 info->func[0] |= PMBUS_HAVE_FAN34;
54 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
55 info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
56 }
Guenter Roeck22e6b232011-07-03 13:08:03 -070057 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
Guenter Roeck442aba72011-01-26 20:09:02 -080058 info->func[0] |= PMBUS_HAVE_TEMP;
Guenter Roeck0e502ec2011-06-30 06:57:41 -070059 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
60 info->func[0] |= PMBUS_HAVE_TEMP2;
61 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
62 info->func[0] |= PMBUS_HAVE_TEMP3;
Guenter Roeck22e6b232011-07-03 13:08:03 -070063 if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
64 | PMBUS_HAVE_TEMP3)
65 && pmbus_check_byte_register(client, 0,
66 PMBUS_STATUS_TEMPERATURE))
67 info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
Guenter Roeck442aba72011-01-26 20:09:02 -080068
69 /* Sensors detected on all pages */
70 for (page = 0; page < info->pages; page++) {
71 if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
72 info->func[page] |= PMBUS_HAVE_VOUT;
73 if (pmbus_check_byte_register(client, page,
74 PMBUS_STATUS_VOUT))
75 info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
76 }
77 if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
78 info->func[page] |= PMBUS_HAVE_IOUT;
79 if (pmbus_check_byte_register(client, 0,
80 PMBUS_STATUS_IOUT))
81 info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
82 }
83 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
84 info->func[page] |= PMBUS_HAVE_POUT;
85 }
86}
87
88/*
89 * Identify chip parameters.
90 */
91static int pmbus_identify(struct i2c_client *client,
92 struct pmbus_driver_info *info)
93{
Guenter Roeck1061d852011-06-25 11:21:49 -070094 int ret = 0;
95
Guenter Roeck442aba72011-01-26 20:09:02 -080096 if (!info->pages) {
97 /*
98 * Check if the PAGE command is supported. If it is,
99 * keep setting the page number until it fails or until the
100 * maximum number of pages has been reached. Assume that
101 * this is the number of pages supported by the chip.
102 */
103 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
104 int page;
105
106 for (page = 1; page < PMBUS_PAGES; page++) {
Guenter Roeck43f33b62020-01-14 09:49:27 -0800107 if (pmbus_set_page(client, page, 0xff) < 0)
Guenter Roeck442aba72011-01-26 20:09:02 -0800108 break;
109 }
Guenter Roeck43f33b62020-01-14 09:49:27 -0800110 pmbus_set_page(client, 0, 0xff);
Guenter Roeck442aba72011-01-26 20:09:02 -0800111 info->pages = page;
112 } else {
113 info->pages = 1;
114 }
Dmitry Bazhenove7c6a552018-10-15 14:21:22 +0500115
116 pmbus_clear_faults(client);
Guenter Roeck442aba72011-01-26 20:09:02 -0800117 }
118
Guenter Roeck1061d852011-06-25 11:21:49 -0700119 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
Vadim Pasternakb9fa0a32020-01-13 15:08:36 +0000120 int vout_mode, i;
Guenter Roeck1061d852011-06-25 11:21:49 -0700121
122 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
123 if (vout_mode >= 0 && vout_mode != 0xff) {
124 switch (vout_mode >> 5) {
125 case 0:
126 break;
127 case 1:
128 info->format[PSC_VOLTAGE_OUT] = vid;
Vadim Pasternakb9fa0a32020-01-13 15:08:36 +0000129 for (i = 0; i < info->pages; i++)
130 info->vrm_version[i] = vr11;
Guenter Roeck1061d852011-06-25 11:21:49 -0700131 break;
132 case 2:
133 info->format[PSC_VOLTAGE_OUT] = direct;
134 break;
135 default:
136 ret = -ENODEV;
137 goto abort;
138 }
139 }
140 }
141
Guenter Roeck442aba72011-01-26 20:09:02 -0800142 /*
143 * We should check if the COEFFICIENTS register is supported.
144 * If it is, and the chip is configured for direct mode, we can read
145 * the coefficients from the chip, one set per group of sensor
146 * registers.
147 *
148 * To do this, we will need access to a chip which actually supports the
149 * COEFFICIENTS command, since the command is too complex to implement
Guenter Roeck1061d852011-06-25 11:21:49 -0700150 * without testing it. Until then, abort if a chip configured for direct
151 * mode was detected.
Guenter Roeck442aba72011-01-26 20:09:02 -0800152 */
Guenter Roeck1061d852011-06-25 11:21:49 -0700153 if (info->format[PSC_VOLTAGE_OUT] == direct) {
154 ret = -ENODEV;
155 goto abort;
156 }
Guenter Roeck442aba72011-01-26 20:09:02 -0800157
158 /* Try to find sensor groups */
159 pmbus_find_sensor_groups(client, info);
Guenter Roeck1061d852011-06-25 11:21:49 -0700160abort:
161 return ret;
Guenter Roeck442aba72011-01-26 20:09:02 -0800162}
163
Stephen Kittdd431932020-08-08 23:00:04 +0200164static int pmbus_probe(struct i2c_client *client)
Guenter Roeck442aba72011-01-26 20:09:02 -0800165{
166 struct pmbus_driver_info *info;
Vadim Pasternakcc00dec2016-07-25 10:55:53 +0000167 struct pmbus_platform_data *pdata = NULL;
168 struct device *dev = &client->dev;
Xiaoting Liu6f4a46f2019-01-10 10:18:20 +0800169 struct pmbus_device_info *device_info;
Guenter Roeck442aba72011-01-26 20:09:02 -0800170
Vadim Pasternakcc00dec2016-07-25 10:55:53 +0000171 info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
Guenter Roeck442aba72011-01-26 20:09:02 -0800172 if (!info)
173 return -ENOMEM;
174
Stephen Kittdd431932020-08-08 23:00:04 +0200175 device_info = (struct pmbus_device_info *)i2c_match_id(pmbus_id, client)->driver_data;
Xiaoting Liu6f4a46f2019-01-10 10:18:20 +0800176 if (device_info->flags & PMBUS_SKIP_STATUS_CHECK) {
Vadim Pasternakcc00dec2016-07-25 10:55:53 +0000177 pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
178 GFP_KERNEL);
179 if (!pdata)
180 return -ENOMEM;
181
182 pdata->flags = PMBUS_SKIP_STATUS_CHECK;
183 }
184
Xiaoting Liu6f4a46f2019-01-10 10:18:20 +0800185 info->pages = device_info->pages;
Guenter Roeck442aba72011-01-26 20:09:02 -0800186 info->identify = pmbus_identify;
Vadim Pasternakcc00dec2016-07-25 10:55:53 +0000187 dev->platform_data = pdata;
Guenter Roeck442aba72011-01-26 20:09:02 -0800188
Stephen Kittdd431932020-08-08 23:00:04 +0200189 return pmbus_do_probe(client, info);
Guenter Roeck442aba72011-01-26 20:09:02 -0800190}
191
Xiaoting Liu6f4a46f2019-01-10 10:18:20 +0800192static const struct pmbus_device_info pmbus_info_one = {
193 .pages = 1,
194 .flags = 0
195};
196static const struct pmbus_device_info pmbus_info_zero = {
197 .pages = 0,
198 .flags = 0
199};
200static const struct pmbus_device_info pmbus_info_one_skip = {
201 .pages = 1,
202 .flags = PMBUS_SKIP_STATUS_CHECK
203};
204
Guenter Roeck442aba72011-01-26 20:09:02 -0800205/*
206 * Use driver_data to set the number of pages supported by the chip.
207 */
208static const struct i2c_device_id pmbus_id[] = {
Xiaoting Liu6f4a46f2019-01-10 10:18:20 +0800209 {"adp4000", (kernel_ulong_t)&pmbus_info_one},
210 {"bmr453", (kernel_ulong_t)&pmbus_info_one},
211 {"bmr454", (kernel_ulong_t)&pmbus_info_one},
212 {"dps460", (kernel_ulong_t)&pmbus_info_one_skip},
Xiaoting Liu705f2c82019-01-10 10:31:44 +0800213 {"dps650ab", (kernel_ulong_t)&pmbus_info_one_skip},
Xiaoting Liu6f4a46f2019-01-10 10:18:20 +0800214 {"dps800", (kernel_ulong_t)&pmbus_info_one_skip},
Guenter Roeck1a1ea122019-12-13 13:36:36 -0800215 {"max20796", (kernel_ulong_t)&pmbus_info_one},
Xiaoting Liu6f4a46f2019-01-10 10:18:20 +0800216 {"mdt040", (kernel_ulong_t)&pmbus_info_one},
217 {"ncp4200", (kernel_ulong_t)&pmbus_info_one},
218 {"ncp4208", (kernel_ulong_t)&pmbus_info_one},
219 {"pdt003", (kernel_ulong_t)&pmbus_info_one},
220 {"pdt006", (kernel_ulong_t)&pmbus_info_one},
221 {"pdt012", (kernel_ulong_t)&pmbus_info_one},
222 {"pmbus", (kernel_ulong_t)&pmbus_info_zero},
223 {"sgd009", (kernel_ulong_t)&pmbus_info_one_skip},
224 {"tps40400", (kernel_ulong_t)&pmbus_info_one},
225 {"tps544b20", (kernel_ulong_t)&pmbus_info_one},
226 {"tps544b25", (kernel_ulong_t)&pmbus_info_one},
227 {"tps544c20", (kernel_ulong_t)&pmbus_info_one},
228 {"tps544c25", (kernel_ulong_t)&pmbus_info_one},
229 {"udt020", (kernel_ulong_t)&pmbus_info_one},
Guenter Roeck442aba72011-01-26 20:09:02 -0800230 {}
231};
232
233MODULE_DEVICE_TABLE(i2c, pmbus_id);
234
235/* This is the driver that will be inserted */
236static struct i2c_driver pmbus_driver = {
237 .driver = {
238 .name = "pmbus",
239 },
Stephen Kittdd431932020-08-08 23:00:04 +0200240 .probe_new = pmbus_probe,
Guenter Roeck442aba72011-01-26 20:09:02 -0800241 .id_table = pmbus_id,
242};
243
Axel Linf0967ee2012-01-20 15:38:18 +0800244module_i2c_driver(pmbus_driver);
Guenter Roeck442aba72011-01-26 20:09:02 -0800245
246MODULE_AUTHOR("Guenter Roeck");
247MODULE_DESCRIPTION("Generic PMBus driver");
248MODULE_LICENSE("GPL");