Skip to contentSkip to conversation

hwmon: Add Baikal-T1 SoC Process, Voltage and Temp sensor support

PATCH Superseded Serge Semin 3 participants 11 messages May 26, 2020 – May 27, 2020

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover hwmon: Add Baikal-T1 SoC Process, Voltage and Temp sensor support

In order to keep track of Baikal-T1 SoC power consumption and make sure the chip heating is within the normal temperature limits, there is a dedicated hardware monitor sensor embedded into the SoC. It is based on the Analog Bits PVT sensor but equipped with a vendor-specific control wrapper, which ease an access to the sensors functionality. Fist of all it provides an accessed to the sampled Temperature, Voltage and Low/Standard/High Voltage thresholds. In addition the wrapper generates an interrupt in case if one enabled for alarm thresholds or data ready event. All of these functionality is implemented in the Baikal-T1 PVT driver submitted within this patchset. Naturally there is also a patch, which creates a corresponding yaml-based dt-binding file for the sensor.

This patchset is rebased and tested on the mainline Linux kernel 5.6-rc4:
base-commit: 0e698dfa2822 ("Linux 5.7-rc4")
tag: v5.7-rc4

Note new vendor prefix for Baikal-T1 PVT device will be added in the framework of the next patchset:
https://lkml.org/lkml/2020/5/6/1047

Changelog v2:
- Don't use a multi-arg clock phandle reference in the examples dt-bindings property. Thus reundant include pre-processor statement can be removed.
- Rearrange the SoBs with adding Maxim' co-development tag.
- Lowercase the node-name in the dt-schema example.
- Add dual license header to the dt-bindings file.
- Replace "additionalProperties: false" property with "unevaluatedProperties: false".
- Discard label definition from the binding example.
- Discard handwritten IO-access wrappers. Use normal readl/writel instead.
- Use generic FIELD_{GET,PREP} macros instead of handwritten ones.
- Since the driver depends on the OF config we can remove of_match_ptr() macro utilization.
- Don't print error-message if no platform IRQ found. Just return an error.
- Remove probe-status info string printout.
- Our corporate email server doesn't change Message-Id anymore, so the patchset is resubmitted being in the cover-letter-threaded format.

Changelog v3:
- Add bt1-pvt into the Documentation/hwmon/index.rst file.
- Discard explicit "default n" from the SENSORS_BT1_PVT_ALARMS config.
- Use "depends on SENSORS_BT1_PVT" statement instead of if-endif kbuild config clause.
- Alphabetically order the include macro operators.
- Discard unneeded include macro in the header file.
- Use new generic interface of the hwmon alarms notifications introduced in the first patch (based on hwmon_notify_event()).
- Add more descriptive information regarding the temp1_trim attribute.
- Discard setting the platforms device private data by using platform_set_drvdata(). It's redundant since unused in the driver.
- Pass "pvt" hwmon name instead of dev_name(dev) to devm_hwmon_device_register_with_info().
- Add "baikal,pvt-temp-trim-millicelsius" temperature trim DT property support.
- Discard kernel log warnings printed from the ISR when either min or max threshold levels are crossed.
- Discard CONFIG_OF dependency since there is non at compile-time.

Cc Alexey Malahov <[email protected]>
Cc Pavel Parkhomenko <[email protected]>
Cc Ramil Zaripov <[email protected]>
Cc Ekaterina Skachko <[email protected]>
Cc Vadim Vlasov <[email protected]>
Cc Alexey Kolotnikov <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Rob Herring <[email protected]>

Guenter Roeck (1):
hwmon: Add notification support

Serge Semin (2):
dt-bindings: hwmon: Add Baikal-T1 PVT sensor binding
hwmon: Add Baikal-T1 PVT sensor driver

 .../bindings/hwmon/baikal,bt1-pvt.yaml        |  107 ++
 Documentation/hwmon/bt1-pvt.rst               |  116 ++
 Documentation/hwmon/index.rst                 |    1 +
 drivers/hwmon/Kconfig                         |   25 +
 drivers/hwmon/Makefile                        |    1 +
 drivers/hwmon/bt1-pvt.c                       | 1155 +++++++++++++++++
 drivers/hwmon/bt1-pvt.h                       |  244 ++++
 drivers/hwmon/hwmon.c                         |   69 +-
 include/linux/hwmon.h                         |    3 +
 9 files changed, 1718 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/hwmon/baikal,bt1-pvt.yaml
 create mode 100644 Documentation/hwmon/bt1-pvt.rst
 create mode 100644 drivers/hwmon/bt1-pvt.c
 create mode 100644 drivers/hwmon/bt1-pvt.h
Signature · 1 line
2.26.2
#1 lore raw

PATCH v3 1/3 dt-bindings: hwmon: Add Baikal-T1 PVT sensor binding

Baikal-T1 SoC is equipped with an embedded process, voltage and temperature sensor to monitor the chip internal environment like temperature, supply voltage and transistors performance.

This bindings describes the external Baikal-T1 PVT control interfaces like MMIO registers space, interrupt request number and clocks source. These are then used by the corresponding hwmon device driver to implement the sysfs files-based access to the sensors functionality.

---

Changelog v2:
- Lowercase the node-name in the example.
- Add dual-license header.
- Don't use a multi-arg clock phandle reference in the examples dt-binding property. Thus reundant include pre-processor statement can be removed.
- Replace "additionalProperties: false" property with "unevaluatedProperties: false".
- Discard label definition from the example.
- Align settings of the "#thermal-sensor-cells" property with two spaces ahead of the property line start.

Changelog v3:
- Add "baikal,pvt-temp-trim-millicelsius" property support.

---
 .../bindings/hwmon/baikal,bt1-pvt.yaml        | 107 ++++++++++++++++++
 1 file changed, 107 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/baikal,bt1-pvt.yaml
Documentation/devicetree/bindings/hwmon/baikal,bt1-pvt.yaml new file +107 -0
new file mode 100644
index 000000000000..6c3a44ec3684
@@ -0,0 +1,107 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+# Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/baikal,bt1-pvt.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Baikal-T1 PVT Sensor
+
+maintainers:
+  - Serge Semin <[email protected]>
+
+description: |
+  Baikal-T1 SoC provides an embedded process, voltage and temperature
+  sensor to monitor an internal SoC environment (chip temperature, supply
+  voltage and process monitor) and on time detect critical situations,
+  which may cause the system instability and even damages. The IP-block
+  is based on the Analog Bits PVT sensor, but is equipped with a dedicated
+  control wrapper, which provides a MMIO registers-based access to the
+  sensor core functionality (APB3-bus based) and exposes an additional
+  functions like thresholds/data ready interrupts, its status and masks,
+  measurements timeout. Its internal structure is depicted on the next
+  diagram:
+
+     Analog Bits core                     Bakal-T1 PVT control block
+  +--------------------+                  +------------------------+
+  | Temperature sensor |-+         +------| Sensors control        |
+  |--------------------| |<---En---|      |------------------------|
+  | Voltage sensor     |-|<--Mode--| +--->| Sampled data           |
+  |--------------------| |<--Trim--+ |    |------------------------|
+  | Low-Vt sensor      |-|           | +--| Thresholds comparator  |
+  |--------------------| |---Data----| |  |------------------------|
+  | High-Vt sensor     |-|           | +->| Interrupts status      |
+  |--------------------| |--Valid--+-+ |  |------------------------|
+  | Standard-Vt sensor |-+         +---+--| Interrupts mask        |
+  +--------------------+                  |------------------------|
+           ^                              | Interrupts timeout     |
+           |                              +------------------------+
+           |                                        ^  ^
+  Rclk-----+----------------------------------------+  |
+  APB3-------------------------------------------------+
+
+  This bindings describes the external Baikal-T1 PVT control interfaces
+  like MMIO registers space, interrupt request number and clocks source.
+  These are then used by the corresponding hwmon device driver to
+  implement the sysfs files-based access to the sensors functionality.
+
+properties:
+  compatible:
+    const: baikal,bt1-pvt
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: PVT reference clock
+      - description: APB3 interface clock
+
+  clock-names:
+    items:
+      - const: ref
+      - const: pclk
+
+  "#thermal-sensor-cells":
+    description: Baikal-T1 can be referenced as the CPU thermal-sensor
+    const: 0
+
+  baikal,pvt-temp-trim-millicelsius:
+    description: |
+      Temperature sensor trimming factor. It can be used to manually adjust the
+      temperature measurements within 7.130 degrees Celsius.
+    maxItems: 1
+    items:
+      default: 0
+      minimum: 0
+      maximum: 7130
+
+unevaluatedProperties: false
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/mips-gic.h>
+
+    pvt@1f200000 {
+      compatible = "baikal,bt1-pvt";
+      reg = <0x1f200000 0x1000>;
+      #thermal-sensor-cells = <0>;
+
+      interrupts = <GIC_SHARED 31 IRQ_TYPE_LEVEL_HIGH>;
+
+      baikal,pvt-temp-trim-millicelsius = <1000>;
+
+      clocks = <&ccu_sys>, <&ccu_sys>;
+      clock-names = "ref", "pclk";
+    };
+...
Signature · 1 line
2.26.2
#2 lore raw

