Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
83f7649c GR |
2 | /* |
3 | * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller | |
4 | * and Digital Power Monitor | |
5 | * | |
6 | * Copyright (c) 2011 Ericsson AB. | |
4ff0ce22 | 7 | * Copyright (c) 2018 Guenter Roeck |
83f7649c GR |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/err.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/i2c.h> | |
99b41608 | 16 | #include <linux/bitops.h> |
c83529c1 AKNPW |
17 | #include <linux/bitfield.h> |
18 | #include <linux/log2.h> | |
83f7649c GR |
19 | #include "pmbus.h" |
20 | ||
4ff0ce22 | 21 | enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 }; |
68a40382 GR |
22 | |
23 | #define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0) | |
24 | #define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5) | |
25 | #define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6) | |
5cf231a3 | 26 | |
c576e30c GR |
27 | #define ADM1275_PEAK_IOUT 0xd0 |
28 | #define ADM1275_PEAK_VIN 0xd1 | |
29 | #define ADM1275_PEAK_VOUT 0xd2 | |
83f7649c GR |
30 | #define ADM1275_PMON_CONFIG 0xd4 |
31 | ||
99b41608 GR |
32 | #define ADM1275_VIN_VOUT_SELECT BIT(6) |
33 | #define ADM1275_VRANGE BIT(5) | |
34 | #define ADM1075_IRANGE_50 BIT(4) | |
35 | #define ADM1075_IRANGE_25 BIT(3) | |
36 | #define ADM1075_IRANGE_MASK (BIT(3) | BIT(4)) | |
83f7649c | 37 | |
4ff0ce22 GR |
38 | #define ADM1272_IRANGE BIT(0) |
39 | ||
b153a0bb | 40 | #define ADM1278_TSFILT BIT(15) |
709066ac GR |
41 | #define ADM1278_TEMP1_EN BIT(3) |
42 | #define ADM1278_VIN_EN BIT(2) | |
43 | #define ADM1278_VOUT_EN BIT(1) | |
44 | ||
b153a0bb GR |
45 | #define ADM1278_PMON_DEFCONFIG (ADM1278_VOUT_EN | ADM1278_TEMP1_EN | ADM1278_TSFILT) |
46 | ||
68a40382 GR |
47 | #define ADM1293_IRANGE_25 0 |
48 | #define ADM1293_IRANGE_50 BIT(6) | |
49 | #define ADM1293_IRANGE_100 BIT(7) | |
50 | #define ADM1293_IRANGE_200 (BIT(6) | BIT(7)) | |
51 | #define ADM1293_IRANGE_MASK (BIT(6) | BIT(7)) | |
52 | ||
53 | #define ADM1293_VIN_SEL_012 BIT(2) | |
54 | #define ADM1293_VIN_SEL_074 BIT(3) | |
55 | #define ADM1293_VIN_SEL_210 (BIT(2) | BIT(3)) | |
56 | #define ADM1293_VIN_SEL_MASK (BIT(2) | BIT(3)) | |
57 | ||
58 | #define ADM1293_VAUX_EN BIT(1) | |
59 | ||
709066ac | 60 | #define ADM1278_PEAK_TEMP 0xd7 |
c5e67636 GR |
61 | #define ADM1275_IOUT_WARN2_LIMIT 0xd7 |
62 | #define ADM1275_DEVICE_CONFIG 0xd8 | |
63 | ||
99b41608 | 64 | #define ADM1275_IOUT_WARN2_SELECT BIT(4) |
c5e67636 | 65 | |
5cf231a3 | 66 | #define ADM1276_PEAK_PIN 0xda |
92711269 GR |
67 | #define ADM1075_READ_VAUX 0xdd |
68 | #define ADM1075_VAUX_OV_WARN_LIMIT 0xde | |
69 | #define ADM1075_VAUX_UV_WARN_LIMIT 0xdf | |
68a40382 GR |
70 | #define ADM1293_IOUT_MIN 0xe3 |
71 | #define ADM1293_PIN_MIN 0xe4 | |
92711269 GR |
72 | #define ADM1075_VAUX_STATUS 0xf6 |
73 | ||
99b41608 GR |
74 | #define ADM1075_VAUX_OV_WARN BIT(7) |
75 | #define ADM1075_VAUX_UV_WARN BIT(6) | |
92711269 | 76 | |
7d45deb3 GR |
77 | #define ADM1275_VI_AVG_SHIFT 0 |
78 | #define ADM1275_VI_AVG_MASK GENMASK(ADM1275_VI_AVG_SHIFT + 2, \ | |
79 | ADM1275_VI_AVG_SHIFT) | |
80 | #define ADM1275_SAMPLES_AVG_MAX 128 | |
81 | ||
82 | #define ADM1278_PWR_AVG_SHIFT 11 | |
83 | #define ADM1278_PWR_AVG_MASK GENMASK(ADM1278_PWR_AVG_SHIFT + 2, \ | |
84 | ADM1278_PWR_AVG_SHIFT) | |
85 | #define ADM1278_VI_AVG_SHIFT 8 | |
86 | #define ADM1278_VI_AVG_MASK GENMASK(ADM1278_VI_AVG_SHIFT + 2, \ | |
87 | ADM1278_VI_AVG_SHIFT) | |
c83529c1 | 88 | |
c5e67636 | 89 | struct adm1275_data { |
5cf231a3 | 90 | int id; |
c5e67636 | 91 | bool have_oc_fault; |
9048539b GR |
92 | bool have_uc_fault; |
93 | bool have_vout; | |
94 | bool have_vaux_status; | |
68a40382 GR |
95 | bool have_mfr_vaux_status; |
96 | bool have_iout_min; | |
97 | bool have_pin_min; | |
9048539b | 98 | bool have_pin_max; |
709066ac | 99 | bool have_temp_max; |
7d45deb3 | 100 | bool have_power_sampling; |
c5e67636 GR |
101 | struct pmbus_driver_info info; |
102 | }; | |
103 | ||
104 | #define to_adm1275_data(x) container_of(x, struct adm1275_data, info) | |
105 | ||
904b296f GR |
106 | struct coefficients { |
107 | s16 m; | |
108 | s16 b; | |
109 | s16 R; | |
110 | }; | |
111 | ||
112 | static const struct coefficients adm1075_coefficients[] = { | |
113 | [0] = { 27169, 0, -1 }, /* voltage */ | |
114 | [1] = { 806, 20475, -1 }, /* current, irange25 */ | |
115 | [2] = { 404, 20475, -1 }, /* current, irange50 */ | |
6faecba0 SD |
116 | [3] = { 8549, 0, -1 }, /* power, irange25 */ |
117 | [4] = { 4279, 0, -1 }, /* power, irange50 */ | |
904b296f GR |
118 | }; |
119 | ||
4ff0ce22 GR |
120 | static const struct coefficients adm1272_coefficients[] = { |
121 | [0] = { 6770, 0, -2 }, /* voltage, vrange 60V */ | |
122 | [1] = { 4062, 0, -2 }, /* voltage, vrange 100V */ | |
123 | [2] = { 1326, 20480, -1 }, /* current, vsense range 15mV */ | |
124 | [3] = { 663, 20480, -1 }, /* current, vsense range 30mV */ | |
125 | [4] = { 3512, 0, -2 }, /* power, vrange 60V, irange 15mV */ | |
126 | [5] = { 21071, 0, -3 }, /* power, vrange 100V, irange 15mV */ | |
127 | [6] = { 17561, 0, -3 }, /* power, vrange 60V, irange 30mV */ | |
128 | [7] = { 10535, 0, -3 }, /* power, vrange 100V, irange 30mV */ | |
129 | [8] = { 42, 31871, -1 }, /* temperature */ | |
130 | ||
131 | }; | |
132 | ||
904b296f GR |
133 | static const struct coefficients adm1275_coefficients[] = { |
134 | [0] = { 19199, 0, -2 }, /* voltage, vrange set */ | |
135 | [1] = { 6720, 0, -1 }, /* voltage, vrange not set */ | |
136 | [2] = { 807, 20475, -1 }, /* current */ | |
137 | }; | |
138 | ||
139 | static const struct coefficients adm1276_coefficients[] = { | |
140 | [0] = { 19199, 0, -2 }, /* voltage, vrange set */ | |
141 | [1] = { 6720, 0, -1 }, /* voltage, vrange not set */ | |
142 | [2] = { 807, 20475, -1 }, /* current */ | |
143 | [3] = { 6043, 0, -2 }, /* power, vrange set */ | |
144 | [4] = { 2115, 0, -1 }, /* power, vrange not set */ | |
145 | }; | |
146 | ||
709066ac GR |
147 | static const struct coefficients adm1278_coefficients[] = { |
148 | [0] = { 19599, 0, -2 }, /* voltage */ | |
149 | [1] = { 800, 20475, -1 }, /* current */ | |
150 | [2] = { 6123, 0, -2 }, /* power */ | |
151 | [3] = { 42, 31880, -1 }, /* temperature */ | |
152 | }; | |
153 | ||
68a40382 GR |
154 | static const struct coefficients adm1293_coefficients[] = { |
155 | [0] = { 3333, -1, 0 }, /* voltage, vrange 1.2V */ | |
156 | [1] = { 5552, -5, -1 }, /* voltage, vrange 7.4V */ | |
157 | [2] = { 19604, -50, -2 }, /* voltage, vrange 21V */ | |
158 | [3] = { 8000, -100, -2 }, /* current, irange25 */ | |
159 | [4] = { 4000, -100, -2 }, /* current, irange50 */ | |
160 | [5] = { 20000, -1000, -3 }, /* current, irange100 */ | |
161 | [6] = { 10000, -1000, -3 }, /* current, irange200 */ | |
162 | [7] = { 10417, 0, -1 }, /* power, 1.2V, irange25 */ | |
163 | [8] = { 5208, 0, -1 }, /* power, 1.2V, irange50 */ | |
164 | [9] = { 26042, 0, -2 }, /* power, 1.2V, irange100 */ | |
165 | [10] = { 13021, 0, -2 }, /* power, 1.2V, irange200 */ | |
166 | [11] = { 17351, 0, -2 }, /* power, 7.4V, irange25 */ | |
167 | [12] = { 8676, 0, -2 }, /* power, 7.4V, irange50 */ | |
168 | [13] = { 4338, 0, -2 }, /* power, 7.4V, irange100 */ | |
169 | [14] = { 21689, 0, -3 }, /* power, 7.4V, irange200 */ | |
170 | [15] = { 6126, 0, -2 }, /* power, 21V, irange25 */ | |
171 | [16] = { 30631, 0, -3 }, /* power, 21V, irange50 */ | |
172 | [17] = { 15316, 0, -3 }, /* power, 21V, irange100 */ | |
173 | [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */ | |
174 | }; | |
175 | ||
7d45deb3 GR |
176 | static int adm1275_read_pmon_config(const struct adm1275_data *data, |
177 | struct i2c_client *client, bool is_power) | |
c83529c1 | 178 | { |
7d45deb3 GR |
179 | int shift, ret; |
180 | u16 mask; | |
181 | ||
182 | /* | |
183 | * The PMON configuration register is a 16-bit register only on chips | |
184 | * supporting power average sampling. On other chips it is an 8-bit | |
185 | * register. | |
186 | */ | |
187 | if (data->have_power_sampling) { | |
188 | ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG); | |
189 | mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK; | |
190 | shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT; | |
191 | } else { | |
192 | ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); | |
193 | mask = ADM1275_VI_AVG_MASK; | |
194 | shift = ADM1275_VI_AVG_SHIFT; | |
195 | } | |
c83529c1 AKNPW |
196 | if (ret < 0) |
197 | return ret; | |
198 | ||
7d45deb3 | 199 | return (ret & mask) >> shift; |
c83529c1 AKNPW |
200 | } |
201 | ||
7d45deb3 GR |
202 | static int adm1275_write_pmon_config(const struct adm1275_data *data, |
203 | struct i2c_client *client, | |
204 | bool is_power, u16 word) | |
c83529c1 | 205 | { |
7d45deb3 GR |
206 | int shift, ret; |
207 | u16 mask; | |
208 | ||
209 | if (data->have_power_sampling) { | |
210 | ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG); | |
211 | mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK; | |
212 | shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT; | |
213 | } else { | |
214 | ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); | |
215 | mask = ADM1275_VI_AVG_MASK; | |
216 | shift = ADM1275_VI_AVG_SHIFT; | |
217 | } | |
c83529c1 AKNPW |
218 | if (ret < 0) |
219 | return ret; | |
220 | ||
7d45deb3 GR |
221 | word = (ret & ~mask) | ((word << shift) & mask); |
222 | if (data->have_power_sampling) | |
223 | ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG, | |
224 | word); | |
225 | else | |
226 | ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG, | |
227 | word); | |
c83529c1 AKNPW |
228 | |
229 | return ret; | |
230 | } | |
231 | ||
43f33b6e GR |
232 | static int adm1275_read_word_data(struct i2c_client *client, int page, |
233 | int phase, int reg) | |
c576e30c | 234 | { |
c5e67636 GR |
235 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
236 | const struct adm1275_data *data = to_adm1275_data(info); | |
5cf231a3 | 237 | int ret = 0; |
c576e30c | 238 | |
ecb29abd | 239 | if (page > 0) |
c5e67636 | 240 | return -ENXIO; |
c576e30c GR |
241 | |
242 | switch (reg) { | |
c5e67636 | 243 | case PMBUS_IOUT_UC_FAULT_LIMIT: |
9048539b GR |
244 | if (!data->have_uc_fault) |
245 | return -ENXIO; | |
43f33b6e GR |
246 | ret = pmbus_read_word_data(client, 0, 0xff, |
247 | ADM1275_IOUT_WARN2_LIMIT); | |
c5e67636 GR |
248 | break; |
249 | case PMBUS_IOUT_OC_FAULT_LIMIT: | |
9048539b GR |
250 | if (!data->have_oc_fault) |
251 | return -ENXIO; | |
43f33b6e GR |
252 | ret = pmbus_read_word_data(client, 0, 0xff, |
253 | ADM1275_IOUT_WARN2_LIMIT); | |
c5e67636 | 254 | break; |
92711269 | 255 | case PMBUS_VOUT_OV_WARN_LIMIT: |
9048539b GR |
256 | if (data->have_vout) |
257 | return -ENODATA; | |
43f33b6e | 258 | ret = pmbus_read_word_data(client, 0, 0xff, |
92711269 GR |
259 | ADM1075_VAUX_OV_WARN_LIMIT); |
260 | break; | |
261 | case PMBUS_VOUT_UV_WARN_LIMIT: | |
9048539b GR |
262 | if (data->have_vout) |
263 | return -ENODATA; | |
43f33b6e | 264 | ret = pmbus_read_word_data(client, 0, 0xff, |
92711269 GR |
265 | ADM1075_VAUX_UV_WARN_LIMIT); |
266 | break; | |
267 | case PMBUS_READ_VOUT: | |
9048539b GR |
268 | if (data->have_vout) |
269 | return -ENODATA; | |
43f33b6e GR |
270 | ret = pmbus_read_word_data(client, 0, 0xff, |
271 | ADM1075_READ_VAUX); | |
92711269 | 272 | break; |
68a40382 GR |
273 | case PMBUS_VIRT_READ_IOUT_MIN: |
274 | if (!data->have_iout_min) | |
275 | return -ENXIO; | |
43f33b6e GR |
276 | ret = pmbus_read_word_data(client, 0, 0xff, |
277 | ADM1293_IOUT_MIN); | |
68a40382 | 278 | break; |
c576e30c | 279 | case PMBUS_VIRT_READ_IOUT_MAX: |
43f33b6e GR |
280 | ret = pmbus_read_word_data(client, 0, 0xff, |
281 | ADM1275_PEAK_IOUT); | |
c576e30c GR |
282 | break; |
283 | case PMBUS_VIRT_READ_VOUT_MAX: | |
43f33b6e GR |
284 | ret = pmbus_read_word_data(client, 0, 0xff, |
285 | ADM1275_PEAK_VOUT); | |
c576e30c GR |
286 | break; |
287 | case PMBUS_VIRT_READ_VIN_MAX: | |
43f33b6e GR |
288 | ret = pmbus_read_word_data(client, 0, 0xff, |
289 | ADM1275_PEAK_VIN); | |
c576e30c | 290 | break; |
68a40382 GR |
291 | case PMBUS_VIRT_READ_PIN_MIN: |
292 | if (!data->have_pin_min) | |
293 | return -ENXIO; | |
43f33b6e GR |
294 | ret = pmbus_read_word_data(client, 0, 0xff, |
295 | ADM1293_PIN_MIN); | |
68a40382 | 296 | break; |
5cf231a3 | 297 | case PMBUS_VIRT_READ_PIN_MAX: |
9048539b GR |
298 | if (!data->have_pin_max) |
299 | return -ENXIO; | |
43f33b6e GR |
300 | ret = pmbus_read_word_data(client, 0, 0xff, |
301 | ADM1276_PEAK_PIN); | |
5cf231a3 | 302 | break; |
709066ac GR |
303 | case PMBUS_VIRT_READ_TEMP_MAX: |
304 | if (!data->have_temp_max) | |
305 | return -ENXIO; | |
43f33b6e GR |
306 | ret = pmbus_read_word_data(client, 0, 0xff, |
307 | ADM1278_PEAK_TEMP); | |
709066ac | 308 | break; |
c576e30c GR |
309 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
310 | case PMBUS_VIRT_RESET_VOUT_HISTORY: | |
311 | case PMBUS_VIRT_RESET_VIN_HISTORY: | |
5cf231a3 GR |
312 | break; |
313 | case PMBUS_VIRT_RESET_PIN_HISTORY: | |
9048539b GR |
314 | if (!data->have_pin_max) |
315 | return -ENXIO; | |
c576e30c | 316 | break; |
709066ac GR |
317 | case PMBUS_VIRT_RESET_TEMP_HISTORY: |
318 | if (!data->have_temp_max) | |
319 | return -ENXIO; | |
320 | break; | |
c83529c1 | 321 | case PMBUS_VIRT_POWER_SAMPLES: |
7d45deb3 GR |
322 | if (!data->have_power_sampling) |
323 | return -ENXIO; | |
324 | ret = adm1275_read_pmon_config(data, client, true); | |
c83529c1 AKNPW |
325 | if (ret < 0) |
326 | break; | |
327 | ret = BIT(ret); | |
328 | break; | |
329 | case PMBUS_VIRT_IN_SAMPLES: | |
330 | case PMBUS_VIRT_CURR_SAMPLES: | |
7d45deb3 | 331 | ret = adm1275_read_pmon_config(data, client, false); |
c83529c1 AKNPW |
332 | if (ret < 0) |
333 | break; | |
334 | ret = BIT(ret); | |
335 | break; | |
c576e30c GR |
336 | default: |
337 | ret = -ENODATA; | |
338 | break; | |
339 | } | |
340 | return ret; | |
341 | } | |
342 | ||
343 | static int adm1275_write_word_data(struct i2c_client *client, int page, int reg, | |
344 | u16 word) | |
345 | { | |
68a40382 GR |
346 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
347 | const struct adm1275_data *data = to_adm1275_data(info); | |
c576e30c GR |
348 | int ret; |
349 | ||
ecb29abd | 350 | if (page > 0) |
c5e67636 | 351 | return -ENXIO; |
c576e30c GR |
352 | |
353 | switch (reg) { | |
c5e67636 GR |
354 | case PMBUS_IOUT_UC_FAULT_LIMIT: |
355 | case PMBUS_IOUT_OC_FAULT_LIMIT: | |
356 | ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT, | |
357 | word); | |
358 | break; | |
c576e30c GR |
359 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
360 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0); | |
68a40382 GR |
361 | if (!ret && data->have_iout_min) |
362 | ret = pmbus_write_word_data(client, 0, | |
363 | ADM1293_IOUT_MIN, 0); | |
c576e30c GR |
364 | break; |
365 | case PMBUS_VIRT_RESET_VOUT_HISTORY: | |
366 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0); | |
367 | break; | |
368 | case PMBUS_VIRT_RESET_VIN_HISTORY: | |
369 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0); | |
370 | break; | |
5cf231a3 GR |
371 | case PMBUS_VIRT_RESET_PIN_HISTORY: |
372 | ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0); | |
68a40382 GR |
373 | if (!ret && data->have_pin_min) |
374 | ret = pmbus_write_word_data(client, 0, | |
375 | ADM1293_PIN_MIN, 0); | |
5cf231a3 | 376 | break; |
709066ac GR |
377 | case PMBUS_VIRT_RESET_TEMP_HISTORY: |
378 | ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0); | |
379 | break; | |
c83529c1 | 380 | case PMBUS_VIRT_POWER_SAMPLES: |
7d45deb3 GR |
381 | if (!data->have_power_sampling) |
382 | return -ENXIO; | |
c83529c1 | 383 | word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX); |
7d45deb3 | 384 | ret = adm1275_write_pmon_config(data, client, true, |
c83529c1 AKNPW |
385 | ilog2(word)); |
386 | break; | |
387 | case PMBUS_VIRT_IN_SAMPLES: | |
388 | case PMBUS_VIRT_CURR_SAMPLES: | |
389 | word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX); | |
7d45deb3 | 390 | ret = adm1275_write_pmon_config(data, client, false, |
c83529c1 AKNPW |
391 | ilog2(word)); |
392 | break; | |
c576e30c GR |
393 | default: |
394 | ret = -ENODATA; | |
395 | break; | |
396 | } | |
397 | return ret; | |
398 | } | |
399 | ||
c5e67636 GR |
400 | static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg) |
401 | { | |
402 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); | |
403 | const struct adm1275_data *data = to_adm1275_data(info); | |
404 | int mfr_status, ret; | |
405 | ||
da8e48ab | 406 | if (page > 0) |
c5e67636 GR |
407 | return -ENXIO; |
408 | ||
409 | switch (reg) { | |
410 | case PMBUS_STATUS_IOUT: | |
411 | ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT); | |
412 | if (ret < 0) | |
413 | break; | |
9048539b GR |
414 | if (!data->have_oc_fault && !data->have_uc_fault) |
415 | break; | |
c5e67636 GR |
416 | mfr_status = pmbus_read_byte_data(client, page, |
417 | PMBUS_STATUS_MFR_SPECIFIC); | |
9048539b GR |
418 | if (mfr_status < 0) |
419 | return mfr_status; | |
c5e67636 GR |
420 | if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) { |
421 | ret |= data->have_oc_fault ? | |
422 | PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT; | |
423 | } | |
424 | break; | |
92711269 | 425 | case PMBUS_STATUS_VOUT: |
9048539b GR |
426 | if (data->have_vout) |
427 | return -ENODATA; | |
92711269 | 428 | ret = 0; |
9048539b GR |
429 | if (data->have_vaux_status) { |
430 | mfr_status = pmbus_read_byte_data(client, 0, | |
431 | ADM1075_VAUX_STATUS); | |
432 | if (mfr_status < 0) | |
433 | return mfr_status; | |
434 | if (mfr_status & ADM1075_VAUX_OV_WARN) | |
435 | ret |= PB_VOLTAGE_OV_WARNING; | |
436 | if (mfr_status & ADM1075_VAUX_UV_WARN) | |
437 | ret |= PB_VOLTAGE_UV_WARNING; | |
68a40382 GR |
438 | } else if (data->have_mfr_vaux_status) { |
439 | mfr_status = pmbus_read_byte_data(client, page, | |
440 | PMBUS_STATUS_MFR_SPECIFIC); | |
441 | if (mfr_status < 0) | |
442 | return mfr_status; | |
443 | if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN) | |
444 | ret |= PB_VOLTAGE_OV_WARNING; | |
445 | if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN) | |
446 | ret |= PB_VOLTAGE_UV_WARNING; | |
9048539b | 447 | } |
92711269 | 448 | break; |
c5e67636 GR |
449 | default: |
450 | ret = -ENODATA; | |
451 | break; | |
452 | } | |
453 | return ret; | |
454 | } | |
455 | ||
87102808 | 456 | static const struct i2c_device_id adm1275_id[] = { |
92711269 | 457 | { "adm1075", adm1075 }, |
4ff0ce22 | 458 | { "adm1272", adm1272 }, |
87102808 GR |
459 | { "adm1275", adm1275 }, |
460 | { "adm1276", adm1276 }, | |
709066ac | 461 | { "adm1278", adm1278 }, |
68a40382 GR |
462 | { "adm1293", adm1293 }, |
463 | { "adm1294", adm1294 }, | |
87102808 GR |
464 | { } |
465 | }; | |
466 | MODULE_DEVICE_TABLE(i2c, adm1275_id); | |
467 | ||
b153a0bb GR |
468 | /* Enable VOUT & TEMP1 if not enabled (disabled by default) */ |
469 | static int adm1275_enable_vout_temp(struct i2c_client *client, int config) | |
470 | { | |
471 | int ret; | |
472 | ||
473 | if ((config & ADM1278_PMON_DEFCONFIG) != ADM1278_PMON_DEFCONFIG) { | |
474 | config |= ADM1278_PMON_DEFCONFIG; | |
475 | ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG, config); | |
476 | if (ret < 0) { | |
477 | dev_err(&client->dev, "Failed to enable VOUT/TEMP1 monitoring\n"); | |
478 | return ret; | |
479 | } | |
480 | } | |
481 | return 0; | |
482 | } | |
483 | ||
dd431939 | 484 | static int adm1275_probe(struct i2c_client *client) |
83f7649c | 485 | { |
6d1d41c0 | 486 | s32 (*config_read_fn)(const struct i2c_client *client, u8 reg); |
87102808 | 487 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; |
c5e67636 | 488 | int config, device_config; |
3b33ca41 | 489 | int ret; |
83f7649c | 490 | struct pmbus_driver_info *info; |
c5e67636 | 491 | struct adm1275_data *data; |
87102808 | 492 | const struct i2c_device_id *mid; |
904b296f | 493 | const struct coefficients *coefficients; |
68a40382 | 494 | int vindex = -1, voindex = -1, cindex = -1, pindex = -1; |
709066ac | 495 | int tindex = -1; |
6e5c06ad | 496 | u32 shunt; |
a3cd66d7 | 497 | u32 avg; |
83f7649c GR |
498 | |
499 | if (!i2c_check_functionality(client->adapter, | |
87102808 GR |
500 | I2C_FUNC_SMBUS_READ_BYTE_DATA |
501 | | I2C_FUNC_SMBUS_BLOCK_DATA)) | |
83f7649c GR |
502 | return -ENODEV; |
503 | ||
87102808 GR |
504 | ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer); |
505 | if (ret < 0) { | |
506 | dev_err(&client->dev, "Failed to read Manufacturer ID\n"); | |
507 | return ret; | |
508 | } | |
509 | if (ret != 3 || strncmp(block_buffer, "ADI", 3)) { | |
510 | dev_err(&client->dev, "Unsupported Manufacturer ID\n"); | |
511 | return -ENODEV; | |
512 | } | |
83f7649c | 513 | |
87102808 GR |
514 | ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer); |
515 | if (ret < 0) { | |
516 | dev_err(&client->dev, "Failed to read Manufacturer Model\n"); | |
517 | return ret; | |
518 | } | |
519 | for (mid = adm1275_id; mid->name[0]; mid++) { | |
520 | if (!strncasecmp(mid->name, block_buffer, strlen(mid->name))) | |
521 | break; | |
522 | } | |
523 | if (!mid->name[0]) { | |
524 | dev_err(&client->dev, "Unsupported device\n"); | |
525 | return -ENODEV; | |
3b33ca41 | 526 | } |
83f7649c | 527 | |
dd431939 | 528 | if (strcmp(client->name, mid->name) != 0) |
87102808 GR |
529 | dev_notice(&client->dev, |
530 | "Device mismatch: Configured %s, detected %s\n", | |
dd431939 | 531 | client->name, mid->name); |
87102808 | 532 | |
6d1d41c0 CL |
533 | if (mid->driver_data == adm1272 || mid->driver_data == adm1278 || |
534 | mid->driver_data == adm1293 || mid->driver_data == adm1294) | |
535 | config_read_fn = i2c_smbus_read_word_data; | |
536 | else | |
537 | config_read_fn = i2c_smbus_read_byte_data; | |
538 | config = config_read_fn(client, ADM1275_PMON_CONFIG); | |
87102808 GR |
539 | if (config < 0) |
540 | return config; | |
541 | ||
6d1d41c0 | 542 | device_config = config_read_fn(client, ADM1275_DEVICE_CONFIG); |
87102808 GR |
543 | if (device_config < 0) |
544 | return device_config; | |
545 | ||
8b313ca7 GR |
546 | data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data), |
547 | GFP_KERNEL); | |
87102808 GR |
548 | if (!data) |
549 | return -ENOMEM; | |
550 | ||
6e5c06ad KY |
551 | if (of_property_read_u32(client->dev.of_node, |
552 | "shunt-resistor-micro-ohms", &shunt)) | |
553 | shunt = 1000; /* 1 mOhm if not set via DT */ | |
554 | ||
555 | if (shunt == 0) | |
556 | return -EINVAL; | |
557 | ||
87102808 | 558 | data->id = mid->driver_data; |
c5e67636 GR |
559 | |
560 | info = &data->info; | |
561 | ||
83f7649c | 562 | info->pages = 1; |
1061d851 GR |
563 | info->format[PSC_VOLTAGE_IN] = direct; |
564 | info->format[PSC_VOLTAGE_OUT] = direct; | |
565 | info->format[PSC_CURRENT_OUT] = direct; | |
904b296f | 566 | info->format[PSC_POWER] = direct; |
709066ac | 567 | info->format[PSC_TEMPERATURE] = direct; |
c83529c1 AKNPW |
568 | info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | |
569 | PMBUS_HAVE_SAMPLES; | |
83f7649c | 570 | |
c576e30c | 571 | info->read_word_data = adm1275_read_word_data; |
c5e67636 | 572 | info->read_byte_data = adm1275_read_byte_data; |
c576e30c GR |
573 | info->write_word_data = adm1275_write_word_data; |
574 | ||
87102808 | 575 | switch (data->id) { |
92711269 | 576 | case adm1075: |
9048539b GR |
577 | if (device_config & ADM1275_IOUT_WARN2_SELECT) |
578 | data->have_oc_fault = true; | |
579 | else | |
580 | data->have_uc_fault = true; | |
581 | data->have_pin_max = true; | |
582 | data->have_vaux_status = true; | |
583 | ||
904b296f GR |
584 | coefficients = adm1075_coefficients; |
585 | vindex = 0; | |
92711269 GR |
586 | switch (config & ADM1075_IRANGE_MASK) { |
587 | case ADM1075_IRANGE_25: | |
904b296f GR |
588 | cindex = 1; |
589 | pindex = 3; | |
92711269 GR |
590 | break; |
591 | case ADM1075_IRANGE_50: | |
904b296f GR |
592 | cindex = 2; |
593 | pindex = 4; | |
92711269 GR |
594 | break; |
595 | default: | |
596 | dev_err(&client->dev, "Invalid input current range"); | |
92711269 GR |
597 | break; |
598 | } | |
904b296f | 599 | |
92711269 GR |
600 | info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN |
601 | | PMBUS_HAVE_STATUS_INPUT; | |
602 | if (config & ADM1275_VIN_VOUT_SELECT) | |
603 | info->func[0] |= | |
604 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; | |
605 | break; | |
4ff0ce22 GR |
606 | case adm1272: |
607 | data->have_vout = true; | |
608 | data->have_pin_max = true; | |
609 | data->have_temp_max = true; | |
7d45deb3 | 610 | data->have_power_sampling = true; |
4ff0ce22 GR |
611 | |
612 | coefficients = adm1272_coefficients; | |
613 | vindex = (config & ADM1275_VRANGE) ? 1 : 0; | |
614 | cindex = (config & ADM1272_IRANGE) ? 3 : 2; | |
615 | /* pindex depends on the combination of the above */ | |
616 | switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) { | |
617 | case 0: | |
618 | default: | |
619 | pindex = 4; | |
620 | break; | |
621 | case ADM1275_VRANGE: | |
622 | pindex = 5; | |
623 | break; | |
624 | case ADM1272_IRANGE: | |
625 | pindex = 6; | |
626 | break; | |
627 | case ADM1275_VRANGE | ADM1272_IRANGE: | |
628 | pindex = 7; | |
629 | break; | |
630 | } | |
631 | tindex = 8; | |
632 | ||
633 | info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | | |
9da9c2dc CL |
634 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | |
635 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; | |
4ff0ce22 | 636 | |
b153a0bb GR |
637 | ret = adm1275_enable_vout_temp(client, config); |
638 | if (ret) | |
639 | return ret; | |
640 | ||
4ff0ce22 GR |
641 | if (config & ADM1278_VIN_EN) |
642 | info->func[0] |= PMBUS_HAVE_VIN; | |
643 | break; | |
5cf231a3 | 644 | case adm1275: |
9048539b GR |
645 | if (device_config & ADM1275_IOUT_WARN2_SELECT) |
646 | data->have_oc_fault = true; | |
647 | else | |
648 | data->have_uc_fault = true; | |
649 | data->have_vout = true; | |
650 | ||
904b296f GR |
651 | coefficients = adm1275_coefficients; |
652 | vindex = (config & ADM1275_VRANGE) ? 0 : 1; | |
653 | cindex = 2; | |
654 | ||
5cf231a3 GR |
655 | if (config & ADM1275_VIN_VOUT_SELECT) |
656 | info->func[0] |= | |
657 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; | |
658 | else | |
659 | info->func[0] |= | |
660 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT; | |
661 | break; | |
662 | case adm1276: | |
9048539b GR |
663 | if (device_config & ADM1275_IOUT_WARN2_SELECT) |
664 | data->have_oc_fault = true; | |
665 | else | |
666 | data->have_uc_fault = true; | |
667 | data->have_vout = true; | |
668 | data->have_pin_max = true; | |
669 | ||
904b296f GR |
670 | coefficients = adm1276_coefficients; |
671 | vindex = (config & ADM1275_VRANGE) ? 0 : 1; | |
672 | cindex = 2; | |
673 | pindex = (config & ADM1275_VRANGE) ? 3 : 4; | |
674 | ||
5cf231a3 GR |
675 | info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN |
676 | | PMBUS_HAVE_STATUS_INPUT; | |
677 | if (config & ADM1275_VIN_VOUT_SELECT) | |
678 | info->func[0] |= | |
679 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; | |
5cf231a3 | 680 | break; |
709066ac GR |
681 | case adm1278: |
682 | data->have_vout = true; | |
683 | data->have_pin_max = true; | |
684 | data->have_temp_max = true; | |
7d45deb3 | 685 | data->have_power_sampling = true; |
709066ac GR |
686 | |
687 | coefficients = adm1278_coefficients; | |
688 | vindex = 0; | |
689 | cindex = 1; | |
690 | pindex = 2; | |
691 | tindex = 3; | |
692 | ||
2b3d0c19 | 693 | info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | |
a37881de ME |
694 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | |
695 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; | |
2b3d0c19 | 696 | |
b153a0bb GR |
697 | ret = adm1275_enable_vout_temp(client, config); |
698 | if (ret) | |
699 | return ret; | |
2b3d0c19 | 700 | |
709066ac GR |
701 | if (config & ADM1278_VIN_EN) |
702 | info->func[0] |= PMBUS_HAVE_VIN; | |
709066ac | 703 | break; |
68a40382 GR |
704 | case adm1293: |
705 | case adm1294: | |
706 | data->have_iout_min = true; | |
707 | data->have_pin_min = true; | |
708 | data->have_pin_max = true; | |
709 | data->have_mfr_vaux_status = true; | |
7d45deb3 | 710 | data->have_power_sampling = true; |
68a40382 GR |
711 | |
712 | coefficients = adm1293_coefficients; | |
713 | ||
714 | voindex = 0; | |
715 | switch (config & ADM1293_VIN_SEL_MASK) { | |
716 | case ADM1293_VIN_SEL_012: /* 1.2V */ | |
717 | vindex = 0; | |
718 | break; | |
719 | case ADM1293_VIN_SEL_074: /* 7.4V */ | |
720 | vindex = 1; | |
721 | break; | |
722 | case ADM1293_VIN_SEL_210: /* 21V */ | |
723 | vindex = 2; | |
724 | break; | |
725 | default: /* disabled */ | |
726 | break; | |
727 | } | |
728 | ||
729 | switch (config & ADM1293_IRANGE_MASK) { | |
730 | case ADM1293_IRANGE_25: | |
731 | cindex = 3; | |
732 | break; | |
733 | case ADM1293_IRANGE_50: | |
734 | cindex = 4; | |
735 | break; | |
736 | case ADM1293_IRANGE_100: | |
737 | cindex = 5; | |
738 | break; | |
739 | case ADM1293_IRANGE_200: | |
740 | cindex = 6; | |
741 | break; | |
742 | } | |
743 | ||
744 | if (vindex >= 0) | |
745 | pindex = 7 + vindex * 4 + (cindex - 3); | |
746 | ||
747 | if (config & ADM1293_VAUX_EN) | |
748 | info->func[0] |= | |
749 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; | |
750 | ||
751 | info->func[0] |= PMBUS_HAVE_PIN | | |
752 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT; | |
753 | ||
754 | break; | |
904b296f GR |
755 | default: |
756 | dev_err(&client->dev, "Unsupported device\n"); | |
757 | return -ENODEV; | |
758 | } | |
68a40382 | 759 | |
a3cd66d7 PL |
760 | if (data->have_power_sampling && |
761 | of_property_read_u32(client->dev.of_node, | |
762 | "adi,power-sample-average", &avg) == 0) { | |
763 | if (!avg || avg > ADM1275_SAMPLES_AVG_MAX || | |
764 | BIT(__fls(avg)) != avg) { | |
765 | dev_err(&client->dev, | |
766 | "Invalid number of power samples"); | |
767 | return -EINVAL; | |
768 | } | |
769 | ret = adm1275_write_pmon_config(data, client, true, | |
770 | ilog2(avg)); | |
771 | if (ret < 0) { | |
772 | dev_err(&client->dev, | |
773 | "Setting power sample averaging failed with error %d", | |
774 | ret); | |
775 | return ret; | |
776 | } | |
777 | } | |
778 | ||
779 | if (of_property_read_u32(client->dev.of_node, | |
780 | "adi,volt-curr-sample-average", &avg) == 0) { | |
781 | if (!avg || avg > ADM1275_SAMPLES_AVG_MAX || | |
782 | BIT(__fls(avg)) != avg) { | |
783 | dev_err(&client->dev, | |
784 | "Invalid number of voltage/current samples"); | |
785 | return -EINVAL; | |
786 | } | |
787 | ret = adm1275_write_pmon_config(data, client, false, | |
788 | ilog2(avg)); | |
789 | if (ret < 0) { | |
790 | dev_err(&client->dev, | |
791 | "Setting voltage and current sample averaging failed with error %d", | |
792 | ret); | |
793 | return ret; | |
794 | } | |
795 | } | |
796 | ||
68a40382 GR |
797 | if (voindex < 0) |
798 | voindex = vindex; | |
904b296f GR |
799 | if (vindex >= 0) { |
800 | info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m; | |
801 | info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b; | |
802 | info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R; | |
68a40382 GR |
803 | } |
804 | if (voindex >= 0) { | |
805 | info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m; | |
806 | info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b; | |
807 | info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R; | |
904b296f GR |
808 | } |
809 | if (cindex >= 0) { | |
6e5c06ad KY |
810 | /* Scale current with sense resistor value */ |
811 | info->m[PSC_CURRENT_OUT] = | |
812 | coefficients[cindex].m * shunt / 1000; | |
904b296f GR |
813 | info->b[PSC_CURRENT_OUT] = coefficients[cindex].b; |
814 | info->R[PSC_CURRENT_OUT] = coefficients[cindex].R; | |
815 | } | |
816 | if (pindex >= 0) { | |
6e5c06ad KY |
817 | info->m[PSC_POWER] = |
818 | coefficients[pindex].m * shunt / 1000; | |
904b296f GR |
819 | info->b[PSC_POWER] = coefficients[pindex].b; |
820 | info->R[PSC_POWER] = coefficients[pindex].R; | |
5cf231a3 | 821 | } |
709066ac GR |
822 | if (tindex >= 0) { |
823 | info->m[PSC_TEMPERATURE] = coefficients[tindex].m; | |
824 | info->b[PSC_TEMPERATURE] = coefficients[tindex].b; | |
825 | info->R[PSC_TEMPERATURE] = coefficients[tindex].R; | |
826 | } | |
83f7649c | 827 | |
dd431939 | 828 | return pmbus_do_probe(client, info); |
83f7649c GR |
829 | } |
830 | ||
83f7649c GR |
831 | static struct i2c_driver adm1275_driver = { |
832 | .driver = { | |
833 | .name = "adm1275", | |
834 | }, | |
dd431939 | 835 | .probe_new = adm1275_probe, |
83f7649c GR |
836 | .id_table = adm1275_id, |
837 | }; | |
838 | ||
f0967eea | 839 | module_i2c_driver(adm1275_driver); |
83f7649c GR |
840 | |
841 | MODULE_AUTHOR("Guenter Roeck"); | |
5cf231a3 | 842 | MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles"); |
83f7649c | 843 | MODULE_LICENSE("GPL"); |
b94ca77e | 844 | MODULE_IMPORT_NS(PMBUS); |