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: http://www.ti.com/lit/gpn/tmp513
11 * Thermal/Power Management with Dual Remote
12 * and Local Temperature Sensor and Current Shunt Monitor
13 * Datasheet: http://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/err.h>
23 #include <linux/hwmon.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/regmap.h>
29 #include <linux/slab.h>
30 #include <linux/util_macros.h>
32 // Common register definition
33 #define TMP51X_SHUNT_CONFIG 0x00
34 #define TMP51X_TEMP_CONFIG 0x01
35 #define TMP51X_STATUS 0x02
36 #define TMP51X_SMBUS_ALERT 0x03
37 #define TMP51X_SHUNT_CURRENT_RESULT 0x04
38 #define TMP51X_BUS_VOLTAGE_RESULT 0x05
39 #define TMP51X_POWER_RESULT 0x06
40 #define TMP51X_BUS_CURRENT_RESULT 0x07
41 #define TMP51X_LOCAL_TEMP_RESULT 0x08
42 #define TMP51X_REMOTE_TEMP_RESULT_1 0x09
43 #define TMP51X_REMOTE_TEMP_RESULT_2 0x0A
44 #define TMP51X_SHUNT_CURRENT_H_LIMIT 0x0C
45 #define TMP51X_SHUNT_CURRENT_L_LIMIT 0x0D
46 #define TMP51X_BUS_VOLTAGE_H_LIMIT 0x0E
47 #define TMP51X_BUS_VOLTAGE_L_LIMIT 0x0F
48 #define TMP51X_POWER_LIMIT 0x10
49 #define TMP51X_LOCAL_TEMP_LIMIT 0x11
50 #define TMP51X_REMOTE_TEMP_LIMIT_1 0x12
51 #define TMP51X_REMOTE_TEMP_LIMIT_2 0x13
52 #define TMP51X_SHUNT_CALIBRATION 0x15
53 #define TMP51X_N_FACTOR_AND_HYST_1 0x16
54 #define TMP51X_N_FACTOR_2 0x17
55 #define TMP51X_MAN_ID_REG 0xFE
56 #define TMP51X_DEVICE_ID_REG 0xFF
58 // TMP513 specific register definition
59 #define TMP513_REMOTE_TEMP_RESULT_3 0x0B
60 #define TMP513_REMOTE_TEMP_LIMIT_3 0x14
61 #define TMP513_N_FACTOR_3 0x18
63 // Common attrs, and NULL
64 #define TMP51X_MANUFACTURER_ID 0x55FF
66 #define TMP512_DEVICE_ID 0x22FF
67 #define TMP513_DEVICE_ID 0x23FF
70 #define TMP51X_SHUNT_CONFIG_DEFAULT 0x399F
71 #define TMP51X_SHUNT_VALUE_DEFAULT 1000
72 #define TMP51X_VBUS_RANGE_DEFAULT TMP51X_VBUS_RANGE_32V
73 #define TMP51X_PGA_DEFAULT 8
74 #define TMP51X_MAX_REGISTER_ADDR 0xFF
76 #define TMP512_TEMP_CONFIG_DEFAULT 0xBF80
77 #define TMP513_TEMP_CONFIG_DEFAULT 0xFF80
80 #define CURRENT_SENSE_VOLTAGE_320_MASK 0x1800
81 #define CURRENT_SENSE_VOLTAGE_160_MASK 0x1000
82 #define CURRENT_SENSE_VOLTAGE_80_MASK 0x0800
83 #define CURRENT_SENSE_VOLTAGE_40_MASK 0
85 #define TMP51X_BUS_VOLTAGE_MASK 0x2000
86 #define TMP51X_NFACTOR_MASK 0xFF00
87 #define TMP51X_HYST_MASK 0x00FF
89 #define TMP51X_BUS_VOLTAGE_SHIFT 3
90 #define TMP51X_TEMP_SHIFT 3
93 #define TMP51X_SHUNT_CURRENT_H_LIMIT_POS 15
94 #define TMP51X_SHUNT_CURRENT_L_LIMIT_POS 14
95 #define TMP51X_BUS_VOLTAGE_H_LIMIT_POS 13
96 #define TMP51X_BUS_VOLTAGE_L_LIMIT_POS 12
97 #define TMP51X_POWER_LIMIT_POS 11
98 #define TMP51X_LOCAL_TEMP_LIMIT_POS 10
99 #define TMP51X_REMOTE_TEMP_LIMIT_1_POS 9
100 #define TMP51X_REMOTE_TEMP_LIMIT_2_POS 8
101 #define TMP513_REMOTE_TEMP_LIMIT_3_POS 7
103 #define TMP51X_VBUS_RANGE_32V 32000000
104 #define TMP51X_VBUS_RANGE_16V 16000000
107 #define MAX_BUS_VOLTAGE_32_LIMIT 32764
108 #define MAX_BUS_VOLTAGE_16_LIMIT 16382
110 // Max possible value is -256 to +256 but datasheet indicated -40 to 125.
111 #define MAX_TEMP_LIMIT 125000
112 #define MIN_TEMP_LIMIT -40000
114 #define MAX_TEMP_HYST 127500
116 static const u8 TMP51X_TEMP_INPUT
[4] = {
117 TMP51X_LOCAL_TEMP_RESULT
,
118 TMP51X_REMOTE_TEMP_RESULT_1
,
119 TMP51X_REMOTE_TEMP_RESULT_2
,
120 TMP513_REMOTE_TEMP_RESULT_3
123 static const u8 TMP51X_TEMP_CRIT
[4] = {
124 TMP51X_LOCAL_TEMP_LIMIT
,
125 TMP51X_REMOTE_TEMP_LIMIT_1
,
126 TMP51X_REMOTE_TEMP_LIMIT_2
,
127 TMP513_REMOTE_TEMP_LIMIT_3
130 static const u8 TMP51X_TEMP_CRIT_ALARM
[4] = {
131 TMP51X_LOCAL_TEMP_LIMIT_POS
,
132 TMP51X_REMOTE_TEMP_LIMIT_1_POS
,
133 TMP51X_REMOTE_TEMP_LIMIT_2_POS
,
134 TMP513_REMOTE_TEMP_LIMIT_3_POS
137 static const u8 TMP51X_TEMP_CRIT_HYST
[4] = {
138 TMP51X_N_FACTOR_AND_HYST_1
,
139 TMP51X_N_FACTOR_AND_HYST_1
,
140 TMP51X_N_FACTOR_AND_HYST_1
,
141 TMP51X_N_FACTOR_AND_HYST_1
144 static const u8 TMP51X_CURR_INPUT
[2] = {
145 TMP51X_SHUNT_CURRENT_RESULT
,
146 TMP51X_BUS_CURRENT_RESULT
149 static struct regmap_config tmp51x_regmap_config
= {
152 .max_register
= TMP51X_MAX_REGISTER_ADDR
,
162 u32 vbus_range_uvolt
;
173 struct regmap
*regmap
;
176 // Set the shift based on the gain 8=4, 4=3, 2=2, 1=1
177 static inline u8
tmp51x_get_pga_shift(struct tmp51x_data
*data
)
179 return 5 - ffs(data
->pga_gain
);
182 static int tmp51x_get_value(struct tmp51x_data
*data
, u8 reg
, u8 pos
,
183 unsigned int regval
, long *val
)
187 *val
= (regval
>> pos
) & 1;
189 case TMP51X_SHUNT_CURRENT_RESULT
:
190 case TMP51X_SHUNT_CURRENT_H_LIMIT
:
191 case TMP51X_SHUNT_CURRENT_L_LIMIT
:
193 * The valus is read in voltage in the chip but reported as
194 * current to the user.
195 * 2's compliment number shifted by one to four depending
196 * on the pga gain setting. 1lsb = 10uV
198 *val
= sign_extend32(regval
, 17 - tmp51x_get_pga_shift(data
));
199 *val
= DIV_ROUND_CLOSEST(*val
* 10000, data
->shunt_uohms
);
201 case TMP51X_BUS_VOLTAGE_RESULT
:
202 case TMP51X_BUS_VOLTAGE_H_LIMIT
:
203 case TMP51X_BUS_VOLTAGE_L_LIMIT
:
205 *val
= (regval
>> TMP51X_BUS_VOLTAGE_SHIFT
) * 4;
207 case TMP51X_POWER_RESULT
:
208 case TMP51X_POWER_LIMIT
:
209 // Power = (current * BusVoltage) / 5000
210 *val
= regval
* data
->pwr_lsb_uw
;
212 case TMP51X_BUS_CURRENT_RESULT
:
213 // Current = (ShuntVoltage * CalibrationRegister) / 4096
214 *val
= sign_extend32(regval
, 16) * data
->curr_lsb_ua
;
215 *val
= DIV_ROUND_CLOSEST(*val
, 1000);
217 case TMP51X_LOCAL_TEMP_RESULT
:
218 case TMP51X_REMOTE_TEMP_RESULT_1
:
219 case TMP51X_REMOTE_TEMP_RESULT_2
:
220 case TMP513_REMOTE_TEMP_RESULT_3
:
221 case TMP51X_LOCAL_TEMP_LIMIT
:
222 case TMP51X_REMOTE_TEMP_LIMIT_1
:
223 case TMP51X_REMOTE_TEMP_LIMIT_2
:
224 case TMP513_REMOTE_TEMP_LIMIT_3
:
225 // 1lsb = 0.0625 degrees centigrade
226 *val
= sign_extend32(regval
, 16) >> TMP51X_TEMP_SHIFT
;
227 *val
= DIV_ROUND_CLOSEST(*val
* 625, 10);
229 case TMP51X_N_FACTOR_AND_HYST_1
:
230 // 1lsb = 0.5 degrees centigrade
231 *val
= (regval
& TMP51X_HYST_MASK
) * 500;
243 static int tmp51x_set_value(struct tmp51x_data
*data
, u8 reg
, long val
)
249 case TMP51X_SHUNT_CURRENT_H_LIMIT
:
250 case TMP51X_SHUNT_CURRENT_L_LIMIT
:
252 * The user enter current value and we convert it to
253 * voltage. 1lsb = 10uV
255 val
= DIV_ROUND_CLOSEST(val
* data
->shunt_uohms
, 10000);
256 max_val
= U16_MAX
>> tmp51x_get_pga_shift(data
);
257 regval
= clamp_val(val
, -max_val
, max_val
);
259 case TMP51X_BUS_VOLTAGE_H_LIMIT
:
260 case TMP51X_BUS_VOLTAGE_L_LIMIT
:
262 max_val
= (data
->vbus_range_uvolt
== TMP51X_VBUS_RANGE_32V
) ?
263 MAX_BUS_VOLTAGE_32_LIMIT
: MAX_BUS_VOLTAGE_16_LIMIT
;
265 val
= clamp_val(DIV_ROUND_CLOSEST(val
, 4), 0, max_val
);
266 regval
= val
<< TMP51X_BUS_VOLTAGE_SHIFT
;
268 case TMP51X_POWER_LIMIT
:
269 regval
= clamp_val(DIV_ROUND_CLOSEST(val
, data
->pwr_lsb_uw
), 0,
272 case TMP51X_LOCAL_TEMP_LIMIT
:
273 case TMP51X_REMOTE_TEMP_LIMIT_1
:
274 case TMP51X_REMOTE_TEMP_LIMIT_2
:
275 case TMP513_REMOTE_TEMP_LIMIT_3
:
276 // 1lsb = 0.0625 degrees centigrade
277 val
= clamp_val(val
, MIN_TEMP_LIMIT
, MAX_TEMP_LIMIT
);
278 regval
= DIV_ROUND_CLOSEST(val
* 10, 625) << TMP51X_TEMP_SHIFT
;
280 case TMP51X_N_FACTOR_AND_HYST_1
:
281 // 1lsb = 0.5 degrees centigrade
282 val
= clamp_val(val
, 0, MAX_TEMP_HYST
);
283 regval
= DIV_ROUND_CLOSEST(val
, 500);
284 mask
= TMP51X_HYST_MASK
;
293 return regmap_write(data
->regmap
, reg
, regval
);
295 return regmap_update_bits(data
->regmap
, reg
, mask
, regval
);
298 static u8
tmp51x_get_reg(enum hwmon_sensor_types type
, u32 attr
, int channel
)
303 case hwmon_temp_input
:
304 return TMP51X_TEMP_INPUT
[channel
];
305 case hwmon_temp_crit_alarm
:
306 return TMP51X_STATUS
;
307 case hwmon_temp_crit
:
308 return TMP51X_TEMP_CRIT
[channel
];
309 case hwmon_temp_crit_hyst
:
310 return TMP51X_TEMP_CRIT_HYST
[channel
];
316 return TMP51X_BUS_VOLTAGE_RESULT
;
317 case hwmon_in_lcrit_alarm
:
318 case hwmon_in_crit_alarm
:
319 return TMP51X_STATUS
;
321 return TMP51X_BUS_VOLTAGE_L_LIMIT
;
323 return TMP51X_BUS_VOLTAGE_H_LIMIT
;
328 case hwmon_curr_input
:
329 return TMP51X_CURR_INPUT
[channel
];
330 case hwmon_curr_lcrit_alarm
:
331 case hwmon_curr_crit_alarm
:
332 return TMP51X_STATUS
;
333 case hwmon_curr_lcrit
:
334 return TMP51X_SHUNT_CURRENT_L_LIMIT
;
335 case hwmon_curr_crit
:
336 return TMP51X_SHUNT_CURRENT_H_LIMIT
;
341 case hwmon_power_input
:
342 return TMP51X_POWER_RESULT
;
343 case hwmon_power_crit_alarm
:
344 return TMP51X_STATUS
;
345 case hwmon_power_crit
:
346 return TMP51X_POWER_LIMIT
;
356 static u8
tmp51x_get_status_pos(enum hwmon_sensor_types type
, u32 attr
,
362 case hwmon_temp_crit_alarm
:
363 return TMP51X_TEMP_CRIT_ALARM
[channel
];
368 case hwmon_in_lcrit_alarm
:
369 return TMP51X_BUS_VOLTAGE_L_LIMIT_POS
;
370 case hwmon_in_crit_alarm
:
371 return TMP51X_BUS_VOLTAGE_H_LIMIT_POS
;
376 case hwmon_curr_lcrit_alarm
:
377 return TMP51X_SHUNT_CURRENT_L_LIMIT_POS
;
378 case hwmon_curr_crit_alarm
:
379 return TMP51X_SHUNT_CURRENT_H_LIMIT_POS
;
384 case hwmon_power_crit_alarm
:
385 return TMP51X_POWER_LIMIT_POS
;
395 static int tmp51x_read(struct device
*dev
, enum hwmon_sensor_types type
,
396 u32 attr
, int channel
, long *val
)
398 struct tmp51x_data
*data
= dev_get_drvdata(dev
);
403 reg
= tmp51x_get_reg(type
, attr
, channel
);
407 if (reg
== TMP51X_STATUS
)
408 pos
= tmp51x_get_status_pos(type
, attr
, channel
);
410 ret
= regmap_read(data
->regmap
, reg
, ®val
);
414 return tmp51x_get_value(data
, reg
, pos
, regval
, val
);
417 static int tmp51x_write(struct device
*dev
, enum hwmon_sensor_types type
,
418 u32 attr
, int channel
, long val
)
422 reg
= tmp51x_get_reg(type
, attr
, channel
);
426 return tmp51x_set_value(dev_get_drvdata(dev
), reg
, val
);
429 static umode_t
tmp51x_is_visible(const void *_data
,
430 enum hwmon_sensor_types type
, u32 attr
,
433 const struct tmp51x_data
*data
= _data
;
437 if (data
->id
== tmp512
&& channel
== 4)
440 case hwmon_temp_input
:
441 case hwmon_temp_crit_alarm
:
443 case hwmon_temp_crit
:
445 case hwmon_temp_crit_hyst
:
454 case hwmon_in_lcrit_alarm
:
455 case hwmon_in_crit_alarm
:
463 if (!data
->shunt_uohms
)
467 case hwmon_curr_input
:
468 case hwmon_curr_lcrit_alarm
:
469 case hwmon_curr_crit_alarm
:
471 case hwmon_curr_lcrit
:
472 case hwmon_curr_crit
:
477 if (!data
->shunt_uohms
)
481 case hwmon_power_input
:
482 case hwmon_power_crit_alarm
:
484 case hwmon_power_crit
:
494 static const struct hwmon_channel_info
*tmp51x_info
[] = {
495 HWMON_CHANNEL_INFO(temp
,
496 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
498 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
500 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
502 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
504 HWMON_CHANNEL_INFO(in
,
505 HWMON_I_INPUT
| HWMON_I_LCRIT
| HWMON_I_LCRIT_ALARM
|
506 HWMON_I_CRIT
| HWMON_I_CRIT_ALARM
),
507 HWMON_CHANNEL_INFO(curr
,
508 HWMON_C_INPUT
| HWMON_C_LCRIT
| HWMON_C_LCRIT_ALARM
|
509 HWMON_C_CRIT
| HWMON_C_CRIT_ALARM
,
511 HWMON_CHANNEL_INFO(power
,
512 HWMON_P_INPUT
| HWMON_P_CRIT
| HWMON_P_CRIT_ALARM
),
516 static const struct hwmon_ops tmp51x_hwmon_ops
= {
517 .is_visible
= tmp51x_is_visible
,
519 .write
= tmp51x_write
,
522 static const struct hwmon_chip_info tmp51x_chip_info
= {
523 .ops
= &tmp51x_hwmon_ops
,
528 * Calibrate the tmp51x following the datasheet method
530 static int tmp51x_calibrate(struct tmp51x_data
*data
)
532 int vshunt_max
= data
->pga_gain
* 40;
537 * If shunt_uohms is equal to 0, the calibration should be set to 0.
538 * The consequence will be that the current and power measurement engine
539 * of the sensor will not work. Temperature and voltage sensing will
542 if (data
->shunt_uohms
== 0)
543 return regmap_write(data
->regmap
, TMP51X_SHUNT_CALIBRATION
, 0);
545 max_curr_ma
= DIV_ROUND_CLOSEST_ULL(vshunt_max
* 1000 * 1000,
549 * Calculate the minimal bit resolution for the current and the power.
550 * Those values will be used during register interpretation.
552 data
->curr_lsb_ua
= DIV_ROUND_CLOSEST_ULL(max_curr_ma
* 1000, 32767);
553 data
->pwr_lsb_uw
= 20 * data
->curr_lsb_ua
;
555 div
= DIV_ROUND_CLOSEST_ULL(data
->curr_lsb_ua
* data
->shunt_uohms
,
558 return regmap_write(data
->regmap
, TMP51X_SHUNT_CALIBRATION
,
559 DIV_ROUND_CLOSEST(40960, div
));
563 * Initialize the configuration and calibration registers.
565 static int tmp51x_init(struct tmp51x_data
*data
)
568 int ret
= regmap_write(data
->regmap
, TMP51X_SHUNT_CONFIG
,
573 ret
= regmap_write(data
->regmap
, TMP51X_TEMP_CONFIG
, data
->temp_config
);
577 // nFactor configuration
578 ret
= regmap_update_bits(data
->regmap
, TMP51X_N_FACTOR_AND_HYST_1
,
579 TMP51X_NFACTOR_MASK
, data
->nfactor
[0] << 8);
583 ret
= regmap_write(data
->regmap
, TMP51X_N_FACTOR_2
,
584 data
->nfactor
[1] << 8);
588 if (data
->id
== tmp513
) {
589 ret
= regmap_write(data
->regmap
, TMP513_N_FACTOR_3
,
590 data
->nfactor
[2] << 8);
595 ret
= tmp51x_calibrate(data
);
599 // Read the status register before using as the datasheet propose
600 return regmap_read(data
->regmap
, TMP51X_STATUS
, ®val
);
603 static const struct i2c_device_id tmp51x_id
[] = {
604 { "tmp512", tmp512
},
605 { "tmp513", tmp513
},
608 MODULE_DEVICE_TABLE(i2c
, tmp51x_id
);
610 static const struct of_device_id tmp51x_of_match
[] = {
612 .compatible
= "ti,tmp512",
613 .data
= (void *)tmp512
616 .compatible
= "ti,tmp513",
617 .data
= (void *)tmp513
621 MODULE_DEVICE_TABLE(of
, tmp51x_of_match
);
623 static int tmp51x_vbus_range_to_reg(struct device
*dev
,
624 struct tmp51x_data
*data
)
626 if (data
->vbus_range_uvolt
== TMP51X_VBUS_RANGE_32V
) {
627 data
->shunt_config
|= TMP51X_BUS_VOLTAGE_MASK
;
628 } else if (data
->vbus_range_uvolt
== TMP51X_VBUS_RANGE_16V
) {
629 data
->shunt_config
&= ~TMP51X_BUS_VOLTAGE_MASK
;
631 dev_err(dev
, "ti,bus-range-microvolt is invalid: %u\n",
632 data
->vbus_range_uvolt
);
638 static int tmp51x_pga_gain_to_reg(struct device
*dev
, struct tmp51x_data
*data
)
640 if (data
->pga_gain
== 8) {
641 data
->shunt_config
|= CURRENT_SENSE_VOLTAGE_320_MASK
;
642 } else if (data
->pga_gain
== 4) {
643 data
->shunt_config
|= CURRENT_SENSE_VOLTAGE_160_MASK
;
644 } else if (data
->pga_gain
== 2) {
645 data
->shunt_config
|= CURRENT_SENSE_VOLTAGE_80_MASK
;
646 } else if (data
->pga_gain
== 1) {
647 data
->shunt_config
|= CURRENT_SENSE_VOLTAGE_40_MASK
;
649 dev_err(dev
, "ti,pga-gain is invalid: %u\n", data
->pga_gain
);
655 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 ret
= device_property_read_u32_array(dev
, "ti,nfactor", nfactor
,
677 (data
->id
== tmp513
) ? 3 : 2);
679 memcpy(data
->nfactor
, nfactor
, (data
->id
== tmp513
) ? 3 : 2);
681 // Check if shunt value is compatible with pga-gain
682 if (data
->shunt_uohms
> data
->pga_gain
* 40 * 1000 * 1000) {
683 dev_err(dev
, "shunt-resistor: %u too big for pga_gain: %u\n",
684 data
->shunt_uohms
, data
->pga_gain
);
691 static void tmp51x_use_default(struct tmp51x_data
*data
)
693 data
->vbus_range_uvolt
= TMP51X_VBUS_RANGE_DEFAULT
;
694 data
->pga_gain
= TMP51X_PGA_DEFAULT
;
695 data
->shunt_uohms
= TMP51X_SHUNT_VALUE_DEFAULT
;
698 static int tmp51x_configure(struct device
*dev
, struct tmp51x_data
*data
)
700 data
->shunt_config
= TMP51X_SHUNT_CONFIG_DEFAULT
;
701 data
->temp_config
= (data
->id
== tmp513
) ?
702 TMP513_TEMP_CONFIG_DEFAULT
: TMP512_TEMP_CONFIG_DEFAULT
;
705 return tmp51x_read_properties(dev
, data
);
707 tmp51x_use_default(data
);
712 static int tmp51x_probe(struct i2c_client
*client
,
713 const struct i2c_device_id
*id
)
715 struct device
*dev
= &client
->dev
;
716 struct tmp51x_data
*data
;
717 struct device
*hwmon_dev
;
720 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
724 if (client
->dev
.of_node
)
725 data
->id
= (enum tmp51x_ids
)device_get_match_data(&client
->dev
);
727 data
->id
= id
->driver_data
;
729 ret
= tmp51x_configure(dev
, data
);
731 dev_err(dev
, "error configuring the device: %d\n", ret
);
735 data
->regmap
= devm_regmap_init_i2c(client
, &tmp51x_regmap_config
);
736 if (IS_ERR(data
->regmap
)) {
737 dev_err(dev
, "failed to allocate register map\n");
738 return PTR_ERR(data
->regmap
);
741 ret
= tmp51x_init(data
);
743 dev_err(dev
, "error configuring the device: %d\n", ret
);
747 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
751 if (IS_ERR(hwmon_dev
))
752 return PTR_ERR(hwmon_dev
);
754 dev_dbg(dev
, "power monitor %s\n", id
->name
);
759 static struct i2c_driver tmp51x_driver
= {
762 .of_match_table
= of_match_ptr(tmp51x_of_match
),
764 .probe
= tmp51x_probe
,
765 .id_table
= tmp51x_id
,
768 module_i2c_driver(tmp51x_driver
);
770 MODULE_AUTHOR("Eric Tremblay <etremblay@distechcontrols.com>");
771 MODULE_DESCRIPTION("tmp51x driver");
772 MODULE_LICENSE("GPL");