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