Skip to contentSkip to conversation

thermal: iio bindings

PATCH In review Srinivas Pandruvada 5 participants 15 messages Sep 26, 2015 – Dec 31, 2015

Subsystems (14)
lore
Share & tools
lore
#0 lore raw

cover thermal: iio bindings

v0
Since this was discussed during LPC in Seattle and three versions of RFC are published and reviwed, this is posted as non RFC version. This has changes in iio interface file as suggested by Jonathan. We may add threshold event direction flag later if required, without much impact by adding new event type.

RFC v2
- Rebase to 4.3.rc1
- Additional parameter to thermal_zone_device_update for event type
- Moved the dummy IIO callbacks to local include file, in this way "not used" warnings can be avoided
- Remove changes from user space governor, rather notify from core
- Validate the trigger, so that when thermal device supports async notifications, this will be exclusive trigger
- Use new thermal core event type to differentiate events for iio threshold notification

RFC v1
- Change commit message to be more clear
- Split in three patches (First for thermal_iio.c, second to integrate to thermal_core and third for user space governor).
- Removed IIO defines in thermal.h
- Removed direct push to buffers
- Other comments from Jonathan except introduce validate_trigger

RFC v0
Base version for first review

Srinivas Pandruvada (6):
thermal: core: enhance thermal_zone_device_update
thermal: documentation update
thermal: iio device for thermal sensor
thermal: use iio binding calls
thermal: iio Documentation
thermal: x86_pkg_temp: Register threshold callbacks

 Documentation/thermal/sysfs-api.txt                |  10 +
 Documentation/thermal/thermal_iio_binding          |  88 +++++
 drivers/acpi/thermal.c                             |   2 +-
 drivers/platform/x86/acerhdf.c                     |   2 +-
 drivers/thermal/Kconfig                            |  12 +
 drivers/thermal/Makefile                           |   1 +
 drivers/thermal/db8500_thermal.c                   |   2 +-
 drivers/thermal/hisi_thermal.c                     |   3 +-
 drivers/thermal/imx_thermal.c                      |   4 +-
 .../thermal/int340x_thermal/int340x_thermal_zone.h |   2 +-
 drivers/thermal/intel_soc_dts_iosf.c               |   3 +-
 drivers/thermal/of-thermal.c                       |   2 +-
 drivers/thermal/qcom-spmi-temp-alarm.c             |   2 +-
 drivers/thermal/rcar_thermal.c                     |   3 +-
 drivers/thermal/rockchip_thermal.c                 |   3 +-
 drivers/thermal/samsung/exynos_tmu.c               |   2 +-
 drivers/thermal/st/st_thermal_memmap.c             |   3 +-
 drivers/thermal/thermal_core.c                     |  21 +-
 drivers/thermal/thermal_iio.c                      | 383 +++++++++++++++++++++
 drivers/thermal/thermal_iio.h                      |  45 +++
 drivers/thermal/ti-soc-thermal/ti-thermal-common.c |   4 +-
 drivers/thermal/x86_pkg_temp_thermal.c             |  11 +-
 include/linux/thermal.h                            |  26 +-
 23 files changed, 609 insertions(+), 25 deletions(-)
 create mode 100644 Documentation/thermal/thermal_iio_binding
 create mode 100644 drivers/thermal/thermal_iio.c
 create mode 100644 drivers/thermal/thermal_iio.h
Signature · 1 line
2.4.3
#1 lore raw

PATCH 1/6 thermal: core: enhance thermal_zone_device_update

thermal_zone_device_update is called by thermal core and clients to trigger reading of temperature, evaluation of trips and take governor specified actions. There can be number of reasons this may be triggered, for example when HW calculates new temperature sample or violation of a threshold. Since this is the only interface available for client drivers to trigger thermal core actions, it should also should have a way to specify the trigger event type.
This change adds additional parameter to specify event. This is optional for the driver to specify event type, in that case this can be just THERMAL_DEVICE_EVENT_NONE. Alternately they can select THERMAL_DEVICE_EVENT_THRESHOLD for temperature threshold event, THERMAL_DEVICE_EVENT_TEMP_SAMPLE for new temperature sample notification, THERMAL_DEVICE_TRIP_TEMP_CHANGE for a temperature trip change.

---
 drivers/acpi/thermal.c                                 |  2 +-
 drivers/platform/x86/acerhdf.c                         |  2 +-
 drivers/thermal/db8500_thermal.c                       |  2 +-
 drivers/thermal/hisi_thermal.c                         |  3 ++-
 drivers/thermal/imx_thermal.c                          |  4 ++--
 drivers/thermal/int340x_thermal/int340x_thermal_zone.h |  2 +-
 drivers/thermal/intel_soc_dts_iosf.c                   |  3 ++-
 drivers/thermal/of-thermal.c                           |  2 +-
 drivers/thermal/qcom-spmi-temp-alarm.c                 |  2 +-
 drivers/thermal/rcar_thermal.c                         |  3 ++-
 drivers/thermal/rockchip_thermal.c                     |  3 ++-
 drivers/thermal/samsung/exynos_tmu.c                   |  2 +-
 drivers/thermal/st/st_thermal_memmap.c                 |  3 ++-
 drivers/thermal/thermal_core.c                         | 11 ++++++-----
 drivers/thermal/ti-soc-thermal/ti-thermal-common.c     |  4 ++--
 drivers/thermal/x86_pkg_temp_thermal.c                 |  3 ++-
 include/linux/thermal.h                                | 13 +++++++++++--
 17 files changed, 40 insertions(+), 24 deletions(-)
drivers/acpi/thermal.c +1 -1
index 30d8518..e4f547b 100644
@@ -520,7 +520,7 @@ static void acpi_thermal_check(void *data)
 	if (!tz->tz_enabled)
 		return;
 
-	thermal_zone_device_update(tz->thermal_zone);
+	thermal_zone_device_update(tz->thermal_zone, THERMAL_DEVICE_EVENT_NONE);
 }
 
 /* sys I/F for generic thermal sysfs support */
drivers/platform/x86/acerhdf.c +1 -1
index 460fa67..6382d4e 100644
@@ -405,7 +405,7 @@ static inline void acerhdf_enable_kernelmode(void)
 	kernelmode = 1;
 
 	thz_dev->polling_delay = interval*1000;
-	thermal_zone_device_update(thz_dev);
+	thermal_zone_device_update(thz_dev, THERMAL_DEVICE_EVENT_NONE);
 	pr_notice("kernel mode fan control ON\n");
 }
 
drivers/thermal/db8500_thermal.c +1 -1
index 652acd8..e4cbbe6 100644
@@ -306,7 +306,7 @@ static void db8500_thermal_work(struct work_struct *work)
 	if (cur_mode == THERMAL_DEVICE_DISABLED)
 		return;
 
-	thermal_zone_device_update(pzone->therm_dev);
+	thermal_zone_device_update(pzone->therm_dev, THERMAL_DEVICE_EVENT_NONE);
 	dev_dbg(&pzone->therm_dev->device, "thermal work finished.\n");
 }
 
drivers/thermal/hisi_thermal.c +2 -1
index 36d0729..65f3432 100644
@@ -227,7 +227,8 @@ static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
 	mutex_unlock(&data->thermal_lock);
 
 	for (i = 0; i < HISI_MAX_SENSORS; i++)
-		thermal_zone_device_update(data->sensors[i].tzd);
+		thermal_zone_device_update(data->sensors[i].tzd,
+					   THERMAL_DEVICE_EVENT_NONE);
 
 	return IRQ_HANDLED;
 }
drivers/thermal/imx_thermal.c +2 -2
index 4bec1d3..24b3aa6 100644
@@ -249,7 +249,7 @@ static int imx_set_mode(struct thermal_zone_device *tz,
 	}
 
 	data->mode = mode;
-	thermal_zone_device_update(tz);
+	thermal_zone_device_update(tz, THERMAL_DEVICE_EVENT_NONE);
 
 	return 0;
 }
@@ -436,7 +436,7 @@ static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
 	dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
 		data->alarm_temp / 1000);
 
-	thermal_zone_device_update(data->tz);
+	thermal_zone_device_update(data->tz, THERMAL_DEVICE_EVENT_NONE);
 
 	return IRQ_HANDLED;
 }
drivers/thermal/int340x_thermal/int340x_thermal_zone.h +1 -1
index aaadf72..2f93b85 100644
@@ -62,7 +62,7 @@ static inline void *int340x_thermal_zone_get_priv_data(
 static inline void int340x_thermal_zone_device_update(
 			struct int34x_thermal_zone *tzone)
 {
-	thermal_zone_device_update(tzone->zone);
+	thermal_zone_device_update(tzone->zone, THERMAL_DEVICE_EVENT_THRESHOLD);
 }
 
 #endif
drivers/thermal/intel_soc_dts_iosf.c +2 -1
index 5841d1d..4402ab0 100644
@@ -392,7 +392,8 @@ void intel_soc_dts_iosf_interrupt_handler(struct intel_soc_dts_sensors *sensors)
 
 		for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) {
 			pr_debug("TZD update for zone %d\n", i);
-			thermal_zone_device_update(sensors->soc_dts[i].tzone);
+			thermal_zone_device_update(sensors->soc_dts[i].tzone,
+						THERMAL_DEVICE_EVENT_THRESHOLD);
 		}
 	} else
 		spin_unlock_irqrestore(&sensors->intr_notify_lock, flags);
drivers/thermal/of-thermal.c +1 -1
index 42b7d42..4168bf9 100644
@@ -292,7 +292,7 @@ static int of_thermal_set_mode(struct thermal_zone_device *tz,
 	mutex_unlock(&tz->lock);
 
 	data->mode = mode;
-	thermal_zone_device_update(tz);
+	thermal_zone_device_update(tz, THERMAL_DEVICE_EVENT_NONE);
 
 	return 0;
 }
drivers/thermal/qcom-spmi-temp-alarm.c +1 -1
index b677aad..bf3da3c 100644
@@ -150,7 +150,7 @@ static irqreturn_t qpnp_tm_isr(int irq, void *data)
 {
 	struct qpnp_tm_chip *chip = data;
 
-	thermal_zone_device_update(chip->tz_dev);
+	thermal_zone_device_update(chip->tz_dev, THERMAL_DEVICE_EVENT_NONE);
 
 	return IRQ_HANDLED;
 }
drivers/thermal/rcar_thermal.c +2 -1
index 5d4ae7d..2eac44a 100644
@@ -308,7 +308,8 @@ static void rcar_thermal_work(struct work_struct *work)
 
 	rcar_thermal_get_temp(priv->zone, &nctemp);
 	if (nctemp != cctemp)
-		thermal_zone_device_update(priv->zone);
+		thermal_zone_device_update(priv->zone,
+					   THERMAL_DEVICE_EVENT_NONE);
 }
 
 static u32 rcar_thermal_had_changed(struct rcar_thermal_priv *priv, u32 status)
drivers/thermal/rockchip_thermal.c +2 -1
index c89ffb2..b3d7214 100644
@@ -361,7 +361,8 @@ static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
 	thermal->chip->irq_ack(thermal->regs);
 
 	for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
-		thermal_zone_device_update(thermal->sensors[i].tzd);
+		thermal_zone_device_update(thermal->sensors[i].tzd,
+					   THERMAL_DEVICE_EVENT_NONE);
 
 	return IRQ_HANDLED;
 }
