forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/drivers/hwmon/pmbus/adm1275.c
....@@ -1,19 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
34 * and Digital Power Monitor
45 *
56 * Copyright (c) 2011 Ericsson AB.
67 * Copyright (c) 2018 Guenter Roeck
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version.
12
- *
13
- * This program is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- * GNU General Public License for more details.
178 */
189
1910 #include <linux/kernel.h>
....@@ -23,6 +14,8 @@
2314 #include <linux/slab.h>
2415 #include <linux/i2c.h>
2516 #include <linux/bitops.h>
17
+#include <linux/bitfield.h>
18
+#include <linux/log2.h>
2619 #include "pmbus.h"
2720
2821 enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
....@@ -78,6 +71,18 @@
7871 #define ADM1075_VAUX_OV_WARN BIT(7)
7972 #define ADM1075_VAUX_UV_WARN BIT(6)
8073
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)
85
+
8186 struct adm1275_data {
8287 int id;
8388 bool have_oc_fault;
....@@ -89,6 +94,7 @@
8994 bool have_pin_min;
9095 bool have_pin_max;
9196 bool have_temp_max;
97
+ bool have_power_sampling;
9298 struct pmbus_driver_info info;
9399 };
94100
....@@ -164,7 +170,64 @@
164170 [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */
165171 };
166172
167
-static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
173
+static int adm1275_read_pmon_config(const struct adm1275_data *data,
174
+ struct i2c_client *client, bool is_power)
175
+{
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
+ }
193
+ if (ret < 0)
194
+ return ret;
195
+
196
+ return (ret & mask) >> shift;
197
+}
198
+
199
+static int adm1275_write_pmon_config(const struct adm1275_data *data,
200
+ struct i2c_client *client,
201
+ bool is_power, u16 word)
202
+{
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
+ }
215
+ if (ret < 0)
216
+ return ret;
217
+
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);
225
+
226
+ return ret;
227
+}
228
+
229
+static int adm1275_read_word_data(struct i2c_client *client, int page,
230
+ int phase, int reg)
168231 {
169232 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
170233 const struct adm1275_data *data = to_adm1275_data(info);
....@@ -177,58 +240,68 @@
177240 case PMBUS_IOUT_UC_FAULT_LIMIT:
178241 if (!data->have_uc_fault)
179242 return -ENXIO;
180
- ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
243
+ ret = pmbus_read_word_data(client, 0, 0xff,
244
+ ADM1275_IOUT_WARN2_LIMIT);
181245 break;
182246 case PMBUS_IOUT_OC_FAULT_LIMIT:
183247 if (!data->have_oc_fault)
184248 return -ENXIO;
185
- ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
249
+ ret = pmbus_read_word_data(client, 0, 0xff,
250
+ ADM1275_IOUT_WARN2_LIMIT);
186251 break;
187252 case PMBUS_VOUT_OV_WARN_LIMIT:
188253 if (data->have_vout)
189254 return -ENODATA;
190
- ret = pmbus_read_word_data(client, 0,
255
+ ret = pmbus_read_word_data(client, 0, 0xff,
191256 ADM1075_VAUX_OV_WARN_LIMIT);
192257 break;
193258 case PMBUS_VOUT_UV_WARN_LIMIT:
194259 if (data->have_vout)
195260 return -ENODATA;
196
- ret = pmbus_read_word_data(client, 0,
261
+ ret = pmbus_read_word_data(client, 0, 0xff,
197262 ADM1075_VAUX_UV_WARN_LIMIT);
198263 break;
199264 case PMBUS_READ_VOUT:
200265 if (data->have_vout)
201266 return -ENODATA;
202
- ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
267
+ ret = pmbus_read_word_data(client, 0, 0xff,
268
+ ADM1075_READ_VAUX);
203269 break;
204270 case PMBUS_VIRT_READ_IOUT_MIN:
205271 if (!data->have_iout_min)
206272 return -ENXIO;
207
- ret = pmbus_read_word_data(client, 0, ADM1293_IOUT_MIN);
273
+ ret = pmbus_read_word_data(client, 0, 0xff,
274
+ ADM1293_IOUT_MIN);
208275 break;
209276 case PMBUS_VIRT_READ_IOUT_MAX:
210
- ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
277
+ ret = pmbus_read_word_data(client, 0, 0xff,
278
+ ADM1275_PEAK_IOUT);
211279 break;
212280 case PMBUS_VIRT_READ_VOUT_MAX:
213
- ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
281
+ ret = pmbus_read_word_data(client, 0, 0xff,
282
+ ADM1275_PEAK_VOUT);
214283 break;
215284 case PMBUS_VIRT_READ_VIN_MAX:
216
- ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
285
+ ret = pmbus_read_word_data(client, 0, 0xff,
286
+ ADM1275_PEAK_VIN);
217287 break;
218288 case PMBUS_VIRT_READ_PIN_MIN:
219289 if (!data->have_pin_min)
220290 return -ENXIO;
221
- ret = pmbus_read_word_data(client, 0, ADM1293_PIN_MIN);
291
+ ret = pmbus_read_word_data(client, 0, 0xff,
292
+ ADM1293_PIN_MIN);
222293 break;
223294 case PMBUS_VIRT_READ_PIN_MAX:
224295 if (!data->have_pin_max)
225296 return -ENXIO;
226
- ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
297
+ ret = pmbus_read_word_data(client, 0, 0xff,
298
+ ADM1276_PEAK_PIN);
227299 break;
228300 case PMBUS_VIRT_READ_TEMP_MAX:
229301 if (!data->have_temp_max)
230302 return -ENXIO;
231
- ret = pmbus_read_word_data(client, 0, ADM1278_PEAK_TEMP);
303
+ ret = pmbus_read_word_data(client, 0, 0xff,
304
+ ADM1278_PEAK_TEMP);
232305 break;
233306 case PMBUS_VIRT_RESET_IOUT_HISTORY:
234307 case PMBUS_VIRT_RESET_VOUT_HISTORY:
....@@ -241,6 +314,21 @@
241314 case PMBUS_VIRT_RESET_TEMP_HISTORY:
242315 if (!data->have_temp_max)
243316 return -ENXIO;
317
+ break;
318
+ case PMBUS_VIRT_POWER_SAMPLES:
319
+ if (!data->have_power_sampling)
320
+ return -ENXIO;
321
+ ret = adm1275_read_pmon_config(data, client, true);
322
+ if (ret < 0)
323
+ break;
324
+ ret = BIT(ret);
325
+ break;
326
+ case PMBUS_VIRT_IN_SAMPLES:
327
+ case PMBUS_VIRT_CURR_SAMPLES:
328
+ ret = adm1275_read_pmon_config(data, client, false);
329
+ if (ret < 0)
330
+ break;
331
+ ret = BIT(ret);
244332 break;
245333 default:
246334 ret = -ENODATA;
....@@ -285,6 +373,19 @@
285373 break;
286374 case PMBUS_VIRT_RESET_TEMP_HISTORY:
287375 ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
376
+ break;
377
+ case PMBUS_VIRT_POWER_SAMPLES:
378
+ if (!data->have_power_sampling)
379
+ return -ENXIO;
380
+ word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
381
+ ret = adm1275_write_pmon_config(data, client, true,
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);
387
+ ret = adm1275_write_pmon_config(data, client, false,
388
+ ilog2(word));
288389 break;
289390 default:
290391 ret = -ENODATA;
....@@ -361,8 +462,7 @@
361462 };
362463 MODULE_DEVICE_TABLE(i2c, adm1275_id);
363464
364
-static int adm1275_probe(struct i2c_client *client,
365
- const struct i2c_device_id *id)
465
+static int adm1275_probe(struct i2c_client *client)
366466 {
367467 s32 (*config_read_fn)(const struct i2c_client *client, u8 reg);
368468 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
....@@ -374,6 +474,7 @@
374474 const struct coefficients *coefficients;
375475 int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
376476 int tindex = -1;
477
+ u32 shunt;
377478
378479 if (!i2c_check_functionality(client->adapter,
379480 I2C_FUNC_SMBUS_READ_BYTE_DATA
....@@ -404,10 +505,10 @@
404505 return -ENODEV;
405506 }
406507
407
- if (id->driver_data != mid->driver_data)
508
+ if (strcmp(client->name, mid->name) != 0)
408509 dev_notice(&client->dev,
409510 "Device mismatch: Configured %s, detected %s\n",
410
- id->name, mid->name);
511
+ client->name, mid->name);
411512
412513 if (mid->driver_data == adm1272 || mid->driver_data == adm1278 ||
413514 mid->driver_data == adm1293 || mid->driver_data == adm1294)
....@@ -427,6 +528,13 @@
427528 if (!data)
428529 return -ENOMEM;
429530
531
+ if (of_property_read_u32(client->dev.of_node,
532
+ "shunt-resistor-micro-ohms", &shunt))
533
+ shunt = 1000; /* 1 mOhm if not set via DT */
534
+
535
+ if (shunt == 0)
536
+ return -EINVAL;
537
+
430538 data->id = mid->driver_data;
431539
432540 info = &data->info;
....@@ -437,7 +545,8 @@
437545 info->format[PSC_CURRENT_OUT] = direct;
438546 info->format[PSC_POWER] = direct;
439547 info->format[PSC_TEMPERATURE] = direct;
440
- info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
548
+ info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
549
+ PMBUS_HAVE_SAMPLES;
441550
442551 info->read_word_data = adm1275_read_word_data;
443552 info->read_byte_data = adm1275_read_byte_data;
....@@ -478,6 +587,7 @@
478587 data->have_vout = true;
479588 data->have_pin_max = true;
480589 data->have_temp_max = true;
590
+ data->have_power_sampling = true;
481591
482592 coefficients = adm1272_coefficients;
483593 vindex = (config & ADM1275_VRANGE) ? 1 : 0;
....@@ -563,6 +673,7 @@
563673 data->have_vout = true;
564674 data->have_pin_max = true;
565675 data->have_temp_max = true;
676
+ data->have_power_sampling = true;
566677
567678 coefficients = adm1278_coefficients;
568679 vindex = 0;
....@@ -571,11 +682,13 @@
571682 tindex = 3;
572683
573684 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
574
- PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
685
+ PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
686
+ PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
575687
576
- /* Enable VOUT if not enabled (it is disabled by default) */
577
- if (!(config & ADM1278_VOUT_EN)) {
578
- config |= ADM1278_VOUT_EN;
688
+ /* Enable VOUT & TEMP1 if not enabled (disabled by default) */
689
+ if ((config & (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) !=
690
+ (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) {
691
+ config |= ADM1278_VOUT_EN | ADM1278_TEMP1_EN;
579692 ret = i2c_smbus_write_byte_data(client,
580693 ADM1275_PMON_CONFIG,
581694 config);
....@@ -586,9 +699,6 @@
586699 }
587700 }
588701
589
- if (config & ADM1278_TEMP1_EN)
590
- info->func[0] |=
591
- PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
592702 if (config & ADM1278_VIN_EN)
593703 info->func[0] |= PMBUS_HAVE_VIN;
594704 break;
....@@ -598,6 +708,7 @@
598708 data->have_pin_min = true;
599709 data->have_pin_max = true;
600710 data->have_mfr_vaux_status = true;
711
+ data->have_power_sampling = true;
601712
602713 coefficients = adm1293_coefficients;
603714
....@@ -660,12 +771,15 @@
660771 info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
661772 }
662773 if (cindex >= 0) {
663
- info->m[PSC_CURRENT_OUT] = coefficients[cindex].m;
774
+ /* Scale current with sense resistor value */
775
+ info->m[PSC_CURRENT_OUT] =
776
+ coefficients[cindex].m * shunt / 1000;
664777 info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
665778 info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
666779 }
667780 if (pindex >= 0) {
668
- info->m[PSC_POWER] = coefficients[pindex].m;
781
+ info->m[PSC_POWER] =
782
+ coefficients[pindex].m * shunt / 1000;
669783 info->b[PSC_POWER] = coefficients[pindex].b;
670784 info->R[PSC_POWER] = coefficients[pindex].R;
671785 }
....@@ -675,14 +789,14 @@
675789 info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
676790 }
677791
678
- return pmbus_do_probe(client, id, info);
792
+ return pmbus_do_probe(client, info);
679793 }
680794
681795 static struct i2c_driver adm1275_driver = {
682796 .driver = {
683797 .name = "adm1275",
684798 },
685
- .probe = adm1275_probe,
799
+ .probe_new = adm1275_probe,
686800 .remove = pmbus_do_remove,
687801 .id_table = adm1275_id,
688802 };