PATCH v3 2/3 hwmon: Add notification support

In reply to Serge Semin

From: Guenter Roeck <[email protected]>

For hwmon drivers using the hwmon_device_register_with_info() API, it is desirable to have a generic notification mechanism available. This mechanism can be used to notify userspace as well as the thermal subsystem if the driver experiences any events, such as warning or critical alarms.

Implement hwmon_notify_event() to provide this mechanism. The function generates a sysfs event and a udev event. If the device is registered with the thermal subsystem and the event is associated with a temperature sensor, also notify the thermal subsystem that a thermal event occurred.

Cc Maxim Kaurkin <[email protected]>
Cc Alexey Malahov <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Rob Herring <[email protected]>

---

Guenter: I have no plan to push this since there are currently no users (and thus no means to test the code). However, I want to make sure that the patch is available if/when needed.
Serge: There is a user now. It's Baikal-T1 PVT driver. So the patch can be merged in.

Changelog v2: Skipped

Changelog v3:
- Fix merge conflicts found in struct hwmon_thermal_data and in hwmon_thermal_add_sensors().
- Use hwmon_attr_base() to get the hwmon attribute base index.
- Use the base value to get the sysfs-attribute name. hwmon_thermal_notify() is supposed to be called with normal index.

---
 drivers/hwmon/hwmon.c | 69 +++++++++++++++++++++++++++++++++++++++++--
 include/linux/hwmon.h |  3 ++
 2 files changed, 69 insertions(+), 3 deletions(-)
drivers/hwmon/hwmon.c +66 -3
index 6a30fb453f7a..e9bc33c18a33 100644
@@ -15,6 +15,7 @@
 #include <linux/gfp.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
+#include <linux/list.h>
 #include <linux/module.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
@@ -31,7 +32,7 @@ struct hwmon_device {
 	const char *name;
 	struct device dev;
 	const struct hwmon_chip_info *chip;
-
+	struct list_head tzdata;
 	struct attribute_group group;
 	const struct attribute_group **groups;
 };
@@ -55,12 +56,12 @@ struct hwmon_device_attribute {
 
 /*
  * Thermal zone information
- * In addition to the reference to the hwmon device,
- * also provides the sensor index.
  */
 struct hwmon_thermal_data {
+	struct list_head node;		/* hwmon tzdata list entry */
 	struct device *dev;		/* Reference to hwmon device */
 	int index;			/* sensor index */
+	struct thermal_zone_device *tzd;/* thermal zone device */
 };
 
 static ssize_t
@@ -156,10 +157,17 @@ static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
 	.get_temp = hwmon_thermal_get_temp,
 };
 
+static void hwmon_thermal_remove_sensor(void *data)
+{
+	list_del(data);
+}
+
 static int hwmon_thermal_add_sensor(struct device *dev, int index)
 {
+	struct hwmon_device *hwdev = to_hwmon_device(dev);
 	struct hwmon_thermal_data *tdata;
 	struct thermal_zone_device *tzd;
+	int err;
 
 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
 	if (!tdata)
@@ -177,13 +185,37 @@ static int hwmon_thermal_add_sensor(struct device *dev, int index)
 	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
 		return PTR_ERR(tzd);
 
+	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
+	if (err)
+		return err;
+
+	tdata->tzd = tzd;
+	list_add(&tdata->node, &hwdev->tzdata);
+
 	return 0;
 }
+
+static void hwmon_thermal_notify(struct device *dev, int index)
+{
+	struct hwmon_device *hwdev = to_hwmon_device(dev);
+	struct hwmon_thermal_data *tzdata;
+
+	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
+		if (tzdata->index == index) {
+			thermal_zone_device_update(tzdata->tzd,
+						   THERMAL_EVENT_UNSPECIFIED);
+		}
+	}
+}
+
 #else
 static int hwmon_thermal_add_sensor(struct device *dev, int index)
 {
 	return 0;
 }
+
+static void hwmon_thermal_notify(struct device *dev, int index) { }
+
 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
 
 static int hwmon_attr_base(enum hwmon_sensor_types type)
@@ -511,6 +543,35 @@ static const int __templates_size[] = {
 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
 };
 