drivers/thermal/samsung/exynos_tmu.c +1 -1
index 0bae8cc..80ccc92 100644
@@ -223,7 +223,7 @@ static void exynos_report_trigger(struct exynos_tmu_data *p)
 		return;
 	}
 
-	thermal_zone_device_update(tz);
+	thermal_zone_device_update(tz, THERMAL_DEVICE_EVENT_NONE);
 
 	mutex_lock(&tz->lock);
 	/* Find the level for which trip happened */
drivers/thermal/st/st_thermal_memmap.c +2 -1
index fc0c9e1..45b6b9f 100644
@@ -42,7 +42,8 @@ static irqreturn_t st_mmap_thermal_trip_handler(int irq, void *sdata)
 {
 	struct st_thermal_sensor *sensor = sdata;
 
-	thermal_zone_device_update(sensor->thermal_dev);
+	thermal_zone_device_update(sensor->thermal_dev,
+				   THERMAL_DEVICE_EVENT_NONE);
 
 	return IRQ_HANDLED;
 }
drivers/thermal/thermal_core.c +6 -5
index 5e5fc70..18e7802 100644
@@ -536,7 +536,8 @@ static void update_temperature(struct thermal_zone_device *tz)
 				tz->last_temperature, tz->temperature);
 }
 
-void thermal_zone_device_update(struct thermal_zone_device *tz)
+void thermal_zone_device_update(struct thermal_zone_device *tz,
+				enum thermal_device_event_type event)
 {
 	int count;
 
@@ -555,7 +556,7 @@ static void thermal_zone_device_check(struct work_struct *work)
 	struct thermal_zone_device *tz = container_of(work, struct
 						      thermal_zone_device,
 						      poll_queue.work);
-	thermal_zone_device_update(tz);
+	thermal_zone_device_update(tz, THERMAL_DEVICE_EVENT_NONE);
 }
 
 /* sys I/F for thermal zone */
@@ -794,7 +795,7 @@ passive_store(struct device *dev, struct device_attribute *attr,
 
 	tz->forced_passive = state;
 
-	thermal_zone_device_update(tz);
+	thermal_zone_device_update(tz, THERMAL_DEVICE_EVENT_NONE);
 
 	return count;
 }
@@ -885,7 +886,7 @@ emul_temp_store(struct device *dev, struct device_attribute *attr,
 	}
 
 	if (!ret)
-		thermal_zone_device_update(tz);
+		thermal_zone_device_update(tz, THERMAL_DEVICE_EVENT_NONE);
 
 	return ret ? ret : count;
 }
@@ -1872,7 +1873,7 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
 
 	INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
 
-	thermal_zone_device_update(tz);
+	thermal_zone_device_update(tz, THERMAL_DEVICE_EVENT_NONE);
 
 	return tz;
 
drivers/thermal/ti-soc-thermal/ti-thermal-common.c +2 -2
index b213a12..ab2a011 100644
@@ -52,7 +52,7 @@ static void ti_thermal_work(struct work_struct *work)
 	struct ti_thermal_data *data = container_of(work,
 					struct ti_thermal_data, thermal_wq);
 
-	thermal_zone_device_update(data->ti_thermal);
+	thermal_zone_device_update(data->ti_thermal, THERMAL_DEVICE_EVENT_NONE);
 
 	dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
 		data->ti_thermal->type);
@@ -205,7 +205,7 @@ static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
 	data->mode = mode;
 	ti_bandgap_write_update_interval(bgp, data->sensor_id,
 					data->ti_thermal->polling_delay);
-	thermal_zone_device_update(data->ti_thermal);
+	thermal_zone_device_update(data->ti_thermal, THERMAL_DEVICE_EVENT_NONE);
 	dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
 		data->ti_thermal->polling_delay);
 
drivers/thermal/x86_pkg_temp_thermal.c +2 -1
index 7fc919f..849f682 100644
@@ -348,7 +348,8 @@ static void pkg_temp_thermal_threshold_work_fn(struct work_struct *work)
 	}
 	if (notify) {
 		pr_debug("thermal_zone_device_update\n");
-		thermal_zone_device_update(phdev->tzone);
+		thermal_zone_device_update(phdev->tzone,
+					   THERMAL_DEVICE_EVENT_THRESHOLD);
 	}
 }
 
include/linux/thermal.h +11 -2
index 17292fe..c074f6a 100644
@@ -87,6 +87,13 @@ enum thermal_trend {
 	THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
 };
 
+enum thermal_device_event_type {
+	THERMAL_DEVICE_EVENT_NONE, /* No specific reason */
+	THERMAL_DEVICE_EVENT_THRESHOLD, /* temp thereshold event */
+	THERMAL_DEVICE_EVENT_TEMP_SAMPLE, /* New temp sample notify */
+	THERMAL_DEVICE_TRIP_TEMP_CHANGE, /* trip temp change */
+};
+
 struct thermal_zone_device_ops {
 	int (*bind) (struct thermal_zone_device *,
 		     struct thermal_cooling_device *);
@@ -393,7 +400,8 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
 				     unsigned int);
 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
 				       struct thermal_cooling_device *);
-void thermal_zone_device_update(struct thermal_zone_device *);
+void thermal_zone_device_update(struct thermal_zone_device *,
+				enum thermal_device_event_type);
 
 struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
 		const struct thermal_cooling_device_ops *);
@@ -436,7 +444,8 @@ static inline int thermal_zone_unbind_cooling_device(
 	struct thermal_zone_device *tz, int trip,
 	struct thermal_cooling_device *cdev)
 { return -ENODEV; }
-static inline void thermal_zone_device_update(struct thermal_zone_device *tz)
+static inline void thermal_zone_device_update(struct thermal_zone_device *tz,
+					      enum thermal_device_event_type)
 { }
 static inline struct thermal_cooling_device *
 thermal_cooling_device_register(char *type, void *devdata,
Signature · 1 line
2.4.3
#2 lore raw

PATCH 2/6 thermal: documentation update

In reply to Srinivas Pandruvada

Added description for thermal_zone_device_update, which is already used by thermal core and client drivers.

---
 Documentation/thermal/sysfs-api.txt | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/thermal/sysfs-api.txt +10 -0
index 10f062e..16770f0 100644
@@ -494,3 +494,13 @@ platform data is provided, this uses the step_wise throttling policy.
 This function serves as an arbitrator to set the state of a cooling
 device. It sets the cooling device to the deepest cooling state if
 possible.
+
+5.5:thermal_zone_device_update:
+This function is used by thermal core and thermal client drivers to trigger
+temperature read and governor specific processing for thermal trip violation.
+Using event type parameter, the caller can specify the reason for this call.
+This reason can be
+THERMAL_DEVICE_EVENT_NONE for unspecified reason
+THERMAL_DEVICE_EVENT_THRESHOLD for temp threshold event
+THERMAL_DEVICE_EVENT_TEMP_SAMPLE for new temperature sample notification
+THERMAL_DEVICE_TRIP_TEMP_CHANGE for notifying trip point temperature change
Signature · 1 line
2.4.3
#3 lore raw

PATCH 3/6 thermal: iio device for thermal sensor

In reply to Srinivas Pandruvada

This change registers temperature sensor in a thermal zone as an IIO temperature device. This allows user space thermal application to fully utilize IIO capability to read temperature events and samples. The primary motivation for this change to improve performance for user space thermal controllers like Linux thermal daemon or Intel® Dynamic Platform and Thermal Framework (DPTF) for Chromium OS.
The current sysfs has several limitations, which forces the current user space to use polling as the safe way. This polling causes performance bottlenecks as some devices requires extremely aggressive response to changing temperature conditions.
These are some limitations, which this change tries to address:
- Temperature reading via sysfs attribute, which needs conversion from string to int
- No way to set threshold settings to avoid polling for temperature change without using a RW passive trip point.
- Event notifications via slow kobject uevents, which needs parsing to id the zone and read temperature
- Only pull interface from user space, kernel drivers can't send samples
- One sample at a time read from user space
These limitations can be addressed by registering temperature sensor as an IIO device. IIO provides
- buffered access via /dev/iio:deviceX node with select/poll capability
- temperature thresholds setting via iio event thresholds
- Wait and receive events
- Able to use different external trigger (data ready indications) and push samples to buffers

Next set of patches uses the IIO binding introduced in this patchset. The iio device created during call to thermal_zone_device_register. Samples are pushed to iio buffers when thermal_zone_device_update is called from client drivers.

New thermal zone device callbacks:
It introduces three new callbacks for thermal client drivers:
get_threshold_temp: Get the current threshold temperature
set_threshold_temp: Set the current threshold temperature check_notification_support: Inform that the client driver has asynchronous notification mechanism. If it is then polling can be avoided for the temperature sensor.

---
 drivers/thermal/Kconfig       |  12 ++
 drivers/thermal/Makefile      |   1 +
 drivers/thermal/thermal_iio.c | 383 ++++++++++++++++++++++++++++++++++++++++++
 drivers/thermal/thermal_iio.h |  45 +++++
 include/linux/thermal.h       |  13 ++
 5 files changed, 454 insertions(+)
 create mode 100644 drivers/thermal/thermal_iio.c
 create mode 100644 drivers/thermal/thermal_iio.h
drivers/thermal/Kconfig +12 -0
index 0390044..bc7836e 100644
@@ -29,6 +29,18 @@ config THERMAL_HWMON
 	  Say 'Y' here if you want all thermal sensors to
 	  have hwmon sysfs interface too.
 
+config THERMAL_IIO
+	bool
+	prompt "Thermal sensor from a zone as IIO sensor device"
+	select IIO
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  This will register thermal sensor in a zone as an IIO temperature
+	  sensor device.
+	  This will help user space thermal controllers to use IIO ABI to
+	  get events and buffered acces to temperature samples.
+
 config THERMAL_OF
 	bool
 	prompt "APIs to parse thermal data out of device tree"
drivers/thermal/Makefile +1 -0
index 26f1608..b72a28d 100644
@@ -7,6 +7,7 @@ thermal_sys-y			+= thermal_core.o
 
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)		+= thermal_hwmon.o
+thermal_sys-$(CONFIG_THERMAL_IIO)		+= thermal_iio.o
 thermal_sys-$(CONFIG_THERMAL_OF)		+= of-thermal.o
 
 # governors
drivers/thermal/thermal_iio.c new file +383 -0

383 lines not on this page · raw message

drivers/thermal/thermal_iio.h new file +45 -0

45 lines not on this page · raw message

include/linux/thermal.h +13 -0
index c074f6a..925d1e5 100644
@@ -114,6 +114,13 @@ struct thermal_zone_device_ops {
 	int (*set_emul_temp) (struct thermal_zone_device *, int);
 	int (*get_trend) (struct thermal_zone_device *, int,
 			  enum thermal_trend *);
+	int (*get_threshold_temp)(struct thermal_zone_device *, int,
+				  int *);
+	int (*set_threshold_temp)(struct thermal_zone_device *, int,
+				  int);
+	int (*set_notification_status)(struct thermal_zone_device *,
+				       bool status);
+	bool (*check_notification_support)(struct thermal_zone_device *);
 	int (*notify) (struct thermal_zone_device *, int,
 		       enum thermal_trip_type);
 };
@@ -148,6 +155,8 @@ struct thermal_attr {
 	char name[THERMAL_NAME_LENGTH];
 };
 
