1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for Texas Instruments TMP512, TMP513 power monitor chips
4 *
5 * TMP513:
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
9 *
10 * TMP512:
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
14 *
15 * Copyright (C) 2019 Eric Tremblay <etremblay@distech-controls.com>
16 *
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.
20 */
21
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>
36
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
62
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
67
68 // Common attrs, and NULL
69 #define TMP51X_MANUFACTURER_ID 0x55FF
70
71 #define TMP512_DEVICE_ID 0x22FF
72 #define TMP513_DEVICE_ID 0x23FF
73
74 // Default config
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
80
81 #define TMP512_TEMP_CONFIG_DEFAULT 0xBF80
82 #define TMP513_TEMP_CONFIG_DEFAULT 0xFF80
83
84 // Mask and shift
85 #define CURRENT_SENSE_VOLTAGE_320_MASK 0x1800
86 #define CURRENT_SENSE_VOLTAGE_160_MASK 0x1000
87 #define CURRENT_SENSE_VOLTAGE_80_MASK 0x0800
88 #define CURRENT_SENSE_VOLTAGE_40_MASK 0
89
90 #define TMP51X_BUS_VOLTAGE_MASK 0x2000
91 #define TMP51X_NFACTOR_MASK 0xFF00
92 #define TMP51X_HYST_MASK 0x00FF
93
94 #define TMP51X_BUS_VOLTAGE_SHIFT 3
95 #define TMP51X_TEMP_SHIFT 3
96
97 // Alarms
98 #define TMP51X_SHUNT_CURRENT_H_LIMIT_POS 15
99 #define TMP51X_SHUNT_CURRENT_L_LIMIT_POS 14
100 #define TMP51X_BUS_VOLTAGE_H_LIMIT_POS 13
101 #define TMP51X_BUS_VOLTAGE_L_LIMIT_POS 12
102 #define TMP51X_POWER_LIMIT_POS 11
103 #define TMP51X_LOCAL_TEMP_LIMIT_POS 10
104 #define TMP51X_REMOTE_TEMP_LIMIT_1_POS 9
105 #define TMP51X_REMOTE_TEMP_LIMIT_2_POS 8
106 #define TMP513_REMOTE_TEMP_LIMIT_3_POS 7
107
108 #define TMP51X_VBUS_RANGE_32V (32 * MICRO)
109 #define TMP51X_VBUS_RANGE_16V (16 * MICRO)
110
111 // Max and Min value
112 #define MAX_BUS_VOLTAGE_32_LIMIT 32764
113 #define MAX_BUS_VOLTAGE_16_LIMIT 16382
114
115 // Max possible value is -256 to +256 but datasheet indicated -40 to 125.
116 #define MAX_TEMP_LIMIT 125000
117 #define MIN_TEMP_LIMIT -40000
118
119 #define MAX_TEMP_HYST 127500
120
121 static const u8 TMP51X_TEMP_INPUT[4] = {
122 TMP51X_LOCAL_TEMP_RESULT,
123 TMP51X_REMOTE_TEMP_RESULT_1,
124 TMP51X_REMOTE_TEMP_RESULT_2,
125 TMP513_REMOTE_TEMP_RESULT_3
126 };
127
128 static const u8 TMP51X_TEMP_CRIT[4] = {
129 TMP51X_LOCAL_TEMP_LIMIT,
130 TMP51X_REMOTE_TEMP_LIMIT_1,
131 TMP51X_REMOTE_TEMP_LIMIT_2,
132 TMP513_REMOTE_TEMP_LIMIT_3
133 };
134
135 static const u8 TMP51X_TEMP_CRIT_ALARM[4] = {
136 TMP51X_LOCAL_TEMP_LIMIT_POS,
137 TMP51X_REMOTE_TEMP_LIMIT_1_POS,
138 TMP51X_REMOTE_TEMP_LIMIT_2_POS,
139 TMP513_REMOTE_TEMP_LIMIT_3_POS
140 };
141
142 static const u8 TMP51X_TEMP_CRIT_HYST[4] = {
143 TMP51X_N_FACTOR_AND_HYST_1,
144 TMP51X_N_FACTOR_AND_HYST_1,
145 TMP51X_N_FACTOR_AND_HYST_1,
146 TMP51X_N_FACTOR_AND_HYST_1
147 };
148
149 static const u8 TMP51X_CURR_INPUT[2] = {
150 TMP51X_SHUNT_CURRENT_RESULT,
151 TMP51X_BUS_CURRENT_RESULT
152 };
153
154 static struct regmap_config tmp51x_regmap_config = {
155 .reg_bits = 8,
156 .val_bits = 16,
157 .max_register = TMP51X_MAX_REGISTER_ADDR,
158 };
159
160 enum tmp51x_ids {
161 tmp512, tmp513
162 };
163
164 struct tmp51x_data {
165 u16 shunt_config;
166 u16 pga_gain;
167 u32 vbus_range_uvolt;
168
169 u16 temp_config;
170 u32 nfactor[3];
171
172 u32 shunt_uohms;
173
174 u32 curr_lsb_ua;
175 u32 pwr_lsb_uw;
176
177 enum tmp51x_ids id;
178 struct regmap *regmap;
179 };
180
181 // Set the shift based on the gain: 8 -> 1, 4 -> 2, 2 -> 3, 1 -> 4
tmp51x_get_pga_shift(struct tmp51x_data * data)182 static inline u8 tmp51x_get_pga_shift(struct tmp51x_data *data)
183 {
184 return 5 - ffs(data->pga_gain);
185 }
186
tmp51x_get_value(struct tmp51x_data * data,u8 reg,u8 pos,unsigned int regval,long * val)187 static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
188 unsigned int regval, long *val)
189 {
190 switch (reg) {
191 case TMP51X_STATUS:
192 *val = (regval >> pos) & 1;
193 break;
194 case TMP51X_SHUNT_CURRENT_RESULT:
195 case TMP51X_SHUNT_CURRENT_H_LIMIT:
196 case TMP51X_SHUNT_CURRENT_L_LIMIT:
197 /*
198 * The valus is read in voltage in the chip but reported as
199 * current to the user.
200 * 2's complement number shifted by one to four depending
201 * on the pga gain setting. 1lsb = 10uV
202 */
203 *val = sign_extend32(regval,
204 reg == TMP51X_SHUNT_CURRENT_RESULT ?
205 16 - tmp51x_get_pga_shift(data) : 15);
206 *val = DIV_ROUND_CLOSEST(*val * 10 * (long)MILLI, (long)data->shunt_uohms);
207
208 break;
209 case TMP51X_BUS_VOLTAGE_RESULT:
210 case TMP51X_BUS_VOLTAGE_H_LIMIT:
211 case TMP51X_BUS_VOLTAGE_L_LIMIT:
212 // 1lsb = 4mV
213 *val = (regval >> TMP51X_BUS_VOLTAGE_SHIFT) * 4;
214 break;
215 case TMP51X_POWER_RESULT:
216 case TMP51X_POWER_LIMIT:
217 // Power = (current * BusVoltage) / 5000
218 *val = regval * data->pwr_lsb_uw;
219 break;
220 case TMP51X_BUS_CURRENT_RESULT:
221 // Current = (ShuntVoltage * CalibrationRegister) / 4096
222 *val = sign_extend32(regval, 15) * (long)data->curr_lsb_ua;
223 *val = DIV_ROUND_CLOSEST(*val, (long)MILLI);
224 break;
225 case TMP51X_LOCAL_TEMP_RESULT:
226 case TMP51X_REMOTE_TEMP_RESULT_1:
227 case TMP51X_REMOTE_TEMP_RESULT_2:
228 case TMP513_REMOTE_TEMP_RESULT_3:
229 case TMP51X_LOCAL_TEMP_LIMIT:
230 case TMP51X_REMOTE_TEMP_LIMIT_1:
231 case TMP51X_REMOTE_TEMP_LIMIT_2:
232 case TMP513_REMOTE_TEMP_LIMIT_3:
233 // 1lsb = 0.0625 degrees centigrade
234 *val = sign_extend32(regval, 15) >> TMP51X_TEMP_SHIFT;
235 *val = DIV_ROUND_CLOSEST(*val * 625, 10);
236 break;
237 case TMP51X_N_FACTOR_AND_HYST_1:
238 // 1lsb = 0.5 degrees centigrade
239 *val = (regval & TMP51X_HYST_MASK) * 500;
240 break;
241 default:
242 // Programmer goofed
243 WARN_ON_ONCE(1);
244 *val = 0;
245 return -EOPNOTSUPP;
246 }
247
248 return 0;
249 }
250
tmp51x_set_value(struct tmp51x_data * data,u8 reg,long val)251 static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val)
252 {
253 int regval, max_val;
254 u32 mask = 0;
255
256 switch (reg) {
257 case TMP51X_SHUNT_CURRENT_H_LIMIT:
258 case TMP51X_SHUNT_CURRENT_L_LIMIT:
259 /*
260 * The user enter current value and we convert it to
261 * voltage. 1lsb = 10uV
262 */
263 val = DIV_ROUND_CLOSEST(val * (long)data->shunt_uohms, 10 * (long)MILLI);
264 max_val = U16_MAX >> tmp51x_get_pga_shift(data);
265 regval = clamp_val(val, -max_val, max_val);
266 break;
267 case TMP51X_BUS_VOLTAGE_H_LIMIT:
268 case TMP51X_BUS_VOLTAGE_L_LIMIT:
269 // 1lsb = 4mV
270 max_val = (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) ?
271 MAX_BUS_VOLTAGE_32_LIMIT : MAX_BUS_VOLTAGE_16_LIMIT;
272
273 val = clamp_val(DIV_ROUND_CLOSEST(val, 4), 0, max_val);
274 regval = val << TMP51X_BUS_VOLTAGE_SHIFT;
275 break;
276 case TMP51X_POWER_LIMIT:
277 regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0,
278 U16_MAX);
279 break;
280 case TMP51X_LOCAL_TEMP_LIMIT:
281 case TMP51X_REMOTE_TEMP_LIMIT_1:
282 case TMP51X_REMOTE_TEMP_LIMIT_2:
283 case TMP513_REMOTE_TEMP_LIMIT_3:
284 // 1lsb = 0.0625 degrees centigrade
285 val = clamp_val(val, MIN_TEMP_LIMIT, MAX_TEMP_LIMIT);
286 regval = DIV_ROUND_CLOSEST(val * 10, 625) << TMP51X_TEMP_SHIFT;
287 break;
288 case TMP51X_N_FACTOR_AND_HYST_1:
289 // 1lsb = 0.5 degrees centigrade
290 val = clamp_val(val, 0, MAX_TEMP_HYST);
291 regval = DIV_ROUND_CLOSEST(val, 500);
292 mask = TMP51X_HYST_MASK;
293 break;
294 default:
295 // Programmer goofed
296 WARN_ON_ONCE(1);
297 return -EOPNOTSUPP;
298 }
299
300 if (mask == 0)
301 return regmap_write(data->regmap, reg, regval);
302 else
303 return regmap_update_bits(data->regmap, reg, mask, regval);
304 }
305
tmp51x_get_reg(enum hwmon_sensor_types type,u32 attr,int channel)306 static u8 tmp51x_get_reg(enum hwmon_sensor_types type, u32 attr, int channel)
307 {
308 switch (type) {
309 case hwmon_temp:
310 switch (attr) {
311 case hwmon_temp_input:
312 return TMP51X_TEMP_INPUT[channel];
313 case hwmon_temp_crit_alarm:
314 return TMP51X_STATUS;
315 case hwmon_temp_crit:
316 return TMP51X_TEMP_CRIT[channel];
317 case hwmon_temp_crit_hyst:
318 return TMP51X_TEMP_CRIT_HYST[channel];
319 }
320 break;
321 case hwmon_in:
322 switch (attr) {
323 case hwmon_in_input:
324 return TMP51X_BUS_VOLTAGE_RESULT;
325 case hwmon_in_lcrit_alarm:
326 case hwmon_in_crit_alarm:
327 return TMP51X_STATUS;
328 case hwmon_in_lcrit:
329 return TMP51X_BUS_VOLTAGE_L_LIMIT;
330 case hwmon_in_crit:
331 return TMP51X_BUS_VOLTAGE_H_LIMIT;
332 }
333 break;
334 case hwmon_curr:
335 switch (attr) {
336 case hwmon_curr_input:
337 return TMP51X_CURR_INPUT[channel];
338 case hwmon_curr_lcrit_alarm:
339 case hwmon_curr_crit_alarm:
340 return TMP51X_STATUS;
341 case hwmon_curr_lcrit:
342 return TMP51X_SHUNT_CURRENT_L_LIMIT;
343 case hwmon_curr_crit:
344 return TMP51X_SHUNT_CURRENT_H_LIMIT;
345 }
346 break;
347 case hwmon_power:
348 switch (attr) {
349 case hwmon_power_input:
350 return TMP51X_POWER_RESULT;
351 case hwmon_power_crit_alarm:
352 return TMP51X_STATUS;
353 case hwmon_power_crit:
354 return TMP51X_POWER_LIMIT;
355 }
356 break;
357 default:
358 break;
359 }
360
361 return 0;
362 }
363
tmp51x_get_status_pos(enum hwmon_sensor_types type,u32 attr,int channel)364 static u8 tmp51x_get_status_pos(enum hwmon_sensor_types type, u32 attr,
365 int channel)
366 {
367 switch (type) {
368 case hwmon_temp:
369 switch (attr) {
370 case hwmon_temp_crit_alarm:
371 return TMP51X_TEMP_CRIT_ALARM[channel];
372 }
373 break;
374 case hwmon_in:
375 switch (attr) {
376 case hwmon_in_lcrit_alarm:
377 return TMP51X_BUS_VOLTAGE_L_LIMIT_POS;
378 case hwmon_in_crit_alarm:
379 return TMP51X_BUS_VOLTAGE_H_LIMIT_POS;
380 }
381 break;
382 case hwmon_curr:
383 switch (attr) {
384 case hwmon_curr_lcrit_alarm:
385 return TMP51X_SHUNT_CURRENT_L_LIMIT_POS;
386 case hwmon_curr_crit_alarm:
387 return TMP51X_SHUNT_CURRENT_H_LIMIT_POS;
388 }
389 break;
390 case hwmon_power:
391 switch (attr) {
392 case hwmon_power_crit_alarm:
393 return TMP51X_POWER_LIMIT_POS;
394 }
395 break;
396 default:
397 break;
398 }
399
400 return 0;
401 }
402
tmp51x_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)403 static int tmp51x_read(struct device *dev, enum hwmon_sensor_types type,
404 u32 attr, int channel, long *val)
405 {
406 struct tmp51x_data *data = dev_get_drvdata(dev);
407 int ret;
408 u32 regval;
409 u8 pos = 0, reg = 0;
410
411 reg = tmp51x_get_reg(type, attr, channel);
412 if (reg == 0)
413 return -EOPNOTSUPP;
414
415 if (reg == TMP51X_STATUS)
416 pos = tmp51x_get_status_pos(type, attr, channel);
417
418 ret = regmap_read(data->regmap, reg, ®val);
419 if (ret < 0)
420 return ret;
421
422 return tmp51x_get_value(data, reg, pos, regval, val);
423 }
424
tmp51x_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)425 static int tmp51x_write(struct device *dev, enum hwmon_sensor_types type,
426 u32 attr, int channel, long val)
427 {
428 u8 reg = 0;
429
430 reg = tmp51x_get_reg(type, attr, channel);
431 if (reg == 0)
432 return -EOPNOTSUPP;
433
434 return tmp51x_set_value(dev_get_drvdata(dev), reg, val);
435 }
436
tmp51x_is_visible(const void * _data,enum hwmon_sensor_types type,u32 attr,int channel)437 static umode_t tmp51x_is_visible(const void *_data,
438 enum hwmon_sensor_types type, u32 attr,
439 int channel)
440 {
441 const struct tmp51x_data *data = _data;
442
443 switch (type) {
444 case hwmon_temp:
445 if (data->id == tmp512 && channel == 3)
446 return 0;
447 switch (attr) {
448 case hwmon_temp_input:
449 case hwmon_temp_crit_alarm:
450 return 0444;
451 case hwmon_temp_crit:
452 return 0644;
453 case hwmon_temp_crit_hyst:
454 if (channel == 0)
455 return 0644;
456 return 0444;
457 }
458 break;
459 case hwmon_in:
460 switch (attr) {
461 case hwmon_in_input:
462 case hwmon_in_lcrit_alarm:
463 case hwmon_in_crit_alarm:
464 return 0444;
465 case hwmon_in_lcrit:
466 case hwmon_in_crit:
467 return 0644;
468 }
469 break;
470 case hwmon_curr:
471 if (!data->shunt_uohms)
472 return 0;
473
474 switch (attr) {
475 case hwmon_curr_input:
476 case hwmon_curr_lcrit_alarm:
477 case hwmon_curr_crit_alarm:
478 return 0444;
479 case hwmon_curr_lcrit:
480 case hwmon_curr_crit:
481 return 0644;
482 }
483 break;
484 case hwmon_power:
485 if (!data->shunt_uohms)
486 return 0;
487
488 switch (attr) {
489 case hwmon_power_input:
490 case hwmon_power_crit_alarm:
491 return 0444;
492 case hwmon_power_crit:
493 return 0644;
494 }
495 break;
496 default:
497 break;
498 }
499 return 0;
500 }
501
502 static const struct hwmon_channel_info * const tmp51x_info[] = {
503 HWMON_CHANNEL_INFO(temp,
504 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
505 HWMON_T_CRIT_HYST,
506 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
507 HWMON_T_CRIT_HYST,
508 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
509 HWMON_T_CRIT_HYST,
510 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
511 HWMON_T_CRIT_HYST),
512 HWMON_CHANNEL_INFO(in,
513 HWMON_I_INPUT | HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM |
514 HWMON_I_CRIT | HWMON_I_CRIT_ALARM),
515 HWMON_CHANNEL_INFO(curr,
516 HWMON_C_INPUT | HWMON_C_LCRIT | HWMON_C_LCRIT_ALARM |
517 HWMON_C_CRIT | HWMON_C_CRIT_ALARM,
518 HWMON_C_INPUT),
519 HWMON_CHANNEL_INFO(power,
520 HWMON_P_INPUT | HWMON_P_CRIT | HWMON_P_CRIT_ALARM),
521 NULL
522 };
523
524 static const struct hwmon_ops tmp51x_hwmon_ops = {
525 .is_visible = tmp51x_is_visible,
526 .read = tmp51x_read,
527 .write = tmp51x_write,
528 };
529
530 static const struct hwmon_chip_info tmp51x_chip_info = {
531 .ops = &tmp51x_hwmon_ops,
532 .info = tmp51x_info,
533 };
534
535 /*
536 * Calibrate the tmp51x following the datasheet method
537 */
tmp51x_calibrate(struct tmp51x_data * data)538 static int tmp51x_calibrate(struct tmp51x_data *data)
539 {
540 int vshunt_max = data->pga_gain * 40;
541 u64 max_curr_ma;
542 u32 div;
543
544 /*
545 * If shunt_uohms is equal to 0, the calibration should be set to 0.
546 * The consequence will be that the current and power measurement engine
547 * of the sensor will not work. Temperature and voltage sensing will
548 * continue to work.
549 */
550 if (data->shunt_uohms == 0)
551 return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0);
552
553 max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * MICRO, data->shunt_uohms);
554
555 /*
556 * Calculate the minimal bit resolution for the current and the power.
557 * Those values will be used during register interpretation.
558 */
559 data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767);
560 data->pwr_lsb_uw = 20 * data->curr_lsb_ua;
561
562 div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO);
563
564 return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION,
565 DIV_ROUND_CLOSEST(40960, div));
566 }
567
568 /*
569 * Initialize the configuration and calibration registers.
570 */
tmp51x_init(struct tmp51x_data * data)571 static int tmp51x_init(struct tmp51x_data *data)
572 {
573 unsigned int regval;
574 int ret = regmap_write(data->regmap, TMP51X_SHUNT_CONFIG,
575 data->shunt_config);
576 if (ret < 0)
577 return ret;
578
579 ret = regmap_write(data->regmap, TMP51X_TEMP_CONFIG, data->temp_config);
580 if (ret < 0)
581 return ret;
582
583 // nFactor configuration
584 ret = regmap_update_bits(data->regmap, TMP51X_N_FACTOR_AND_HYST_1,
585 TMP51X_NFACTOR_MASK, data->nfactor[0] << 8);
586 if (ret < 0)
587 return ret;
588
589 ret = regmap_write(data->regmap, TMP51X_N_FACTOR_2,
590 data->nfactor[1] << 8);
591 if (ret < 0)
592 return ret;
593
594 if (data->id == tmp513) {
595 ret = regmap_write(data->regmap, TMP513_N_FACTOR_3,
596 data->nfactor[2] << 8);
597 if (ret < 0)
598 return ret;
599 }
600
601 ret = tmp51x_calibrate(data);
602 if (ret < 0)
603 return ret;
604
605 // Read the status register before using as the datasheet propose
606 return regmap_read(data->regmap, TMP51X_STATUS, ®val);
607 }
608
609 static const struct i2c_device_id tmp51x_id[] = {
610 { "tmp512", tmp512 },
611 { "tmp513", tmp513 },
612 { }
613 };
614 MODULE_DEVICE_TABLE(i2c, tmp51x_id);
615
616 static const struct of_device_id tmp51x_of_match[] = {
617 {
618 .compatible = "ti,tmp512",
619 .data = (void *)tmp512
620 },
621 {
622 .compatible = "ti,tmp513",
623 .data = (void *)tmp513
624 },
625 { },
626 };
627 MODULE_DEVICE_TABLE(of, tmp51x_of_match);
628
tmp51x_vbus_range_to_reg(struct device * dev,struct tmp51x_data * data)629 static int tmp51x_vbus_range_to_reg(struct device *dev,
630 struct tmp51x_data *data)
631 {
632 if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) {
633 data->shunt_config |= TMP51X_BUS_VOLTAGE_MASK;
634 } else if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_16V) {
635 data->shunt_config &= ~TMP51X_BUS_VOLTAGE_MASK;
636 } else {
637 return dev_err_probe(dev, -EINVAL,
638 "ti,bus-range-microvolt is invalid: %u\n",
639 data->vbus_range_uvolt);
640 }
641 return 0;
642 }
643
tmp51x_pga_gain_to_reg(struct device * dev,struct tmp51x_data * data)644 static int tmp51x_pga_gain_to_reg(struct device *dev, struct tmp51x_data *data)
645 {
646 if (data->pga_gain == 8) {
647 data->shunt_config |= CURRENT_SENSE_VOLTAGE_320_MASK;
648 } else if (data->pga_gain == 4) {
649 data->shunt_config |= CURRENT_SENSE_VOLTAGE_160_MASK;
650 } else if (data->pga_gain == 2) {
651 data->shunt_config |= CURRENT_SENSE_VOLTAGE_80_MASK;
652 } else if (data->pga_gain == 1) {
653 data->shunt_config |= CURRENT_SENSE_VOLTAGE_40_MASK;
654 } else {
655 return dev_err_probe(dev, -EINVAL,
656 "ti,pga-gain is invalid: %u\n", data->pga_gain);
657 }
658 return 0;
659 }
660
tmp51x_read_properties(struct device * dev,struct tmp51x_data * data)661 static int tmp51x_read_properties(struct device *dev, struct tmp51x_data *data)
662 {
663 int ret;
664 u32 nfactor[3];
665 u32 val;
666
667 ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", &val);
668 data->shunt_uohms = (ret >= 0) ? val : TMP51X_SHUNT_VALUE_DEFAULT;
669
670 ret = device_property_read_u32(dev, "ti,bus-range-microvolt", &val);
671 data->vbus_range_uvolt = (ret >= 0) ? val : TMP51X_VBUS_RANGE_DEFAULT;
672 ret = tmp51x_vbus_range_to_reg(dev, data);
673 if (ret < 0)
674 return ret;
675
676 ret = device_property_read_u32(dev, "ti,pga-gain", &val);
677 data->pga_gain = (ret >= 0) ? val : TMP51X_PGA_DEFAULT;
678 ret = tmp51x_pga_gain_to_reg(dev, data);
679 if (ret < 0)
680 return ret;
681
682 ret = device_property_read_u32_array(dev, "ti,nfactor", nfactor,
683 (data->id == tmp513) ? 3 : 2);
684 if (ret >= 0)
685 memcpy(data->nfactor, nfactor, (data->id == tmp513) ? 3 : 2);
686
687 // Check if shunt value is compatible with pga-gain
688 if (data->shunt_uohms > data->pga_gain * 40 * MICRO) {
689 return dev_err_probe(dev, -EINVAL,
690 "shunt-resistor: %u too big for pga_gain: %u\n",
691 data->shunt_uohms, data->pga_gain);
692 }
693
694 return 0;
695 }
696
tmp51x_use_default(struct tmp51x_data * data)697 static void tmp51x_use_default(struct tmp51x_data *data)
698 {
699 data->vbus_range_uvolt = TMP51X_VBUS_RANGE_DEFAULT;
700 data->pga_gain = TMP51X_PGA_DEFAULT;
701 data->shunt_uohms = TMP51X_SHUNT_VALUE_DEFAULT;
702 }
703
tmp51x_configure(struct device * dev,struct tmp51x_data * data)704 static int tmp51x_configure(struct device *dev, struct tmp51x_data *data)
705 {
706 data->shunt_config = TMP51X_SHUNT_CONFIG_DEFAULT;
707 data->temp_config = (data->id == tmp513) ?
708 TMP513_TEMP_CONFIG_DEFAULT : TMP512_TEMP_CONFIG_DEFAULT;
709
710 if (dev->of_node)
711 return tmp51x_read_properties(dev, data);
712
713 tmp51x_use_default(data);
714
715 return 0;
716 }
717
tmp51x_probe(struct i2c_client * client)718 static int tmp51x_probe(struct i2c_client *client)
719 {
720 struct device *dev = &client->dev;
721 struct tmp51x_data *data;
722 struct device *hwmon_dev;
723 int ret;
724
725 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
726 if (!data)
727 return -ENOMEM;
728
729 data->id = (uintptr_t)i2c_get_match_data(client);
730
731 ret = tmp51x_configure(dev, data);
732 if (ret < 0)
733 return dev_err_probe(dev, ret, "error configuring the device\n");
734
735 data->regmap = devm_regmap_init_i2c(client, &tmp51x_regmap_config);
736 if (IS_ERR(data->regmap))
737 return dev_err_probe(dev, PTR_ERR(data->regmap),
738 "failed to allocate register map\n");
739
740 ret = tmp51x_init(data);
741 if (ret < 0)
742 return dev_err_probe(dev, ret, "error configuring the device\n");
743
744 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
745 data,
746 &tmp51x_chip_info,
747 NULL);
748 if (IS_ERR(hwmon_dev))
749 return PTR_ERR(hwmon_dev);
750
751 dev_dbg(dev, "power monitor %s\n", client->name);
752
753 return 0;
754 }
755
756 static struct i2c_driver tmp51x_driver = {
757 .driver = {
758 .name = "tmp51x",
759 .of_match_table = tmp51x_of_match,
760 },
761 .probe = tmp51x_probe,
762 .id_table = tmp51x_id,
763 };
764
765 module_i2c_driver(tmp51x_driver);
766
767 MODULE_AUTHOR("Eric Tremblay <etremblay@distechcontrols.com>");
768 MODULE_DESCRIPTION("tmp51x driver");
769 MODULE_LICENSE("GPL");
770