+int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
+		       u32 attr, int channel)
+{
+	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
+	const char * const *templates;
+	const char *template;
+	int base;
+
+	if (type >= ARRAY_SIZE(__templates))
+		return -EINVAL;
+	if (attr >= __templates_size[type])
+		return -EINVAL;
+
+	templates = __templates[type];
+	template = templates[attr];
+
+	base = hwmon_attr_base(type);
+
+	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
+	sysfs_notify(&dev->kobj, NULL, sattr);
+	kobject_uevent(&dev->kobj, KOBJ_CHANGE);
+
+	if (type == hwmon_temp)
+		hwmon_thermal_notify(dev, channel);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(hwmon_notify_event);
+
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
 {
 	int i, n;
@@ -661,6 +722,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 	if (err)
 		goto free_hwmon;
 
+	INIT_LIST_HEAD(&hwdev->tzdata);
+
 	if (dev && dev->of_node && chip && chip->ops->read &&
 	    chip->info[0]->type == hwmon_chip &&
 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
include/linux/hwmon.h +3 -0
index 5e609f25878c..363d4a814aa1 100644
@@ -436,6 +436,9 @@ devm_hwmon_device_register_with_info(struct device *dev,
 void hwmon_device_unregister(struct device *dev);
 void devm_hwmon_device_unregister(struct device *dev);
 
+int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
+		       u32 attr, int channel);
+
 /**
  * hwmon_is_bad_char - Is the char invalid in a hwmon name
  * @ch: the char to be considered
Signature · 1 line
2.26.2
#3 lore raw

PATCH v3 3/3 hwmon: Add Baikal-T1 PVT sensor driver

In reply to Serge Semin

Baikal-T1 SoC provides an embedded process, voltage and temperature sensor to monitor an internal SoC environment (chip temperature, supply voltage and process monitor) and on time detect critical situations, which may cause the system instability and even damages. The IP-block is based on the Analog Bits PVT sensor, but is equipped with a dedicated control wrapper, which provides a MMIO registers-based access to the sensor core functionality (APB3-bus based) and exposes an additional functions like thresholds/data ready interrupts, its status and masks, measurements timeout. All of these is used to create a hwmon driver being added to the kernel by this commit.

The driver implements support for the hardware monitoring capabilities of Baikal-T1 process, voltage and temperature sensors. PVT IP-core consists of one temperature and four voltage sensors, each of which is implemented as a dedicated hwmon channel config.

The driver can optionally provide the hwmon alarms for each sensor the PVT controller supports. The alarms functionality is made compile-time configurable due to the hardware interface implementation peculiarity, which is connected with an ability to convert data from only one sensor at a time. Additional limitation is that the controller performs the thresholds checking synchronously with the data conversion procedure. Due to these limitations in order to have the hwmon alarms automatically detected the driver code must switch from one sensor to another, read converted data and manually check the threshold status bits. Depending on the measurements timeout settings this design may cause additional burden on the system performance. By default if the alarms kernel config is disabled the data conversion is performed by the driver on demand when read operation is requested via corresponding _input-file.

---

Changelog v2:
- Discard handwritten IO-access wrappers. Use normal readl/writel instead.
- Use generic FIELD_{GET,PREP} macros instead of handwritten ones.
- Since the driver depends on the OF config we can remove of_match_ptr() macro utilization.
- Don't print error-message if no platform IRQ found. Just return an error.
- Remove probe-status info string printout.

Changelog v3:
- Add bt1-pvt into the Documentation/hwmon/index.rst file.
- Discard explicit "default n" from the SENSORS_BT1_PVT_ALARMS config.
- Use "depends on SENSORS_BT1_PVT" statement instead of if-endif kbuild config clause.
- Alphabetically order the include macro operators.
- Discard unneeded include macro in the header file.
- Use new generic interface of the hwmon alarms notifications introduced in the first patch (based on hwmon_notify_event()).
- Add more descriptive information regarding the temp1_trim attribute.
- Discard setting the platforms device private data by using platform_set_drvdata(). It's redundant since unused in the driver.
- Pass "pvt" hwmon name instead of dev_name(dev) to devm_hwmon_device_register_with_info().
- Add "baikal,pvt-temp-trim-millicelsius" temperature trim DT property support.
- Discard kernel log warnings printed from the ISR when either min or max threshold levels are crossed.
- Discard CONFIG_OF dependency since there is non at compile-time.

---
 Documentation/hwmon/bt1-pvt.rst |  116 ++++
 Documentation/hwmon/index.rst   |    1 +
 drivers/hwmon/Kconfig           |   25 +
 drivers/hwmon/Makefile          |    1 +
 drivers/hwmon/bt1-pvt.c         | 1155 +++++++++++++++++++++++++++++++
 drivers/hwmon/bt1-pvt.h         |  244 +++++++
 6 files changed, 1542 insertions(+)
 create mode 100644 Documentation/hwmon/bt1-pvt.rst
 create mode 100644 drivers/hwmon/bt1-pvt.c
 create mode 100644 drivers/hwmon/bt1-pvt.h
Documentation/hwmon/bt1-pvt.rst new file +116 -0
new file mode 100644
index 000000000000..f5f47891d87a
@@ -0,0 +1,116 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Kernel driver bt1-pvt
+=====================
+
+Supported chips:
+
+  * Baikal-T1 PVT sensor (in SoC)
+
+    Prefix: 'bt1-pvt'
+
+    Addresses scanned: -
+
+    Datasheet: Provided by BAIKAL ELECTRONICS upon request and under NDA
+
+Authors:
+    Maxim Kaurkin <[email protected]>
+    Serge Semin <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the hardware monitoring capabilities of the
+embedded into Baikal-T1 process, voltage and temperature sensors. PVT IP-core
+consists of one temperature and four voltage sensors, which can be used to
+monitor the chip internal environment like heating, supply voltage and
+transistors performance. The driver can optionally provide the hwmon alarms
+for each sensor the PVT controller supports. The alarms functionality is made
+compile-time configurable due to the hardware interface implementation
+peculiarity, which is connected with an ability to convert data from only one
+sensor at a time. Additional limitation is that the controller performs the
+thresholds checking synchronously with the data conversion procedure. Due to
+these in order to have the hwmon alarms automatically detected the driver code
+must switch from one sensor to another, read converted data and manually check
+the threshold status bits. Depending on the measurements timeout settings
+(update_interval sysfs node value) this design may cause additional burden on
+the system performance. So in case if alarms are unnecessary in your system
+design it's recommended to have them disabled to prevent the PVT IRQs being
+periodically raised to get the data cache/alarms status up to date. By default
+in alarm-less configuration the data conversion is performed by the driver
+on demand when read operation is requested via corresponding _input-file.
+
+Temperature Monitoring
+----------------------
+
+Temperature is measured with 10-bit resolution and reported in millidegree
+Celsius. The driver performs all the scaling by itself therefore reports true
+temperatures that don't need any user-space adjustments. While the data
+translation formulae isn't linear, which gives us non-linear discreteness,
+it's close to one, but giving a bit better accuracy for higher temperatures.
+The temperature input is mapped as follows (the last column indicates the input
+ranges)::
+
+	temp1: CPU embedded diode	-48.38C - +147.438C
+
+In case if the alarms kernel config is enabled in the driver the temperature input
+has associated min and max limits which trigger an alarm when crossed.
+
+Voltage Monitoring
+------------------
+
+The voltage inputs are also sampled with 10-bit resolution and reported in
+millivolts. But in this case the data translation formulae is linear, which
+provides a constant measurements discreteness. The data scaling is also
+performed by the driver, so returning true millivolts. The voltage inputs are
+mapped as follows (the last column indicates the input ranges)::
+
+	in0: VDD		(processor core)		0.62V - 1.168V
+	in1: Low-Vt		(low voltage threshold)		0.62V - 1.168V
+	in2: High-Vt		(high voltage threshold)	0.62V - 1.168V
+	in3: Standard-Vt	(standard voltage threshold)	0.62V - 1.168V
+
+In case if the alarms configis enabled in the driver the voltage inputs
+have associated min and max limits which trigger an alarm when crossed.
+
+Sysfs Attributes
+----------------
+
+Following is a list of all sysfs attributes that the driver provides, their
+permissions and a short description:
+
+=============================== ======= =======================================
+Name				Perm	Description
+=============================== ======= =======================================
+update_interval			RW	Measurements update interval per
+					sensor.
+temp1_type			RO	Sensor type (always 1 as CPU embedded
+					diode).
+temp1_label			RO	CPU Core Temperature sensor.
+temp1_input			RO	Measured temperature in millidegree
+					Celsius.
+temp1_min			RW	Low limit for temp input.
+temp1_max			RW	High limit for temp input.
+temp1_min_alarm			RO	Temperature input alarm. Returns 1 if
+					temperature input went below min limit,
+					0 otherwise.
+temp1_max_alarm			RO	Temperature input alarm. Returns 1 if
+					temperature input went above max limit,
+					0 otherwise.
+temp1_trim			RW	Temperature sensor trimming factor in
+					millidegree Celsius. It can be used to
+					manually adjust the temperature
+					measurements within 7.130 degrees
+					Celsius.
+in[0-3]_label			RO	CPU Voltage sensor (either core or
+					low/high/standard thresholds).
+in[0-3]_input			RO	Measured voltage in millivolts.
+in[0-3]_min			RW	Low limit for voltage input.
+in[0-3]_max			RW	High limit for voltage input.
+in[0-3]_min_alarm		RO	Voltage input alarm. Returns 1 if
+					voltage input went below min limit,
+					0 otherwise.
+in[0-3]_max_alarm		RO	Voltage input alarm. Returns 1 if
+					voltage input went above max limit,
+					0 otherwise.
+=============================== ======= =======================================
Documentation/hwmon/index.rst +1 -0
index 8ef62fd39787..3f322d2f1d20 100644
@@ -43,6 +43,7 @@ Hardware Monitoring Kernel Drivers
    asc7621
    aspeed-pwm-tacho
    bel-pfe
+   bt1-pvt
    coretemp
    da9052
    da9055
drivers/hwmon/Kconfig +25 -0
index 4c62f900bf7e..d1c6c5d46c08 100644
@@ -404,6 +404,31 @@ config SENSORS_ATXP1
 	  This driver can also be built as a module. If so, the module
 	  will be called atxp1.
 
+config SENSORS_BT1_PVT
+	tristate "Baikal-T1 Process, Voltage, Temperature sensor driver"
+	depends on MIPS_BAIKAL_T1 || COMPILE_TEST
+	help
+	  If you say yes here you get support for Baikal-T1 PVT sensor
+	  embedded into the SoC.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called bt1-pvt.
+
+config SENSORS_BT1_PVT_ALARMS
+	bool "Enable Baikal-T1 PVT sensor alarms"
+	depends on SENSORS_BT1_PVT
+	help
+	  Baikal-T1 PVT IP-block provides threshold registers for each
+	  supported sensor. But the corresponding interrupts might be
+	  generated by the thresholds comparator only in synchronization with
+	  a data conversion. Additionally there is only one sensor data can
+	  be converted at a time. All of these makes the interface impossible
+	  to be used for the hwmon alarms implementation without periodic
+	  switch between the PVT sensors. By default the data conversion is
+	  performed on demand from the user-space. If this config is enabled
+	  the data conversion will be periodically performed and the data will be
+	  saved in the internal driver cache.
+
 config SENSORS_DRIVETEMP
 	tristate "Hard disk drives with temperature sensors"
 	depends on SCSI && ATA
drivers/hwmon/Makefile +1 -0
index b0b9c8e57176..408b86a5e71d 100644
@@ -53,6 +53,7 @@ obj-$(CONFIG_SENSORS_ASC7621)	+= asc7621.o
 obj-$(CONFIG_SENSORS_ASPEED)	+= aspeed-pwm-tacho.o
 obj-$(CONFIG_SENSORS_ATXP1)	+= atxp1.o
 obj-$(CONFIG_SENSORS_AXI_FAN_CONTROL) += axi-fan-control.o
+obj-$(CONFIG_SENSORS_BT1_PVT)	+= bt1-pvt.o
 obj-$(CONFIG_SENSORS_CORETEMP)	+= coretemp.o
 obj-$(CONFIG_SENSORS_DA9052_ADC)+= da9052-hwmon.o
 obj-$(CONFIG_SENSORS_DA9055)+= da9055-hwmon.o
drivers/hwmon/bt1-pvt.c new file +1155 -0

1,155 lines not on this page · raw message

drivers/hwmon/bt1-pvt.h new file +244 -0

244 lines not on this page · raw message

Signature · 1 line
2.26.2
maintainer
#4 lore raw

Re: [PATCH v3 3/3] hwmon: Add Baikal-T1 PVT sensor driver

Quoted 181 lines · Serge Semin

Baikal-T1 SoC provides an embedded process, voltage and temperature sensor to monitor an internal SoC environment (chip temperature, supply voltage and process monitor) and on time detect critical situations, which may cause the system instability and even damages. The IP-block is based on the Analog Bits PVT sensor, but is equipped with a dedicated control wrapper, which provides a MMIO registers-based access to the sensor core functionality (APB3-bus based) and exposes an additional functions like thresholds/data ready interrupts, its status and masks, measurements timeout. All of these is used to create a hwmon driver being added to the kernel by this commit.

The driver implements support for the hardware monitoring capabilities of Baikal-T1 process, voltage and temperature sensors. PVT IP-core consists of one temperature and four voltage sensors, each of which is implemented as a dedicated hwmon channel config.

The driver can optionally provide the hwmon alarms for each sensor the PVT controller supports. The alarms functionality is made compile-time configurable due to the hardware interface implementation peculiarity, which is connected with an ability to convert data from only one sensor at a time. Additional limitation is that the controller performs the thresholds checking synchronously with the data conversion procedure. Due to these limitations in order to have the hwmon alarms automatically detected the driver code must switch from one sensor to another, read converted data and manually check the threshold status bits. Depending on the measurements timeout settings this design may cause additional burden on the system performance. By default if the alarms kernel config is disabled the data conversion is performed by the driver on demand when read operation is requested via corresponding _input-file.

---

Changelog v2:
- Discard handwritten IO-access wrappers. Use normal readl/writel instead.
- Use generic FIELD_{GET,PREP} macros instead of handwritten ones.
- Since the driver depends on the OF config we can remove of_match_ptr() macro utilization.
- Don't print error-message if no platform IRQ found. Just return an error.
- Remove probe-status info string printout.

Changelog v3:
- Add bt1-pvt into the Documentation/hwmon/index.rst file.
- Discard explicit "default n" from the SENSORS_BT1_PVT_ALARMS config.
- Use "depends on SENSORS_BT1_PVT" statement instead of if-endif kbuild config clause.
- Alphabetically order the include macro operators.
- Discard unneeded include macro in the header file.
- Use new generic interface of the hwmon alarms notifications introduced in the first patch (based on hwmon_notify_event()).
- Add more descriptive information regarding the temp1_trim attribute.
- Discard setting the platforms device private data by using platform_set_drvdata(). It's redundant since unused in the driver.
- Pass "pvt" hwmon name instead of dev_name(dev) to devm_hwmon_device_register_with_info().
- Add "baikal,pvt-temp-trim-millicelsius" temperature trim DT property support.
- Discard kernel log warnings printed from the ISR when either min or max threshold levels are crossed.
- Discard CONFIG_OF dependency since there is non at compile-time.

---
 Documentation/hwmon/bt1-pvt.rst |  116 ++++
 Documentation/hwmon/index.rst   |    1 +
 drivers/hwmon/Kconfig           |   25 +
 drivers/hwmon/Makefile          |    1 +
 drivers/hwmon/bt1-pvt.c         | 1155 +++++++++++++++++++++++++++++++
 drivers/hwmon/bt1-pvt.h         |  244 +++++++
 6 files changed, 1542 insertions(+)
 create mode 100644 Documentation/hwmon/bt1-pvt.rst
 create mode 100644 drivers/hwmon/bt1-pvt.c
 create mode 100644 drivers/hwmon/bt1-pvt.h
Documentation/hwmon/bt1-pvt.rst new file +94 -0
new file mode 100644
index 000000000000..f5f47891d87a
@@ -0,0 +1,116 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Kernel driver bt1-pvt
+=====================
+
+Supported chips:
+
+  * Baikal-T1 PVT sensor (in SoC)
+
+    Prefix: 'bt1-pvt'
+
+    Addresses scanned: -
+
+    Datasheet: Provided by BAIKAL ELECTRONICS upon request and under NDA
+
+Authors:
+    Maxim Kaurkin <[email protected]>
+    Serge Semin <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the hardware monitoring capabilities of the
+embedded into Baikal-T1 process, voltage and temperature sensors. PVT IP-core
+consists of one temperature and four voltage sensors, which can be used to
+monitor the chip internal environment like heating, supply voltage and
+transistors performance. The driver can optionally provide the hwmon alarms
+for each sensor the PVT controller supports. The alarms functionality is made
+compile-time configurable due to the hardware interface implementation
+peculiarity, which is connected with an ability to convert data from only one
+sensor at a time. Additional limitation is that the controller performs the
+thresholds checking synchronously with the data conversion procedure. Due to
+these in order to have the hwmon alarms automatically detected the driver code
+must switch from one sensor to another, read converted data and manually check
+the threshold status bits. Depending on the measurements timeout settings
+(update_interval sysfs node value) this design may cause additional burden on
+the system performance. So in case if alarms are unnecessary in your system
+design it's recommended to have them disabled to prevent the PVT IRQs being
+periodically raised to get the data cache/alarms status up to date. By default
+in alarm-less configuration the data conversion is performed by the driver
+on demand when read operation is requested via corresponding _input-file.
+
+Temperature Monitoring
+----------------------
+
+Temperature is measured with 10-bit resolution and reported in millidegree
+Celsius. The driver performs all the scaling by itself therefore reports true
+temperatures that don't need any user-space adjustments. While the data
+translation formulae isn't linear, which gives us non-linear discreteness,
+it's close to one, but giving a bit better accuracy for higher temperatures.
+The temperature input is mapped as follows (the last column indicates the input
+ranges)::
+
+	temp1: CPU embedded diode	-48.38C - +147.438C
+
+In case if the alarms kernel config is enabled in the driver the temperature input
+has associated min and max limits which trigger an alarm when crossed.
+
+Voltage Monitoring
+------------------
+
+The voltage inputs are also sampled with 10-bit resolution and reported in
+millivolts. But in this case the data translation formulae is linear, which
+provides a constant measurements discreteness. The data scaling is also
+performed by the driver, so returning true millivolts. The voltage inputs are
+mapped as follows (the last column indicates the input ranges)::
+
+	in0: VDD		(processor core)		0.62V - 1.168V
+	in1: Low-Vt		(low voltage threshold)		0.62V - 1.168V
+	in2: High-Vt		(high voltage threshold)	0.62V - 1.168V
+	in3: Standard-Vt	(standard voltage threshold)	0.62V - 1.168V
+
+In case if the alarms configis enabled in the driver the voltage inputs
+have associated min and max limits which trigger an alarm when crossed.
+
+Sysfs Attributes
+----------------
+
+Following is a list of all sysfs attributes that the driver provides, their
+permissions and a short description:
+
+=============================== ======= =======================================
+Name				Perm	Description
+=============================== ======= =======================================
+update_interval			RW	Measurements update interval per
+					sensor.
+temp1_type			RO	Sensor type (always 1 as CPU embedded
+					diode).
+temp1_label			RO	CPU Core Temperature sensor.
+temp1_input			RO	Measured temperature in millidegree
+					Celsius.
+temp1_min			RW	Low limit for temp input.
+temp1_max			RW	High limit for temp input.
+temp1_min_alarm			RO	Temperature input alarm. Returns 1 if

Documentation/hwmon/bt1-pvt.rst line 104

Quoted 10 lines
Documentation/hwmon/bt1-pvt.rst new file +10 -0
+					temperature input went below min limit,
+					0 otherwise.
+temp1_max_alarm			RO	Temperature input alarm. Returns 1 if
+					temperature input went above max limit,
+					0 otherwise.
+temp1_trim			RW	Temperature sensor trimming factor in
+					millidegree Celsius. It can be used to
+					manually adjust the temperature
+					measurements within 7.130 degrees
+					Celsius.

vs. standard ABI:

temp[1-*]_offset`
Temperature offset which is added to the temperature reading by the chip.

Unit: millidegree Celsius

If you really think this is necessary, why not use the standard ABI ?

Guenter

#5 lore raw

Re: [PATCH v3 3/3] hwmon: Add Baikal-T1 PVT sensor driver

Quoted 1 line · Guenter Roeck

On Tue, May 26, 2020 at 04:38:23PM +0300, Serge Semin wrote:

[nip]

Quoted 27 lines
Quoted 14 lines
Documentation/hwmon/bt1-pvt.rst new file +14 -0
+
+=============================== ======= =======================================
+Name				Perm	Description
+=============================== ======= =======================================
+update_interval			RW	Measurements update interval per
+					sensor.
+temp1_type			RO	Sensor type (always 1 as CPU embedded
+					diode).
+temp1_label			RO	CPU Core Temperature sensor.
+temp1_input			RO	Measured temperature in millidegree
+					Celsius.
+temp1_min			RW	Low limit for temp input.
+temp1_max			RW	High limit for temp input.
+temp1_min_alarm			RO	Temperature input alarm. Returns 1 if

Documentation/hwmon/bt1-pvt.rst line 104

Quoted 10 lines
Documentation/hwmon/bt1-pvt.rst new file +10 -0
+					temperature input went below min limit,
+					0 otherwise.
+temp1_max_alarm			RO	Temperature input alarm. Returns 1 if
+					temperature input went above max limit,
+					0 otherwise.
+temp1_trim			RW	Temperature sensor trimming factor in
+					millidegree Celsius. It can be used to
+					manually adjust the temperature
+					measurements within 7.130 degrees
+					Celsius.

vs. standard ABI:

Quoted 7 lines

temp[1-*]_offset`
Temperature offset which is added to the temperature reading by the chip.

Unit: millidegree Celsius

If you really think this is necessary, why not use the standard ABI ?

That would have made much more sense.) I'll replace the handwritten temp1_trim with the standard temp1_offset attribute in v4 shortly today. Thanks for pointing this out.

-Sergey

Quoted 2 lines

Guenter

maintainer
#6 lore raw

Re: [PATCH v3 3/3] hwmon: Add Baikal-T1 PVT sensor driver

Quoted 36 lines · Serge Semin
Quoted 1 line · Guenter Roeck

On Tue, May 26, 2020 at 04:38:23PM +0300, Serge Semin wrote:

[nip]

Quoted 31 lines
Quoted 14 lines
Documentation/hwmon/bt1-pvt.rst new file +14 -0
+
+=============================== ======= =======================================
+Name				Perm	Description
+=============================== ======= =======================================
+update_interval			RW	Measurements update interval per
+					sensor.
+temp1_type			RO	Sensor type (always 1 as CPU embedded
+					diode).
+temp1_label			RO	CPU Core Temperature sensor.
+temp1_input			RO	Measured temperature in millidegree
+					Celsius.
+temp1_min			RW	Low limit for temp input.
+temp1_max			RW	High limit for temp input.
+temp1_min_alarm			RO	Temperature input alarm. Returns 1 if

Documentation/hwmon/bt1-pvt.rst line 104

Quoted 10 lines
Documentation/hwmon/bt1-pvt.rst new file +10 -0
+					temperature input went below min limit,
+					0 otherwise.
+temp1_max_alarm			RO	Temperature input alarm. Returns 1 if
+					temperature input went above max limit,
+					0 otherwise.
+temp1_trim			RW	Temperature sensor trimming factor in
+					millidegree Celsius. It can be used to
+					manually adjust the temperature
+					measurements within 7.130 degrees
+					Celsius.

vs. standard ABI:

temp[1-*]_offset`
Temperature offset which is added to the temperature reading by the chip.

Quoted 8 lines
Quoted 3 lines

Unit: millidegree Celsius

If you really think this is necessary, why not use the standard ABI ?

That would have made much more sense.) I'll replace the handwritten temp1_trim with the standard temp1_offset attribute in v4 shortly today. Thanks for pointing this out.

Sorry for not realizing this earlier. The added explanation made all the difference.

Guenter

#7 lore raw

Re: [PATCH v3 3/3] hwmon: Add Baikal-T1 PVT sensor driver

Quoted 41 lines · Guenter Roeck
Quoted 40 lines · Serge Semin
Quoted 1 line · Guenter Roeck

On Tue, May 26, 2020 at 04:38:23PM +0300, Serge Semin wrote:

[nip]

Quoted 34 lines
Quoted 14 lines
Documentation/hwmon/bt1-pvt.rst new file +14 -0
+
+=============================== ======= =======================================
+Name				Perm	Description
+=============================== ======= =======================================
+update_interval			RW	Measurements update interval per
+					sensor.
+temp1_type			RO	Sensor type (always 1 as CPU embedded
+					diode).
+temp1_label			RO	CPU Core Temperature sensor.
+temp1_input			RO	Measured temperature in millidegree
+					Celsius.
+temp1_min			RW	Low limit for temp input.
+temp1_max			RW	High limit for temp input.
+temp1_min_alarm			RO	Temperature input alarm. Returns 1 if

Documentation/hwmon/bt1-pvt.rst line 104

Quoted 10 lines
Documentation/hwmon/bt1-pvt.rst new file +10 -0
+					temperature input went below min limit,
+					0 otherwise.
+temp1_max_alarm			RO	Temperature input alarm. Returns 1 if
+					temperature input went above max limit,
+					0 otherwise.
+temp1_trim			RW	Temperature sensor trimming factor in
+					millidegree Celsius. It can be used to
+					manually adjust the temperature
+					measurements within 7.130 degrees
+					Celsius.

vs. standard ABI:

temp[1-*]_offset`
Temperature offset which is added to the temperature reading by the chip.

Unit: millidegree Celsius

If you really think this is necessary, why not use the standard ABI ?

Quoted 7 lines
Quoted 4 lines

That would have made much more sense.) I'll replace the handwritten temp1_trim with the standard temp1_offset attribute in v4 shortly today. Thanks for pointing this out.

Sorry for not realizing this earlier. The added explanation made all the difference.

No worries. I'll fix it in v4. What about the clk_get_rate() part of the code? You had a comment regarding it in v2. I responded with justification that we can leave it as is. If you still disagree, then I create the clock rate caching in the private data at the probe() stage.

-Sergey

Quoted 2 lines

Guenter

maintainer
#8 lore raw

Re: [PATCH v3 3/3] hwmon: Add Baikal-T1 PVT sensor driver

Quoted 47 lines · Serge Semin
Quoted 46 lines · Guenter Roeck
Quoted 44 lines · Serge Semin
Quoted 1 line · Guenter Roeck

On Tue, May 26, 2020 at 04:38:23PM +0300, Serge Semin wrote:

[nip]

Quoted 27 lines
Quoted 14 lines
Documentation/hwmon/bt1-pvt.rst new file +14 -0
+
+=============================== ======= =======================================
+Name				Perm	Description
+=============================== ======= =======================================
+update_interval			RW	Measurements update interval per
+					sensor.
+temp1_type			RO	Sensor type (always 1 as CPU embedded
+					diode).
+temp1_label			RO	CPU Core Temperature sensor.
+temp1_input			RO	Measured temperature in millidegree
+					Celsius.
+temp1_min			RW	Low limit for temp input.
+temp1_max			RW	High limit for temp input.
+temp1_min_alarm			RO	Temperature input alarm. Returns 1 if

Documentation/hwmon/bt1-pvt.rst line 104

Quoted 10 lines
Documentation/hwmon/bt1-pvt.rst new file +10 -0
+					temperature input went below min limit,
+					0 otherwise.
+temp1_max_alarm			RO	Temperature input alarm. Returns 1 if
+					temperature input went above max limit,
+					0 otherwise.
+temp1_trim			RW	Temperature sensor trimming factor in
+					millidegree Celsius. It can be used to
+					manually adjust the temperature
+					measurements within 7.130 degrees
+					Celsius.

vs. standard ABI:

Quoted 7 lines

temp[1-*]_offset`
Temperature offset which is added to the temperature reading by the chip.

Unit: millidegree Celsius

If you really think this is necessary, why not use the standard ABI ?

That would have made much more sense.) I'll replace the handwritten temp1_trim with the standard temp1_offset attribute in v4 shortly today. Thanks for pointing this out.

Quoted 8 lines
Quoted 2 lines

Sorry for not realizing this earlier. The added explanation made all the difference.

No worries. I'll fix it in v4. What about the clk_get_rate() part of the code? You had a comment regarding it in v2. I responded with justification that we can leave it as is. If you still disagree, then I create the clock rate caching in the private data at the probe() stage.

Reason asking for it is that clk_get_rate() is unnecessarily costly if the rate doesn't change. But it isn't worth bike shedding about it.

Guenter

Quoted 4 lines

-Sergey

Quoted 2 lines

Guenter

#9 lore raw

Re: [PATCH v3 3/3] hwmon: Add Baikal-T1 PVT sensor driver

In reply to Serge Semin
I love your patch! Yet something to improve:

Hi Serge,

I love your patch! Yet something to improve:

[auto build test ERROR on v5.7-rc7]
[cannot apply to hwmon/hwmon-next next-20200526]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Serge-Semin/hwmon-Add-Baikal-T1-SoC-Process-Voltage-and-Temp-sensor-support/20200526-214218
base:    9cb1fd0efd195590b828b9b865421ad345a4a145
config: s390-allyesconfig (attached as .config)
compiler: s390-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=s390

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>, old ones prefixed by <<):

drivers/hwmon/bt1-pvt.c:65:1: warning: 'static' is not at beginning of declaration [-Wold-style-declaration] 65 | const static struct pvt_poly poly_temp_to_N = {
| ^~~~~
drivers/hwmon/bt1-pvt.c:76:1: warning: 'static' is not at beginning of declaration [-Wold-style-declaration] 76 | const static struct pvt_poly poly_N_to_temp = {
| ^~~~~
drivers/hwmon/bt1-pvt.c:97:1: warning: 'static' is not at beginning of declaration [-Wold-style-declaration] 97 | const static struct pvt_poly poly_volt_to_N = {
| ^~~~~
drivers/hwmon/bt1-pvt.c:105:1: warning: 'static' is not@beginning of declaration [-Wold-style-declaration] 105 | const static struct pvt_poly poly_N_to_volt = {
| ^~~~~
drivers/hwmon/bt1-pvt.c: In function 'pvt_update':

>> drivers/hwmon/bt1-pvt.c:142:8: error: implicit declaration of function 'readl_relaxed' [-Werror=implicit-function-declaration]
142 |  old = readl_relaxed(reg);
|        ^~~~~~~~~~~~~
>> drivers/hwmon/bt1-pvt.c:143:2: error: implicit declaration of function 'writel' [-Werror=implicit-function-declaration]
143 |  writel((old & ~mask) | (data & mask), reg);
|  ^~~~~~
drivers/hwmon/bt1-pvt.c: In function 'pvt_soft_isr':
>> drivers/hwmon/bt1-pvt.c:237:14: error: implicit declaration of function 'readl' [-Werror=implicit-function-declaration]

237 | thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
| ^~~~~
drivers/hwmon/bt1-pvt.c: At top level:
drivers/hwmon/bt1-pvt.c:795:5: warning: no previous prototype for 'pvt_hwmon_write' [-Wmissing-prototypes]
795 | int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
| ^~~~~~~~~~~~~~~
drivers/hwmon/bt1-pvt.c: In function 'pvt_init_iface':
drivers/hwmon/bt1-pvt.c:1013:7: error: implicit declaration of function 'of_property_read_u32' [-Werror=implicit-function-declaration] 1013 | if (!of_property_read_u32(pvt->dev->of_node,
| ^~~~~~~~~~~~~~~~~~~~
drivers/hwmon/bt1-pvt.c: At top level:
drivers/hwmon/bt1-pvt.c:1138:34: error: array type has incomplete element type 'struct of_device_id'
1138 | static const struct of_device_id pvt_of_match[] = {
| ^~~~~~~~~~~~
drivers/hwmon/bt1-pvt.c:1139:4: error: field name not in record or union initializer
1139 | { .compatible = "baikal,bt1-pvt" },
| ^
drivers/hwmon/bt1-pvt.c:1139:4: note: (near initialization for 'pvt_of_match')
drivers/hwmon/bt1-pvt.c:1138:34: warning: 'pvt_of_match' defined but not used [-Wunused-variable]
1138 | static const struct of_device_id pvt_of_match[] = {
| ^~~~~~~~~~~~
cc1: some warnings being treated as errors

vim +/readl_relaxed +142 drivers/hwmon/bt1-pvt.c

137
138 static inline u32 pvt_update(void __iomem *reg, u32 mask, u32 data)
139 {
140 u32 old;
141

 > 142		old = readl_relaxed(reg);
 > 143		writel((old & ~mask) | (data & mask), reg);
   144	
   145		return old & mask;
   146	}
   147	
   148	/*
   149	 * Baikal-T1 PVT mode can be updated only when the controller is disabled.
   150	 * So first we disable it, then set the new mode together with the controller
   151	 * getting back enabled. The same concerns the temperature trim and
   152	 * measurements timeout. If it is necessary the interface mutex is supposed
   153	 * to be locked at the time the operations are performed.
   154	 */
   155	static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
   156	{
   157		u32 old;
   158	
   159		mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
   160	
   161		old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
   162		pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN,
   163			   mode | old);
   164	}
   165	
   166	static inline u32 pvt_calc_trim(unsigned int temp)
   167	{
   168		temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
   169	
   170		return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
   171	}
   172	
   173	static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
   174	{
   175		u32 old;
   176	
   177		trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
   178	
   179		old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
   180		pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN,
   181			   trim | old);
   182	}
   183	
   184	static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
   185	{
   186		u32 old;
   187	
   188		old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
   189		writel(tout, pvt->regs + PVT_TTIMEOUT);
   190		pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, old);
   191	}
   192	
   193	/*
   194	 * This driver can optionally provide the hwmon alarms for each sensor the PVT
   195	 * controller supports. The alarms functionality is made compile-time
   196	 * configurable due to the hardware interface implementation peculiarity
   197	 * described further in this comment. So in case if alarms are unnecessary in
   198	 * your system design it's recommended to have them disabled to prevent the PVT
   199	 * IRQs being periodically raised to get the data cache/alarms status up to
   200	 * date.
   201	 *
   202	 * Baikal-T1 PVT embedded controller is based on the Analog Bits PVT sensor,
   203	 * but is equipped with a dedicated control wrapper. It exposes the PVT
   204	 * sub-block registers space via the APB3 bus. In addition the wrapper provides
   205	 * a common interrupt vector of the sensors conversion completion events and
   206	 * threshold value alarms. Alas the wrapper interface hasn't been fully thought
   207	 * through. There is only one sensor can be activated at a time, for which the
   208	 * thresholds comparator is enabled right after the data conversion is
   209	 * completed. Due to this if alarms need to be implemented for all available
   210	 * sensors we can't just set the thresholds and enable the interrupts. We need
   211	 * to enable the sensors one after another and let the controller to detect
   212	 * the alarms by itself at each conversion. This also makes pointless to handle
   213	 * the alarms interrupts, since in occasion they happen synchronously with
   214	 * data conversion completion. The best driver design would be to have the
   215	 * completion interrupts enabled only and keep the converted value in the
   216	 * driver data cache. This solution is implemented if hwmon alarms are enabled
   217	 * in this driver. In case if the alarms are disabled, the conversion is
   218	 * performed on demand at the time a sensors input file is read.
   219	 */
   220	
   221	#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
   222	
   223	#define pvt_hard_isr NULL
   224	
   225	static irqreturn_t pvt_soft_isr(int irq, void *data)
   226	{
   227		const struct pvt_sensor_info *info;
   228		struct pvt_hwmon *pvt = data;
   229		struct pvt_cache *cache;
   230		u32 val, thres_sts, old;
   231	
   232		/*
   233		 * DVALID bit will be cleared by reading the data. We need to save the
   234		 * status before the next conversion happens. Threshold events will be
   235		 * handled a bit later.
   236		 */
 > 237		thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);