+struct iio_dev;
+
 /**
  * struct thermal_zone_device - structure for a thermal zone
  * @id:		unique id number for each thermal zone
@@ -182,6 +191,7 @@ struct thermal_attr {
  * @lock:	lock to protect thermal_instances list
  * @node:	node in thermal_tz_list (in thermal_core.c)
  * @poll_queue:	delayed work for polling
+ * @indio_dev:	pointer to instance of an IIO dev for this zone
  */
 struct thermal_zone_device {
 	int id;
@@ -208,6 +218,9 @@ struct thermal_zone_device {
 	struct mutex lock;
 	struct list_head node;
 	struct delayed_work poll_queue;
+#if defined(CONFIG_THERMAL_IIO)
+	struct iio_dev *indio_dev;
+#endif
 };
 
 /**
Signature · 1 line
2.4.3
#8 lore raw

PATCH 4/6 thermal: use iio binding calls

In reply to Srinivas Pandruvada

This change:
Register zone temperature sensor as IIO device during zone registration, Remove iio device during zone removal,
Notify iio binding during thermal_device_update call.

---
 drivers/thermal/thermal_core.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/thermal/thermal_core.c +9 -1
index 18e7802..dbde29f 100644
@@ -43,6 +43,7 @@
 
 #include "thermal_core.h"
 #include "thermal_hwmon.h"
+#include "thermal_iio.h"
 
 MODULE_AUTHOR("Zhang Rui");
 MODULE_DESCRIPTION("Generic thermal management sysfs support");
@@ -544,6 +545,7 @@ void thermal_zone_device_update(struct thermal_zone_device *tz,
 	if (!tz->ops->get_temp)
 		return;
 
+	thermal_iio_sensor_notify(tz, event);
 	update_temperature(tz);
 
 	for (count = 0; count < tz->trips; count++)
@@ -1858,10 +1860,15 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
 
 	mutex_unlock(&thermal_governor_lock);
 
+	if (thermal_iio_sensor_register(tz))
+		goto unregister;
+
 	if (!tz->tzp || !tz->tzp->no_hwmon) {
 		result = thermal_add_hwmon_sysfs(tz);
-		if (result)
+		if (result) {
+			thermal_iio_sensor_unregister(tz);
 			goto unregister;
+		}
 	}
 
 	mutex_lock(&thermal_list_lock);
@@ -1943,6 +1950,7 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
 	remove_trip_attrs(tz);
 	thermal_set_governor(tz, NULL);
 
+	thermal_iio_sensor_unregister(tz);
 	thermal_remove_hwmon_sysfs(tz);
 	release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
 	idr_destroy(&tz->idr);
Signature · 1 line
2.4.3
#9 lore raw

PATCH 5/6 thermal: iio Documentation

In reply to Srinivas Pandruvada

An overview document about thermal iio binding.

---
 Documentation/thermal/thermal_iio_binding | 88 +++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 Documentation/thermal/thermal_iio_binding
Documentation/thermal/thermal_iio_binding new file +88 -0
new file mode 100644
index 0000000..df501f1
@@ -0,0 +1,88 @@
+
+Thermal IIO Bindings
+
+When CONFIG_THERMAL_IIO is enabled, in addition to the regular thermal
+zone attributes, an additional entry for an IIO device is created.
+IIO sysfs ABI documents is available at Documentation/ABI/sysfs-bus-iio.
+
+The following example shows the contents of the IIO:device in a thermal
+zone.
+
+thermal_zoneX
+├── iio:device9
+│   ├── buffer
+│   │   ├── enable
+│   │   ├── length
+│   │   └── watermark
+│   ├── events
+│   │   ├── in_temp_thresh_either_en
+│   │   └── in_temp_thresh_either_value
+│   ├── in_temp_raw
+│   ├── name
+│   ├── scan_elements
+│   │   ├── in_temp_en
+│   │   ├── in_temp_index
+│   │   ├── in_temp_type
+│   │   ├── in_timestamp_en
+│   │   ├── in_timestamp_index
+│   │   └── in_timestamp_type
+│   ├── subsystem -> ../../../../../bus/iio
+│   ├── trigger0
+│   │   └── name
+│   ├── trigger
+│   │   └── current_trigger
+│   └── uevent
+├── integral_cutoff
+...
+... other thermal_zone attributes
+...
+
+
+IIO Attributes
+
+name: This shows iio device name, which will match thermal_zone/type attribute
+in_temp_raw: This will show raw temperature values same as thermal_zone/temp
+attribute.
+
+trigger/current_trigger: This is IIO way of specifying data ready indication.
+If the thermal client can provide asynchronous notifications, then a iio trigger
+device is created under trigger0. The name of this trigger will be thermal
+device name suffixed with devX. To enable iio buffer notifications,
+trigger/current_trigger cab be updated with this trigger. For example
+
+#echo "x86_pkg_temp-dev9" > trigger/current_trigger
+
+In addition of samples can be pushed to iio buffers with external standard IIO
+device triggers, E.g. sysfs trigger.
+
+How to enable events?
+To enable events a suitable threshold temperature can be written to
+in_temp_thresh_either_value
+and enable by writing 1 to in_temp_thresh_either_en.
+
+To monitor events a good user space program is here:
+tools/iio/iio_event_monitor.c
+
+For example
+# echo 50000 > /sys/devices/virtual/thermal/thermal_zone9/iio:device9/events/in_temp_thresh_either_value
+# echo 1 > /sys/devices/virtual/thermal/thermal_zone9/iio:device9/events/in_temp_thresh_either_en
+# ./iio_event_monitor x86_pkg_temp
+Found IIO device with name x86_pkg_temp with device number 9
+Event: time: 1443297126626859289, type: temp, channel: 0, evtype: thresh, direction: either
+
+How to read temperature samples?
+When thermal client or core driver calls thermal_zone_device_update, this will
+result in sample pushed to iio buffers. These samples can be read from
+/dev/iio:deviceX file.
+
+To enable buffer select scan_elements
+# echo 1 > scan_elements/in_temp_en
+
+Set buffer length to n samples and enable buffer.
+# echo 2 > buffer/length
+# echo 1 > buffer/enable
+
+Then user can poll/select or /dev/iio:deviceX (Here X correspond to iio device number),
+and read temperature samples. A good example is at tools/iio/generic_buffer.c.
+
+
Signature · 1 line
2.4.3
#12 lore raw

PATCH 6/6 thermal: x86_pkg_temp: Register threshold callbacks

In reply to Srinivas Pandruvada

Register callbacks to get/set temperature thresholds.

---
 drivers/thermal/x86_pkg_temp_thermal.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/thermal/x86_pkg_temp_thermal.c +8 -0
index 849f682..58d0770 100644
@@ -273,12 +273,20 @@ static int sys_get_trip_type(struct thermal_zone_device *thermal,
 	return 0;
 }
 
+static bool sys_check_notify_support(struct thermal_zone_device *tz)
+{
+	return true;
+}
+
 /* Thermal zone callback registry */
 static struct thermal_zone_device_ops tzone_ops = {
 	.get_temp = sys_get_curr_temp,
 	.get_trip_temp = sys_get_trip_temp,
 	.get_trip_type = sys_get_trip_type,
 	.set_trip_temp = sys_set_trip_temp,
+	.get_threshold_temp = sys_get_trip_temp,
+	.set_threshold_temp = sys_set_trip_temp,
+	.check_notification_support = sys_check_notify_support,
 };
 
 static bool pkg_temp_thermal_platform_thermal_rate_control(void)
Signature · 1 line
2.4.3
#13 lore raw
In reply to Srinivas Pandruvada
Quoted 6 lines · Srinivas Pandruvada

v0
Since this was discussed during LPC in Seattle and three versions of RFC are published and reviwed, this is posted as non RFC version. This has changes in iio interface file as suggested by Jonathan. We may add threshold event direction flag later if required, without much impact by adding new event type.

Not everyone was at LPC.

You should fix the changelog(s) and explain what you are trying to solve and why it is important.

Quoted 58 lines

RFC v2
- Rebase to 4.3.rc1
- Additional parameter to thermal_zone_device_update for event type
- Moved the dummy IIO callbacks to local include file, in this way "not used" warnings can be avoided
- Remove changes from user space governor, rather notify from core
- Validate the trigger, so that when thermal device supports async notifications, this will be exclusive trigger
- Use new thermal core event type to differentiate events for iio threshold notification

RFC v1
- Change commit message to be more clear
- Split in three patches (First for thermal_iio.c, second to integrate to thermal_core and third for user space governor).
- Removed IIO defines in thermal.h
- Removed direct push to buffers
- Other comments from Jonathan except introduce validate_trigger

RFC v0
Base version for first review

Srinivas Pandruvada (6):
thermal: core: enhance thermal_zone_device_update
thermal: documentation update
thermal: iio device for thermal sensor
thermal: use iio binding calls
thermal: iio Documentation
thermal: x86_pkg_temp: Register threshold callbacks

  Documentation/thermal/sysfs-api.txt                |  10 +
  Documentation/thermal/thermal_iio_binding          |  88 +++++
  drivers/acpi/thermal.c                             |   2 +-
  drivers/platform/x86/acerhdf.c                     |   2 +-
  drivers/thermal/Kconfig                            |  12 +
  drivers/thermal/Makefile                           |   1 +
  drivers/thermal/db8500_thermal.c                   |   2 +-
  drivers/thermal/hisi_thermal.c                     |   3 +-
  drivers/thermal/imx_thermal.c                      |   4 +-
  .../thermal/int340x_thermal/int340x_thermal_zone.h |   2 +-
  drivers/thermal/intel_soc_dts_iosf.c               |   3 +-
  drivers/thermal/of-thermal.c                       |   2 +-
  drivers/thermal/qcom-spmi-temp-alarm.c             |   2 +-
  drivers/thermal/rcar_thermal.c                     |   3 +-
  drivers/thermal/rockchip_thermal.c                 |   3 +-
  drivers/thermal/samsung/exynos_tmu.c               |   2 +-
  drivers/thermal/st/st_thermal_memmap.c             |   3 +-
  drivers/thermal/thermal_core.c                     |  21 +-
  drivers/thermal/thermal_iio.c                      | 383 +++++++++++++++++++++
  drivers/thermal/thermal_iio.h                      |  45 +++
  drivers/thermal/ti-soc-thermal/ti-thermal-common.c |   4 +-
  drivers/thermal/x86_pkg_temp_thermal.c             |  11 +-
  include/linux/thermal.h                            |  26 +-
  23 files changed, 609 insertions(+), 25 deletions(-)

create mode 100644 Documentation/thermal/thermal_iio_binding create mode 100644 drivers/thermal/thermal_iio.c create mode 100644 drivers/thermal/thermal_iio.h

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#14 lore raw
Quoted 11 lines · Daniel Lezcano
Quoted 9 lines · Srinivas Pandruvada

v0
Since this was discussed during LPC in Seattle and three versions of RFC are
published and reviwed, this is posted as non RFC version. This has changes in iio interface file as suggested by Jonathan. We may add threshold
event direction flag later if required, without much impact by adding new
event type.

Quoted 5 lines

Not everyone was at LPC.

You should fix the changelog(s) and explain what you are trying to solve
and why it is important.

Sure. The very first RFC had this. I hope you did get chance to look at this.

Quoted 64 lines
Quoted 62 lines

RFC v2
- Rebase to 4.3.rc1
- Additional parameter to thermal_zone_device_update for event type
- Moved the dummy IIO callbacks to local include file, in this way "not used" warnings can be avoided
- Remove changes from user space governor, rather notify from core
- Validate the trigger, so that when thermal device supports async notifications, this will be exclusive trigger
- Use new thermal core event type to differentiate events for iio threshold notification

RFC v1
- Change commit message to be more clear
- Split in three patches (First for thermal_iio.c, second to integrate
to thermal_core and third for user space governor).
- Removed IIO defines in thermal.h
- Removed direct push to buffers
- Other comments from Jonathan except introduce validate_trigger

RFC v0
Base version for first review

Srinivas Pandruvada (6):
thermal: core: enhance thermal_zone_device_update
thermal: documentation update
thermal: iio device for thermal sensor
thermal: use iio binding calls
thermal: iio Documentation
thermal: x86_pkg_temp: Register threshold callbacks

  Documentation/thermal/sysfs-api.txt                |  10 +
  Documentation/thermal/thermal_iio_binding          |  88 +++++
  drivers/acpi/thermal.c                             |   2 +-
  drivers/platform/x86/acerhdf.c                     |   2 +-
  drivers/thermal/Kconfig                            |  12 +
  drivers/thermal/Makefile                           |   1 +
  drivers/thermal/db8500_thermal.c                   |   2 +-
  drivers/thermal/hisi_thermal.c                     |   3 +-
  drivers/thermal/imx_thermal.c                      |   4 +-
  .../thermal/int340x_thermal/int340x_thermal_zone.h |   2 +-
  drivers/thermal/intel_soc_dts_iosf.c               |   3 +-
  drivers/thermal/of-thermal.c                       |   2 +-
  drivers/thermal/qcom-spmi-temp-alarm.c             |   2 +-
  drivers/thermal/rcar_thermal.c                     |   3 +-
  drivers/thermal/rockchip_thermal.c                 |   3 +-
  drivers/thermal/samsung/exynos_tmu.c               |   2 +-
  drivers/thermal/st/st_thermal_memmap.c             |   3 +-
  drivers/thermal/thermal_core.c                     |  21 +-
  drivers/thermal/thermal_iio.c                      | 383
+++++++++++++++++++++
  drivers/thermal/thermal_iio.h                      |  45 +++
  drivers/thermal/ti-soc-thermal/ti-thermal-common.c |   4 +-
  drivers/thermal/x86_pkg_temp_thermal.c             |  11 +-
  include/linux/thermal.h                            |  26 +-
  23 files changed, 609 insertions(+), 25 deletions(-)

create mode 100644 Documentation/thermal/thermal_iio_binding create mode 100644 drivers/thermal/thermal_iio.c create mode 100644 drivers/thermal/thermal_iio.h

maintainer Reviewed-by on v1 3/6
#4 lore raw

Re: [PATCH 3/6] thermal: iio device for thermal sensor

In reply to Srinivas Pandruvada
Quoted 40 lines · Srinivas Pandruvada

This change registers temperature sensor in a thermal zone as an IIO temperature device. This allows user space thermal application to fully utilize IIO capability to read temperature events and samples. The primary motivation for this change to improve performance for user space thermal controllers like Linux thermal daemon or Intel® Dynamic Platform and Thermal Framework (DPTF) for Chromium OS.
The current sysfs has several limitations, which forces the current user space to use polling as the safe way. This polling causes performance bottlenecks as some devices requires extremely aggressive response to changing temperature conditions.
These are some limitations, which this change tries to address:
- Temperature reading via sysfs attribute, which needs conversion from string to int
- No way to set threshold settings to avoid polling for temperature change without using a RW passive trip point.
- Event notifications via slow kobject uevents, which needs parsing to id the zone and read temperature
- Only pull interface from user space, kernel drivers can't send samples
- One sample at a time read from user space
These limitations can be addressed by registering temperature sensor as an IIO device. IIO provides
- buffered access via /dev/iio:deviceX node with select/poll capability
- temperature thresholds setting via iio event thresholds
- Wait and receive events
- Able to use different external trigger (data ready indications) and push samples to buffers

Next set of patches uses the IIO binding introduced in this patchset. The iio device created during call to thermal_zone_device_register. Samples are pushed to iio buffers when thermal_zone_device_update is called from client drivers.

New thermal zone device callbacks:
It introduces three new callbacks for thermal client drivers:
get_threshold_temp: Get the current threshold temperature
set_threshold_temp: Set the current threshold temperature check_notification_support: Inform that the client driver has asynchronous notification mechanism. If it is then polling can be avoided for the temperature sensor.

Quoted 1 line

I think I confused you in a previous review.

To avoid complexity with a non interrupt trigger like this one you need both the validate_trigger and validate_device callbacks (in the device and trigger respectively). See below.

Fix that little bit up (which is really simple!) and you can add

Thanks,

Jonathan

Quoted 151 lines
---
 drivers/thermal/Kconfig       |  12 ++
 drivers/thermal/Makefile      |   1 +
 drivers/thermal/thermal_iio.c | 383 ++++++++++++++++++++++++++++++++++++++++++
 drivers/thermal/thermal_iio.h |  45 +++++
 include/linux/thermal.h       |  13 ++
 5 files changed, 454 insertions(+)
 create mode 100644 drivers/thermal/thermal_iio.c
 create mode 100644 drivers/thermal/thermal_iio.h
drivers/thermal/Kconfig +12 -0
index 0390044..bc7836e 100644
@@ -29,6 +29,18 @@ config THERMAL_HWMON
 	  Say 'Y' here if you want all thermal sensors to
 	  have hwmon sysfs interface too.
 
+config THERMAL_IIO
+	bool
+	prompt "Thermal sensor from a zone as IIO sensor device"
+	select IIO
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  This will register thermal sensor in a zone as an IIO temperature
+	  sensor device.
+	  This will help user space thermal controllers to use IIO ABI to
+	  get events and buffered acces to temperature samples.
+
 config THERMAL_OF
 	bool
 	prompt "APIs to parse thermal data out of device tree"
drivers/thermal/Makefile +1 -0
index 26f1608..b72a28d 100644
@@ -7,6 +7,7 @@ thermal_sys-y			+= thermal_core.o
 
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)		+= thermal_hwmon.o
+thermal_sys-$(CONFIG_THERMAL_IIO)		+= thermal_iio.o
 thermal_sys-$(CONFIG_THERMAL_OF)		+= of-thermal.o
 
 # governors
