]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/hwmon/pmbus/pmbus.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 60
[mirror_ubuntu-jammy-kernel.git] / drivers / hwmon / pmbus / pmbus.c
CommitLineData
442aba78
GR
1/*
2 * Hardware monitoring driver for PMBus devices
3 *
4 * Copyright (c) 2010, 2011 Ericsson AB.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/slab.h>
26#include <linux/mutex.h>
27#include <linux/i2c.h>
4ba1bb12 28#include <linux/pmbus.h>
442aba78
GR
29#include "pmbus.h"
30
6f4a46f0
XL
31struct pmbus_device_info {
32 int pages;
33 u32 flags;
34};
35
442aba78
GR
36/*
37 * Find sensor groups and status registers on each page.
38 */
39static void pmbus_find_sensor_groups(struct i2c_client *client,
40 struct pmbus_driver_info *info)
41{
42 int page;
43
44 /* Sensors detected on page 0 only */
45 if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
46 info->func[0] |= PMBUS_HAVE_VIN;
47 if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
48 info->func[0] |= PMBUS_HAVE_VCAP;
49 if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
50 info->func[0] |= PMBUS_HAVE_IIN;
51 if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
52 info->func[0] |= PMBUS_HAVE_PIN;
53 if (info->func[0]
54 && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
55 info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
81ae6814
GR
56 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
57 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
442aba78
GR
58 info->func[0] |= PMBUS_HAVE_FAN12;
59 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
60 info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
61 }
81ae6814
GR
62 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
63 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
442aba78
GR
64 info->func[0] |= PMBUS_HAVE_FAN34;
65 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
66 info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
67 }
22e6b231 68 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
442aba78 69 info->func[0] |= PMBUS_HAVE_TEMP;
0e502ec8
GR
70 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
71 info->func[0] |= PMBUS_HAVE_TEMP2;
72 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
73 info->func[0] |= PMBUS_HAVE_TEMP3;
22e6b231
GR
74 if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
75 | PMBUS_HAVE_TEMP3)
76 && pmbus_check_byte_register(client, 0,
77 PMBUS_STATUS_TEMPERATURE))
78 info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
442aba78
GR
79
80 /* Sensors detected on all pages */
81 for (page = 0; page < info->pages; page++) {
82 if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
83 info->func[page] |= PMBUS_HAVE_VOUT;
84 if (pmbus_check_byte_register(client, page,
85 PMBUS_STATUS_VOUT))
86 info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
87 }
88 if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
89 info->func[page] |= PMBUS_HAVE_IOUT;
90 if (pmbus_check_byte_register(client, 0,
91 PMBUS_STATUS_IOUT))
92 info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
93 }
94 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
95 info->func[page] |= PMBUS_HAVE_POUT;
96 }
97}
98
99/*
100 * Identify chip parameters.
101 */
102static int pmbus_identify(struct i2c_client *client,
103 struct pmbus_driver_info *info)
104{
1061d851
GR
105 int ret = 0;
106
442aba78
GR
107 if (!info->pages) {
108 /*
109 * Check if the PAGE command is supported. If it is,
110 * keep setting the page number until it fails or until the
111 * maximum number of pages has been reached. Assume that
112 * this is the number of pages supported by the chip.
113 */
114 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
115 int page;
116
117 for (page = 1; page < PMBUS_PAGES; page++) {
118 if (pmbus_set_page(client, page) < 0)
119 break;
120 }
121 pmbus_set_page(client, 0);
122 info->pages = page;
123 } else {
124 info->pages = 1;
125 }
e7c6a556
DB
126
127 pmbus_clear_faults(client);
442aba78
GR
128 }
129
1061d851
GR
130 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
131 int vout_mode;
132
133 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
134 if (vout_mode >= 0 && vout_mode != 0xff) {
135 switch (vout_mode >> 5) {
136 case 0:
137 break;
138 case 1:
139 info->format[PSC_VOLTAGE_OUT] = vid;
068c2270 140 info->vrm_version = vr11;
1061d851
GR
141 break;
142 case 2:
143 info->format[PSC_VOLTAGE_OUT] = direct;
144 break;
145 default:
146 ret = -ENODEV;
147 goto abort;
148 }
149 }
150 }
151
442aba78
GR
152 /*
153 * We should check if the COEFFICIENTS register is supported.
154 * If it is, and the chip is configured for direct mode, we can read
155 * the coefficients from the chip, one set per group of sensor
156 * registers.
157 *
158 * To do this, we will need access to a chip which actually supports the
159 * COEFFICIENTS command, since the command is too complex to implement
1061d851
GR
160 * without testing it. Until then, abort if a chip configured for direct
161 * mode was detected.
442aba78 162 */
1061d851
GR
163 if (info->format[PSC_VOLTAGE_OUT] == direct) {
164 ret = -ENODEV;
165 goto abort;
166 }
442aba78
GR
167
168 /* Try to find sensor groups */
169 pmbus_find_sensor_groups(client, info);
1061d851
GR
170abort:
171 return ret;
442aba78
GR
172}
173
174static int pmbus_probe(struct i2c_client *client,
175 const struct i2c_device_id *id)
176{
177 struct pmbus_driver_info *info;
cc00decf
VP
178 struct pmbus_platform_data *pdata = NULL;
179 struct device *dev = &client->dev;
6f4a46f0 180 struct pmbus_device_info *device_info;
442aba78 181
cc00decf 182 info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
442aba78
GR
183 if (!info)
184 return -ENOMEM;
185
6f4a46f0
XL
186 device_info = (struct pmbus_device_info *)id->driver_data;
187 if (device_info->flags & PMBUS_SKIP_STATUS_CHECK) {
cc00decf
VP
188 pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
189 GFP_KERNEL);
190 if (!pdata)
191 return -ENOMEM;
192
193 pdata->flags = PMBUS_SKIP_STATUS_CHECK;
194 }
195
6f4a46f0 196 info->pages = device_info->pages;
442aba78 197 info->identify = pmbus_identify;
cc00decf 198 dev->platform_data = pdata;
442aba78 199
8b313ca7 200 return pmbus_do_probe(client, id, info);
442aba78
GR
201}
202
6f4a46f0
XL
203static const struct pmbus_device_info pmbus_info_one = {
204 .pages = 1,
205 .flags = 0
206};
207static const struct pmbus_device_info pmbus_info_zero = {
208 .pages = 0,
209 .flags = 0
210};
211static const struct pmbus_device_info pmbus_info_one_skip = {
212 .pages = 1,
213 .flags = PMBUS_SKIP_STATUS_CHECK
214};
215
442aba78
GR
216/*
217 * Use driver_data to set the number of pages supported by the chip.
218 */
219static const struct i2c_device_id pmbus_id[] = {
6f4a46f0
XL
220 {"adp4000", (kernel_ulong_t)&pmbus_info_one},
221 {"bmr453", (kernel_ulong_t)&pmbus_info_one},
222 {"bmr454", (kernel_ulong_t)&pmbus_info_one},
223 {"dps460", (kernel_ulong_t)&pmbus_info_one_skip},
705f2c81 224 {"dps650ab", (kernel_ulong_t)&pmbus_info_one_skip},
6f4a46f0
XL
225 {"dps800", (kernel_ulong_t)&pmbus_info_one_skip},
226 {"mdt040", (kernel_ulong_t)&pmbus_info_one},
227 {"ncp4200", (kernel_ulong_t)&pmbus_info_one},
228 {"ncp4208", (kernel_ulong_t)&pmbus_info_one},
229 {"pdt003", (kernel_ulong_t)&pmbus_info_one},
230 {"pdt006", (kernel_ulong_t)&pmbus_info_one},
231 {"pdt012", (kernel_ulong_t)&pmbus_info_one},
232 {"pmbus", (kernel_ulong_t)&pmbus_info_zero},
233 {"sgd009", (kernel_ulong_t)&pmbus_info_one_skip},
234 {"tps40400", (kernel_ulong_t)&pmbus_info_one},
235 {"tps544b20", (kernel_ulong_t)&pmbus_info_one},
236 {"tps544b25", (kernel_ulong_t)&pmbus_info_one},
237 {"tps544c20", (kernel_ulong_t)&pmbus_info_one},
238 {"tps544c25", (kernel_ulong_t)&pmbus_info_one},
239 {"udt020", (kernel_ulong_t)&pmbus_info_one},
442aba78
GR
240 {}
241};
242
243MODULE_DEVICE_TABLE(i2c, pmbus_id);
244
245/* This is the driver that will be inserted */
246static struct i2c_driver pmbus_driver = {
247 .driver = {
248 .name = "pmbus",
249 },
250 .probe = pmbus_probe,
dd285ad7 251 .remove = pmbus_do_remove,
442aba78
GR
252 .id_table = pmbus_id,
253};
254
f0967eea 255module_i2c_driver(pmbus_driver);
442aba78
GR
256
257MODULE_AUTHOR("Guenter Roeck");
258MODULE_DESCRIPTION("Generic PMBus driver");
259MODULE_LICENSE("GPL");