238
239 /*
240 * Then lets recharge the PVT interface with the next sampling mode.
241 * Lock the interface mutex to serialize trim, timeouts and alarm
242 * thresholds settings.
243 */
244 cache = &pvt->cache[pvt->sensor];
245 info = &pvt_info[pvt->sensor];
246 pvt->sensor = (pvt->sensor == PVT_SENSOR_LAST) ?
247 PVT_SENSOR_FIRST : (pvt->sensor + 1);
248
249 /*
250 * For some reason we have to mask the interrupt before changing the
251 * mode, otherwise sometimes the temperature mode doesn't get
252 * activated even though the actual mode in the ctrl register
253 * corresponds to one. Then we read the data. By doing so we also
254 * recharge the data conversion. After this the mode corresponding
255 * to the next sensor in the row is set. Finally we enable the
256 * interrupts back.
257 */
258 mutex_lock(&pvt->iface_mtx);
259
260 old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
261 PVT_INTR_DVALID);
262
263 val = readl(pvt->regs + PVT_DATA);
264
265 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
266
267 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
268
269 mutex_unlock(&pvt->iface_mtx);
270
271 /*
272 * We can now update the data cache with data just retrieved from the
273 * sensor. Lock write-seqlock to make sure the reader has a coherent
274 * data.
275 */
276 write_seqlock(&cache->data_seqlock);
277
278 cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
279
280 write_sequnlock(&cache->data_seqlock);
281
282 /*
283 * While PVT core is doing the next mode data conversion, we'll check
284 * whether the alarms were triggered for the current sensor. Note that
285 * according to the documentation only one threshold IRQ status can be
286 * set at a time, that's why if-else statement is utilized.
287 */
288 if ((thres_sts & info->thres_sts_lo) ^ cache->thres_sts_lo) {
289 WRITE_ONCE(cache->thres_sts_lo, thres_sts & info->thres_sts_lo);
290 hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
291 info->channel);
292 } else if ((thres_sts & info->thres_sts_hi) ^ cache->thres_sts_hi) {
293 WRITE_ONCE(cache->thres_sts_hi, thres_sts & info->thres_sts_hi);
294 hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
295 info->channel);
296 }
297
298 return IRQ_HANDLED;
299 }
300

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#10 lore raw

