All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v7 0/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips.
@ 2019-11-11 20:34 etremblay
  2019-11-11 20:34 ` [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513 etremblay
  2019-11-11 20:34 ` [PATCH v7 2/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips etremblay
  0 siblings, 2 replies; 8+ messages in thread
From: etremblay @ 2019-11-11 20:34 UTC (permalink / raw)
  To: linux, linux-hwmon, devicetree, linux-doc
  Cc: jdelvare, robh+dt, mark.rutland, corbet

From: Eric Tremblay <etremblay@distech-controls.com>

Version seven of the driver for Texas Instruments TMP512/513 sensors.

Thanks for the review

Main changes from version 6:
	- Add tmp513 to index.rst
	- Use microvolt instead of volt for bus voltage range
	- Fix current value interpretation
	- Wrap dt-bindings lines to 80 characters

Main changes from version 5:
	- Use shunt-resistor-micro-ohms standard property
	- Fix alignment between 'enum' and 'default' in dt-bindings

Main changes from version 4:
	- Remove config_lock mutex
	- Rewrite dt-bindings in yaml

Main changes from version 3:
	- Remove tmp51x_config structure, there was no useful static
	  field in the structure. The data was moved to tmp51x_data
	  structure.
	- Remove platform data
	- use device_ API instead of of_device API
	- Use array for n-Factor values.
	- Set shunt default value to 1000 uOhms

Main changes from version 2:
	- Remove the neccessity to provide a shunt-resistor value
	- Remove max-expected-current-ma configuration and always
	  use max possible current for calibration
	- Make sure calculation during calibration can't overflow
	- Add value clamping for DT value and runtime parameters
	- Support non DT system
	- Move hysteresis from DT to standard attribute

Main changes from version 1:
	- Use the with_info API instead of sysfs attributes.
	- Remove non-standard attributes and raw value.
	- Move settings that were non-standard attributes to
	  device tree, update documentation as well.
	- Fix coding style issues

Eric Tremblay (2):
  dt-bindings: hwmon: Add TMP512/513
  hwmon: Add driver for Texas Instruments TMP512/513 sensor chips.

 .../devicetree/bindings/hwmon/ti,tmp513.yaml  |  89 ++
 Documentation/hwmon/index.rst                 |   1 +
 Documentation/hwmon/tmp513.rst                | 104 +++
 MAINTAINERS                                   |   7 +
 drivers/hwmon/Kconfig                         |  10 +
 drivers/hwmon/Makefile                        |   1 +
 drivers/hwmon/tmp513.c                        | 789 ++++++++++++++++++
 7 files changed, 1001 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
 create mode 100644 Documentation/hwmon/tmp513.rst
 create mode 100644 drivers/hwmon/tmp513.c

-- 
2.17.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513
  2019-11-11 20:34 [PATCH v7 0/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips etremblay
@ 2019-11-11 20:34 ` etremblay
  2019-11-12  1:03   ` Rob Herring
  2019-11-12  1:45   ` Guenter Roeck
  2019-11-11 20:34 ` [PATCH v7 2/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips etremblay
  1 sibling, 2 replies; 8+ messages in thread
From: etremblay @ 2019-11-11 20:34 UTC (permalink / raw)
  To: linux, linux-hwmon, devicetree, linux-doc
  Cc: jdelvare, robh+dt, mark.rutland, corbet

From: Eric Tremblay <etremblay@distech-controls.com>

Document the TMP513/512 device devicetree bindings

Signed-off-by: Eric Tremblay <etremblay@distech-controls.com>
---
 .../devicetree/bindings/hwmon/ti,tmp513.yaml  | 89 +++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml

diff --git a/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
new file mode 100644
index 000000000000..de4ed3645e0f
--- /dev/null
+++ b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
@@ -0,0 +1,89 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+
+$id: http://devicetree.org/schemas/hwmon/ti,tmp513.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: TMP513/512 system monitor sensor
+
+maintainers:
+  - Eric Tremblay <etremblay@distech-controls.com>
+
+description: |
+  This driver implements support for Texas Instruments TMP512, and TMP513.
+  The TMP512 (dual-channel) and TMP513 (triple-channel) are system monitors
+  that include remote sensors, a local temperature sensor, and a high-side
+  current shunt monitor. These system monitors have the capability of measuring
+  remote temperatures, on-chip temperatures, and system voltage/power/current
+  consumption.
+
+  Datasheets:
+  http://www.ti.com/lit/gpn/tmp513
+  http://www.ti.com/lit/gpn/tmp512
+
+
+properties:
+  compatible:
+    enum:
+      - ti,tmp512
+      - ti,tmp513
+
+  reg:
+    maxItems: 1
+
+  shunt-resistor-micro-ohms:
+    description: |
+      If 0, the calibration process will be skiped and the current and power
+      measurement engine will not work. Temperature and voltage measurement
+      will continue to work. The shunt value also need to respect:
+      rshunt <= pga-gain * 40 * 1000 * 1000.
+      If not, it's not possible to compute a valid calibration value.
+    default: 1000
+
+  ti,pga-gain:
+    description: |
+      The gain value for the PGA function. This is 8, 4, 2 or 1.
+      The PGA gain affect the shunt voltage range.
+      The range will be equal to: pga-gain * 40mV
+    allOf:
+      - $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [1, 2, 4, 8]
+    default: 8
+
+  ti,bus-range-microvolt:
+    description: |
+      This is the operating range of the bus voltage in microvolt
+    allOf:
+      - $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [16000000, 32000000]
+    default: 32000000
+
+  ti,nfactor:
+    description: |
+      Array of three(TMP513) or two(TMP512) n-Factor value for each remote
+      temperature channel.
+      See datasheet Table 11 for n-Factor range list and value interpretation.
+    allOf:
+      - $ref: /schemas/types.yaml#definitions/uint8-array
+      - minItems: 2
+        maxItems: 3
+        items:
+          default: 0
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    i2c {
+        tmp513@5c {
+            compatible = "ti,tmp513";
+            reg = <0x5C>;
+            shunt-resistor-micro-ohms = <330000>;
+            ti,bus-range-microvolt = <32000000>;
+            ti,pga-gain = <8>;
+            ti,nfactor = [01 F3 00];
+        };
+    };
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v7 2/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips.
  2019-11-11 20:34 [PATCH v7 0/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips etremblay
  2019-11-11 20:34 ` [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513 etremblay
@ 2019-11-11 20:34 ` etremblay
  2019-11-12 13:41   ` Guenter Roeck
  1 sibling, 1 reply; 8+ messages in thread
From: etremblay @ 2019-11-11 20:34 UTC (permalink / raw)
  To: linux, linux-hwmon, devicetree, linux-doc
  Cc: jdelvare, robh+dt, mark.rutland, corbet

From: Eric Tremblay <etremblay@distech-controls.com>

TI's TMP512/513 are I2C/SMBus system monitor chips. These chips
monitor the supply voltage, supply current, power consumption
and provide one local and up to three (TMP513) remote temperature sensors.

It has been tested using a TI TMP513 development kit (TMP513EVM)

Signed-off-by: Eric Tremblay <etremblay@distech-controls.com>
---
 Documentation/hwmon/index.rst  |   1 +
 Documentation/hwmon/tmp513.rst | 104 +++++
 MAINTAINERS                    |   7 +
 drivers/hwmon/Kconfig          |  10 +
 drivers/hwmon/Makefile         |   1 +
 drivers/hwmon/tmp513.c         | 789 +++++++++++++++++++++++++++++++++
 6 files changed, 912 insertions(+)
 create mode 100644 Documentation/hwmon/tmp513.rst
 create mode 100644 drivers/hwmon/tmp513.c

diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst
index 230ad59b462b..d708a371fce1 100644
--- a/Documentation/hwmon/index.rst
+++ b/Documentation/hwmon/index.rst
@@ -153,6 +153,7 @@ Hardware Monitoring Kernel Drivers
    tmp108
    tmp401
    tmp421
+   tmp513
    tps40422
    twl4030-madc-hwmon
    ucd9000
diff --git a/Documentation/hwmon/tmp513.rst b/Documentation/hwmon/tmp513.rst
new file mode 100644
index 000000000000..d41a207e96e6
--- /dev/null
+++ b/Documentation/hwmon/tmp513.rst
@@ -0,0 +1,104 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Kernel driver tmp513
+====================
+
+Supported chips:
+
+  * Texas Instruments TMP512
+
+    Prefix: 'tmp512'
+
+    Datasheet: http://www.ti.com/lit/ds/symlink/tmp512.pdf
+
+  * Texas Instruments TMP513
+
+    Prefix: 'tmp513'
+
+    Datasheet: http://www.ti.com/lit/ds/symlink/tmp513.pdf
+
+Authors:
+
+	Eric Tremblay <etremblay@distech-controls.com>
+
+Description
+-----------
+
+This driver implements support for Texas Instruments TMP512, and TMP513.
+The TMP512 (dual-channel) and TMP513 (triple-channel) are system monitors
+that include remote sensors, a local temperature sensor, and a high-side current
+shunt monitor. These system monitors have the capability of measuring remote
+temperatures, on-chip temperatures, and system voltage/power/current
+consumption.
+
+The temperatures are measured in degrees Celsius with a range of
+-40 to + 125 degrees with a resolution of 0.0625 degree C.
+
+For hysteresis value, only the first channel is writable. Writing to it
+will affect all other values since each channels are sharing the same
+hysteresis value. The hysteresis is in degrees Celsius with a range of
+0 to 127.5 degrees with a resolution of 0.5 degree.
+
+The driver exports the temperature values via the following sysfs files:
+
+**temp[1-4]_input**
+
+**temp[1-4]_crit**
+
+**temp[1-4]_crit_alarm**
+
+**temp[1-4]_crit_hyst**
+
+The shunt voltage is measured in micro volts. There are four configurable
+range: 320mV, 160mV, 80mV and 40mV all of them with a resolution of 10 uV.
+The range depends on the pga gain specified in the device tree
+with "pga-gain" (default to 8). The range will be equal to pga gain
+multiply by 40mV.
+
+The driver exports the shunt voltage values via the following sysFs files:
+
+**in0_input**
+
+**in0_lcrit**
+
+**in0_lcrit_alarm**
+
+**in0_crit**
+
+**in0_crit_alarm**
+
+The bus voltage is measured in milli volts. There are two configuable
+range: 32V and 16V both with a resolution of 4mV. It can be configured in the
+device tree with "bus-voltage-range" (default to 32V);
+
+The driver exports the bus voltage values via the following sysFs files:
+
+**in0_input**
+
+**in0_lcrit**
+
+**in0_lcrit_alarm**
+
+**in0_crit**
+
+**in0_crit_alarm**
+
+The power and the currents range and resolution depends on the calibration
+register value. Those values are calculate by the hardware using those
+formula:
+
+Current = (ShuntVoltage * CalibrationRegister) / 4096
+Power   = (Current * BusVoltage) / 5000
+
+The driver exports the current and power values via the following sysFs files:
+
+**curr0_input**
+
+**power1_input**
+
+**power1_crit**
+
+**power1_crit_alarm**
+
+The calibration process follow the procedure of the datasheet (without overflow)
+and depend on the shunt resistor value and the pga_gain value.
diff --git a/MAINTAINERS b/MAINTAINERS
index eb19fad370d7..75db98a0913c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -16378,6 +16378,13 @@ S:	Maintained
 F:	Documentation/hwmon/tmp401.rst
 F:	drivers/hwmon/tmp401.c
 
+TMP513 HARDWARE MONITOR DRIVER
+M:	Eric Tremblay <etremblay@distech-controls.com>
+L:	linux-hwmon@vger.kernel.org
+S:	Maintained
+F:	Documentation/hwmon/tmp513.rst
+F:	drivers/hwmon/tmp513.c
+
 TMPFS (SHMEM FILESYSTEM)
 M:	Hugh Dickins <hughd@google.com>
 L:	linux-mm@kvack.org
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 13a6b4afb4b3..926e3c98dbdf 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -1709,6 +1709,16 @@ config SENSORS_TMP421
 	  This driver can also be built as a module. If so, the module
 	  will be called tmp421.
 
+config SENSORS_TMP513
+	tristate "Texas Instruments TMP513 and compatibles"
+	depends on I2C
+	help
+	  If you say yes here you get support for Texas Instruments TMP512,
+	  and TMP513 temperature and power supply sensor chips.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called tmp513.
+
 config SENSORS_VEXPRESS
 	tristate "Versatile Express"
 	depends on VEXPRESS_CONFIG
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index 40c036ea45e6..762642291914 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -166,6 +166,7 @@ obj-$(CONFIG_SENSORS_TMP103)	+= tmp103.o
 obj-$(CONFIG_SENSORS_TMP108)	+= tmp108.o
 obj-$(CONFIG_SENSORS_TMP401)	+= tmp401.o
 obj-$(CONFIG_SENSORS_TMP421)	+= tmp421.o
+obj-$(CONFIG_SENSORS_TMP513)	+= tmp513.o
 obj-$(CONFIG_SENSORS_VEXPRESS)	+= vexpress-hwmon.o
 obj-$(CONFIG_SENSORS_VIA_CPUTEMP)+= via-cputemp.o
 obj-$(CONFIG_SENSORS_VIA686A)	+= via686a.o
diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
new file mode 100644
index 000000000000..098ce2b0e253
--- /dev/null
+++ b/drivers/hwmon/tmp513.c
@@ -0,0 +1,789 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Texas Instruments TMP512, TMP513 power monitor chips
+ *
+ * TMP513:
+ * Thermal/Power Management with Triple Remote and
+ * Local Temperature Sensor and Current Shunt Monitor
+ * Datasheet: http://www.ti.com/lit/gpn/tmp513
+ *
+ * TMP512:
+ * Thermal/Power Management with Dual Remote
+ *	and Local Temperature Sensor and Current Shunt Monitor
+ * Datasheet: http://www.ti.com/lit/gpn/tmp512
+ *
+ * Copyright (C) 2019 Eric Tremblay <etremblay@distech-controls.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ */
+
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/util_macros.h>
+
+// Common register definition
+#define TMP51X_SHUNT_CONFIG		0x00
+#define TMP51X_TEMP_CONFIG		0x01
+#define TMP51X_STATUS			0x02
+#define TMP51X_SMBUS_ALERT		0x03
+#define TMP51X_SHUNT_VOLTAGE_RESULT	0x04
+#define TMP51X_BUS_VOLTAGE_RESULT	0x05
+#define TMP51X_POWER_RESULT		0x06
+#define TMP51X_SHUNT_CURRENT_RESULT	0x07
+#define TMP51X_LOCAL_TEMP_RESULT	0x08
+#define TMP51X_REMOTE_TEMP_RESULT_1	0x09
+#define TMP51X_REMOTE_TEMP_RESULT_2	0x0A
+#define TMP51X_SHUNT_VOLTAGE_H_LIMIT	0x0C
+#define TMP51X_SHUNT_VOLTAGE_L_LIMIT	0x0D
+#define TMP51X_BUS_VOLTAGE_H_LIMIT	0x0E
+#define TMP51X_BUS_VOLTAGE_L_LIMIT	0x0F
+#define TMP51X_POWER_LIMIT		0x10
+#define TMP51X_LOCAL_TEMP_LIMIT	0x11
+#define TMP51X_REMOTE_TEMP_LIMIT_1	0x12
+#define TMP51X_REMOTE_TEMP_LIMIT_2	0x13
+#define TMP51X_SHUNT_CALIBRATION	0x15
+#define TMP51X_N_FACTOR_AND_HYST_1	0x16
+#define TMP51X_N_FACTOR_2		0x17
+#define TMP51X_MAN_ID_REG		0xFE
+#define TMP51X_DEVICE_ID_REG		0xFF
+
+// TMP513 specific register definition
+#define TMP513_REMOTE_TEMP_RESULT_3	0x0B
+#define TMP513_REMOTE_TEMP_LIMIT_3	0x14
+#define TMP513_N_FACTOR_3		0x18
+
+// Common attrs, and NULL
+#define TMP51X_MANUFACTURER_ID		0x55FF
+
+#define TMP512_DEVICE_ID		0x22FF
+#define TMP513_DEVICE_ID		0x23FF
+
+// Default config
+#define TMP51X_SHUNT_CONFIG_DEFAULT	0x399F
+#define TMP51X_SHUNT_VALUE_DEFAULT	1000
+#define TMP51X_VBUS_MAX_DEFAULT		TMP51X_VBUS_MAX_32V
+#define TMP51X_PGA_DEFAULT		8
+#define TMP51X_MAX_REGISTER_ADDR	0xFF
+
+#define TMP512_TEMP_CONFIG_DEFAULT	0xBF80
+#define TMP513_TEMP_CONFIG_DEFAULT	0xFF80
+
+// Mask and shift
+#define CURRENT_SENSE_VOLTAGE_320_MASK	0x1800
+#define CURRENT_SENSE_VOLTAGE_160_MASK	0x1000
+#define CURRENT_SENSE_VOLTAGE_80_MASK	0x0800
+#define CURRENT_SENSE_VOLTAGE_40_MASK	0
+
+#define TMP51X_BUS_VOLTAGE_MASK		0x2000
+#define TMP51X_PGA_MASK			0x1800
+#define TMP51X_NFACTOR_MASK		0xFF00
+#define TMP51X_HYST_MASK		0x00FF
+
+#define TMP51X_BUS_VOLTAGE_SHIFT	3
+#define TMP51X_TEMP_SHIFT		3
+
+// Alarms
+#define TMP51X_SHUNT_VOLTAGE_H_LIMIT_POS	15
+#define TMP51X_SHUNT_VOLTAGE_L_LIMIT_POS	14
+#define TMP51X_BUS_VOLTAGE_H_LIMIT_POS		13
+#define TMP51X_BUS_VOLTAGE_L_LIMIT_POS		12
+#define TMP51X_POWER_LIMIT_POS			11
+#define TMP51X_LOCAL_TEMP_LIMIT_POS		10
+#define TMP51X_REMOTE_TEMP_LIMIT_1_POS		9
+#define TMP51X_REMOTE_TEMP_LIMIT_2_POS		8
+#define TMP513_REMOTE_TEMP_LIMIT_3_POS		7
+
+#define TMP51X_VBUS_MAX_32V		32000000
+#define TMP51X_VBUS_MAX_16V		16000000
+
+// Max and Min value
+#define MAX_BUS_VOLTAGE_32_LIMIT	32764
+#define MAX_BUS_VOLTAGE_16_LIMIT	16382
+
+// Max possible value is -256 to +256 but datasheet indicated -40 to 125.
+#define MAX_TEMP_LIMIT			125000
+#define MIN_TEMP_LIMIT			-40000
+
+#define MAX_NFACTOR			127
+#define MIN_NFACTOR			-128
+#define MAX_TEMP_HYST			127500
+
+static const u8 TMP51X_TEMP_INPUT[4] = {
+	TMP51X_LOCAL_TEMP_RESULT,
+	TMP51X_REMOTE_TEMP_RESULT_1,
+	TMP51X_REMOTE_TEMP_RESULT_2,
+	TMP513_REMOTE_TEMP_RESULT_3
+};
+
+static const u8 TMP51X_TEMP_CRIT[4] = {
+	TMP51X_LOCAL_TEMP_LIMIT,
+	TMP51X_REMOTE_TEMP_LIMIT_1,
+	TMP51X_REMOTE_TEMP_LIMIT_2,
+	TMP513_REMOTE_TEMP_LIMIT_3
+};
+
+static const u8 TMP51X_TEMP_CRIT_ALARM[4] = {
+	TMP51X_LOCAL_TEMP_LIMIT_POS,
+	TMP51X_REMOTE_TEMP_LIMIT_1_POS,
+	TMP51X_REMOTE_TEMP_LIMIT_2_POS,
+	TMP513_REMOTE_TEMP_LIMIT_3_POS
+};
+
+static const u8 TMP51X_TEMP_CRIT_HYST[4] = {
+	TMP51X_N_FACTOR_AND_HYST_1,
+	TMP51X_N_FACTOR_AND_HYST_1,
+	TMP51X_N_FACTOR_AND_HYST_1,
+	TMP51X_N_FACTOR_AND_HYST_1
+};
+
+static const u8 TMP51X_IN_INPUT[2] = {
+	TMP51X_SHUNT_VOLTAGE_RESULT,
+	TMP51X_BUS_VOLTAGE_RESULT
+};
+
+static const u8 TMP51X_IN_CRIT[2] = {
+	TMP51X_SHUNT_VOLTAGE_H_LIMIT,
+	TMP51X_BUS_VOLTAGE_H_LIMIT
+};
+
+static const u8 TMP51X_IN_LCRIT[2] = {
+	TMP51X_SHUNT_VOLTAGE_L_LIMIT,
+	TMP51X_BUS_VOLTAGE_L_LIMIT
+};
+
+static const u8 TMP51X_IN_CRIT_ALARM[2] = {
+	TMP51X_SHUNT_VOLTAGE_H_LIMIT_POS,
+	TMP51X_BUS_VOLTAGE_H_LIMIT_POS
+};
+
+static const u8 TMP51X_IN_LCRIT_ALARM[2] = {
+	TMP51X_SHUNT_VOLTAGE_L_LIMIT_POS,
+	TMP51X_BUS_VOLTAGE_L_LIMIT_POS
+};
+
+static const u8 TMP51X_CURR_INPUT[1] = {
+	TMP51X_SHUNT_CURRENT_RESULT
+};
+
+static const u8 TMP51X_POWER_INPUT[1] = {
+	TMP51X_POWER_RESULT
+};
+
+static const u8 TMP51X_POWER_CRIT[1] = {
+	TMP51X_POWER_LIMIT
+};
+
+static const u8 TMP51X_POWER_CRIT_ALARM[1] = {
+	TMP51X_POWER_LIMIT_POS
+};
+
+static struct regmap_config tmp51x_regmap_config = {
+	.reg_bits = 8,
+	.val_bits = 16,
+	.max_register = TMP51X_MAX_REGISTER_ADDR,
+};
+
+enum tmp51x_ids {
+	tmp512, tmp513
+};
+
+struct tmp51x_data {
+	u16 shunt_config;
+	u16 pga_gain;
+	u32 vbus_max_uvolt;
+
+	u16 temp_config;
+	u8 nfactor[3];
+
+	u32 shunt_uohms;
+
+	u32 curr_lsb_ua;
+	u32 pwr_lsb_uw;
+
+	enum tmp51x_ids id;
+	struct regmap *regmap;
+};
+
+// Set the shift based on the gain 8=4, 4=3, 2=2, 1=1
+static inline u8 tmp51x_get_pga_shift(struct tmp51x_data *data)
+{
+	return 5 - ffs(data->pga_gain);
+}
+
+static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
+			    unsigned int regval, long *val)
+{
+	switch (reg) {
+	case TMP51X_STATUS:
+		*val = (regval >> pos) & 1;
+		break;
+	case TMP51X_SHUNT_VOLTAGE_RESULT:
+	case TMP51X_SHUNT_VOLTAGE_H_LIMIT:
+	case TMP51X_SHUNT_VOLTAGE_L_LIMIT:
+		/*
+		 * 2's compliment number shifted by one to four depending
+		 * on the pga gain setting.
+		 * 1lsb = 10uV
+		 */
+		*val = sign_extend32(regval, 17 - tmp51x_get_pga_shift(data));
+		*val = DIV_ROUND_CLOSEST(*val, 100);
+		break;
+	case TMP51X_BUS_VOLTAGE_RESULT:
+	case TMP51X_BUS_VOLTAGE_H_LIMIT:
+	case TMP51X_BUS_VOLTAGE_L_LIMIT:
+		// 1lsb = 4mV
+		*val = (regval >> TMP51X_BUS_VOLTAGE_SHIFT) * 4;
+		break;
+	case TMP51X_POWER_RESULT:
+	case TMP51X_POWER_LIMIT:
+		// Power = (current * BusVoltage) / 5000
+		*val = regval * data->pwr_lsb_uw;
+		break;
+	case TMP51X_SHUNT_CURRENT_RESULT:
+		// Current = (ShuntVoltage * CalibrationRegister) / 4096
+		*val = sign_extend32(regval, 16) * data->curr_lsb_ua;
+		*val = DIV_ROUND_CLOSEST(*val, 1000);
+		break;
+	case TMP51X_LOCAL_TEMP_RESULT:
+	case TMP51X_REMOTE_TEMP_RESULT_1:
+	case TMP51X_REMOTE_TEMP_RESULT_2:
+	case TMP513_REMOTE_TEMP_RESULT_3:
+	case TMP51X_LOCAL_TEMP_LIMIT:
+	case TMP51X_REMOTE_TEMP_LIMIT_1:
+	case TMP51X_REMOTE_TEMP_LIMIT_2:
+	case TMP513_REMOTE_TEMP_LIMIT_3:
+		// 1lsb = 0.0625 degrees centigrade
+		*val = sign_extend32(regval, 16) >> TMP51X_TEMP_SHIFT;
+		*val = DIV_ROUND_CLOSEST(*val * 625, 10);
+		break;
+	case TMP51X_N_FACTOR_AND_HYST_1:
+		// 1lsb = 0.5 degrees centigrade
+		*val = (regval & TMP51X_HYST_MASK) * 500;
+		break;
+	default:
+		// Programmer goofed
+		WARN_ON_ONCE(1);
+		*val = 0;
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
+static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val)
+{
+	int regval, max_val;
+	u32 mask = 0;
+
+	switch (reg) {
+	case TMP51X_SHUNT_VOLTAGE_H_LIMIT:
+	case TMP51X_SHUNT_VOLTAGE_L_LIMIT:
+		// 1lsb = 10uV
+		val *= 100;
+		max_val = U16_MAX >> tmp51x_get_pga_shift(data);
+		regval = clamp_val(val, -max_val, max_val);
+		break;
+	case TMP51X_BUS_VOLTAGE_H_LIMIT:
+	case TMP51X_BUS_VOLTAGE_L_LIMIT:
+		// 1lsb = 4mV
+		max_val = (data->vbus_max_uvolt == TMP51X_VBUS_MAX_32V) ?
+			MAX_BUS_VOLTAGE_32_LIMIT : MAX_BUS_VOLTAGE_16_LIMIT;
+
+		val = clamp_val(DIV_ROUND_CLOSEST(val, 4), 0, max_val);
+		regval = val << TMP51X_BUS_VOLTAGE_SHIFT;
+		break;
+	case TMP51X_POWER_LIMIT:
+		regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0,
+				   U16_MAX);
+		break;
+	case TMP51X_LOCAL_TEMP_LIMIT:
+	case TMP51X_REMOTE_TEMP_LIMIT_1:
+	case TMP51X_REMOTE_TEMP_LIMIT_2:
+	case TMP513_REMOTE_TEMP_LIMIT_3:
+		// 1lsb = 0.0625 degrees centigrade
+		val = clamp_val(val, MIN_TEMP_LIMIT, MAX_TEMP_LIMIT);
+		regval = DIV_ROUND_CLOSEST(val * 10, 625) << TMP51X_TEMP_SHIFT;
+		break;
+	case TMP51X_N_FACTOR_AND_HYST_1:
+		// 1lsb = 0.5 degrees centigrade
+		val = clamp_val(val, 0, MAX_TEMP_HYST);
+		regval = DIV_ROUND_CLOSEST(val, 500);
+		mask = TMP51X_HYST_MASK;
+		break;
+	default:
+		// Programmer goofed
+		WARN_ON_ONCE(1);
+		return -EOPNOTSUPP;
+	}
+
+	if (mask == 0)
+		return regmap_write(data->regmap, reg, regval);
+	else
+		return regmap_update_bits(data->regmap, reg, mask, regval);
+}
+
+static int tmp51x_read(struct device *dev, enum hwmon_sensor_types type,
+		       u32 attr, int channel, long *val)
+{
+	struct tmp51x_data *data = dev_get_drvdata(dev);
+	int ret;
+	u32 regval;
+	u8 pos = 0, reg = 0;
+
+	switch (type) {
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_input:
+			reg = TMP51X_TEMP_INPUT[channel];
+			break;
+		case hwmon_temp_crit_alarm:
+			reg = TMP51X_STATUS;
+			pos = TMP51X_TEMP_CRIT_ALARM[channel];
+			break;
+		case hwmon_temp_crit:
+			reg = TMP51X_TEMP_CRIT[channel];
+			break;
+		case hwmon_temp_crit_hyst:
+			reg = TMP51X_TEMP_CRIT_HYST[channel];
+			break;
+		}
+		break;
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+			reg = TMP51X_IN_INPUT[channel];
+			break;
+		case hwmon_in_lcrit_alarm:
+			reg = TMP51X_STATUS;
+			pos = TMP51X_IN_LCRIT_ALARM[channel];
+			break;
+		case hwmon_in_crit_alarm:
+			reg = TMP51X_STATUS;
+			pos = TMP51X_IN_CRIT_ALARM[channel];
+			break;
+		case hwmon_in_lcrit:
+			reg = TMP51X_IN_LCRIT[channel];
+			break;
+		case hwmon_in_crit:
+			reg = TMP51X_IN_CRIT[channel];
+			break;
+		}
+		break;
+	case hwmon_curr:
+		switch (attr) {
+		case hwmon_curr_input:
+			reg = TMP51X_CURR_INPUT[channel];
+			break;
+		}
+		break;
+	case hwmon_power:
+		switch (attr) {
+		case hwmon_power_input:
+			reg = TMP51X_POWER_INPUT[channel];
+			break;
+		case hwmon_power_crit_alarm:
+			reg = TMP51X_STATUS;
+			pos = TMP51X_POWER_CRIT_ALARM[channel];
+			break;
+		case hwmon_power_crit:
+			reg = TMP51X_POWER_CRIT[channel];
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+
+	if (reg == 0)
+		return -EOPNOTSUPP;
+
+	ret = regmap_read(data->regmap, reg, &regval);
+	if (ret < 0)
+		return ret;
+
+	return tmp51x_get_value(data, reg, pos, regval, val);
+}
+
+static int tmp51x_write(struct device *dev, enum hwmon_sensor_types type,
+			u32 attr, int channel, long val)
+{
+	u8 reg = 0;
+
+	switch (type) {
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_crit:
+			reg = TMP51X_TEMP_CRIT[channel];
+			break;
+		case hwmon_temp_crit_hyst:
+			reg = TMP51X_TEMP_CRIT_HYST[channel];
+			break;
+		}
+		break;
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_lcrit:
+			reg = TMP51X_IN_LCRIT[channel];
+			break;
+		case hwmon_in_crit:
+			reg = TMP51X_IN_CRIT[channel];
+			break;
+		}
+		break;
+	case hwmon_power:
+		switch (attr) {
+		case hwmon_power_crit:
+			reg = TMP51X_POWER_CRIT[channel];
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+
+	if (reg == 0)
+		return -EOPNOTSUPP;
+
+	return tmp51x_set_value(dev_get_drvdata(dev), reg, val);
+}
+
+static umode_t tmp51x_is_visible(const void *_data,
+				 enum hwmon_sensor_types type, u32 attr,
+				 int channel)
+{
+	const struct tmp51x_data *data = _data;
+
+	switch (type) {
+	case hwmon_temp:
+		if (data->id == tmp512 && channel == 4)
+			return 0;
+		switch (attr) {
+		case hwmon_temp_input:
+		case hwmon_temp_crit_alarm:
+			return 0444;
+		case hwmon_temp_crit:
+			return 0644;
+		case hwmon_temp_crit_hyst:
+			if (channel == 0)
+				return 0644;
+			return 0444;
+		}
+		break;
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+		case hwmon_in_lcrit_alarm:
+		case hwmon_in_crit_alarm:
+			return 0444;
+		case hwmon_in_lcrit:
+		case hwmon_in_crit:
+			return 0644;
+		}
+		break;
+	case hwmon_curr:
+		switch (attr) {
+		case hwmon_curr_input:
+			return 0444;
+		}
+		break;
+	case hwmon_power:
+		switch (attr) {
+		case hwmon_power_input:
+		case hwmon_power_crit_alarm:
+			return 0444;
+		case hwmon_power_crit:
+			return 0644;
+		}
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
+static const struct hwmon_channel_info *tmp51x_info[] = {
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
+			   HWMON_T_CRIT_HYST,
+			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
+			   HWMON_T_CRIT_HYST,
+			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
+			   HWMON_T_CRIT_HYST,
+			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
+			   HWMON_T_CRIT_HYST),
+	HWMON_CHANNEL_INFO(in,
+			   HWMON_I_INPUT | HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM |
+			   HWMON_I_CRIT | HWMON_I_CRIT_ALARM,
+			   HWMON_I_INPUT | HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM |
+			   HWMON_I_CRIT | HWMON_I_CRIT_ALARM),
+	HWMON_CHANNEL_INFO(curr,
+			   HWMON_C_INPUT),
+	HWMON_CHANNEL_INFO(power,
+			   HWMON_P_INPUT | HWMON_P_CRIT | HWMON_P_CRIT_ALARM),
+	NULL
+};
+
+static const struct hwmon_ops tmp51x_hwmon_ops = {
+	.is_visible = tmp51x_is_visible,
+	.read = tmp51x_read,
+	.write = tmp51x_write,
+};
+
+static const struct hwmon_chip_info tmp51x_chip_info = {
+	.ops = &tmp51x_hwmon_ops,
+	.info = tmp51x_info,
+};
+
+/*
+ * Calibrate the tmp51x following the datasheet method
+ */
+static int tmp51x_calibrate(struct tmp51x_data *data)
+{
+	int vshunt_max = data->pga_gain * 40;
+	u64 max_curr_ma;
+	u32 div;
+
+	/*
+	 * If shunt_uohms is equal to 0, the calibration should be set to 0.
+	 * The consequence will be that the current and power measurement engine
+	 * of the sensor will not work. Temperature and voltage sensing will
+	 * continue to work.
+	 */
+	if (data->shunt_uohms == 0)
+		return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0);
+
+	max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * 1000 * 1000,
+					    data->shunt_uohms);
+
+	/*
+	 * Calculate the minimal bit resolution for the current and the power.
+	 * Those values will be used during register interpretation.
+	 */
+	data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * 1000, 32767);
+	data->pwr_lsb_uw = 20 * data->curr_lsb_ua;
+
+	div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms,
+				    1000 * 1000);
+
+	return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION,
+			    DIV_ROUND_CLOSEST(40960, div));
+}
+
+/*
+ * Initialize the configuration and calibration registers.
+ */
+static int tmp51x_init(struct tmp51x_data *data)
+{
+	unsigned int regval;
+	int ret = regmap_write(data->regmap, TMP51X_SHUNT_CONFIG,
+			       data->shunt_config);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_write(data->regmap, TMP51X_TEMP_CONFIG, data->temp_config);
+	if (ret < 0)
+		return ret;
+
+	// nFactor configuration
+	ret = regmap_update_bits(data->regmap, TMP51X_N_FACTOR_AND_HYST_1,
+				 TMP51X_NFACTOR_MASK, data->nfactor[0] << 8);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_write(data->regmap, TMP51X_N_FACTOR_2,
+			   data->nfactor[1] << 8);
+	if (ret < 0)
+		return ret;
+
+	if (data->id == tmp513) {
+		ret = regmap_write(data->regmap, TMP513_N_FACTOR_3,
+				   data->nfactor[2] << 8);
+		if (ret < 0)
+			return ret;
+	}
+
+	ret = tmp51x_calibrate(data);
+	if (ret < 0)
+		return ret;
+
+	// Read the status register before using as the datasheet propose
+	return regmap_read(data->regmap, TMP51X_STATUS, &regval);
+}
+
+static const struct i2c_device_id tmp51x_id[] = {
+	{ "tmp512", tmp512 },
+	{ "tmp513", tmp513 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, tmp51x_id);
+
+static const struct of_device_id tmp51x_of_match[] = {
+	{
+		.compatible = "ti,tmp512",
+		.data = (void *)tmp512
+	},
+	{
+		.compatible = "ti,tmp513",
+		.data = (void *)tmp513
+	},
+	{ },
+};
+MODULE_DEVICE_TABLE(of, tmp51x_of_match);
+
+static int tmp51x_vbus_max_to_reg(struct device *dev, struct tmp51x_data *data)
+{
+	if (data->vbus_max_uvolt == TMP51X_VBUS_MAX_32V) {
+		data->shunt_config |= TMP51X_BUS_VOLTAGE_MASK;
+	} else if (data->vbus_max_uvolt == TMP51X_VBUS_MAX_16V) {
+		data->shunt_config &= ~TMP51X_BUS_VOLTAGE_MASK;
+	} else {
+		dev_err(dev, "ti,bus-range-microvolt is invalid: %u\n",
+			data->vbus_max_uvolt);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int tmp51x_pga_gain_to_reg(struct device *dev, struct tmp51x_data *data)
+{
+	if (data->pga_gain == 8) {
+		data->shunt_config |= CURRENT_SENSE_VOLTAGE_320_MASK;
+	} else if (data->pga_gain == 4) {
+		data->shunt_config |= CURRENT_SENSE_VOLTAGE_160_MASK;
+	} else if (data->pga_gain == 2) {
+		data->shunt_config |= CURRENT_SENSE_VOLTAGE_80_MASK;
+	} else if (data->pga_gain == 1) {
+		data->shunt_config |= CURRENT_SENSE_VOLTAGE_40_MASK;
+	} else {
+		dev_err(dev, "ti,pga-gain is invalid: %u\n", data->pga_gain);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int tmp51x_read_properties(struct device *dev, struct tmp51x_data *data)
+{
+	int ret;
+	u8 nfactor[3];
+	u32 val;
+
+	ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", &val);
+	data->shunt_uohms = (ret >= 0) ? val : TMP51X_SHUNT_VALUE_DEFAULT;
+
+	ret = device_property_read_u32(dev, "ti,bus-range-microvolt", &val);
+	data->vbus_max_uvolt = (ret >= 0) ? val : TMP51X_VBUS_MAX_DEFAULT;
+	ret = tmp51x_vbus_max_to_reg(dev, data);
+	if (ret < 0)
+		return ret;
+
+	ret = device_property_read_u32(dev, "ti,pga-gain", &val);
+	data->pga_gain = (ret >= 0) ? val : TMP51X_PGA_DEFAULT;
+	ret = tmp51x_pga_gain_to_reg(dev, data);
+	if (ret < 0)
+		return ret;
+
+	ret = device_property_read_u8_array(dev, "ti,nfactor", nfactor,
+					    (data->id == tmp513) ? 3 : 2);
+	if (ret >= 0)
+		memcpy(data->nfactor, nfactor, (data->id == tmp513) ? 3 : 2);
+
+	// Check if shunt value is compatible with pga-gain
+	if (data->shunt_uohms > data->pga_gain * 40 * 1000 * 1000) {
+		dev_err(dev, "shunt-resistor: %u too big for pga_gain: %u\n",
+			data->shunt_uohms, data->pga_gain);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static void tmp51x_use_default(struct tmp51x_data *data)
+{
+	data->vbus_max_uvolt = TMP51X_VBUS_MAX_DEFAULT;
+	data->pga_gain = TMP51X_PGA_DEFAULT;
+	data->shunt_uohms = TMP51X_SHUNT_VALUE_DEFAULT;
+}
+
+static int tmp51x_configure(struct device *dev, struct tmp51x_data *data)
+{
+	data->shunt_config = TMP51X_SHUNT_CONFIG_DEFAULT;
+	data->temp_config = (data->id == tmp513) ?
+			TMP513_TEMP_CONFIG_DEFAULT : TMP512_TEMP_CONFIG_DEFAULT;
+
+	if (dev->of_node)
+		return tmp51x_read_properties(dev, data);
+
+	tmp51x_use_default(data);
+
+	return 0;
+}
+
+static int tmp51x_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	struct device *dev = &client->dev;
+	struct tmp51x_data *data;
+	struct device *hwmon_dev;
+	int ret;
+
+	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	if (client->dev.of_node)
+		data->id = (enum tmp51x_ids)device_get_match_data(&client->dev);
+	else
+		data->id = id->driver_data;
+
+	ret = tmp51x_configure(dev, data);
+	if (ret < 0) {
+		dev_err(dev, "error configuring the device: %d\n", ret);
+		return ret;
+	}
+
+	data->regmap = devm_regmap_init_i2c(client, &tmp51x_regmap_config);
+	if (IS_ERR(data->regmap)) {
+		dev_err(dev, "failed to allocate register map\n");
+		return PTR_ERR(data->regmap);
+	}
+
+	ret = tmp51x_init(data);
+	if (ret < 0) {
+		dev_err(dev, "error configuring the device: %d\n", ret);
+		return -ENODEV;
+	}
+
+	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
+							 data,
+							 &tmp51x_chip_info,
+							 NULL);
+	if (IS_ERR(hwmon_dev))
+		return PTR_ERR(hwmon_dev);
+
+	dev_dbg(dev, "power monitor %s\n", id->name);
+
+	return 0;
+}
+
+static struct i2c_driver tmp51x_driver = {
+	.driver = {
+		.name	= "tmp51x",
+		.of_match_table = of_match_ptr(tmp51x_of_match),
+	},
+	.probe		= tmp51x_probe,
+	.id_table	= tmp51x_id,
+};
+
+module_i2c_driver(tmp51x_driver);
+
+MODULE_AUTHOR("Eric Tremblay <etremblay@distechcontrols.com>");
+MODULE_DESCRIPTION("tmp51x driver");
+MODULE_LICENSE("GPL");
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513
  2019-11-11 20:34 ` [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513 etremblay
@ 2019-11-12  1:03   ` Rob Herring
  2019-11-12 15:01     ` Tremblay, Eric
  2019-11-12  1:45   ` Guenter Roeck
  1 sibling, 1 reply; 8+ messages in thread
From: Rob Herring @ 2019-11-12  1:03 UTC (permalink / raw)
  To: etremblay
  Cc: linux, linux-hwmon, devicetree, linux-doc, jdelvare,
	mark.rutland, corbet

On Mon, Nov 11, 2019 at 03:34:44PM -0500, etremblay@distech-controls.com wrote:
> From: Eric Tremblay <etremblay@distech-controls.com>
> 
> Document the TMP513/512 device devicetree bindings
> 
> Signed-off-by: Eric Tremblay <etremblay@distech-controls.com>
> ---
>  .../devicetree/bindings/hwmon/ti,tmp513.yaml  | 89 +++++++++++++++++++
>  1 file changed, 89 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> 
> diff --git a/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> new file mode 100644
> index 000000000000..de4ed3645e0f
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> @@ -0,0 +1,89 @@
> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +
> +$id: http://devicetree.org/schemas/hwmon/ti,tmp513.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: TMP513/512 system monitor sensor
> +
> +maintainers:
> +  - Eric Tremblay <etremblay@distech-controls.com>
> +
> +description: |
> +  This driver implements support for Texas Instruments TMP512, and TMP513.
> +  The TMP512 (dual-channel) and TMP513 (triple-channel) are system monitors
> +  that include remote sensors, a local temperature sensor, and a high-side
> +  current shunt monitor. These system monitors have the capability of measuring
> +  remote temperatures, on-chip temperatures, and system voltage/power/current
> +  consumption.
> +
> +  Datasheets:
> +  http://www.ti.com/lit/gpn/tmp513
> +  http://www.ti.com/lit/gpn/tmp512
> +
> +
> +properties:
> +  compatible:
> +    enum:
> +      - ti,tmp512
> +      - ti,tmp513
> +
> +  reg:
> +    maxItems: 1
> +
> +  shunt-resistor-micro-ohms:
> +    description: |
> +      If 0, the calibration process will be skiped and the current and power
> +      measurement engine will not work. Temperature and voltage measurement
> +      will continue to work. The shunt value also need to respect:
> +      rshunt <= pga-gain * 40 * 1000 * 1000.
> +      If not, it's not possible to compute a valid calibration value.
> +    default: 1000
> +
> +  ti,pga-gain:
> +    description: |
> +      The gain value for the PGA function. This is 8, 4, 2 or 1.
> +      The PGA gain affect the shunt voltage range.
> +      The range will be equal to: pga-gain * 40mV
> +    allOf:
> +      - $ref: /schemas/types.yaml#/definitions/uint32
> +    enum: [1, 2, 4, 8]
> +    default: 8
> +
> +  ti,bus-range-microvolt:
> +    description: |
> +      This is the operating range of the bus voltage in microvolt
> +    allOf:
> +      - $ref: /schemas/types.yaml#/definitions/uint32
> +    enum: [16000000, 32000000]
> +    default: 32000000
> +
> +  ti,nfactor:
> +    description: |
> +      Array of three(TMP513) or two(TMP512) n-Factor value for each remote
> +      temperature channel.
> +      See datasheet Table 11 for n-Factor range list and value interpretation.
> +    allOf:
> +      - $ref: /schemas/types.yaml#definitions/uint8-array
> +      - minItems: 2
> +        maxItems: 3
> +        items:
> +          default: 0
> +
> +required:
> +  - compatible
> +  - reg
> +
> +examples:
> +  - |
> +    i2c {

'make dt_binding_check' fails. You need #address-cells and #size-cells 
in here:

Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:20.17-30: Warning (reg_format): /example-0/i2c/tmp513@5c:reg: property has invalid length (4 bytes) (#address-cells == 2, #size-cells == 1)
Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dt.yaml: Warning (pci_device_bus_num): Failed prerequisite 'reg_format'
Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:17.5-26.11: Warning (i2c_bus_bridge): /example-0/i2c: incorrect #address-cells for I2C bus
Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:17.5-26.11: Warning (i2c_bus_bridge): /example-0/i2c: incorrect #size-cells for I2C bus
Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dt.yaml: Warning (i2c_bus_reg): Failed prerequisite 'reg_format'
Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dt.yaml: Warning (i2c_bus_reg): Failed prerequisite 'i2c_bus_bridge'
Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dt.yaml: Warning (spi_bus_reg): Failed prerequisite 'reg_format'
Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:18.23-25.15: 
Warning (avoid_default_addr_size): /example-0/i2c/tmp513@5c: Relying on default #address-cells value
Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:18.23-25.15: Warning (avoid_default_addr_size): /example-0/i2c/tmp513@5c: Relying on default #size-cells value

> +        tmp513@5c {
> +            compatible = "ti,tmp513";
> +            reg = <0x5C>;
> +            shunt-resistor-micro-ohms = <330000>;
> +            ti,bus-range-microvolt = <32000000>;
> +            ti,pga-gain = <8>;
> +            ti,nfactor = [01 F3 00];
> +        };
> +    };
> -- 
> 2.17.1
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513
  2019-11-11 20:34 ` [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513 etremblay
  2019-11-12  1:03   ` Rob Herring
@ 2019-11-12  1:45   ` Guenter Roeck
  2019-11-12 14:40     ` Tremblay, Eric
  1 sibling, 1 reply; 8+ messages in thread
From: Guenter Roeck @ 2019-11-12  1:45 UTC (permalink / raw)
  To: etremblay, linux-hwmon, devicetree, linux-doc
  Cc: jdelvare, robh+dt, mark.rutland, corbet

On 11/11/19 12:34 PM, etremblay@distech-controls.com wrote:
> From: Eric Tremblay <etremblay@distech-controls.com>
> 
> Document the TMP513/512 device devicetree bindings
> 
> Signed-off-by: Eric Tremblay <etremblay@distech-controls.com>
> ---
>   .../devicetree/bindings/hwmon/ti,tmp513.yaml  | 89 +++++++++++++++++++
>   1 file changed, 89 insertions(+)
>   create mode 100644 Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> 
> diff --git a/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> new file mode 100644
> index 000000000000..de4ed3645e0f
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> @@ -0,0 +1,89 @@
> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +
> +$id: http://devicetree.org/schemas/hwmon/ti,tmp513.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: TMP513/512 system monitor sensor
> +
> +maintainers:
> +  - Eric Tremblay <etremblay@distech-controls.com>
> +
> +description: |
> +  This driver implements support for Texas Instruments TMP512, and TMP513.

I think this needs some rewording. This is not a driver description, but
a system description.

> +  The TMP512 (dual-channel) and TMP513 (triple-channel) are system monitors
> +  that include remote sensors, a local temperature sensor, and a high-side
> +  current shunt monitor. These system monitors have the capability of measuring
> +  remote temperatures, on-chip temperatures, and system voltage/power/current
> +  consumption.
> +
> +  Datasheets:
> +  http://www.ti.com/lit/gpn/tmp513
> +  http://www.ti.com/lit/gpn/tmp512
> +
> +
> +properties:
> +  compatible:
> +    enum:
> +      - ti,tmp512
> +      - ti,tmp513
> +
> +  reg:
> +    maxItems: 1
> +
> +  shunt-resistor-micro-ohms:
> +    description: |
> +      If 0, the calibration process will be skiped and the current and power
> +      measurement engine will not work. Temperature and voltage measurement
> +      will continue to work. The shunt value also need to respect:
> +      rshunt <= pga-gain * 40 * 1000 * 1000.
> +      If not, it's not possible to compute a valid calibration value.
> +    default: 1000
> +
> +  ti,pga-gain:
> +    description: |
> +      The gain value for the PGA function. This is 8, 4, 2 or 1.
> +      The PGA gain affect the shunt voltage range.
> +      The range will be equal to: pga-gain * 40mV
> +    allOf:
> +      - $ref: /schemas/types.yaml#/definitions/uint32
> +    enum: [1, 2, 4, 8]
> +    default: 8
> +
> +  ti,bus-range-microvolt:
> +    description: |
> +      This is the operating range of the bus voltage in microvolt
> +    allOf:
> +      - $ref: /schemas/types.yaml#/definitions/uint32
> +    enum: [16000000, 32000000]
> +    default: 32000000
> +
> +  ti,nfactor:
> +    description: |
> +      Array of three(TMP513) or two(TMP512) n-Factor value for each remote
> +      temperature channel.
> +      See datasheet Table 11 for n-Factor range list and value interpretation.
> +    allOf:
> +      - $ref: /schemas/types.yaml#definitions/uint8-array
> +      - minItems: 2
> +        maxItems: 3
> +        items:
> +          default: 0
> +
> +required:
> +  - compatible
> +  - reg
> +
> +examples:
> +  - |
> +    i2c {
> +        tmp513@5c {
> +            compatible = "ti,tmp513";
> +            reg = <0x5C>;
> +            shunt-resistor-micro-ohms = <330000>;
> +            ti,bus-range-microvolt = <32000000>;
> +            ti,pga-gain = <8>;
> +            ti,nfactor = [01 F3 00];
> +        };
> +    };
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v7 2/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips.
  2019-11-11 20:34 ` [PATCH v7 2/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips etremblay
@ 2019-11-12 13:41   ` Guenter Roeck
  0 siblings, 0 replies; 8+ messages in thread
From: Guenter Roeck @ 2019-11-12 13:41 UTC (permalink / raw)
  To: etremblay, linux-hwmon, devicetree, linux-doc
  Cc: jdelvare, robh+dt, mark.rutland, corbet

On 11/11/19 12:34 PM, etremblay@distech-controls.com wrote:
> From: Eric Tremblay <etremblay@distech-controls.com>
> 
> TI's TMP512/513 are I2C/SMBus system monitor chips. These chips
> monitor the supply voltage, supply current, power consumption
> and provide one local and up to three (TMP513) remote temperature sensors.
> 
> It has been tested using a TI TMP513 development kit (TMP513EVM)
> 
> Signed-off-by: Eric Tremblay <etremblay@distech-controls.com>
> ---
>   Documentation/hwmon/index.rst  |   1 +
>   Documentation/hwmon/tmp513.rst | 104 +++++
>   MAINTAINERS                    |   7 +
>   drivers/hwmon/Kconfig          |  10 +
>   drivers/hwmon/Makefile         |   1 +
>   drivers/hwmon/tmp513.c         | 789 +++++++++++++++++++++++++++++++++
>   6 files changed, 912 insertions(+)
>   create mode 100644 Documentation/hwmon/tmp513.rst
>   create mode 100644 drivers/hwmon/tmp513.c
> 
> diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst
> index 230ad59b462b..d708a371fce1 100644
> --- a/Documentation/hwmon/index.rst
> +++ b/Documentation/hwmon/index.rst
> @@ -153,6 +153,7 @@ Hardware Monitoring Kernel Drivers
>      tmp108
>      tmp401
>      tmp421
> +   tmp513
>      tps40422
>      twl4030-madc-hwmon
>      ucd9000
> diff --git a/Documentation/hwmon/tmp513.rst b/Documentation/hwmon/tmp513.rst
> new file mode 100644
> index 000000000000..d41a207e96e6
> --- /dev/null
> +++ b/Documentation/hwmon/tmp513.rst
> @@ -0,0 +1,104 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +
> +Kernel driver tmp513
> +====================
> +
> +Supported chips:
> +
> +  * Texas Instruments TMP512
> +
> +    Prefix: 'tmp512'
> +
> +    Datasheet: http://www.ti.com/lit/ds/symlink/tmp512.pdf
> +
> +  * Texas Instruments TMP513
> +
> +    Prefix: 'tmp513'
> +
> +    Datasheet: http://www.ti.com/lit/ds/symlink/tmp513.pdf
> +
> +Authors:
> +
> +	Eric Tremblay <etremblay@distech-controls.com>
> +
> +Description
> +-----------
> +
> +This driver implements support for Texas Instruments TMP512, and TMP513.
> +The TMP512 (dual-channel) and TMP513 (triple-channel) are system monitors
> +that include remote sensors, a local temperature sensor, and a high-side current
> +shunt monitor. These system monitors have the capability of measuring remote
> +temperatures, on-chip temperatures, and system voltage/power/current
> +consumption.
> +
> +The temperatures are measured in degrees Celsius with a range of
> +-40 to + 125 degrees with a resolution of 0.0625 degree C.
> +
> +For hysteresis value, only the first channel is writable. Writing to it
> +will affect all other values since each channels are sharing the same
> +hysteresis value. The hysteresis is in degrees Celsius with a range of
> +0 to 127.5 degrees with a resolution of 0.5 degree.
> +
> +The driver exports the temperature values via the following sysfs files:
> +
> +**temp[1-4]_input**
> +
> +**temp[1-4]_crit**
> +
> +**temp[1-4]_crit_alarm**
> +
> +**temp[1-4]_crit_hyst**
> +
> +The shunt voltage is measured in micro volts. There are four configurable
> +range: 320mV, 160mV, 80mV and 40mV all of them with a resolution of 10 uV.
> +The range depends on the pga gain specified in the device tree
> +with "pga-gain" (default to 8). The range will be equal to pga gain
> +multiply by 40mV.
> +
I should have paid closer attention to this.
Per ABI, voltages are _always_ reported in milli-Volt.

Since the purpose of shunt voltages is to report currents, not voltages,
reporting it as voltage doesn't really make much sense to me.

> +The driver exports the shunt voltage values via the following sysFs files:
> +
> +**in0_input**
> +
> +**in0_lcrit**
> +
> +**in0_lcrit_alarm**
> +
> +**in0_crit**
> +
> +**in0_crit_alarm**
> +
The above should really be reported as (and, for limits, configured as) currents.

> +The bus voltage is measured in milli volts. There are two configuable
> +range: 32V and 16V both with a resolution of 4mV. It can be configured in the
> +device tree with "bus-voltage-range" (default to 32V);
> +
> +The driver exports the bus voltage values via the following sysFs files:
> +
> +**in0_input**
> +
> +**in0_lcrit**
> +
> +**in0_lcrit_alarm**
> +
> +**in0_crit**
> +
> +**in0_crit_alarm**
> +

The above is confusing and almost certainly incorrect.
in0_XXX is listed to report both shunt and bus voltage.

> +The power and the currents range and resolution depends on the calibration
> +register value. Those values are calculate by the hardware using those
> +formula:
> +
> +Current = (ShuntVoltage * CalibrationRegister) / 4096
> +Power   = (Current * BusVoltage) / 5000
> +
> +The driver exports the current and power values via the following sysFs files:
> +
> +**curr0_input**

There is no curr0_XXX attribute. Current attributes start with curr1.

> +
> +**power1_input**
> +
> +**power1_crit**
> +
> +**power1_crit_alarm**
> +
> +The calibration process follow the procedure of the datasheet (without overflow)
> +and depend on the shunt resistor value and the pga_gain value.
> diff --git a/MAINTAINERS b/MAINTAINERS
> index eb19fad370d7..75db98a0913c 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -16378,6 +16378,13 @@ S:	Maintained
>   F:	Documentation/hwmon/tmp401.rst
>   F:	drivers/hwmon/tmp401.c
>   
> +TMP513 HARDWARE MONITOR DRIVER
> +M:	Eric Tremblay <etremblay@distech-controls.com>
> +L:	linux-hwmon@vger.kernel.org
> +S:	Maintained
> +F:	Documentation/hwmon/tmp513.rst
> +F:	drivers/hwmon/tmp513.c
> +
>   TMPFS (SHMEM FILESYSTEM)
>   M:	Hugh Dickins <hughd@google.com>
>   L:	linux-mm@kvack.org
> diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
> index 13a6b4afb4b3..926e3c98dbdf 100644
> --- a/drivers/hwmon/Kconfig
> +++ b/drivers/hwmon/Kconfig
> @@ -1709,6 +1709,16 @@ config SENSORS_TMP421
>   	  This driver can also be built as a module. If so, the module
>   	  will be called tmp421.
>   
> +config SENSORS_TMP513
> +	tristate "Texas Instruments TMP513 and compatibles"
> +	depends on I2C
> +	help
> +	  If you say yes here you get support for Texas Instruments TMP512,
> +	  and TMP513 temperature and power supply sensor chips.
> +
> +	  This driver can also be built as a module. If so, the module
> +	  will be called tmp513.
> +
>   config SENSORS_VEXPRESS
>   	tristate "Versatile Express"
>   	depends on VEXPRESS_CONFIG
> diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
> index 40c036ea45e6..762642291914 100644
> --- a/drivers/hwmon/Makefile
> +++ b/drivers/hwmon/Makefile
> @@ -166,6 +166,7 @@ obj-$(CONFIG_SENSORS_TMP103)	+= tmp103.o
>   obj-$(CONFIG_SENSORS_TMP108)	+= tmp108.o
>   obj-$(CONFIG_SENSORS_TMP401)	+= tmp401.o
>   obj-$(CONFIG_SENSORS_TMP421)	+= tmp421.o
> +obj-$(CONFIG_SENSORS_TMP513)	+= tmp513.o
>   obj-$(CONFIG_SENSORS_VEXPRESS)	+= vexpress-hwmon.o
>   obj-$(CONFIG_SENSORS_VIA_CPUTEMP)+= via-cputemp.o
>   obj-$(CONFIG_SENSORS_VIA686A)	+= via686a.o
> diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
> new file mode 100644
> index 000000000000..098ce2b0e253
> --- /dev/null
> +++ b/drivers/hwmon/tmp513.c
> @@ -0,0 +1,789 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Driver for Texas Instruments TMP512, TMP513 power monitor chips
> + *
> + * TMP513:
> + * Thermal/Power Management with Triple Remote and
> + * Local Temperature Sensor and Current Shunt Monitor
> + * Datasheet: http://www.ti.com/lit/gpn/tmp513
> + *
> + * TMP512:
> + * Thermal/Power Management with Dual Remote
> + *	and Local Temperature Sensor and Current Shunt Monitor
> + * Datasheet: http://www.ti.com/lit/gpn/tmp512
> + *
> + * Copyright (C) 2019 Eric Tremblay <etremblay@distech-controls.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; version 2 of the License.
> + */
> +
> +#include <linux/err.h>
> +#include <linux/hwmon.h>
> +#include <linux/i2c.h>
> +#include <linux/init.h>
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/regmap.h>
> +#include <linux/slab.h>
> +#include <linux/util_macros.h>
> +
> +// Common register definition
> +#define TMP51X_SHUNT_CONFIG		0x00
> +#define TMP51X_TEMP_CONFIG		0x01
> +#define TMP51X_STATUS			0x02
> +#define TMP51X_SMBUS_ALERT		0x03
> +#define TMP51X_SHUNT_VOLTAGE_RESULT	0x04
> +#define TMP51X_BUS_VOLTAGE_RESULT	0x05
> +#define TMP51X_POWER_RESULT		0x06
> +#define TMP51X_SHUNT_CURRENT_RESULT	0x07
> +#define TMP51X_LOCAL_TEMP_RESULT	0x08
> +#define TMP51X_REMOTE_TEMP_RESULT_1	0x09
> +#define TMP51X_REMOTE_TEMP_RESULT_2	0x0A
> +#define TMP51X_SHUNT_VOLTAGE_H_LIMIT	0x0C
> +#define TMP51X_SHUNT_VOLTAGE_L_LIMIT	0x0D
> +#define TMP51X_BUS_VOLTAGE_H_LIMIT	0x0E
> +#define TMP51X_BUS_VOLTAGE_L_LIMIT	0x0F
> +#define TMP51X_POWER_LIMIT		0x10
> +#define TMP51X_LOCAL_TEMP_LIMIT	0x11
> +#define TMP51X_REMOTE_TEMP_LIMIT_1	0x12
> +#define TMP51X_REMOTE_TEMP_LIMIT_2	0x13
> +#define TMP51X_SHUNT_CALIBRATION	0x15
> +#define TMP51X_N_FACTOR_AND_HYST_1	0x16
> +#define TMP51X_N_FACTOR_2		0x17
> +#define TMP51X_MAN_ID_REG		0xFE
> +#define TMP51X_DEVICE_ID_REG		0xFF
> +
> +// TMP513 specific register definition
> +#define TMP513_REMOTE_TEMP_RESULT_3	0x0B
> +#define TMP513_REMOTE_TEMP_LIMIT_3	0x14
> +#define TMP513_N_FACTOR_3		0x18
> +
> +// Common attrs, and NULL
> +#define TMP51X_MANUFACTURER_ID		0x55FF
> +
> +#define TMP512_DEVICE_ID		0x22FF
> +#define TMP513_DEVICE_ID		0x23FF
> +
> +// Default config
> +#define TMP51X_SHUNT_CONFIG_DEFAULT	0x399F
> +#define TMP51X_SHUNT_VALUE_DEFAULT	1000
> +#define TMP51X_VBUS_MAX_DEFAULT		TMP51X_VBUS_MAX_32V
> +#define TMP51X_PGA_DEFAULT		8
> +#define TMP51X_MAX_REGISTER_ADDR	0xFF
> +
> +#define TMP512_TEMP_CONFIG_DEFAULT	0xBF80
> +#define TMP513_TEMP_CONFIG_DEFAULT	0xFF80
> +
> +// Mask and shift
> +#define CURRENT_SENSE_VOLTAGE_320_MASK	0x1800
> +#define CURRENT_SENSE_VOLTAGE_160_MASK	0x1000
> +#define CURRENT_SENSE_VOLTAGE_80_MASK	0x0800
> +#define CURRENT_SENSE_VOLTAGE_40_MASK	0
> +
> +#define TMP51X_BUS_VOLTAGE_MASK		0x2000
> +#define TMP51X_PGA_MASK			0x1800
> +#define TMP51X_NFACTOR_MASK		0xFF00
> +#define TMP51X_HYST_MASK		0x00FF
> +
> +#define TMP51X_BUS_VOLTAGE_SHIFT	3
> +#define TMP51X_TEMP_SHIFT		3
> +
> +// Alarms
> +#define TMP51X_SHUNT_VOLTAGE_H_LIMIT_POS	15
> +#define TMP51X_SHUNT_VOLTAGE_L_LIMIT_POS	14
> +#define TMP51X_BUS_VOLTAGE_H_LIMIT_POS		13
> +#define TMP51X_BUS_VOLTAGE_L_LIMIT_POS		12
> +#define TMP51X_POWER_LIMIT_POS			11
> +#define TMP51X_LOCAL_TEMP_LIMIT_POS		10
> +#define TMP51X_REMOTE_TEMP_LIMIT_1_POS		9
> +#define TMP51X_REMOTE_TEMP_LIMIT_2_POS		8
> +#define TMP513_REMOTE_TEMP_LIMIT_3_POS		7
> +
> +#define TMP51X_VBUS_MAX_32V		32000000
> +#define TMP51X_VBUS_MAX_16V		16000000
> +
> +// Max and Min value
> +#define MAX_BUS_VOLTAGE_32_LIMIT	32764
> +#define MAX_BUS_VOLTAGE_16_LIMIT	16382
> +
> +// Max possible value is -256 to +256 but datasheet indicated -40 to 125.
> +#define MAX_TEMP_LIMIT			125000
> +#define MIN_TEMP_LIMIT			-40000
> +
> +#define MAX_NFACTOR			127
> +#define MIN_NFACTOR			-128
> +#define MAX_TEMP_HYST			127500
> +
> +static const u8 TMP51X_TEMP_INPUT[4] = {
> +	TMP51X_LOCAL_TEMP_RESULT,
> +	TMP51X_REMOTE_TEMP_RESULT_1,
> +	TMP51X_REMOTE_TEMP_RESULT_2,
> +	TMP513_REMOTE_TEMP_RESULT_3
> +};
> +
> +static const u8 TMP51X_TEMP_CRIT[4] = {
> +	TMP51X_LOCAL_TEMP_LIMIT,
> +	TMP51X_REMOTE_TEMP_LIMIT_1,
> +	TMP51X_REMOTE_TEMP_LIMIT_2,
> +	TMP513_REMOTE_TEMP_LIMIT_3
> +};
> +
> +static const u8 TMP51X_TEMP_CRIT_ALARM[4] = {
> +	TMP51X_LOCAL_TEMP_LIMIT_POS,
> +	TMP51X_REMOTE_TEMP_LIMIT_1_POS,
> +	TMP51X_REMOTE_TEMP_LIMIT_2_POS,
> +	TMP513_REMOTE_TEMP_LIMIT_3_POS
> +};
> +
> +static const u8 TMP51X_TEMP_CRIT_HYST[4] = {
> +	TMP51X_N_FACTOR_AND_HYST_1,
> +	TMP51X_N_FACTOR_AND_HYST_1,
> +	TMP51X_N_FACTOR_AND_HYST_1,
> +	TMP51X_N_FACTOR_AND_HYST_1
> +};
> +
> +static const u8 TMP51X_IN_INPUT[2] = {
> +	TMP51X_SHUNT_VOLTAGE_RESULT,
> +	TMP51X_BUS_VOLTAGE_RESULT
> +};
> +
> +static const u8 TMP51X_IN_CRIT[2] = {
> +	TMP51X_SHUNT_VOLTAGE_H_LIMIT,
> +	TMP51X_BUS_VOLTAGE_H_LIMIT
> +};
> +
> +static const u8 TMP51X_IN_LCRIT[2] = {
> +	TMP51X_SHUNT_VOLTAGE_L_LIMIT,
> +	TMP51X_BUS_VOLTAGE_L_LIMIT
> +};
> +
> +static const u8 TMP51X_IN_CRIT_ALARM[2] = {
> +	TMP51X_SHUNT_VOLTAGE_H_LIMIT_POS,
> +	TMP51X_BUS_VOLTAGE_H_LIMIT_POS
> +};
> +
> +static const u8 TMP51X_IN_LCRIT_ALARM[2] = {
> +	TMP51X_SHUNT_VOLTAGE_L_LIMIT_POS,
> +	TMP51X_BUS_VOLTAGE_L_LIMIT_POS
> +};
> +
> +static const u8 TMP51X_CURR_INPUT[1] = {
> +	TMP51X_SHUNT_CURRENT_RESULT
> +};
> +
> +static const u8 TMP51X_POWER_INPUT[1] = {
> +	TMP51X_POWER_RESULT
> +};
> +
> +static const u8 TMP51X_POWER_CRIT[1] = {
> +	TMP51X_POWER_LIMIT
> +};
> +
> +static const u8 TMP51X_POWER_CRIT_ALARM[1] = {
> +	TMP51X_POWER_LIMIT_POS
> +};
> +
> +static struct regmap_config tmp51x_regmap_config = {
> +	.reg_bits = 8,
> +	.val_bits = 16,
> +	.max_register = TMP51X_MAX_REGISTER_ADDR,
> +};
> +
> +enum tmp51x_ids {
> +	tmp512, tmp513
> +};
> +
> +struct tmp51x_data {
> +	u16 shunt_config;
> +	u16 pga_gain;
> +	u32 vbus_max_uvolt;
> +
> +	u16 temp_config;
> +	u8 nfactor[3];
> +
> +	u32 shunt_uohms;
> +
> +	u32 curr_lsb_ua;
> +	u32 pwr_lsb_uw;
> +
> +	enum tmp51x_ids id;
> +	struct regmap *regmap;
> +};
> +
> +// Set the shift based on the gain 8=4, 4=3, 2=2, 1=1
> +static inline u8 tmp51x_get_pga_shift(struct tmp51x_data *data)
> +{
> +	return 5 - ffs(data->pga_gain);
> +}
> +
> +static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
> +			    unsigned int regval, long *val)
> +{
> +	switch (reg) {
> +	case TMP51X_STATUS:
> +		*val = (regval >> pos) & 1;
> +		break;
> +	case TMP51X_SHUNT_VOLTAGE_RESULT:
> +	case TMP51X_SHUNT_VOLTAGE_H_LIMIT:
> +	case TMP51X_SHUNT_VOLTAGE_L_LIMIT:
> +		/*
> +		 * 2's compliment number shifted by one to four depending
> +		 * on the pga gain setting.
> +		 * 1lsb = 10uV
> +		 */
> +		*val = sign_extend32(regval, 17 - tmp51x_get_pga_shift(data));
> +		*val = DIV_ROUND_CLOSEST(*val, 100);
> +		break;
> +	case TMP51X_BUS_VOLTAGE_RESULT:
> +	case TMP51X_BUS_VOLTAGE_H_LIMIT:
> +	case TMP51X_BUS_VOLTAGE_L_LIMIT:
> +		// 1lsb = 4mV
> +		*val = (regval >> TMP51X_BUS_VOLTAGE_SHIFT) * 4;
> +		break;
> +	case TMP51X_POWER_RESULT:
> +	case TMP51X_POWER_LIMIT:
> +		// Power = (current * BusVoltage) / 5000
> +		*val = regval * data->pwr_lsb_uw;
> +		break;
> +	case TMP51X_SHUNT_CURRENT_RESULT:
> +		// Current = (ShuntVoltage * CalibrationRegister) / 4096
> +		*val = sign_extend32(regval, 16) * data->curr_lsb_ua;
> +		*val = DIV_ROUND_CLOSEST(*val, 1000);
> +		break;
> +	case TMP51X_LOCAL_TEMP_RESULT:
> +	case TMP51X_REMOTE_TEMP_RESULT_1:
> +	case TMP51X_REMOTE_TEMP_RESULT_2:
> +	case TMP513_REMOTE_TEMP_RESULT_3:
> +	case TMP51X_LOCAL_TEMP_LIMIT:
> +	case TMP51X_REMOTE_TEMP_LIMIT_1:
> +	case TMP51X_REMOTE_TEMP_LIMIT_2:
> +	case TMP513_REMOTE_TEMP_LIMIT_3:
> +		// 1lsb = 0.0625 degrees centigrade
> +		*val = sign_extend32(regval, 16) >> TMP51X_TEMP_SHIFT;
> +		*val = DIV_ROUND_CLOSEST(*val * 625, 10);
> +		break;
> +	case TMP51X_N_FACTOR_AND_HYST_1:
> +		// 1lsb = 0.5 degrees centigrade
> +		*val = (regval & TMP51X_HYST_MASK) * 500;
> +		break;
> +	default:
> +		// Programmer goofed
> +		WARN_ON_ONCE(1);
> +		*val = 0;
> +		return -EOPNOTSUPP;
> +	}
> +
> +	return 0;
> +}
> +
> +static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val)
> +{
> +	int regval, max_val;
> +	u32 mask = 0;
> +
> +	switch (reg) {
> +	case TMP51X_SHUNT_VOLTAGE_H_LIMIT:
> +	case TMP51X_SHUNT_VOLTAGE_L_LIMIT:
> +		// 1lsb = 10uV
> +		val *= 100;
> +		max_val = U16_MAX >> tmp51x_get_pga_shift(data);
> +		regval = clamp_val(val, -max_val, max_val);
> +		break;
> +	case TMP51X_BUS_VOLTAGE_H_LIMIT:
> +	case TMP51X_BUS_VOLTAGE_L_LIMIT:
> +		// 1lsb = 4mV
> +		max_val = (data->vbus_max_uvolt == TMP51X_VBUS_MAX_32V) ?
> +			MAX_BUS_VOLTAGE_32_LIMIT : MAX_BUS_VOLTAGE_16_LIMIT;
> +
> +		val = clamp_val(DIV_ROUND_CLOSEST(val, 4), 0, max_val);
> +		regval = val << TMP51X_BUS_VOLTAGE_SHIFT;
> +		break;
> +	case TMP51X_POWER_LIMIT:
> +		regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0,
> +				   U16_MAX);
> +		break;
> +	case TMP51X_LOCAL_TEMP_LIMIT:
> +	case TMP51X_REMOTE_TEMP_LIMIT_1:
> +	case TMP51X_REMOTE_TEMP_LIMIT_2:
> +	case TMP513_REMOTE_TEMP_LIMIT_3:
> +		// 1lsb = 0.0625 degrees centigrade
> +		val = clamp_val(val, MIN_TEMP_LIMIT, MAX_TEMP_LIMIT);
> +		regval = DIV_ROUND_CLOSEST(val * 10, 625) << TMP51X_TEMP_SHIFT;
> +		break;
> +	case TMP51X_N_FACTOR_AND_HYST_1:
> +		// 1lsb = 0.5 degrees centigrade
> +		val = clamp_val(val, 0, MAX_TEMP_HYST);
> +		regval = DIV_ROUND_CLOSEST(val, 500);
> +		mask = TMP51X_HYST_MASK;
> +		break;
> +	default:
> +		// Programmer goofed
> +		WARN_ON_ONCE(1);
> +		return -EOPNOTSUPP;
> +	}
> +
> +	if (mask == 0)
> +		return regmap_write(data->regmap, reg, regval);
> +	else
> +		return regmap_update_bits(data->regmap, reg, mask, regval);
> +}
> +
> +static int tmp51x_read(struct device *dev, enum hwmon_sensor_types type,
> +		       u32 attr, int channel, long *val)
> +{
> +	struct tmp51x_data *data = dev_get_drvdata(dev);
> +	int ret;
> +	u32 regval;
> +	u8 pos = 0, reg = 0;
> +
> +	switch (type) {
> +	case hwmon_temp:
> +		switch (attr) {
> +		case hwmon_temp_input:
> +			reg = TMP51X_TEMP_INPUT[channel];
> +			break;
> +		case hwmon_temp_crit_alarm:
> +			reg = TMP51X_STATUS;
> +			pos = TMP51X_TEMP_CRIT_ALARM[channel];
> +			break;
> +		case hwmon_temp_crit:
> +			reg = TMP51X_TEMP_CRIT[channel];
> +			break;
> +		case hwmon_temp_crit_hyst:
> +			reg = TMP51X_TEMP_CRIT_HYST[channel];
> +			break;
> +		}
> +		break;
> +	case hwmon_in:
> +		switch (attr) {
> +		case hwmon_in_input:
> +			reg = TMP51X_IN_INPUT[channel];
> +			break;
> +		case hwmon_in_lcrit_alarm:
> +			reg = TMP51X_STATUS;
> +			pos = TMP51X_IN_LCRIT_ALARM[channel];
> +			break;
> +		case hwmon_in_crit_alarm:
> +			reg = TMP51X_STATUS;
> +			pos = TMP51X_IN_CRIT_ALARM[channel];
> +			break;
> +		case hwmon_in_lcrit:
> +			reg = TMP51X_IN_LCRIT[channel];
> +			break;
> +		case hwmon_in_crit:
> +			reg = TMP51X_IN_CRIT[channel];
> +			break;
> +		}
> +		break;
> +	case hwmon_curr:
> +		switch (attr) {
> +		case hwmon_curr_input:
> +			reg = TMP51X_CURR_INPUT[channel];
> +			break;
> +		}
> +		break;
> +	case hwmon_power:
> +		switch (attr) {
> +		case hwmon_power_input:
> +			reg = TMP51X_POWER_INPUT[channel];
> +			break;
> +		case hwmon_power_crit_alarm:
> +			reg = TMP51X_STATUS;
> +			pos = TMP51X_POWER_CRIT_ALARM[channel];
> +			break;
> +		case hwmon_power_crit:
> +			reg = TMP51X_POWER_CRIT[channel];
> +			break;
> +		}
> +		break;
> +	default:
> +		break;
> +	}
> +
> +	if (reg == 0)
> +		return -EOPNOTSUPP;
> +
> +	ret = regmap_read(data->regmap, reg, &regval);
> +	if (ret < 0)
> +		return ret;
> +
> +	return tmp51x_get_value(data, reg, pos, regval, val);
> +}
> +
> +static int tmp51x_write(struct device *dev, enum hwmon_sensor_types type,
> +			u32 attr, int channel, long val)
> +{
> +	u8 reg = 0;
> +
> +	switch (type) {
> +	case hwmon_temp:
> +		switch (attr) {
> +		case hwmon_temp_crit:
> +			reg = TMP51X_TEMP_CRIT[channel];
> +			break;
> +		case hwmon_temp_crit_hyst:
> +			reg = TMP51X_TEMP_CRIT_HYST[channel];
> +			break;
> +		}
> +		break;
> +	case hwmon_in:
> +		switch (attr) {
> +		case hwmon_in_lcrit:
> +			reg = TMP51X_IN_LCRIT[channel];
> +			break;
> +		case hwmon_in_crit:
> +			reg = TMP51X_IN_CRIT[channel];
> +			break;
> +		}
> +		break;
> +	case hwmon_power:
> +		switch (attr) {
> +		case hwmon_power_crit:
> +			reg = TMP51X_POWER_CRIT[channel];
> +			break;
> +		}
> +		break;
> +	default:
> +		break;
> +	}
> +
> +	if (reg == 0)
> +		return -EOPNOTSUPP;
> +
> +	return tmp51x_set_value(dev_get_drvdata(dev), reg, val);
> +}
> +
> +static umode_t tmp51x_is_visible(const void *_data,
> +				 enum hwmon_sensor_types type, u32 attr,
> +				 int channel)
> +{
> +	const struct tmp51x_data *data = _data;
> +
> +	switch (type) {
> +	case hwmon_temp:
> +		if (data->id == tmp512 && channel == 4)
> +			return 0;
> +		switch (attr) {
> +		case hwmon_temp_input:
> +		case hwmon_temp_crit_alarm:
> +			return 0444;
> +		case hwmon_temp_crit:
> +			return 0644;
> +		case hwmon_temp_crit_hyst:
> +			if (channel == 0)
> +				return 0644;
> +			return 0444;
> +		}
> +		break;
> +	case hwmon_in:
> +		switch (attr) {
> +		case hwmon_in_input:
> +		case hwmon_in_lcrit_alarm:
> +		case hwmon_in_crit_alarm:
> +			return 0444;
> +		case hwmon_in_lcrit:
> +		case hwmon_in_crit:
> +			return 0644;
> +		}
> +		break;
> +	case hwmon_curr:
> +		switch (attr) {
> +		case hwmon_curr_input:
> +			return 0444;
> +		}
> +		break;
> +	case hwmon_power:
> +		switch (attr) {
> +		case hwmon_power_input:
> +		case hwmon_power_crit_alarm:
> +			return 0444;
> +		case hwmon_power_crit:
> +			return 0644;
> +		}
> +		break;
> +	default:
> +		break;
> +	}
> +	return 0;
> +}
> +
> +static const struct hwmon_channel_info *tmp51x_info[] = {
> +	HWMON_CHANNEL_INFO(temp,
> +			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
> +			   HWMON_T_CRIT_HYST,
> +			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
> +			   HWMON_T_CRIT_HYST,
> +			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
> +			   HWMON_T_CRIT_HYST,
> +			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
> +			   HWMON_T_CRIT_HYST),
> +	HWMON_CHANNEL_INFO(in,
> +			   HWMON_I_INPUT | HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM |
> +			   HWMON_I_CRIT | HWMON_I_CRIT_ALARM,
> +			   HWMON_I_INPUT | HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM |
> +			   HWMON_I_CRIT | HWMON_I_CRIT_ALARM),
> +	HWMON_CHANNEL_INFO(curr,
> +			   HWMON_C_INPUT),
> +	HWMON_CHANNEL_INFO(power,
> +			   HWMON_P_INPUT | HWMON_P_CRIT | HWMON_P_CRIT_ALARM),
> +	NULL
> +};
> +
> +static const struct hwmon_ops tmp51x_hwmon_ops = {
> +	.is_visible = tmp51x_is_visible,
> +	.read = tmp51x_read,
> +	.write = tmp51x_write,
> +};
> +
> +static const struct hwmon_chip_info tmp51x_chip_info = {
> +	.ops = &tmp51x_hwmon_ops,
> +	.info = tmp51x_info,
> +};
> +
> +/*
> + * Calibrate the tmp51x following the datasheet method
> + */
> +static int tmp51x_calibrate(struct tmp51x_data *data)
> +{
> +	int vshunt_max = data->pga_gain * 40;
> +	u64 max_curr_ma;
> +	u32 div;
> +
> +	/*
> +	 * If shunt_uohms is equal to 0, the calibration should be set to 0.
> +	 * The consequence will be that the current and power measurement engine
> +	 * of the sensor will not work. Temperature and voltage sensing will
> +	 * continue to work.
> +	 */
> +	if (data->shunt_uohms == 0)
> +		return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0);
> +
> +	max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * 1000 * 1000,
> +					    data->shunt_uohms);
> +
> +	/*
> +	 * Calculate the minimal bit resolution for the current and the power.
> +	 * Those values will be used during register interpretation.
> +	 */
> +	data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * 1000, 32767);
> +	data->pwr_lsb_uw = 20 * data->curr_lsb_ua;
> +
> +	div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms,
> +				    1000 * 1000);
> +
> +	return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION,
> +			    DIV_ROUND_CLOSEST(40960, div));
> +}
> +
> +/*
> + * Initialize the configuration and calibration registers.
> + */
> +static int tmp51x_init(struct tmp51x_data *data)
> +{
> +	unsigned int regval;
> +	int ret = regmap_write(data->regmap, TMP51X_SHUNT_CONFIG,
> +			       data->shunt_config);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = regmap_write(data->regmap, TMP51X_TEMP_CONFIG, data->temp_config);
> +	if (ret < 0)
> +		return ret;
> +
> +	// nFactor configuration
> +	ret = regmap_update_bits(data->regmap, TMP51X_N_FACTOR_AND_HYST_1,
> +				 TMP51X_NFACTOR_MASK, data->nfactor[0] << 8);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = regmap_write(data->regmap, TMP51X_N_FACTOR_2,
> +			   data->nfactor[1] << 8);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (data->id == tmp513) {
> +		ret = regmap_write(data->regmap, TMP513_N_FACTOR_3,
> +				   data->nfactor[2] << 8);
> +		if (ret < 0)
> +			return ret;
> +	}
> +
> +	ret = tmp51x_calibrate(data);
> +	if (ret < 0)
> +		return ret;
> +
> +	// Read the status register before using as the datasheet propose
> +	return regmap_read(data->regmap, TMP51X_STATUS, &regval);
> +}
> +
> +static const struct i2c_device_id tmp51x_id[] = {
> +	{ "tmp512", tmp512 },
> +	{ "tmp513", tmp513 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, tmp51x_id);
> +
> +static const struct of_device_id tmp51x_of_match[] = {
> +	{
> +		.compatible = "ti,tmp512",
> +		.data = (void *)tmp512
> +	},
> +	{
> +		.compatible = "ti,tmp513",
> +		.data = (void *)tmp513
> +	},
> +	{ },
> +};
> +MODULE_DEVICE_TABLE(of, tmp51x_of_match);
> +
> +static int tmp51x_vbus_max_to_reg(struct device *dev, struct tmp51x_data *data)
> +{
> +	if (data->vbus_max_uvolt == TMP51X_VBUS_MAX_32V) {
> +		data->shunt_config |= TMP51X_BUS_VOLTAGE_MASK;
> +	} else if (data->vbus_max_uvolt == TMP51X_VBUS_MAX_16V) {
> +		data->shunt_config &= ~TMP51X_BUS_VOLTAGE_MASK;
> +	} else {
> +		dev_err(dev, "ti,bus-range-microvolt is invalid: %u\n",
> +			data->vbus_max_uvolt);
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
> +static int tmp51x_pga_gain_to_reg(struct device *dev, struct tmp51x_data *data)
> +{
> +	if (data->pga_gain == 8) {
> +		data->shunt_config |= CURRENT_SENSE_VOLTAGE_320_MASK;
> +	} else if (data->pga_gain == 4) {
> +		data->shunt_config |= CURRENT_SENSE_VOLTAGE_160_MASK;
> +	} else if (data->pga_gain == 2) {
> +		data->shunt_config |= CURRENT_SENSE_VOLTAGE_80_MASK;
> +	} else if (data->pga_gain == 1) {
> +		data->shunt_config |= CURRENT_SENSE_VOLTAGE_40_MASK;
> +	} else {
> +		dev_err(dev, "ti,pga-gain is invalid: %u\n", data->pga_gain);
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
> +static int tmp51x_read_properties(struct device *dev, struct tmp51x_data *data)
> +{
> +	int ret;
> +	u8 nfactor[3];
> +	u32 val;
> +
> +	ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", &val);
> +	data->shunt_uohms = (ret >= 0) ? val : TMP51X_SHUNT_VALUE_DEFAULT;
> +
> +	ret = device_property_read_u32(dev, "ti,bus-range-microvolt", &val);
> +	data->vbus_max_uvolt = (ret >= 0) ? val : TMP51X_VBUS_MAX_DEFAULT;
> +	ret = tmp51x_vbus_max_to_reg(dev, data);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = device_property_read_u32(dev, "ti,pga-gain", &val);
> +	data->pga_gain = (ret >= 0) ? val : TMP51X_PGA_DEFAULT;
> +	ret = tmp51x_pga_gain_to_reg(dev, data);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = device_property_read_u8_array(dev, "ti,nfactor", nfactor,
> +					    (data->id == tmp513) ? 3 : 2);
> +	if (ret >= 0)
> +		memcpy(data->nfactor, nfactor, (data->id == tmp513) ? 3 : 2);
> +
> +	// Check if shunt value is compatible with pga-gain
> +	if (data->shunt_uohms > data->pga_gain * 40 * 1000 * 1000) {
> +		dev_err(dev, "shunt-resistor: %u too big for pga_gain: %u\n",
> +			data->shunt_uohms, data->pga_gain);
> +		return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
> +static void tmp51x_use_default(struct tmp51x_data *data)
> +{
> +	data->vbus_max_uvolt = TMP51X_VBUS_MAX_DEFAULT;
> +	data->pga_gain = TMP51X_PGA_DEFAULT;
> +	data->shunt_uohms = TMP51X_SHUNT_VALUE_DEFAULT;
> +}
> +
> +static int tmp51x_configure(struct device *dev, struct tmp51x_data *data)
> +{
> +	data->shunt_config = TMP51X_SHUNT_CONFIG_DEFAULT;
> +	data->temp_config = (data->id == tmp513) ?
> +			TMP513_TEMP_CONFIG_DEFAULT : TMP512_TEMP_CONFIG_DEFAULT;
> +
> +	if (dev->of_node)
> +		return tmp51x_read_properties(dev, data);
> +
> +	tmp51x_use_default(data);
> +
> +	return 0;
> +}
> +
> +static int tmp51x_probe(struct i2c_client *client,
> +			const struct i2c_device_id *id)
> +{
> +	struct device *dev = &client->dev;
> +	struct tmp51x_data *data;
> +	struct device *hwmon_dev;
> +	int ret;
> +
> +	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
> +	if (!data)
> +		return -ENOMEM;
> +
> +	if (client->dev.of_node)
> +		data->id = (enum tmp51x_ids)device_get_match_data(&client->dev);
> +	else
> +		data->id = id->driver_data;
> +
> +	ret = tmp51x_configure(dev, data);
> +	if (ret < 0) {
> +		dev_err(dev, "error configuring the device: %d\n", ret);
> +		return ret;
> +	}
> +
> +	data->regmap = devm_regmap_init_i2c(client, &tmp51x_regmap_config);
> +	if (IS_ERR(data->regmap)) {
> +		dev_err(dev, "failed to allocate register map\n");
> +		return PTR_ERR(data->regmap);
> +	}
> +
> +	ret = tmp51x_init(data);
> +	if (ret < 0) {
> +		dev_err(dev, "error configuring the device: %d\n", ret);
> +		return -ENODEV;
> +	}
> +
> +	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
> +							 data,
> +							 &tmp51x_chip_info,
> +							 NULL);
> +	if (IS_ERR(hwmon_dev))
> +		return PTR_ERR(hwmon_dev);
> +
> +	dev_dbg(dev, "power monitor %s\n", id->name);
> +
> +	return 0;
> +}
> +
> +static struct i2c_driver tmp51x_driver = {
> +	.driver = {
> +		.name	= "tmp51x",
> +		.of_match_table = of_match_ptr(tmp51x_of_match),
> +	},
> +	.probe		= tmp51x_probe,
> +	.id_table	= tmp51x_id,
> +};
> +
> +module_i2c_driver(tmp51x_driver);
> +
> +MODULE_AUTHOR("Eric Tremblay <etremblay@distechcontrols.com>");
> +MODULE_DESCRIPTION("tmp51x driver");
> +MODULE_LICENSE("GPL");
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513
  2019-11-12  1:45   ` Guenter Roeck
@ 2019-11-12 14:40     ` Tremblay, Eric
  0 siblings, 0 replies; 8+ messages in thread
From: Tremblay, Eric @ 2019-11-12 14:40 UTC (permalink / raw)
  To: Guenter Roeck, linux-hwmon, devicetree, linux-doc
  Cc: jdelvare, robh+dt, mark.rutland, corbet

> From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
> Sent: Monday, November 11, 2019 8:45 PM
> To: Tremblay, Eric <etremblay@distech-controls.com>; linux-hwmon@vger.kernel.org; devicetree@vger.kernel.org; linux-
> doc@vger.kernel.org
> Cc: jdelvare@suse.com; robh+dt@kernel.org; mark.rutland@arm.com; corbet@lwn.net
> Subject: Re: [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513
> 
> On 11/11/19 12:34 PM, etremblay@distech-controls.com wrote:
> > From: Eric Tremblay <etremblay@distech-controls.com>
> >
> > Document the TMP513/512 device devicetree bindings
> >
> > Signed-off-by: Eric Tremblay <etremblay@distech-controls.com>
> > ---
> >   .../devicetree/bindings/hwmon/ti,tmp513.yaml  | 89 +++++++++++++++++++
> >   1 file changed, 89 insertions(+)
> >   create mode 100644
> > Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> >
> > diff --git a/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> > b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> > new file mode 100644
> > index 000000000000..de4ed3645e0f
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> > @@ -0,0 +1,89 @@
> > +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2
> > +---
> > +
> > +$id:
> > +https://urldefense.proofpoint.com/v2/url?u=http-3A__devicetree.org_sc
> > +hemas_hwmon_ti-2Ctmp513.yaml-23&d=DwICaQ&c=tvvHx3uC0XdtgG-ImPDjfM0Qna
> > +kIsmmGcwejQDVxD-g&r=y34zLl9_AvBy5NSrvskG6SFbiKoCzI99WW2xhKlsVV4&m=d33
> > +wBKig7BZ0kss0OOUmmNbl5HWsWnMBDtfFv5-rsXo&s=KeOmfQeqUEi1WkJCgcjnWTIIsS
> > +kzY2hiRajIVVkq-lc&e=
> > +$schema:
> > +https://urldefense.proofpoint.com/v2/url?u=http-3A__devicetree.org_me
> > +ta-2Dschemas_core.yaml-23&d=DwICaQ&c=tvvHx3uC0XdtgG-ImPDjfM0QnakIsmmG
> > +cwejQDVxD-g&r=y34zLl9_AvBy5NSrvskG6SFbiKoCzI99WW2xhKlsVV4&m=d33wBKig7
> > +BZ0kss0OOUmmNbl5HWsWnMBDtfFv5-rsXo&s=Nel6OcDfyv8xWX3hGYbELKfEo9XLEwL7
> > +lVrzLKHcB6E&e=
> > +
> > +title: TMP513/512 system monitor sensor
> > +
> > +maintainers:
> > +  - Eric Tremblay <etremblay@distech-controls.com>
> > +
> > +description: |
> > +  This driver implements support for Texas Instruments TMP512, and TMP513.
> 
> I think this needs some rewording. This is not a driver description, but a system description.

Ok, I will only keep the chip description.
> 
> > +  The TMP512 (dual-channel) and TMP513 (triple-channel) are system
> > + monitors  that include remote sensors, a local temperature sensor,
> > + and a high-side  current shunt monitor. These system monitors have
> > + the capability of measuring  remote temperatures, on-chip
> > + temperatures, and system voltage/power/current  consumption.
> > +
> > +  Datasheets:
> > +
> > + https://urldefense.proofpoint.com/v2/url?u=http-3A__www.ti.com_lit_g
> > + pn_tmp513&d=DwICaQ&c=tvvHx3uC0XdtgG-ImPDjfM0QnakIsmmGcwejQDVxD-g&r=y
> > + 34zLl9_AvBy5NSrvskG6SFbiKoCzI99WW2xhKlsVV4&m=d33wBKig7BZ0kss0OOUmmNb
> > + l5HWsWnMBDtfFv5-rsXo&s=7QkDr8n0JXPVmCGRf6jhQPK9h4ECRFMfZJrqttVxWG8&e
> > + =
> > + https://urldefense.proofpoint.com/v2/url?u=http-3A__www.ti.com_lit_g
> > + pn_tmp512&d=DwICaQ&c=tvvHx3uC0XdtgG-ImPDjfM0QnakIsmmGcwejQDVxD-g&r=y
> > + 34zLl9_AvBy5NSrvskG6SFbiKoCzI99WW2xhKlsVV4&m=d33wBKig7BZ0kss0OOUmmNb
> > + l5HWsWnMBDtfFv5-rsXo&s=2c47z9hddR3vxWAytRxK08SO939Tsi12ePH1uGK_oII&e
> > + =
> > +
> > +
> > +properties:
> > +  compatible:
> > +    enum:
> > +      - ti,tmp512
> > +      - ti,tmp513
> > +
> > +  reg:
> > +    maxItems: 1
> > +
> > +  shunt-resistor-micro-ohms:
> > +    description: |
> > +      If 0, the calibration process will be skiped and the current and power
> > +      measurement engine will not work. Temperature and voltage measurement
> > +      will continue to work. The shunt value also need to respect:
> > +      rshunt <= pga-gain * 40 * 1000 * 1000.
> > +      If not, it's not possible to compute a valid calibration value.
> > +    default: 1000
> > +
> > +  ti,pga-gain:
> > +    description: |
> > +      The gain value for the PGA function. This is 8, 4, 2 or 1.
> > +      The PGA gain affect the shunt voltage range.
> > +      The range will be equal to: pga-gain * 40mV
> > +    allOf:
> > +      - $ref: /schemas/types.yaml#/definitions/uint32
> > +    enum: [1, 2, 4, 8]
> > +    default: 8
> > +
> > +  ti,bus-range-microvolt:
> > +    description: |
> > +      This is the operating range of the bus voltage in microvolt
> > +    allOf:
> > +      - $ref: /schemas/types.yaml#/definitions/uint32
> > +    enum: [16000000, 32000000]
> > +    default: 32000000
> > +
> > +  ti,nfactor:
> > +    description: |
> > +      Array of three(TMP513) or two(TMP512) n-Factor value for each remote
> > +      temperature channel.
> > +      See datasheet Table 11 for n-Factor range list and value interpretation.
> > +    allOf:
> > +      - $ref: /schemas/types.yaml#definitions/uint8-array
> > +      - minItems: 2
> > +        maxItems: 3
> > +        items:
> > +          default: 0
> > +
> > +required:
> > +  - compatible
> > +  - reg
> > +
> > +examples:
> > +  - |
> > +    i2c {
> > +        tmp513@5c {
> > +            compatible = "ti,tmp513";
> > +            reg = <0x5C>;
> > +            shunt-resistor-micro-ohms = <330000>;
> > +            ti,bus-range-microvolt = <32000000>;
> > +            ti,pga-gain = <8>;
> > +            ti,nfactor = [01 F3 00];
> > +        };
> > +    };
> >


^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513
  2019-11-12  1:03   ` Rob Herring
@ 2019-11-12 15:01     ` Tremblay, Eric
  0 siblings, 0 replies; 8+ messages in thread
From: Tremblay, Eric @ 2019-11-12 15:01 UTC (permalink / raw)
  To: Rob Herring
  Cc: linux, linux-hwmon, devicetree, linux-doc, jdelvare,
	mark.rutland, corbet

> From: Rob Herring <robh@kernel.org>
> Sent: Monday, November 11, 2019 8:04 PM
> To: Tremblay, Eric <etremblay@distech-controls.com>
> Cc: linux@roeck-us.net; linux-hwmon@vger.kernel.org; devicetree@vger.kernel.org; linux-doc@vger.kernel.org;
> jdelvare@suse.com; mark.rutland@arm.com; corbet@lwn.net
> Subject: Re: [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513
> 
> On Mon, Nov 11, 2019 at 03:34:44PM -0500, etremblay@distech-controls.com wrote:
> > From: Eric Tremblay <etremblay@distech-controls.com>
> >
> > Document the TMP513/512 device devicetree bindings
> >
> > Signed-off-by: Eric Tremblay <etremblay@distech-controls.com>
> > ---
> >  .../devicetree/bindings/hwmon/ti,tmp513.yaml  | 89
> > +++++++++++++++++++
> >  1 file changed, 89 insertions(+)
> >  create mode 100644
> > Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> >
> > diff --git a/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> > b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> > new file mode 100644
> > index 000000000000..de4ed3645e0f
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml
> > @@ -0,0 +1,89 @@
> > +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2
> > +---
> > +
> > +$id:
> > +https://urldefense.proofpoint.com/v2/url?u=http-3A__devicetree.org_sc
> > +hemas_hwmon_ti-2Ctmp513.yaml-23&d=DwIBAg&c=tvvHx3uC0XdtgG-ImPDjfM0Qna
> > +kIsmmGcwejQDVxD-g&r=y34zLl9_AvBy5NSrvskG6SFbiKoCzI99WW2xhKlsVV4&m=sqT
> > +mLsfhqAI5ubkZyE2FvNS0zZ42lJ4Xsiyf12MKPbI&s=cqxgi-ff4mlqg1dH-w0rmoWT9T
> > +G9UEzI4jP4boAzSTQ&e=
> > +$schema:
> > +https://urldefense.proofpoint.com/v2/url?u=http-3A__devicetree.org_me
> > +ta-2Dschemas_core.yaml-23&d=DwIBAg&c=tvvHx3uC0XdtgG-ImPDjfM0QnakIsmmG
> > +cwejQDVxD-g&r=y34zLl9_AvBy5NSrvskG6SFbiKoCzI99WW2xhKlsVV4&m=sqTmLsfhq
> > +AI5ubkZyE2FvNS0zZ42lJ4Xsiyf12MKPbI&s=MFdE05g19en41dOAGudb8oQzrSKdBS_5
> > +uhKXoz3xbfM&e=
> > +
> > +title: TMP513/512 system monitor sensor
> > +
> > +maintainers:
> > +  - Eric Tremblay <etremblay@distech-controls.com>
> > +
> > +description: |
> > +  This driver implements support for Texas Instruments TMP512, and TMP513.
> > +  The TMP512 (dual-channel) and TMP513 (triple-channel) are system
> > +monitors
> > +  that include remote sensors, a local temperature sensor, and a
> > +high-side
> > +  current shunt monitor. These system monitors have the capability of
> > +measuring
> > +  remote temperatures, on-chip temperatures, and system
> > +voltage/power/current
> > +  consumption.
> > +
> > +  Datasheets:
> > +
> > + https://urldefense.proofpoint.com/v2/url?u=http-3A__www.ti.com_lit_g
> > + pn_tmp513&d=DwIBAg&c=tvvHx3uC0XdtgG-ImPDjfM0QnakIsmmGcwejQDVxD-g&r=y
> > + 34zLl9_AvBy5NSrvskG6SFbiKoCzI99WW2xhKlsVV4&m=sqTmLsfhqAI5ubkZyE2FvNS
> > + 0zZ42lJ4Xsiyf12MKPbI&s=aRuMhzI-UQfQORXhmK3OaA7UZ_6segIbgQ4k5SoKF0E&e
> > + =
> > + https://urldefense.proofpoint.com/v2/url?u=http-3A__www.ti.com_lit_g
> > + pn_tmp512&d=DwIBAg&c=tvvHx3uC0XdtgG-ImPDjfM0QnakIsmmGcwejQDVxD-g&r=y
> > + 34zLl9_AvBy5NSrvskG6SFbiKoCzI99WW2xhKlsVV4&m=sqTmLsfhqAI5ubkZyE2FvNS
> > + 0zZ42lJ4Xsiyf12MKPbI&s=mMNKG3dkkscHl5e73jC-ESFBdu05NhcCuJ-Pfpwls7E&e
> > + =
> > +
> > +
> > +properties:
> > +  compatible:
> > +    enum:
> > +      - ti,tmp512
> > +      - ti,tmp513
> > +
> > +  reg:
> > +    maxItems: 1
> > +
> > +  shunt-resistor-micro-ohms:
> > +    description: |
> > +      If 0, the calibration process will be skiped and the current and power
> > +      measurement engine will not work. Temperature and voltage measurement
> > +      will continue to work. The shunt value also need to respect:
> > +      rshunt <= pga-gain * 40 * 1000 * 1000.
> > +      If not, it's not possible to compute a valid calibration value.
> > +    default: 1000
> > +
> > +  ti,pga-gain:
> > +    description: |
> > +      The gain value for the PGA function. This is 8, 4, 2 or 1.
> > +      The PGA gain affect the shunt voltage range.
> > +      The range will be equal to: pga-gain * 40mV
> > +    allOf:
> > +      - $ref: /schemas/types.yaml#/definitions/uint32
> > +    enum: [1, 2, 4, 8]
> > +    default: 8
> > +
> > +  ti,bus-range-microvolt:
> > +    description: |
> > +      This is the operating range of the bus voltage in microvolt
> > +    allOf:
> > +      - $ref: /schemas/types.yaml#/definitions/uint32
> > +    enum: [16000000, 32000000]
> > +    default: 32000000
> > +
> > +  ti,nfactor:
> > +    description: |
> > +      Array of three(TMP513) or two(TMP512) n-Factor value for each remote
> > +      temperature channel.
> > +      See datasheet Table 11 for n-Factor range list and value interpretation.
> > +    allOf:
> > +      - $ref: /schemas/types.yaml#definitions/uint8-array
> > +      - minItems: 2
> > +        maxItems: 3
> > +        items:
> > +          default: 0
> > +
> > +required:
> > +  - compatible
> > +  - reg
> > +
> > +examples:
> > +  - |
> > +    i2c {
> 
> 'make dt_binding_check' fails. You need #address-cells and #size-cells in here:
> 
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:20.17-30: Warning (reg_format): /example-
> 0/i2c/tmp513@5c:reg: property has invalid length (4 bytes) (#address-cells == 2, #size-cells == 1)
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dt.yaml: Warning (pci_device_bus_num): Failed prerequisite
> 'reg_format'
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:17.5-26.11: Warning (i2c_bus_bridge): /example-0/i2c: incorrect
> #address-cells for I2C bus
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:17.5-26.11: Warning (i2c_bus_bridge): /example-0/i2c: incorrect
> #size-cells for I2C bus
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dt.yaml: Warning (i2c_bus_reg): Failed prerequisite 'reg_format'
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dt.yaml: Warning (i2c_bus_reg): Failed prerequisite 'i2c_bus_bridge'
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dt.yaml: Warning (spi_bus_reg): Failed prerequisite 'reg_format'
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:18.23-25.15:
> Warning (avoid_default_addr_size): /example-0/i2c/tmp513@5c: Relying on default #address-cells value
> Documentation/devicetree/bindings/hwmon/ti,tmp513.example.dts:18.23-25.15: Warning (avoid_default_addr_size): /example-
> 0/i2c/tmp513@5c: Relying on default #size-cells value

Sorry I was confused a little by the documentation. I did not saw that I could run `make dt_binding_check' on a single file.

I ran it on my tree and it gives me a warning on another file. I wrongly assume that it ran all over the tree.

I`ll fix those issue.

Thanks
> 
> > +        tmp513@5c {
> > +            compatible = "ti,tmp513";
> > +            reg = <0x5C>;
> > +            shunt-resistor-micro-ohms = <330000>;
> > +            ti,bus-range-microvolt = <32000000>;
> > +            ti,pga-gain = <8>;
> > +            ti,nfactor = [01 F3 00];
> > +        };
> > +    };
> > --
> > 2.17.1
> >

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-11-12 15:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-11 20:34 [PATCH v7 0/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips etremblay
2019-11-11 20:34 ` [PATCH v7 1/2] dt-bindings: hwmon: Add TMP512/513 etremblay
2019-11-12  1:03   ` Rob Herring
2019-11-12 15:01     ` Tremblay, Eric
2019-11-12  1:45   ` Guenter Roeck
2019-11-12 14:40     ` Tremblay, Eric
2019-11-11 20:34 ` [PATCH v7 2/2] hwmon: Add driver for Texas Instruments TMP512/513 sensor chips etremblay
2019-11-12 13:41   ` Guenter Roeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.