1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Texas Instruments TMP512, TMP513 power monitor chips
6 * Thermal/Power Management with Triple Remote and
7 * Local Temperature Sensor and Current Shunt Monitor
8 * Datasheet: https://www.ti.com/lit/gpn/tmp513
11 * Thermal/Power Management with Dual Remote
12 * and Local Temperature Sensor and Current Shunt Monitor
13 * Datasheet: https://www.ti.com/lit/gpn/tmp512
15 * Copyright (C) 2019 Eric Tremblay <etremblay@distech-controls.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; version 2 of the License.
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/device.h>
25 #include <linux/err.h>
26 #include <linux/hwmon.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/math.h>
30 #include <linux/module.h>
31 #include <linux/property.h>
32 #include <linux/regmap.h>
33 #include <linux/slab.h>
34 #include <linux/types.h>
35 #include <linux/units.h>
37 // Common register definition
38 #define TMP51X_SHUNT_CONFIG 0x00
39 #define TMP51X_TEMP_CONFIG 0x01
40 #define TMP51X_STATUS 0x02
41 #define TMP51X_SMBUS_ALERT 0x03
42 #define TMP51X_SHUNT_CURRENT_RESULT 0x04
43 #define TMP51X_BUS_VOLTAGE_RESULT 0x05
44 #define TMP51X_POWER_RESULT 0x06
45 #define TMP51X_BUS_CURRENT_RESULT 0x07
46 #define TMP51X_LOCAL_TEMP_RESULT 0x08
47 #define TMP51X_REMOTE_TEMP_RESULT_1 0x09
48 #define TMP51X_REMOTE_TEMP_RESULT_2 0x0A
49 #define TMP51X_SHUNT_CURRENT_H_LIMIT 0x0C
50 #define TMP51X_SHUNT_CURRENT_L_LIMIT 0x0D
51 #define TMP51X_BUS_VOLTAGE_H_LIMIT 0x0E
52 #define TMP51X_BUS_VOLTAGE_L_LIMIT 0x0F
53 #define TMP51X_POWER_LIMIT 0x10
54 #define TMP51X_LOCAL_TEMP_LIMIT 0x11
55 #define TMP51X_REMOTE_TEMP_LIMIT_1 0x12
56 #define TMP51X_REMOTE_TEMP_LIMIT_2 0x13
57 #define TMP51X_SHUNT_CALIBRATION 0x15
58 #define TMP51X_N_FACTOR_AND_HYST_1 0x16
59 #define TMP51X_N_FACTOR_2 0x17
60 #define TMP51X_MAN_ID_REG 0xFE
61 #define TMP51X_DEVICE_ID_REG 0xFF
63 // TMP513 specific register definition
64 #define TMP513_REMOTE_TEMP_RESULT_3 0x0B
65 #define TMP513_REMOTE_TEMP_LIMIT_3 0x14
66 #define TMP513_N_FACTOR_3 0x18
68 // Common attrs, and NULL
69 #define TMP51X_MANUFACTURER_ID 0x55FF
71 #define TMP512_DEVICE_ID 0x22FF
72 #define TMP513_DEVICE_ID 0x23FF
75 #define TMP51X_SHUNT_CONFIG_DEFAULT 0x399F
76 #define TMP51X_SHUNT_VALUE_DEFAULT 1000
77 #define TMP51X_VBUS_RANGE_DEFAULT TMP51X_VBUS_RANGE_32V
78 #define TMP51X_PGA_DEFAULT 8
79 #define TMP51X_MAX_REGISTER_ADDR 0xFF
82 #define CURRENT_SENSE_VOLTAGE_320_MASK 0x1800
83 #define CURRENT_SENSE_VOLTAGE_160_MASK 0x1000
84 #define CURRENT_SENSE_VOLTAGE_80_MASK 0x0800
85 #define CURRENT_SENSE_VOLTAGE_40_MASK 0
87 #define TMP51X_BUS_VOLTAGE_MASK 0x2000
88 #define TMP51X_NFACTOR_MASK 0xFF00
89 #define TMP51X_HYST_MASK 0x00FF
91 #define TMP51X_BUS_VOLTAGE_SHIFT 3
92 #define TMP51X_TEMP_SHIFT 3
95 #define TMP51X_SHUNT_CURRENT_H_LIMIT_POS 15
96 #define TMP51X_SHUNT_CURRENT_L_LIMIT_POS 14
97 #define TMP51X_BUS_VOLTAGE_H_LIMIT_POS 13
98 #define TMP51X_BUS_VOLTAGE_L_LIMIT_POS 12
99 #define TMP51X_POWER_LIMIT_POS 11
100 #define TMP51X_LOCAL_TEMP_LIMIT_POS 10
101 #define TMP51X_REMOTE_TEMP_LIMIT_1_POS 9
102 #define TMP51X_REMOTE_TEMP_LIMIT_2_POS 8
103 #define TMP513_REMOTE_TEMP_LIMIT_3_POS 7
105 #define TMP51X_VBUS_RANGE_32V (32 * MICRO)
106 #define TMP51X_VBUS_RANGE_16V (16 * MICRO)
109 #define MAX_BUS_VOLTAGE_32_LIMIT 32764
110 #define MAX_BUS_VOLTAGE_16_LIMIT 16382
112 // Max possible value is -256 to +256 but datasheet indicated -40 to 125.
113 #define MAX_TEMP_LIMIT 125000
114 #define MIN_TEMP_LIMIT -40000
116 #define MAX_TEMP_HYST 127500
118 #define TMP512_MAX_CHANNELS 3
119 #define TMP513_MAX_CHANNELS 4
121 #define TMP51X_TEMP_CONFIG_CONV_RATE GENMASK(9, 7)
122 #define TMP51X_TEMP_CONFIG_RC BIT(10)
123 #define TMP51X_TEMP_CHANNEL_MASK(n) (GENMASK((n) - 1, 0) << 11)
124 #define TMP51X_TEMP_CONFIG_CONT BIT(15)
125 #define TMP51X_TEMP_CONFIG_DEFAULT(n) \
126 (TMP51X_TEMP_CHANNEL_MASK(n) | TMP51X_TEMP_CONFIG_CONT | \
127 TMP51X_TEMP_CONFIG_CONV_RATE | TMP51X_TEMP_CONFIG_RC)
129 static const u8 TMP51X_TEMP_INPUT
[4] = {
130 TMP51X_LOCAL_TEMP_RESULT
,
131 TMP51X_REMOTE_TEMP_RESULT_1
,
132 TMP51X_REMOTE_TEMP_RESULT_2
,
133 TMP513_REMOTE_TEMP_RESULT_3
136 static const u8 TMP51X_TEMP_CRIT
[4] = {
137 TMP51X_LOCAL_TEMP_LIMIT
,
138 TMP51X_REMOTE_TEMP_LIMIT_1
,
139 TMP51X_REMOTE_TEMP_LIMIT_2
,
140 TMP513_REMOTE_TEMP_LIMIT_3
143 static const u8 TMP51X_TEMP_CRIT_ALARM
[4] = {
144 TMP51X_LOCAL_TEMP_LIMIT_POS
,
145 TMP51X_REMOTE_TEMP_LIMIT_1_POS
,
146 TMP51X_REMOTE_TEMP_LIMIT_2_POS
,
147 TMP513_REMOTE_TEMP_LIMIT_3_POS
150 static const u8 TMP51X_TEMP_CRIT_HYST
[4] = {
151 TMP51X_N_FACTOR_AND_HYST_1
,
152 TMP51X_N_FACTOR_AND_HYST_1
,
153 TMP51X_N_FACTOR_AND_HYST_1
,
154 TMP51X_N_FACTOR_AND_HYST_1
157 static const u8 TMP51X_CURR_INPUT
[2] = {
158 TMP51X_SHUNT_CURRENT_RESULT
,
159 TMP51X_BUS_CURRENT_RESULT
162 static const struct regmap_config tmp51x_regmap_config
= {
165 .max_register
= TMP51X_MAX_REGISTER_ADDR
,
171 u32 vbus_range_uvolt
;
182 struct regmap
*regmap
;
185 // Set the shift based on the gain 8=4, 4=3, 2=2, 1=1
186 static inline u8
tmp51x_get_pga_shift(struct tmp51x_data
*data
)
188 return 5 - ffs(data
->pga_gain
);
191 static int tmp51x_get_value(struct tmp51x_data
*data
, u8 reg
, u8 pos
,
192 unsigned int regval
, long *val
)
196 *val
= (regval
>> pos
) & 1;
198 case TMP51X_SHUNT_CURRENT_RESULT
:
199 case TMP51X_SHUNT_CURRENT_H_LIMIT
:
200 case TMP51X_SHUNT_CURRENT_L_LIMIT
:
202 * The valus is read in voltage in the chip but reported as
203 * current to the user.
204 * 2's complement number shifted by one to four depending
205 * on the pga gain setting. 1lsb = 10uV
207 *val
= sign_extend32(regval
, 17 - tmp51x_get_pga_shift(data
));
208 *val
= DIV_ROUND_CLOSEST(*val
* 10 * MILLI
, data
->shunt_uohms
);
210 case TMP51X_BUS_VOLTAGE_RESULT
:
211 case TMP51X_BUS_VOLTAGE_H_LIMIT
:
212 case TMP51X_BUS_VOLTAGE_L_LIMIT
:
214 *val
= (regval
>> TMP51X_BUS_VOLTAGE_SHIFT
) * 4;
216 case TMP51X_POWER_RESULT
:
217 case TMP51X_POWER_LIMIT
:
218 // Power = (current * BusVoltage) / 5000
219 *val
= regval
* data
->pwr_lsb_uw
;
221 case TMP51X_BUS_CURRENT_RESULT
:
222 // Current = (ShuntVoltage * CalibrationRegister) / 4096
223 *val
= sign_extend32(regval
, 16) * data
->curr_lsb_ua
;
224 *val
= DIV_ROUND_CLOSEST(*val
, MILLI
);
226 case TMP51X_LOCAL_TEMP_RESULT
:
227 case TMP51X_REMOTE_TEMP_RESULT_1
:
228 case TMP51X_REMOTE_TEMP_RESULT_2
:
229 case TMP513_REMOTE_TEMP_RESULT_3
:
230 case TMP51X_LOCAL_TEMP_LIMIT
:
231 case TMP51X_REMOTE_TEMP_LIMIT_1
:
232 case TMP51X_REMOTE_TEMP_LIMIT_2
:
233 case TMP513_REMOTE_TEMP_LIMIT_3
:
234 // 1lsb = 0.0625 degrees centigrade
235 *val
= sign_extend32(regval
, 16) >> TMP51X_TEMP_SHIFT
;
236 *val
= DIV_ROUND_CLOSEST(*val
* 625, 10);
238 case TMP51X_N_FACTOR_AND_HYST_1
:
239 // 1lsb = 0.5 degrees centigrade
240 *val
= (regval
& TMP51X_HYST_MASK
) * 500;
252 static int tmp51x_set_value(struct tmp51x_data
*data
, u8 reg
, long val
)
258 case TMP51X_SHUNT_CURRENT_H_LIMIT
:
259 case TMP51X_SHUNT_CURRENT_L_LIMIT
:
261 * The user enter current value and we convert it to
262 * voltage. 1lsb = 10uV
264 val
= DIV_ROUND_CLOSEST(val
* data
->shunt_uohms
, 10 * MILLI
);
265 max_val
= U16_MAX
>> tmp51x_get_pga_shift(data
);
266 regval
= clamp_val(val
, -max_val
, max_val
);
268 case TMP51X_BUS_VOLTAGE_H_LIMIT
:
269 case TMP51X_BUS_VOLTAGE_L_LIMIT
:
271 max_val
= (data
->vbus_range_uvolt
== TMP51X_VBUS_RANGE_32V
) ?
272 MAX_BUS_VOLTAGE_32_LIMIT
: MAX_BUS_VOLTAGE_16_LIMIT
;
274 val
= clamp_val(DIV_ROUND_CLOSEST(val
, 4), 0, max_val
);
275 regval
= val
<< TMP51X_BUS_VOLTAGE_SHIFT
;
277 case TMP51X_POWER_LIMIT
:
278 regval
= clamp_val(DIV_ROUND_CLOSEST(val
, data
->pwr_lsb_uw
), 0,
281 case TMP51X_LOCAL_TEMP_LIMIT
:
282 case TMP51X_REMOTE_TEMP_LIMIT_1
:
283 case TMP51X_REMOTE_TEMP_LIMIT_2
:
284 case TMP513_REMOTE_TEMP_LIMIT_3
:
285 // 1lsb = 0.0625 degrees centigrade
286 val
= clamp_val(val
, MIN_TEMP_LIMIT
, MAX_TEMP_LIMIT
);
287 regval
= DIV_ROUND_CLOSEST(val
* 10, 625) << TMP51X_TEMP_SHIFT
;
289 case TMP51X_N_FACTOR_AND_HYST_1
:
290 // 1lsb = 0.5 degrees centigrade
291 val
= clamp_val(val
, 0, MAX_TEMP_HYST
);
292 regval
= DIV_ROUND_CLOSEST(val
, 500);
293 mask
= TMP51X_HYST_MASK
;
302 return regmap_write(data
->regmap
, reg
, regval
);
304 return regmap_update_bits(data
->regmap
, reg
, mask
, regval
);
307 static u8
tmp51x_get_reg(enum hwmon_sensor_types type
, u32 attr
, int channel
)
312 case hwmon_temp_input
:
313 return TMP51X_TEMP_INPUT
[channel
];
314 case hwmon_temp_crit_alarm
:
315 return TMP51X_STATUS
;
316 case hwmon_temp_crit
:
317 return TMP51X_TEMP_CRIT
[channel
];
318 case hwmon_temp_crit_hyst
:
319 return TMP51X_TEMP_CRIT_HYST
[channel
];
325 return TMP51X_BUS_VOLTAGE_RESULT
;
326 case hwmon_in_lcrit_alarm
:
327 case hwmon_in_crit_alarm
:
328 return TMP51X_STATUS
;
330 return TMP51X_BUS_VOLTAGE_L_LIMIT
;
332 return TMP51X_BUS_VOLTAGE_H_LIMIT
;
337 case hwmon_curr_input
:
338 return TMP51X_CURR_INPUT
[channel
];
339 case hwmon_curr_lcrit_alarm
:
340 case hwmon_curr_crit_alarm
:
341 return TMP51X_STATUS
;
342 case hwmon_curr_lcrit
:
343 return TMP51X_SHUNT_CURRENT_L_LIMIT
;
344 case hwmon_curr_crit
:
345 return TMP51X_SHUNT_CURRENT_H_LIMIT
;
350 case hwmon_power_input
:
351 return TMP51X_POWER_RESULT
;
352 case hwmon_power_crit_alarm
:
353 return TMP51X_STATUS
;
354 case hwmon_power_crit
:
355 return TMP51X_POWER_LIMIT
;
365 static u8
tmp51x_get_status_pos(enum hwmon_sensor_types type
, u32 attr
,
371 case hwmon_temp_crit_alarm
:
372 return TMP51X_TEMP_CRIT_ALARM
[channel
];
377 case hwmon_in_lcrit_alarm
:
378 return TMP51X_BUS_VOLTAGE_L_LIMIT_POS
;
379 case hwmon_in_crit_alarm
:
380 return TMP51X_BUS_VOLTAGE_H_LIMIT_POS
;
385 case hwmon_curr_lcrit_alarm
:
386 return TMP51X_SHUNT_CURRENT_L_LIMIT_POS
;
387 case hwmon_curr_crit_alarm
:
388 return TMP51X_SHUNT_CURRENT_H_LIMIT_POS
;
393 case hwmon_power_crit_alarm
:
394 return TMP51X_POWER_LIMIT_POS
;
404 static int tmp51x_read(struct device
*dev
, enum hwmon_sensor_types type
,
405 u32 attr
, int channel
, long *val
)
407 struct tmp51x_data
*data
= dev_get_drvdata(dev
);
412 reg
= tmp51x_get_reg(type
, attr
, channel
);
416 if (reg
== TMP51X_STATUS
)
417 pos
= tmp51x_get_status_pos(type
, attr
, channel
);
419 ret
= regmap_read(data
->regmap
, reg
, ®val
);
423 return tmp51x_get_value(data
, reg
, pos
, regval
, val
);
426 static int tmp51x_write(struct device
*dev
, enum hwmon_sensor_types type
,
427 u32 attr
, int channel
, long val
)
431 reg
= tmp51x_get_reg(type
, attr
, channel
);
435 return tmp51x_set_value(dev_get_drvdata(dev
), reg
, val
);
438 static umode_t
tmp51x_is_visible(const void *_data
,
439 enum hwmon_sensor_types type
, u32 attr
,
442 const struct tmp51x_data
*data
= _data
;
446 if (channel
>= data
->max_channels
)
449 case hwmon_temp_input
:
450 case hwmon_temp_crit_alarm
:
452 case hwmon_temp_crit
:
454 case hwmon_temp_crit_hyst
:
463 case hwmon_in_lcrit_alarm
:
464 case hwmon_in_crit_alarm
:
472 if (!data
->shunt_uohms
)
476 case hwmon_curr_input
:
477 case hwmon_curr_lcrit_alarm
:
478 case hwmon_curr_crit_alarm
:
480 case hwmon_curr_lcrit
:
481 case hwmon_curr_crit
:
486 if (!data
->shunt_uohms
)
490 case hwmon_power_input
:
491 case hwmon_power_crit_alarm
:
493 case hwmon_power_crit
:
503 static const struct hwmon_channel_info
* const tmp51x_info
[] = {
504 HWMON_CHANNEL_INFO(temp
,
505 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
507 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
509 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
511 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
513 HWMON_CHANNEL_INFO(in
,
514 HWMON_I_INPUT
| HWMON_I_LCRIT
| HWMON_I_LCRIT_ALARM
|
515 HWMON_I_CRIT
| HWMON_I_CRIT_ALARM
),
516 HWMON_CHANNEL_INFO(curr
,
517 HWMON_C_INPUT
| HWMON_C_LCRIT
| HWMON_C_LCRIT_ALARM
|
518 HWMON_C_CRIT
| HWMON_C_CRIT_ALARM
,
520 HWMON_CHANNEL_INFO(power
,
521 HWMON_P_INPUT
| HWMON_P_CRIT
| HWMON_P_CRIT_ALARM
),
525 static const struct hwmon_ops tmp51x_hwmon_ops
= {
526 .is_visible
= tmp51x_is_visible
,
528 .write
= tmp51x_write
,
531 static const struct hwmon_chip_info tmp51x_chip_info
= {
532 .ops
= &tmp51x_hwmon_ops
,
537 * Calibrate the tmp51x following the datasheet method
539 static int tmp51x_calibrate(struct tmp51x_data
*data
)
541 int vshunt_max
= data
->pga_gain
* 40;
546 * If shunt_uohms is equal to 0, the calibration should be set to 0.
547 * The consequence will be that the current and power measurement engine
548 * of the sensor will not work. Temperature and voltage sensing will
551 if (data
->shunt_uohms
== 0)
552 return regmap_write(data
->regmap
, TMP51X_SHUNT_CALIBRATION
, 0);
554 max_curr_ma
= DIV_ROUND_CLOSEST_ULL(vshunt_max
* MICRO
, data
->shunt_uohms
);
557 * Calculate the minimal bit resolution for the current and the power.
558 * Those values will be used during register interpretation.
560 data
->curr_lsb_ua
= DIV_ROUND_CLOSEST_ULL(max_curr_ma
* MILLI
, 32767);
561 data
->pwr_lsb_uw
= 20 * data
->curr_lsb_ua
;
563 div
= DIV_ROUND_CLOSEST_ULL(data
->curr_lsb_ua
* data
->shunt_uohms
, MICRO
);
565 return regmap_write(data
->regmap
, TMP51X_SHUNT_CALIBRATION
,
566 DIV_ROUND_CLOSEST(40960, div
));
570 * Initialize the configuration and calibration registers.
572 static int tmp51x_init(struct tmp51x_data
*data
)
575 int ret
= regmap_write(data
->regmap
, TMP51X_SHUNT_CONFIG
,
580 ret
= regmap_write(data
->regmap
, TMP51X_TEMP_CONFIG
, data
->temp_config
);
584 // nFactor configuration
585 ret
= regmap_update_bits(data
->regmap
, TMP51X_N_FACTOR_AND_HYST_1
,
586 TMP51X_NFACTOR_MASK
, data
->nfactor
[0] << 8);
590 ret
= regmap_write(data
->regmap
, TMP51X_N_FACTOR_2
,
591 data
->nfactor
[1] << 8);
595 if (data
->max_channels
== TMP513_MAX_CHANNELS
) {
596 ret
= regmap_write(data
->regmap
, TMP513_N_FACTOR_3
,
597 data
->nfactor
[2] << 8);
602 ret
= tmp51x_calibrate(data
);
606 // Read the status register before using as the datasheet propose
607 return regmap_read(data
->regmap
, TMP51X_STATUS
, ®val
);
610 static const struct i2c_device_id tmp51x_id
[] = {
611 { "tmp512", TMP512_MAX_CHANNELS
},
612 { "tmp513", TMP513_MAX_CHANNELS
},
615 MODULE_DEVICE_TABLE(i2c
, tmp51x_id
);
617 static const struct of_device_id tmp51x_of_match
[] = {
618 { .compatible
= "ti,tmp512", .data
= (void *)TMP512_MAX_CHANNELS
},
619 { .compatible
= "ti,tmp513", .data
= (void *)TMP513_MAX_CHANNELS
},
622 MODULE_DEVICE_TABLE(of
, tmp51x_of_match
);
624 static int tmp51x_vbus_range_to_reg(struct device
*dev
,
625 struct tmp51x_data
*data
)
627 if (data
->vbus_range_uvolt
== TMP51X_VBUS_RANGE_32V
) {
628 data
->shunt_config
|= TMP51X_BUS_VOLTAGE_MASK
;
629 } else if (data
->vbus_range_uvolt
== TMP51X_VBUS_RANGE_16V
) {
630 data
->shunt_config
&= ~TMP51X_BUS_VOLTAGE_MASK
;
632 return dev_err_probe(dev
, -EINVAL
,
633 "ti,bus-range-microvolt is invalid: %u\n",
634 data
->vbus_range_uvolt
);
639 static int tmp51x_pga_gain_to_reg(struct device
*dev
, struct tmp51x_data
*data
)
641 if (data
->pga_gain
== 8) {
642 data
->shunt_config
|= CURRENT_SENSE_VOLTAGE_320_MASK
;
643 } else if (data
->pga_gain
== 4) {
644 data
->shunt_config
|= CURRENT_SENSE_VOLTAGE_160_MASK
;
645 } else if (data
->pga_gain
== 2) {
646 data
->shunt_config
|= CURRENT_SENSE_VOLTAGE_80_MASK
;
647 } else if (data
->pga_gain
== 1) {
648 data
->shunt_config
|= CURRENT_SENSE_VOLTAGE_40_MASK
;
650 return dev_err_probe(dev
, -EINVAL
,
651 "ti,pga-gain is invalid: %u\n", data
->pga_gain
);
656 static int tmp51x_read_properties(struct device
*dev
, struct tmp51x_data
*data
)
661 ret
= device_property_read_u32(dev
, "shunt-resistor-micro-ohms", &val
);
662 data
->shunt_uohms
= (ret
>= 0) ? val
: TMP51X_SHUNT_VALUE_DEFAULT
;
664 ret
= device_property_read_u32(dev
, "ti,bus-range-microvolt", &val
);
665 data
->vbus_range_uvolt
= (ret
>= 0) ? val
: TMP51X_VBUS_RANGE_DEFAULT
;
666 ret
= tmp51x_vbus_range_to_reg(dev
, data
);
670 ret
= device_property_read_u32(dev
, "ti,pga-gain", &val
);
671 data
->pga_gain
= (ret
>= 0) ? val
: TMP51X_PGA_DEFAULT
;
672 ret
= tmp51x_pga_gain_to_reg(dev
, data
);
676 device_property_read_u32_array(dev
, "ti,nfactor", data
->nfactor
,
677 data
->max_channels
- 1);
679 // Check if shunt value is compatible with pga-gain
680 if (data
->shunt_uohms
> data
->pga_gain
* 40 * MICRO
) {
681 return dev_err_probe(dev
, -EINVAL
,
682 "shunt-resistor: %u too big for pga_gain: %u\n",
683 data
->shunt_uohms
, data
->pga_gain
);
689 static void tmp51x_use_default(struct tmp51x_data
*data
)
691 data
->vbus_range_uvolt
= TMP51X_VBUS_RANGE_DEFAULT
;
692 data
->pga_gain
= TMP51X_PGA_DEFAULT
;
693 data
->shunt_uohms
= TMP51X_SHUNT_VALUE_DEFAULT
;
696 static int tmp51x_configure(struct device
*dev
, struct tmp51x_data
*data
)
698 data
->shunt_config
= TMP51X_SHUNT_CONFIG_DEFAULT
;
699 data
->temp_config
= TMP51X_TEMP_CONFIG_DEFAULT(data
->max_channels
);
702 return tmp51x_read_properties(dev
, data
);
704 tmp51x_use_default(data
);
709 static int tmp51x_probe(struct i2c_client
*client
)
711 struct device
*dev
= &client
->dev
;
712 struct tmp51x_data
*data
;
713 struct device
*hwmon_dev
;
716 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
720 data
->max_channels
= (uintptr_t)i2c_get_match_data(client
);
722 ret
= tmp51x_configure(dev
, data
);
724 return dev_err_probe(dev
, ret
, "error configuring the device\n");
726 data
->regmap
= devm_regmap_init_i2c(client
, &tmp51x_regmap_config
);
727 if (IS_ERR(data
->regmap
))
728 return dev_err_probe(dev
, PTR_ERR(data
->regmap
),
729 "failed to allocate register map\n");
731 ret
= tmp51x_init(data
);
733 return dev_err_probe(dev
, ret
, "error configuring the device\n");
735 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
739 if (IS_ERR(hwmon_dev
))
740 return PTR_ERR(hwmon_dev
);
742 dev_dbg(dev
, "power monitor %s\n", client
->name
);
747 static struct i2c_driver tmp51x_driver
= {
750 .of_match_table
= tmp51x_of_match
,
752 .probe
= tmp51x_probe
,
753 .id_table
= tmp51x_id
,
756 module_i2c_driver(tmp51x_driver
);
758 MODULE_AUTHOR("Eric Tremblay <etremblay@distechcontrols.com>");
759 MODULE_DESCRIPTION("tmp51x driver");
760 MODULE_LICENSE("GPL");