Re: [PATCH v3 3/3] hwmon: Add Baikal-T1 PVT sensor driver

In reply to Serge Semin
I love your patch! Yet something to improve:

Hi Serge,

I love your patch! Yet something to improve:

[auto build test ERROR on v5.7-rc7]
[cannot apply to hwmon/hwmon-next next-20200526]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Serge-Semin/hwmon-Add-Baikal-T1-SoC-Process-Voltage-and-Temp-sensor-support/20200526-214218
base:    9cb1fd0efd195590b828b9b865421ad345a4a145
config: ia64-allmodconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=ia64

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>, old ones prefixed by <<):

drivers/hwmon/bt1-pvt.c:65:1: warning: 'static' is not at beginning of declaration [-Wold-style-declaration]
65 | const static struct pvt_poly poly_temp_to_N = {
| ^~~~~
drivers/hwmon/bt1-pvt.c:76:1: warning: 'static' is not at beginning of declaration [-Wold-style-declaration]
76 | const static struct pvt_poly poly_N_to_temp = {
| ^~~~~
drivers/hwmon/bt1-pvt.c:97:1: warning: 'static' is not at beginning of declaration [-Wold-style-declaration]
97 | const static struct pvt_poly poly_volt_to_N = {
| ^~~~~
drivers/hwmon/bt1-pvt.c:105:1: warning: 'static' is not@beginning of declaration [-Wold-style-declaration]
105 | const static struct pvt_poly poly_N_to_volt = {
| ^~~~~
drivers/hwmon/bt1-pvt.c:795:5: warning: no previous prototype for 'pvt_hwmon_write' [-Wmissing-prototypes]
795 | int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
| ^~~~~~~~~~~~~~~
drivers/hwmon/bt1-pvt.c: In function 'pvt_init_iface':
drivers/hwmon/bt1-pvt.c:1013:7: error: implicit declaration of function 'of_property_read_u32' [-Werror=implicit-function-declaration] 1013 | if (!of_property_read_u32(pvt->dev->of_node,
| ^~~~~~~~~~~~~~~~~~~~
drivers/hwmon/bt1-pvt.c: At top level:

>> drivers/hwmon/bt1-pvt.c:1138:34: error: array type has incomplete element type 'struct of_device_id'
1138 | static const struct of_device_id pvt_of_match[] = {
|                                  ^~~~~~~~~~~~
>> drivers/hwmon/bt1-pvt.c:1139:4: error: field name not in record or union initializer

1139 | { .compatible = "baikal,bt1-pvt" },
| ^
drivers/hwmon/bt1-pvt.c:1139:4: note: (near initialization for 'pvt_of_match')
cc1: some warnings being treated as errors

vim +1138 drivers/hwmon/bt1-pvt.c

992
993 static void pvt_init_iface(struct pvt_hwmon *pvt)
994 {
995 unsigned int temp;
996 u32 trim;
997
998 /*
999 * Make sure all interrupts and controller are disabled so not to
1000 * accidentally have ISR executed before the driver data is fully
1001 * initialized. Clear the IRQ status as well.
1002 */
1003 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
1004 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1005 readl(pvt->regs + PVT_CLR_INTR);
1006 readl(pvt->regs + PVT_DATA);
1007
1008 /* Setup default sensor mode, timeout and temperature trim. */
1009 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
1010 pvt_set_tout(pvt, PVT_TOUT_DEF);
1011
1012 trim = PVT_TRIM_DEF;

> 1013		if (!of_property_read_u32(pvt->dev->of_node,
  1014		     "baikal,pvt-temp-trim-millicelsius", &temp))
  1015			trim = pvt_calc_trim(temp);
  1016	
  1017		pvt_set_trim(pvt, trim);
  1018	}
  1019	
  1020	static int pvt_request_irq(struct pvt_hwmon *pvt)
  1021	{
  1022		struct platform_device *pdev = to_platform_device(pvt->dev);
  1023		int ret;
  1024	
  1025		pvt->irq = platform_get_irq(pdev, 0);
  1026		if (pvt->irq < 0)
  1027			return pvt->irq;
  1028	
  1029		ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
  1030						pvt_hard_isr, pvt_soft_isr,
  1031	#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  1032						IRQF_SHARED | IRQF_TRIGGER_HIGH |
  1033						IRQF_ONESHOT,
  1034	#else
  1035						IRQF_SHARED | IRQF_TRIGGER_HIGH,
  1036	#endif
  1037						"pvt", pvt);
  1038		if (ret) {
  1039			dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
  1040			return ret;
  1041		}
  1042	
  1043		return 0;
  1044	}
  1045	
  1046	static int pvt_create_hwmon(struct pvt_hwmon *pvt)
  1047	{
  1048		pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
  1049			&pvt_hwmon_info, pvt_hwmon_groups);
  1050		if (IS_ERR(pvt->hwmon)) {
  1051			dev_err(pvt->dev, "Couldn't create hwmon device\n");
  1052			return PTR_ERR(pvt->hwmon);
  1053		}
  1054	
  1055		return 0;
  1056	}
  1057	
  1058	#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  1059	
  1060	static void pvt_disable_iface(void *data)
  1061	{
  1062		struct pvt_hwmon *pvt = data;
  1063	
  1064		mutex_lock(&pvt->iface_mtx);
  1065		pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
  1066		pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
  1067			   PVT_INTR_DVALID);
  1068		mutex_unlock(&pvt->iface_mtx);
  1069	}
  1070	
  1071	static int pvt_enable_iface(struct pvt_hwmon *pvt)
  1072	{
  1073		int ret;
  1074	
  1075		ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
  1076		if (ret) {
  1077			dev_err(pvt->dev, "Can't add PVT disable interface action\n");
  1078			return ret;
  1079		}
  1080	
  1081		/*
  1082		 * Enable sensors data conversion and IRQ. We need to lock the
  1083		 * interface mutex since hwmon has just been created and the
  1084		 * corresponding sysfs files are accessible from user-space,
  1085		 * which theoretically may cause races.
  1086		 */
  1087		mutex_lock(&pvt->iface_mtx);
  1088		pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
  1089		pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
  1090		mutex_unlock(&pvt->iface_mtx);
  1091	
  1092		return 0;
  1093	}
  1094	
  1095	#else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
  1096	
  1097	static int pvt_enable_iface(struct pvt_hwmon *pvt)
  1098	{
  1099		return 0;
  1100	}
  1101	
  1102	#endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
  1103	
  1104	static int pvt_probe(struct platform_device *pdev)
  1105	{
  1106		struct pvt_hwmon *pvt;
  1107		int ret;
  1108	
  1109		pvt = pvt_create_data(pdev);
  1110		if (IS_ERR(pvt))
  1111			return PTR_ERR(pvt);
  1112	
  1113		ret = pvt_request_regs(pvt);
  1114		if (ret)
  1115			return ret;
  1116	
  1117		ret = pvt_request_clks(pvt);
  1118		if (ret)
  1119			return ret;
  1120	
  1121		pvt_init_iface(pvt);
  1122	
  1123		ret = pvt_request_irq(pvt);
  1124		if (ret)
  1125			return ret;
  1126	
  1127		ret = pvt_create_hwmon(pvt);
  1128		if (ret)
  1129			return ret;
  1130	
  1131		ret = pvt_enable_iface(pvt);
  1132		if (ret)
  1133			return ret;
  1134	
  1135		return 0;
  1136	}
  1137	
> 1138	static const struct of_device_id pvt_of_match[] = {
> 1139		{ .compatible = "baikal,bt1-pvt" },

1140 { }
1141 };
1142 MODULE_DEVICE_TABLE(of, pvt_of_match);
1143

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org