drivers/thermal/thermal_iio.c new file +100 -0
new file mode 100644
index 0000000..492eaff
@@ -0,0 +1,383 @@
+/*
+ * thermal iio interface driver
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#include <linux/thermal.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/events.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+struct thermal_iio_data {
+	struct thermal_zone_device *tz;
+	struct iio_trigger *interrupt_trig;
+	struct iio_chan_spec *channels;
+	bool enable_trigger;
+	int temp_thres;
+	bool ev_enable_state;
+	struct mutex mutex;
+};
+
+static const struct iio_event_spec thermal_event = {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
+				 BIT(IIO_EV_INFO_ENABLE)
+};
+
+static const struct iio_chan_spec thermal_iio_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_index = 0,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 32,
+			.storagebits = 32,
+			.endianness = IIO_CPU,
+		}
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+static const struct iio_chan_spec thermal_iio_channels_with_events[] = {
+	{
+		.type = IIO_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_index = 0,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 32,
+			.storagebits = 32,
+			.endianness = IIO_CPU,
+		},
+		.event_spec = &thermal_event,
+		.num_event_specs = 1,
+
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+static int thermal_iio_read_raw(struct iio_dev *indio_dev,
+				struct iio_chan_spec const *chan,
+				int *val, int *val2, long mask)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int temp;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = thermal_zone_get_temp(iio_data->tz, &temp);
+		if (!ret) {
+			*val = temp;
+			ret = IIO_VAL_INT;
+		}
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static irqreturn_t thermal_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;

drivers/thermal/thermal_iio.c line 110

Quoted 10 lines
drivers/thermal/thermal_iio.c new file +10 -0
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	u32 buffer[4];
+	int temp;
+	int ret;
+
+	ret = thermal_zone_get_temp(iio_data->tz, &temp);
+	if (ret)
+		goto err_read;
+
+	*(u32 *)buffer = temp;

Umm. I'm confused by the above. buffer is a u32 array, so you are casting it to a u32 pointer then dereferencing that. Doesn't that just give you buffer[0] = temp?

(might be too late on a Sunday!) Given temp is signed, wby not use s32 buffer[4] and be done with it?

Quoted 31 lines
drivers/thermal/thermal_iio.c new file +31 -0
+	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
+					   iio_get_time_ns());
+err_read:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int thermal_data_rdy_trigger_set_state(struct iio_trigger *trig,
+					      bool state)
+{
+	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int ret = 0;
+
+	mutex_lock(&iio_data->mutex);
+	if (iio_data->tz->ops->set_notification_status) {
+		ret = iio_data->tz->ops->set_notification_status(iio_data->tz,
+								 state);
+		if (!ret)
+			iio_data->enable_trigger = state;
+	} else
+		iio_data->enable_trigger = state;
+	mutex_unlock(&iio_data->mutex);
+
+	return ret;
+}
+
+static int thermal_iio_validate_trigger(struct iio_dev *indio_dev,
+					struct iio_trigger *trig)
+{

drivers/thermal/thermal_iio.c line 151

Quoted 10 lines
drivers/thermal/thermal_iio.c new file +10 -0
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+
+	if (iio_data->interrupt_trig && iio_data->interrupt_trig != trig)
+		return -EINVAL;
+
+	return 0;
+}
+
+static const struct iio_trigger_ops thermal_trigger_ops = {
+	.set_trigger_state = thermal_data_rdy_trigger_set_state,

For reasons explained later, you want validate_device to be defined as well.

Quoted 217 lines
drivers/thermal/thermal_iio.c new file +217 -0
+	.owner = THIS_MODULE,
+};
+
+static int thermal_iio_read_event(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *chan,
+				  enum iio_event_type type,
+				  enum iio_event_direction dir,
+				  enum iio_event_info info,
+				  int *val, int *val2)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&iio_data->mutex);
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		*val = iio_data->temp_thres;
+		ret = IIO_VAL_INT;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+	mutex_unlock(&iio_data->mutex);
+
+	return ret;
+}
+
+static int thermal_iio_write_event(struct iio_dev *indio_dev,
+				   const struct iio_chan_spec *chan,
+				   enum iio_event_type type,
+				   enum iio_event_direction dir,
+				   enum iio_event_info info,
+				   int val, int val2)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int ret = 0;
+
+	mutex_lock(&iio_data->mutex);
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		iio_data->temp_thres = val;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+	mutex_unlock(&iio_data->mutex);
+
+	return ret;
+}
+
+static int thermal_iio_read_event_config(struct iio_dev *indio_dev,
+					 const struct iio_chan_spec *chan,
+					 enum iio_event_type type,
+					 enum iio_event_direction dir)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	bool state;
+
+	mutex_lock(&iio_data->mutex);
+	state = iio_data->ev_enable_state;
+	mutex_unlock(&iio_data->mutex);
+
+	return state;
+}
+
+static int thermal_iio_write_event_config(struct iio_dev *indio_dev,
+					  const struct iio_chan_spec *chan,
+					  enum iio_event_type type,
+					  enum iio_event_direction dir,
+					  int state)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int temp_thres;
+	int ret = 0;
+
+	mutex_lock(&iio_data->mutex);
+	if ((state && iio_data->ev_enable_state) ||
+	    (!state && !iio_data->ev_enable_state))
+		goto done_write_event;
+
+	if (state && !iio_data->temp_thres) {
+		ret = -EINVAL;
+		goto done_write_event;
+	}
+
+	if (state)
+		temp_thres = iio_data->temp_thres;
+	else
+		temp_thres = 0;
+
+	ret = iio_data->tz->ops->set_threshold_temp(iio_data->tz, 0,
+						    temp_thres);
+	if (ret)
+		goto done_write_event;
+
+	if (iio_data->tz->ops->set_notification_status) {
+		ret = iio_data->tz->ops->set_notification_status(iio_data->tz,
+								 state > 0);
+		if (!ret)
+			iio_data->ev_enable_state = state;
+	} else
+		iio_data->ev_enable_state = state;
+
+done_write_event:
+	mutex_unlock(&iio_data->mutex);
+
+	return ret;
+}
+
+static int thermal_iio_setup_trig(struct iio_trigger **iio_trig,
+				  struct thermal_zone_device *tz,
+				  const char *format)
+{
+	struct iio_trigger *trig;
+	int ret;
+
+	trig = devm_iio_trigger_alloc(&tz->device, format, tz->type,
+				      tz->indio_dev->id);
+	if (!trig) {
+		dev_err(&tz->device, "Trigger Allocate Failed\n");
+		return -ENOMEM;
+	}
+	trig->dev.parent = &tz->device;
+	trig->ops = &thermal_trigger_ops;
+	iio_trigger_set_drvdata(trig, tz->indio_dev);
+	ret = iio_trigger_register(trig);
+	if (ret) {
+		dev_err(&tz->device, "Trigger Allocate Failed\n");
+		return ret;
+	}
+	*iio_trig = trig;
+
+	return 0;
+}
+
+static const struct iio_info thermal_iio_info = {
+	.read_raw		= thermal_iio_read_raw,
+	.read_event_value	= thermal_iio_read_event,
+	.write_event_value	= thermal_iio_write_event,
+	.write_event_config	= thermal_iio_write_event_config,
+	.read_event_config	= thermal_iio_read_event_config,
+	.validate_trigger	= thermal_iio_validate_trigger,
+	.driver_module		= THIS_MODULE,
+};
+
+int thermal_iio_sensor_register(struct thermal_zone_device *tz)
+{
+	struct thermal_iio_data *iio_data;
+	int ret;
+
+	tz->indio_dev = devm_iio_device_alloc(&tz->device, sizeof(*iio_data));
+	if (!tz->indio_dev)
+		return -ENOMEM;
+
+	iio_data = iio_priv(tz->indio_dev);
+	iio_data->tz = tz;
+	mutex_init(&iio_data->mutex);
+
+	tz->indio_dev->dev.parent = &tz->device;
+	if (tz->ops->set_threshold_temp)
+		tz->indio_dev->channels = thermal_iio_channels_with_events;
+	else
+		tz->indio_dev->channels = thermal_iio_channels;
+	tz->indio_dev->num_channels = ARRAY_SIZE(thermal_iio_channels);
+	tz->indio_dev->name = tz->type;
+	tz->indio_dev->info = &thermal_iio_info;
+	tz->indio_dev->modes = INDIO_DIRECT_MODE;
+
+	if (tz->ops->check_notification_support &&
+	    tz->ops->check_notification_support(tz)) {
+		ret = thermal_iio_setup_trig(&iio_data->interrupt_trig, tz,
+					     "%s-dev%d");
+		if (ret)
+			return ret;
+
+		tz->indio_dev->trig = iio_trigger_get(iio_data->interrupt_trig);
+	}
+	ret = iio_triggered_buffer_setup(tz->indio_dev,
+					 &iio_pollfunc_store_time,
+					 thermal_trigger_handler, NULL);
+	if (ret) {
+		dev_err(&tz->device, "failed to init trigger buffer\n");
+		goto err_unreg_int_trig;
+	}
+	ret = iio_device_register(tz->indio_dev);
+	if (ret < 0) {
+		dev_err(&tz->device, "unable to register iio device\n");
+		goto err_cleanup_trig;
+	}
+
+	return 0;
+
+err_cleanup_trig:
+	iio_triggered_buffer_cleanup(tz->indio_dev);
+err_unreg_int_trig:
+	if (iio_data->interrupt_trig)
+		iio_trigger_unregister(iio_data->interrupt_trig);
+
+	return ret;
+}
+
+int thermal_iio_sensor_unregister(struct thermal_zone_device *tz)
+{
+	struct thermal_iio_data *iio_data = iio_priv(tz->indio_dev);
+
+	iio_device_unregister(tz->indio_dev);
+	iio_triggered_buffer_cleanup(tz->indio_dev);
+	if (iio_data->interrupt_trig)
+		iio_trigger_unregister(iio_data->interrupt_trig);
+
+	return 0;
+}
+
+int thermal_iio_sensor_notify(struct thermal_zone_device *tz,
+			      enum thermal_device_event_type event)
+{
+	struct thermal_iio_data *iio_data = iio_priv(tz->indio_dev);

drivers/thermal/thermal_iio.c line 378

Quoted 10 lines
drivers/thermal/thermal_iio.c new file +10 -0
+
+	mutex_lock(&iio_data->mutex);
+	if (iio_data->ev_enable_state &&
+	    event == THERMAL_DEVICE_EVENT_THRESHOLD)
+		iio_push_event(tz->indio_dev,
+			       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
+						    IIO_EV_TYPE_THRESH,
+						    IIO_EV_DIR_EITHER),
+			       iio_get_time_ns());
+	if (iio_data->enable_trigger)

I didn't pick up on this earlier, but is this running in interrupt context or not? iio_trigger_poll is generally assumed to do so and I suspect this is not guaranteed to be given the mutex!

Validating the trigger means that only the right trigger can drive the thermal buffer, but unless you validate the other way as well there is nothing stopping something else registering to use this trigger. If it's not a true interrupt, all sorts of mess can occur.

What you need is the validate_device callback for the trigger to check the device is the one providing the trigger.

Once you've validated both ways, using iio_trigger_poll_chained is fine and makes it clear we are calling a bottom half (these days normally an interrupt thread, but not one here).

The alternative is to go through the irq_work dance, but I doubt there is any reason to do that here.

Quoted 102 lines
drivers/thermal/thermal_iio.c new file +5 -0
+		iio_trigger_poll(tz->indio_dev->trig);
+	mutex_unlock(&iio_data->mutex);
+
+	return 0;
+}
drivers/thermal/thermal_iio.h new file +45 -0
new file mode 100644
index 0000000..850ea7d
@@ -0,0 +1,45 @@
+/*
+ * thermal iio interface driver interface file
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef __THERMAL_IIO_H__
+#define __THERMAL_IIO_H__
+
+#if defined(CONFIG_THERMAL_IIO)
+
+int thermal_iio_sensor_register(struct thermal_zone_device *tz);
+int thermal_iio_sensor_unregister(struct thermal_zone_device *tz);
+int thermal_iio_sensor_notify(struct thermal_zone_device *tz,
+			      enum thermal_device_event_type event);
+
+#else
+
+static int thermal_iio_sensor_register(struct thermal_zone_device *tz)
+{
+	return 0;
+}
+
+static int thermal_iio_sensor_unregister(struct thermal_zone_device *tz)
+{
+	return 0;
+}
+
+static int thermal_iio_sensor_notify(struct thermal_zone_device *tz,
+				     enum thermal_device_event_type event)
+
+{
+	return 0;
+}
+
+#endif
+#endif
include/linux/thermal.h +13 -0
index c074f6a..925d1e5 100644
@@ -114,6 +114,13 @@ struct thermal_zone_device_ops {
 	int (*set_emul_temp) (struct thermal_zone_device *, int);
 	int (*get_trend) (struct thermal_zone_device *, int,
 			  enum thermal_trend *);
+	int (*get_threshold_temp)(struct thermal_zone_device *, int,
+				  int *);
+	int (*set_threshold_temp)(struct thermal_zone_device *, int,
+				  int);
+	int (*set_notification_status)(struct thermal_zone_device *,
+				       bool status);
+	bool (*check_notification_support)(struct thermal_zone_device *);
 	int (*notify) (struct thermal_zone_device *, int,
 		       enum thermal_trip_type);
 };
@@ -148,6 +155,8 @@ struct thermal_attr {
 	char name[THERMAL_NAME_LENGTH];
 };
 
+struct iio_dev;
+
 /**
  * struct thermal_zone_device - structure for a thermal zone
  * @id:		unique id number for each thermal zone
@@ -182,6 +191,7 @@ struct thermal_attr {
  * @lock:	lock to protect thermal_instances list
  * @node:	node in thermal_tz_list (in thermal_core.c)
  * @poll_queue:	delayed work for polling
+ * @indio_dev:	pointer to instance of an IIO dev for this zone
  */
 struct thermal_zone_device {
 	int id;
@@ -208,6 +218,9 @@ struct thermal_zone_device {
 	struct mutex lock;
 	struct list_head node;
 	struct delayed_work poll_queue;
+#if defined(CONFIG_THERMAL_IIO)
+	struct iio_dev *indio_dev;
+#endif
 };
 
 /**
maintainer
#10 lore raw

Re: [PATCH 5/6] thermal: iio Documentation

In reply to Srinivas Pandruvada
Quoted 51 lines · Srinivas Pandruvada

An overview document about thermal iio binding.

---
 Documentation/thermal/thermal_iio_binding | 88 +++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 Documentation/thermal/thermal_iio_binding
Documentation/thermal/thermal_iio_binding new file +37 -0
new file mode 100644
index 0000000..df501f1
@@ -0,0 +1,88 @@
+
+Thermal IIO Bindings
+
+When CONFIG_THERMAL_IIO is enabled, in addition to the regular thermal
+zone attributes, an additional entry for an IIO device is created.
+IIO sysfs ABI documents is available at Documentation/ABI/sysfs-bus-iio.
+
+The following example shows the contents of the IIO:device in a thermal
+zone.
+
+thermal_zoneX
+├── iio:device9
+│   ├── buffer
+│   │   ├── enable
+│   │   ├── length
+│   │   └── watermark
+│   ├── events
+│   │   ├── in_temp_thresh_either_en
+│   │   └── in_temp_thresh_either_value
+│   ├── in_temp_raw
+│   ├── name
+│   ├── scan_elements
+│   │   ├── in_temp_en
+│   │   ├── in_temp_index
+│   │   ├── in_temp_type
+│   │   ├── in_timestamp_en
+│   │   ├── in_timestamp_index
+│   │   └── in_timestamp_type
+│   ├── subsystem -> ../../../../../bus/iio
+│   ├── trigger0
+│   │   └── name
+│   ├── trigger
+│   │   └── current_trigger
+│   └── uevent
+├── integral_cutoff
+...
+... other thermal_zone attributes

Documentation/thermal/thermal_iio_binding line 47

Quoted 10 lines
Documentation/thermal/thermal_iio_binding new file +10 -0
+...
+
+
+IIO Attributes
+
+name: This shows iio device name, which will match thermal_zone/type attribute
+in_temp_raw: This will show raw temperature values same as thermal_zone/temp
+attribute.
+
+trigger/current_trigger: This is IIO way of specifying data ready indication.

Kind of, given it's not exposed to userspace to read, but rather hooked directly to the filling of the buffer. You might want to reword to not imply userspace can discover that it has fired directly.

Quoted 30 lines
Documentation/thermal/thermal_iio_binding new file +30 -0
+If the thermal client can provide asynchronous notifications, then a iio trigger
+device is created under trigger0. The name of this trigger will be thermal
+device name suffixed with devX. To enable iio buffer notifications,
+trigger/current_trigger cab be updated with this trigger. For example
+
+#echo "x86_pkg_temp-dev9" > trigger/current_trigger
+
+In addition of samples can be pushed to iio buffers with external standard IIO
+device triggers, E.g. sysfs trigger.
+
+How to enable events?
+To enable events a suitable threshold temperature can be written to
+in_temp_thresh_either_value
+and enable by writing 1 to in_temp_thresh_either_en.
+
+To monitor events a good user space program is here:
+tools/iio/iio_event_monitor.c
+
+For example
+# echo 50000 > /sys/devices/virtual/thermal/thermal_zone9/iio:device9/events/in_temp_thresh_either_value
+# echo 1 > /sys/devices/virtual/thermal/thermal_zone9/iio:device9/events/in_temp_thresh_either_en
+# ./iio_event_monitor x86_pkg_temp
+Found IIO device with name x86_pkg_temp with device number 9
+Event: time: 1443297126626859289, type: temp, channel: 0, evtype: thresh, direction: either
+
+How to read temperature samples?
+When thermal client or core driver calls thermal_zone_device_update, this will
+result in sample pushed to iio buffers. These samples can be read from
+/dev/iio:deviceX file.
+

Documentation/thermal/thermal_iio_binding line 87

Quoted 10 lines
Documentation/thermal/thermal_iio_binding new file +10 -0
+To enable buffer select scan_elements
+# echo 1 > scan_elements/in_temp_en
+
+Set buffer length to n samples and enable buffer.
+# echo 2 > buffer/length
+# echo 1 > buffer/enable
+
+Then user can poll/select or /dev/iio:deviceX (Here X correspond to iio device number),
+and read temperature samples. A good example is at tools/iio/generic_buffer.c.
+

One too many blank lines at the end.

Documentation/thermal/thermal_iio_binding line 88

Quoted 1 line
Documentation/thermal/thermal_iio_binding new file +1 -0
+

Reasonable docs to get people started I think, but whether they are detailed enough is going to be a question for the thermal guys!

Also, perhaps a cross reference to Daniel's doc book on IIO? Documentation/DocBook/iio/

As that expands in detail it might include lots of stuff that isn't relevant to thermal, but it might also fill in details that we don't want to explicitly layout here.

Jonathan

#11 lore raw

Re: [PATCH 5/6] thermal: iio Documentation

Quoted 68 lines · Jonathan Cameron
Quoted 67 lines · Srinivas Pandruvada

An overview document about thermal iio binding.

Signed-off-by: Srinivas Pandruvada <
[email protected]>

---
 Documentation/thermal/thermal_iio_binding | 88
+++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 Documentation/thermal/thermal_iio_binding
Documentation/thermal/thermal_iio_binding new file +4 -0
new file mode 100644
index 0000000..df501f1
@@ -0,0 +1,88 @@
+
+Thermal IIO Bindings
+
+When CONFIG_THERMAL_IIO is enabled, in addition to the regular

thermal

Documentation/thermal/thermal_iio_binding new file +4 -1
+zone attributes, an additional entry for an IIO device is created.
+IIO sysfs ABI documents is available at Documentation/ABI/sysfs
-bus-iio.
+
+The following example shows the contents of the IIO:device in a

thermal

Documentation/thermal/thermal_iio_binding new file +35 -0
+zone.
+
+thermal_zoneX
+├── iio:device9
+│   ├── buffer
+│   │   ├── enable
+│   │   ├── length
+│   │   └── watermark
+│   ├── events
+│   │   ├── in_temp_thresh_either_en
+│   │   └── in_temp_thresh_either_value
+│   ├── in_temp_raw
+│   ├── name
+│   ├── scan_elements
+│   │   ├── in_temp_en
+│   │   ├── in_temp_index
+│   │   ├── in_temp_type
+│   │   ├── in_timestamp_en
+│   │   ├── in_timestamp_index
+│   │   └── in_timestamp_type
+│   ├── subsystem -> ../../../../../bus/iio
+│   ├── trigger0
+│   │   └── name
+│   ├── trigger
+│   │   └── current_trigger
+│   └── uevent
+├── integral_cutoff
+...
+... other thermal_zone attributes
+...
+
+
+IIO Attributes
+
+name: This shows iio device name, which will match

thermal_zone/type attribute
+in_temp_raw: This will show raw temperature values same as thermal_zone/temp
+attribute.

Quoted 8 lines
Quoted 3 lines
Documentation/thermal/thermal_iio_binding new file +2 -0
+
+trigger/current_trigger: This is IIO way of specifying data ready

indication.

Kind of, given it's not exposed to userspace to read, but rather hooked directly
to the filling of the buffer. You might want to reword to not imply userspace can
discover that it has fired directly.

OK

Quoted 56 lines
Quoted 51 lines

+If the thermal client can provide asynchronous notifications, then a iio trigger
+device is created under trigger0. The name of this trigger will be thermal
+device name suffixed with devX. To enable iio buffer notifications,
+trigger/current_trigger cab be updated with this trigger. For example

Documentation/thermal/thermal_iio_binding new file +4 -0
+
+#echo "x86_pkg_temp-dev9" > trigger/current_trigger
+
+In addition of samples can be pushed to iio buffers with external

standard IIO

Documentation/thermal/thermal_iio_binding new file +4 -0
+device triggers, E.g. sysfs trigger.
+
+How to enable events?
+To enable events a suitable threshold temperature can be written

to

Documentation/thermal/thermal_iio_binding new file +8 -0
+in_temp_thresh_either_value
+and enable by writing 1 to in_temp_thresh_either_en.
+
+To monitor events a good user space program is here:
+tools/iio/iio_event_monitor.c
+
+For example
+# echo 50000 >

/sys/devices/virtual/thermal/thermal_zone9/iio:device9/events/in_te mp_thresh_either_value
+# echo 1 > /sys/devices/virtual/thermal/thermal_zone9/iio:device9/events/in_te mp_thresh_either_en

Documentation/thermal/thermal_iio_binding new file +3 -0
+# ./iio_event_monitor x86_pkg_temp
+Found IIO device with name x86_pkg_temp with device number 9
+Event: time: 1443297126626859289, type: temp, channel: 0, evtype:

thresh, direction: either

Documentation/thermal/thermal_iio_binding new file +3 -0
+
+How to read temperature samples?
+When thermal client or core driver calls

thermal_zone_device_update, this will
+result in sample pushed to iio buffers. These samples can be read from

Documentation/thermal/thermal_iio_binding new file +10 -0
+/dev/iio:deviceX file.
+
+To enable buffer select scan_elements
+# echo 1 > scan_elements/in_temp_en
+
+Set buffer length to n samples and enable buffer.
+# echo 2 > buffer/length
+# echo 1 > buffer/enable
+
+Then user can poll/select or /dev/iio:deviceX (Here X correspond
Quoted 4 lines

to iio device number),
+and read temperature samples. A good example is at tools/iio/generic_buffer.c.
+

One too many blank lines at the end.

Quoted 5 lines
Quoted 1 line

+

Reasonable docs to get people started I think, but whether they are detailed enough is
going to be a question for the thermal guys!

Idea here is to just give pointers to the IIO documentation and examples to get developers to explore if they want to use this I/F.

Quoted 9 lines

Also, perhaps a cross reference to Daniel's doc book on IIO? Documentation/DocBook/iio/

As that expands in detail it might include lots of stuff that isn't relevant
to thermal, but it might also fill in details that we don't want to explicitly
layout here.

Thanks for your reviews.

-Srinivas

Quoted 7 lines

Jonathan

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#5 lore raw

Re: [PATCH 3/6] thermal: iio device for thermal sensor

In reply to Srinivas Pandruvada
Quoted 531 lines · Srinivas Pandruvada

This change registers temperature sensor in a thermal zone as an IIO temperature device. This allows user space thermal application to fully utilize IIO capability to read temperature events and samples. The primary motivation for this change to improve performance for user space thermal controllers like Linux thermal daemon or Intel® Dynamic Platform and Thermal Framework (DPTF) for Chromium OS.
The current sysfs has several limitations, which forces the current user space to use polling as the safe way. This polling causes performance bottlenecks as some devices requires extremely aggressive response to changing temperature conditions.
These are some limitations, which this change tries to address:
- Temperature reading via sysfs attribute, which needs conversion from string to int
- No way to set threshold settings to avoid polling for temperature change without using a RW passive trip point.
- Event notifications via slow kobject uevents, which needs parsing to id the zone and read temperature
- Only pull interface from user space, kernel drivers can't send samples
- One sample at a time read from user space
These limitations can be addressed by registering temperature sensor as an IIO device. IIO provides
- buffered access via /dev/iio:deviceX node with select/poll capability
- temperature thresholds setting via iio event thresholds
- Wait and receive events
- Able to use different external trigger (data ready indications) and push samples to buffers

Next set of patches uses the IIO binding introduced in this patchset. The iio device created during call to thermal_zone_device_register. Samples are pushed to iio buffers when thermal_zone_device_update is called from client drivers.

New thermal zone device callbacks:
It introduces three new callbacks for thermal client drivers:
get_threshold_temp: Get the current threshold temperature
set_threshold_temp: Set the current threshold temperature check_notification_support: Inform that the client driver has asynchronous notification mechanism. If it is then polling can be avoided for the temperature sensor.

---
 drivers/thermal/Kconfig       |  12 ++
 drivers/thermal/Makefile      |   1 +
 drivers/thermal/thermal_iio.c | 383 ++++++++++++++++++++++++++++++++++++++++++
 drivers/thermal/thermal_iio.h |  45 +++++
 include/linux/thermal.h       |  13 ++
 5 files changed, 454 insertions(+)
 create mode 100644 drivers/thermal/thermal_iio.c
 create mode 100644 drivers/thermal/thermal_iio.h
drivers/thermal/Kconfig +12 -0
index 0390044..bc7836e 100644
@@ -29,6 +29,18 @@ config THERMAL_HWMON
 	  Say 'Y' here if you want all thermal sensors to
 	  have hwmon sysfs interface too.
 
+config THERMAL_IIO
+	bool
+	prompt "Thermal sensor from a zone as IIO sensor device"
+	select IIO
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  This will register thermal sensor in a zone as an IIO temperature
+	  sensor device.
+	  This will help user space thermal controllers to use IIO ABI to
+	  get events and buffered acces to temperature samples.
+
 config THERMAL_OF
 	bool
 	prompt "APIs to parse thermal data out of device tree"
drivers/thermal/Makefile +1 -0
index 26f1608..b72a28d 100644
@@ -7,6 +7,7 @@ thermal_sys-y			+= thermal_core.o
 
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)		+= thermal_hwmon.o
+thermal_sys-$(CONFIG_THERMAL_IIO)		+= thermal_iio.o
 thermal_sys-$(CONFIG_THERMAL_OF)		+= of-thermal.o
 
 # governors
drivers/thermal/thermal_iio.c new file +383 -0
new file mode 100644
index 0000000..492eaff
@@ -0,0 +1,383 @@
+/*
+ * thermal iio interface driver
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#include <linux/thermal.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/events.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+struct thermal_iio_data {
+	struct thermal_zone_device *tz;
+	struct iio_trigger *interrupt_trig;
+	struct iio_chan_spec *channels;
+	bool enable_trigger;
+	int temp_thres;
+	bool ev_enable_state;
+	struct mutex mutex;
+};
+
+static const struct iio_event_spec thermal_event = {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
+				 BIT(IIO_EV_INFO_ENABLE)
+};
+
+static const struct iio_chan_spec thermal_iio_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_index = 0,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 32,
+			.storagebits = 32,
+			.endianness = IIO_CPU,
+		}
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+static const struct iio_chan_spec thermal_iio_channels_with_events[] = {
+	{
+		.type = IIO_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_index = 0,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 32,
+			.storagebits = 32,
+			.endianness = IIO_CPU,
+		},
+		.event_spec = &thermal_event,
+		.num_event_specs = 1,
+
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+static int thermal_iio_read_raw(struct iio_dev *indio_dev,
+				struct iio_chan_spec const *chan,
+				int *val, int *val2, long mask)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int temp;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = thermal_zone_get_temp(iio_data->tz, &temp);
+		if (!ret) {
+			*val = temp;
+			ret = IIO_VAL_INT;
+		}
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static irqreturn_t thermal_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	u32 buffer[4];
+	int temp;
+	int ret;
+
+	ret = thermal_zone_get_temp(iio_data->tz, &temp);
+	if (ret)
+		goto err_read;
+
+	*(u32 *)buffer = temp;
+	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
+					   iio_get_time_ns());
+err_read:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static int thermal_data_rdy_trigger_set_state(struct iio_trigger *trig,
+					      bool state)
+{
+	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int ret = 0;
+
+	mutex_lock(&iio_data->mutex);
+	if (iio_data->tz->ops->set_notification_status) {
+		ret = iio_data->tz->ops->set_notification_status(iio_data->tz,
+								 state);
+		if (!ret)
+			iio_data->enable_trigger = state;
+	} else
+		iio_data->enable_trigger = state;
+	mutex_unlock(&iio_data->mutex);
+
+	return ret;
+}
+
+static int thermal_iio_validate_trigger(struct iio_dev *indio_dev,
+					struct iio_trigger *trig)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+
+	if (iio_data->interrupt_trig && iio_data->interrupt_trig != trig)
+		return -EINVAL;
+
+	return 0;
+}
+
+static const struct iio_trigger_ops thermal_trigger_ops = {
+	.set_trigger_state = thermal_data_rdy_trigger_set_state,
+	.owner = THIS_MODULE,
+};
+
+static int thermal_iio_read_event(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *chan,
+				  enum iio_event_type type,
+				  enum iio_event_direction dir,
+				  enum iio_event_info info,
+				  int *val, int *val2)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&iio_data->mutex);
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		*val = iio_data->temp_thres;
+		ret = IIO_VAL_INT;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+	mutex_unlock(&iio_data->mutex);
+
+	return ret;
+}
+
+static int thermal_iio_write_event(struct iio_dev *indio_dev,
+				   const struct iio_chan_spec *chan,
+				   enum iio_event_type type,
+				   enum iio_event_direction dir,
+				   enum iio_event_info info,
+				   int val, int val2)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int ret = 0;
+
+	mutex_lock(&iio_data->mutex);
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		iio_data->temp_thres = val;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+	mutex_unlock(&iio_data->mutex);
+
+	return ret;
+}
+
+static int thermal_iio_read_event_config(struct iio_dev *indio_dev,
+					 const struct iio_chan_spec *chan,
+					 enum iio_event_type type,
+					 enum iio_event_direction dir)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	bool state;
+
+	mutex_lock(&iio_data->mutex);
+	state = iio_data->ev_enable_state;
+	mutex_unlock(&iio_data->mutex);
+
+	return state;
+}
+
+static int thermal_iio_write_event_config(struct iio_dev *indio_dev,
+					  const struct iio_chan_spec *chan,
+					  enum iio_event_type type,
+					  enum iio_event_direction dir,
+					  int state)
+{
+	struct thermal_iio_data *iio_data = iio_priv(indio_dev);
+	int temp_thres;
+	int ret = 0;
+
+	mutex_lock(&iio_data->mutex);
+	if ((state && iio_data->ev_enable_state) ||
+	    (!state && !iio_data->ev_enable_state))
+		goto done_write_event;
+
+	if (state && !iio_data->temp_thres) {
+		ret = -EINVAL;
+		goto done_write_event;
+	}
+
+	if (state)
+		temp_thres = iio_data->temp_thres;
+	else
+		temp_thres = 0;
+
+	ret = iio_data->tz->ops->set_threshold_temp(iio_data->tz, 0,
+						    temp_thres);
+	if (ret)
+		goto done_write_event;
+
+	if (iio_data->tz->ops->set_notification_status) {
+		ret = iio_data->tz->ops->set_notification_status(iio_data->tz,
+								 state > 0);
+		if (!ret)
+			iio_data->ev_enable_state = state;
+	} else
+		iio_data->ev_enable_state = state;
+
+done_write_event:
+	mutex_unlock(&iio_data->mutex);
+
+	return ret;
+}
+
+static int thermal_iio_setup_trig(struct iio_trigger **iio_trig,
+				  struct thermal_zone_device *tz,
+				  const char *format)
+{
+	struct iio_trigger *trig;
+	int ret;
+
+	trig = devm_iio_trigger_alloc(&tz->device, format, tz->type,
+				      tz->indio_dev->id);
+	if (!trig) {
+		dev_err(&tz->device, "Trigger Allocate Failed\n");
+		return -ENOMEM;
+	}
+	trig->dev.parent = &tz->device;
+	trig->ops = &thermal_trigger_ops;
+	iio_trigger_set_drvdata(trig, tz->indio_dev);
+	ret = iio_trigger_register(trig);
+	if (ret) {
+		dev_err(&tz->device, "Trigger Allocate Failed\n");
+		return ret;
+	}
+	*iio_trig = trig;
+
+	return 0;
+}
+
+static const struct iio_info thermal_iio_info = {
+	.read_raw		= thermal_iio_read_raw,
+	.read_event_value	= thermal_iio_read_event,
+	.write_event_value	= thermal_iio_write_event,
+	.write_event_config	= thermal_iio_write_event_config,
+	.read_event_config	= thermal_iio_read_event_config,
+	.validate_trigger	= thermal_iio_validate_trigger,
+	.driver_module		= THIS_MODULE,
+};
+
+int thermal_iio_sensor_register(struct thermal_zone_device *tz)
+{
+	struct thermal_iio_data *iio_data;
+	int ret;
+
+	tz->indio_dev = devm_iio_device_alloc(&tz->device, sizeof(*iio_data));
+	if (!tz->indio_dev)
+		return -ENOMEM;
+
+	iio_data = iio_priv(tz->indio_dev);
+	iio_data->tz = tz;
+	mutex_init(&iio_data->mutex);
+
+	tz->indio_dev->dev.parent = &tz->device;
+	if (tz->ops->set_threshold_temp)
+		tz->indio_dev->channels = thermal_iio_channels_with_events;
+	else
+		tz->indio_dev->channels = thermal_iio_channels;
+	tz->indio_dev->num_channels = ARRAY_SIZE(thermal_iio_channels);
+	tz->indio_dev->name = tz->type;
+	tz->indio_dev->info = &thermal_iio_info;
+	tz->indio_dev->modes = INDIO_DIRECT_MODE;
+
+	if (tz->ops->check_notification_support &&
+	    tz->ops->check_notification_support(tz)) {
+		ret = thermal_iio_setup_trig(&iio_data->interrupt_trig, tz,
+					     "%s-dev%d");
+		if (ret)
+			return ret;
+
+		tz->indio_dev->trig = iio_trigger_get(iio_data->interrupt_trig);
+	}
+	ret = iio_triggered_buffer_setup(tz->indio_dev,
+					 &iio_pollfunc_store_time,
+					 thermal_trigger_handler, NULL);
+	if (ret) {
+		dev_err(&tz->device, "failed to init trigger buffer\n");
+		goto err_unreg_int_trig;
+	}
+	ret = iio_device_register(tz->indio_dev);
+	if (ret < 0) {
+		dev_err(&tz->device, "unable to register iio device\n");
+		goto err_cleanup_trig;
+	}
+
+	return 0;
+
+err_cleanup_trig:
+	iio_triggered_buffer_cleanup(tz->indio_dev);
+err_unreg_int_trig:
+	if (iio_data->interrupt_trig)
+		iio_trigger_unregister(iio_data->interrupt_trig);
+
+	return ret;
+}
+
+int thermal_iio_sensor_unregister(struct thermal_zone_device *tz)
+{
+	struct thermal_iio_data *iio_data = iio_priv(tz->indio_dev);
+
+	iio_device_unregister(tz->indio_dev);
+	iio_triggered_buffer_cleanup(tz->indio_dev);
+	if (iio_data->interrupt_trig)
+		iio_trigger_unregister(iio_data->interrupt_trig);
+
+	return 0;
+}
+
+int thermal_iio_sensor_notify(struct thermal_zone_device *tz,
+			      enum thermal_device_event_type event)
+{
+	struct thermal_iio_data *iio_data = iio_priv(tz->indio_dev);
+
+	mutex_lock(&iio_data->mutex);
+	if (iio_data->ev_enable_state &&
+	    event == THERMAL_DEVICE_EVENT_THRESHOLD)
+		iio_push_event(tz->indio_dev,
+			       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
+						    IIO_EV_TYPE_THRESH,
+						    IIO_EV_DIR_EITHER),
+			       iio_get_time_ns());
+	if (iio_data->enable_trigger)
+		iio_trigger_poll(tz->indio_dev->trig);
+	mutex_unlock(&iio_data->mutex);
+
+	return 0;
+}
drivers/thermal/thermal_iio.h new file +45 -0
new file mode 100644
index 0000000..850ea7d
@@ -0,0 +1,45 @@
+/*
+ * thermal iio interface driver interface file
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef __THERMAL_IIO_H__
+#define __THERMAL_IIO_H__
+
+#if defined(CONFIG_THERMAL_IIO)
+
+int thermal_iio_sensor_register(struct thermal_zone_device *tz);
+int thermal_iio_sensor_unregister(struct thermal_zone_device *tz);
+int thermal_iio_sensor_notify(struct thermal_zone_device *tz,
+			      enum thermal_device_event_type event);
+
+#else
+
+static int thermal_iio_sensor_register(struct thermal_zone_device *tz)
+{
+	return 0;
+}
+
+static int thermal_iio_sensor_unregister(struct thermal_zone_device *tz)
+{
+	return 0;
+}
+
+static int thermal_iio_sensor_notify(struct thermal_zone_device *tz,
+				     enum thermal_device_event_type event)
+
+{
+	return 0;
+}
+
+#endif
+#endif
include/linux/thermal.h +0 -0
index c074f6a..925d1e5 100644

include/linux/thermal.h struct thermal_zone_device_ops { · line 123

Quoted 10 lines
include/linux/thermal.h +7 -0
@@ -114,6 +114,13 @@ struct thermal_zone_device_ops {
 	int (*set_emul_temp) (struct thermal_zone_device *, int);
 	int (*get_trend) (struct thermal_zone_device *, int,
 			  enum thermal_trend *);
+	int (*get_threshold_temp)(struct thermal_zone_device *, int,
+				  int *);
+	int (*set_threshold_temp)(struct thermal_zone_device *, int,
+				  int);
+	int (*set_notification_status)(struct thermal_zone_device *,
+				       bool status);
+	bool (*check_notification_support)(struct thermal_zone_device *);

Can an existing thermal sensor benefit of the new functionality? If yes, that is the correct path for it, which code changes are required?

The problem I have with the above is the fact that they add callbacks and behavior eligible only for a subset of thermal zones.

If we are moving to IIO integrated mode, the existing drivers need to be converted, to avoid diversification of behavior. For example, there is existing notification callbacks. Also, hot trip points are described as notification points.

I believe the patch set needs to document also the next steps on this move.

Quoted 33 lines
include/linux/thermal.h +6 -0
 	int (*notify) (struct thermal_zone_device *, int,
 		       enum thermal_trip_type);
 };
@@ -148,6 +155,8 @@ struct thermal_attr {
 	char name[THERMAL_NAME_LENGTH];
 };
 
+struct iio_dev;
+
 /**
  * struct thermal_zone_device - structure for a thermal zone
  * @id:		unique id number for each thermal zone
@@ -182,6 +191,7 @@ struct thermal_attr {
  * @lock:	lock to protect thermal_instances list
  * @node:	node in thermal_tz_list (in thermal_core.c)
  * @poll_queue:	delayed work for polling
+ * @indio_dev:	pointer to instance of an IIO dev for this zone
  */
 struct thermal_zone_device {
 	int id;
@@ -208,6 +218,9 @@ struct thermal_zone_device {
 	struct mutex lock;
 	struct list_head node;
 	struct delayed_work poll_queue;
+#if defined(CONFIG_THERMAL_IIO)
+	struct iio_dev *indio_dev;
+#endif
 };
 
 /**
Signature · 1 line
2.4.3
#6 lore raw

Re: [PATCH 3/6] thermal: iio device for thermal sensor

Quoted 62 lines · Eduardo Valentin
Quoted 61 lines · Srinivas Pandruvada

This change registers temperature sensor in a thermal zone as an IIO
temperature device. This allows user space thermal application to fully
utilize IIO capability to read temperature events and samples. The primary motivation for this change to improve performance for user
space thermal controllers like Linux thermal daemon or Intel® Dynamic
Platform and Thermal Framework (DPTF) for Chromium OS. The current sysfs has several limitations, which forces the current user space to use polling as the safe way. This polling causes performance
bottlenecks as some devices requires extremely aggressive response to
changing temperature conditions.
These are some limitations, which this change tries to address:
- Temperature reading via sysfs attribute, which needs conversion from
string to int
- No way to set threshold settings to avoid polling for temperature change
without using a RW passive trip point.
- Event notifications via slow kobject uevents, which needs parsing to id
the zone and read temperature
- Only pull interface from user space, kernel drivers can't send samples
- One sample at a time read from user space
These limitations can be addressed by registering temperature sensor
as an IIO device. IIO provides
- buffered access via /dev/iio:deviceX node with select/poll capability
- temperature thresholds setting via iio event thresholds
- Wait and receive events
- Able to use different external trigger (data ready indications) and push
samples to buffers

Next set of patches uses the IIO binding introduced in this patchset.
The iio device created during call to thermal_zone_device_register. Samples
are pushed to iio buffers when thermal_zone_device_update is called from
client drivers.

New thermal zone device callbacks:
It introduces three new callbacks for thermal client drivers:
get_threshold_temp: Get the current threshold temperature
set_threshold_temp: Set the current threshold temperature check_notification_support: Inform that the client driver has asynchronous
notification mechanism. If it is then polling can be avoided for the
temperature sensor.

Signed-off-by: Srinivas Pandruvada < srinivas.pandruvada-VuQAYsv1563Yd54FQh9/[email protected]>
---

[Cut]

Quoted 16 lines
Quoted 11 lines
(quoted diff) +1 -0
+	int (*get_threshold_temp)(struct thermal_zone_device *, > > 

int,

(quoted diff) +2 -0
+				  int *);
+	int (*set_threshold_temp)(struct thermal_zone_device *,

int,

(quoted diff) +2 -0
+				  int);
+	int (*set_notification_status)(struct thermal_zone_device

*,

(quoted diff) +2 -0
+				       bool status);
+	bool (*check_notification_support)(struct
Quoted 1 line

thermal_zone_device *);

Can an existing thermal sensor benefit of the new functionality? If yes,

Yes. The existing driver will be using the trips instead of thresholds. As we discussed during LPC,
trips (pasive/active/..): These are temperature points to which a cooling action can be performed by binding a cooling device. The existing drivers who support RW trips, user space still can update those and get sample notifications. Existing drivers either have polling or interrupt suport to evaluate trips and call thermal_zone_device_update(), which will result in sample pushed to user space.

Thresholds: These are sensor temperature points which user space wants to get notification of temperature change. They will not be presented as passive/active/hot trips. User space can decide what to do with the notifications. If existing drivers don't want to present as fake trips, then they need to add the callback support.

Quoted 11 lines

The problem I have with the above is the fact that they add callbacks and behavior eligible only for a subset of thermal zones.

If we are moving to IIO integrated mode, the existing drivers need to be
converted, to avoid diversification of behavior. For example, there is
existing notification callbacks. Also, hot trip points are described as
notification points.

Quoted 3 lines

I believe the patch set needs to document also the next steps on this move.

If we decide to merge this functionality, I will push a documentation and IIO sample program to use these features.

Thanks,
Srinivas

Quoted 32 lines
Quoted 32 lines
include/linux/thermal.h +3 -0
 	int (*notify) (struct thermal_zone_device *, int,
 		       enum thermal_trip_type);
 };
@@ -148,6 +155,8 @@ struct thermal_attr {
 	char name[THERMAL_NAME_LENGTH];
 };
 
+struct iio_dev;
+
 /**
  * struct thermal_zone_device - structure for a thermal zone
  * @id:		unique id number for each thermal zone
@@ -182,6 +191,7 @@ struct thermal_attr {
  * @lock:	lock to protect thermal_instances list
  * @node:	node in thermal_tz_list (in thermal_core.c)
  * @poll_queue:	delayed work for polling
+ * @indio_dev:	pointer to instance of an IIO dev for this

zone
*/
struct thermal_zone_device {

include/linux/thermal.h +3 -0
 	int id;
@@ -208,6 +218,9 @@ struct thermal_zone_device {
 	struct mutex lock;
 	struct list_head node;
 	struct delayed_work poll_queue;
+#if defined(CONFIG_THERMAL_IIO)
+	struct iio_dev *indio_dev;
+#endif
 };
 
 /**
#7 lore raw

Re: [PATCH 3/6] thermal: iio device for thermal sensor

Quoted 126 lines · Srinivas Pandruvada
Quoted 69 lines · Eduardo Valentin
Quoted 67 lines · Srinivas Pandruvada

This change registers temperature sensor in a thermal zone as an IIO
temperature device. This allows user space thermal application to fully
utilize IIO capability to read temperature events and samples. The primary motivation for this change to improve performance for user
space thermal controllers like Linux thermal daemon or Intel® Dynamic
Platform and Thermal Framework (DPTF) for Chromium OS. The current sysfs has several limitations, which forces the current
user space to use polling as the safe way. This polling causes performance
bottlenecks as some devices requires extremely aggressive response
to
changing temperature conditions.
These are some limitations, which this change tries to address:
- Temperature reading via sysfs attribute, which needs conversion from
string to int
- No way to set threshold settings to avoid polling for temperature
change
without using a RW passive trip point.
- Event notifications via slow kobject uevents, which needs parsing
to id
the zone and read temperature
- Only pull interface from user space, kernel drivers can't send samples
- One sample at a time read from user space
These limitations can be addressed by registering temperature sensor
as an IIO device. IIO provides
- buffered access via /dev/iio:deviceX node with select/poll capability
- temperature thresholds setting via iio event thresholds
- Wait and receive events
- Able to use different external trigger (data ready indications) and push
samples to buffers

Next set of patches uses the IIO binding introduced in this patchset.
The iio device created during call to thermal_zone_device_register.
Samples
are pushed to iio buffers when thermal_zone_device_update is called
from
client drivers.

New thermal zone device callbacks:
It introduces three new callbacks for thermal client drivers:
get_threshold_temp: Get the current threshold temperature
set_threshold_temp: Set the current threshold temperature check_notification_support: Inform that the client driver has asynchronous
notification mechanism. If it is then polling can be avoided for the
temperature sensor.

Signed-off-by: Srinivas Pandruvada <
[email protected]>
---

[Cut]

Quoted 18 lines
Quoted 13 lines
(quoted diff) +1 -0
+	int (*get_threshold_temp)(struct thermal_zone_device *,
Quoted 1 line
Quoted 1 line

int,

(quoted diff) +2 -0
+				  int *);
+	int (*set_threshold_temp)(struct thermal_zone_device *,

int,

(quoted diff) +2 -0
+				  int);
+	int (*set_notification_status)(struct

thermal_zone_device
*,

(quoted diff) +2 -0
+				       bool status);
+	bool (*check_notification_support)(struct
Quoted 1 line

thermal_zone_device *);

Can an existing thermal sensor benefit of the new functionality? If yes,

Yes. The existing driver will be using the trips instead of thresholds.
As we discussed during LPC,
trips (pasive/active/..): These are temperature points to which a cooling action can be performed by binding a cooling device. The existing drivers who support RW trips, user space still can update those and get sample notifications. Existing drivers either have polling or interrupt suport to evaluate trips and call thermal_zone_device_update(), which will result in sample pushed to user space.

Thresholds: These are sensor temperature points which user space wants
to get notification of temperature change. They will not be presented as passive/active/hot trips. User space can decide what to do with the
notifications. If existing drivers don't want to present as fake trips,
then they need to add the callback support.

Quoted 14 lines

The problem I have with the above is the fact that they add callbacks
and behavior eligible only for a subset of thermal zones.

If we are moving to IIO integrated mode, the existing drivers need to
be
converted, to avoid diversification of behavior. For example, there is
existing notification callbacks. Also, hot trip points are described
as
notification points.

Quoted 8 lines
Quoted 4 lines

I believe the patch set needs to document also the next steps on this
move.

If we decide to merge this functionality, I will push a documentation and IIO sample program to use these features.

The seris is old enough, I forgot. The patch adds documentation [PATCH 5/6] thermal: iio Documentation
There is always scope to add more.

Thanks,
Srinivas

Quoted 34 lines

Thanks,
Srinivas

Quoted 31 lines
Quoted 31 lines
include/linux/thermal.h +3 -0
 	int (*notify) (struct thermal_zone_device *, int,
 		       enum thermal_trip_type);
 };
@@ -148,6 +155,8 @@ struct thermal_attr {
 	char name[THERMAL_NAME_LENGTH];
 };
 
+struct iio_dev;
+
 /**
  * struct thermal_zone_device - structure for a thermal zone
  * @id:		unique id number for each thermal zone
@@ -182,6 +191,7 @@ struct thermal_attr {
  * @lock:	lock to protect thermal_instances list
  * @node:	node in thermal_tz_list (in thermal_core.c)
  * @poll_queue:	delayed work for polling
+ * @indio_dev:	pointer to instance of an IIO dev for this

zone
*/
struct thermal_zone_device {

include/linux/thermal.h +3 -0
 	int id;
@@ -208,6 +218,9 @@ struct thermal_zone_device {
 	struct mutex lock;
 	struct list_head node;
 	struct delayed_work poll_queue;
+#if defined(CONFIG_THERMAL_IIO)
+	struct iio_dev *indio_dev;
+#endif
 };