Skip to contentSkip to conversation

thermal: Add msm tsens thermal sensor driver

PATCH Superseded Narendran Rajan 7 participants 21 messages Jan 27, 2015 – Jan 31, 2015

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal: Add msm tsens thermal sensor driver

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>
---
 drivers/thermal/Kconfig         |  14 +
 drivers/thermal/Makefile        |   1 +
 drivers/thermal/msm8960_tsens.c | 841 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 856 insertions(+)
 create mode 100644 drivers/thermal/msm8960_tsens.c
drivers/thermal/Kconfig +14 -0
index 20915ca..a4cb2c0 100644
@@ -261,6 +261,20 @@ config INTEL_SOC_DTS_THERMAL
 	  notification methods.The other trip is a critical trip point, which
 	  was set by the driver based on the TJ MAX temperature.
 
+config THERMAL_TSENS8960
+	tristate "Qualcomm 8960 Tsens Temperature driver"
+	depends on THERMAL
+	depends on ARCH_QCOM
+	help
+	  QCOM tsens thermal driver provides support for Temperature sensor
+	  (TSENS) found on QCOM SoCs. It supports four configurable trip points
+	  and controls multiple sensors on the SOC. The four trip points are
+	  common across all sensors present in the SoC. The number of sensors
+	  present vary from chip to chip and are set through device tree entry.
+	  The driver presents as a standard thermal zone device with configurable
+	  trip points and cooling device mapping through standard thermal zone
+	  device tree
+
 menu "ACPI INT340X thermal drivers"
 source drivers/thermal/int340x_thermal/Kconfig
 endmenu
drivers/thermal/Makefile +1 -0
index fa0dc48..23c7a34 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_TI_SOC_THERMAL)	+= ti-soc-thermal/
 obj-$(CONFIG_INT340X_THERMAL)  += int340x_thermal/
 obj-$(CONFIG_ST_THERMAL)	+= st/
 obj-$(CONFIG_TEGRA_SOCTHERM)	+= tegra_soctherm.o
+obj-$(CONFIG_THERMAL_TSENS8960)	+= msm8960_tsens.o
drivers/thermal/msm8960_tsens.c new file +841 -0

841 lines not on this page · raw message

Signature · 1 line
Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
#1 lore raw

Thankyou for sending the patch..
here are some comments

On 27/01/15 04:09, Narendran Rajan wrote:
...

Quoted 4 lines
(quoted diff) +4 -0
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>

Review

Quoted 10 lines
(quoted diff) +10 -0
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/of_address.h>

??

Quoted 1 line

+#include <linux/of_platform.h>

???

Review

Quoted 11 lines
(quoted diff) +11 -0
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */
+enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};

Do you need this if trips are actually coming from DT?

Quoted 40 lines
(quoted diff) +40 -0
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c

Review

Quoted 10 lines
(quoted diff) +10 -0
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc

?? Why is this offset defined in the driver, Is'nt this supposed to come via dt?

Quoted 20 lines
(quoted diff) +20 -0
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 << TSENS_8660_CONFIG_SHIFT)
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;

Hmm.. I think you should remove this global variable and find a better way to get hold of this.

Quoted 82 lines
(quoted diff) +82 -0
+
+/* Temperature on y axis and ADC-code on x-axis */
+static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s)
+{
+	return adc_code * s->slope + s->offset;
+}
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map, TSENS_INT_STATUS_ADDR, &trdy) &&
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg | TSENS_SW_RST);
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA | TSENS_EN;
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA | TSENS_EN;
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled. Disabling subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS

Should this be a kernel config?
Or by default this should'nt be On?
Do you need this code if the trip values are actually coming from DT?

Quoted 301 lines
(quoted diff) +301 -0
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor)
+{
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp);
+}
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct tsens_sensor *s)
+{
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE, TSENS_THRESHOLD_MAX_CODE);
+}
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >> TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >> TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp, tm_sensor);
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR, mask, code);
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high)
+{
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work)
+{
+	struct tsens_device *tmdev;
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR);
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status, &code);
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev->sensor[i].tz_dev);
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR | TSENS_LOWER_STATUS_CLR, mask);
+}
+
+static irqreturn_t tsens_isr(int irq, void *data)
+{
+	schedule_work(data);
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int tsens_suspend(struct device *dev)
+{
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev->pm_tsens_thr_data);
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+
+	return 0;
+}
+
+static int tsens_resume(struct device *dev)
+{
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST, TSENS_SW_RST);
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev->pm_tsens_thr_data);
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev)
+{
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+}
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+static void tsens_hw_init(struct tsens_device *tmdev)
+{
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR, TSENS_SW_RST, reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);

Can these magic shift values go some where in defines.?

Quoted 29 lines
(quoted diff) +29 -0
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map, TSENS_8960_CONFIG_ADDR,
+				   TSENS_8960_CONFIG_MASK, TSENS_8960_CONFIG);
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG << TSENS_8660_CONFIG_SHIFT;
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) << TSENS_SENSOR0_SHIFT;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH << TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |
+		(TSENS_UPPER_LIMIT_TH << TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |
+		(TSENS_MIN_LIMIT_TH << TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |
+		(TSENS_MAX_LIMIT_TH << TSENS_THRESHOLD_MAX_LIMIT_SHIFT);
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr);
+}
+

...

Quoted 7 lines
(quoted diff) +7 -0
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap *map)
+{
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];
+	u8 *byte_data;
+	u32 fuse, redun, num_read;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes seprately
+	*/
+

It looks like workaround for the problem in syscon driver. Please use this patch to fix it.

--------------------->cut<----------------------
From c50ea9c1ac61d73b1edcbc5469614cdc6655a38c Mon Sep 17 00:00:00 2001
From: Srinivas Kandagatla <[email protected]>
Date: Mon, 26 Jan 2015 07:30:25 +0000
Subject: [RFC PATCH 1/2] WIP: mfd: syscon: Add register stride to DT bindings.

This patch adds register stride to dt bindings so that the consumers of the syscon could change it to there need. One of the the use case for this feature is Qualcomm qfprom which needs a byte access to regmap returned from syscon.

---
 Documentation/devicetree/bindings/mfd/syscon.txt | 3 +++
 drivers/mfd/syscon.c                             | 9 +++++++++
 2 files changed, 12 insertions(+)
Documentation/devicetree/bindings/mfd/syscon.txt +3 -0
index fe8150b..7f06ec1 100644
@@ -13,6 +13,9 @@ Required properties:
 - compatible: Should contain "syscon".
 - reg: the register region can be accessed from syscon
 
+Optional properties:
+- stride : register address stride in bytes.
+
 Examples:
 gpr: iomuxc-gpr@020e0000 {
 	compatible = "fsl,imx6q-iomuxc-gpr", "syscon";
drivers/mfd/syscon.c +9 -0
index 176bf0f..98769d5 100644
@@ -48,6 +48,7 @@ static struct syscon *of_syscon_register(struct device_node *np)
 	struct regmap *regmap;
 	void __iomem *base;
 	int ret;
+	u32 stride;
 	struct regmap_config syscon_config = syscon_regmap_config;
 
 	if (!of_device_is_compatible(np, "syscon"))
@@ -69,6 +70,14 @@ static struct syscon *of_syscon_register(struct device_node *np)
 	 else if (of_property_read_bool(np, "little-endian"))
 		syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
 
+	if (!of_property_read_u32(np, "stride", &stride)) {
+		if (stride > 4)
+			stride = 4;
+
+		syscon_config.reg_stride = stride;
+		syscon_config.val_bits = 8 * stride;
+	}
+
 	regmap = regmap_init_mmio(NULL, base, &syscon_config);
 	if (IS_ERR(regmap)) {
 		pr_err("regmap init failed\n");
Signature · 3 lines
1.9.1

--------------------->cut<----------------------
Quoted 32 lines
(quoted diff) +32 -0
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops = {
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i)
+{
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;

This magic values should be going in to some descriptive constants..

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);

Hmm where is the name actually used??

Review

Quoted 9 lines
(quoted diff) +9 -0
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;

So does that mean that driver would not register tsens unless there is a DT node for it?
For apq8064 which has 11 sensors, its becomes necessary to add all 11 sensors in DT with trip points and cooling devices. Which IMO is not right. You could probably do something like:

	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
					&tsens_thermal_of_ops);

	if (IS_ERR(s->tz_dev)) {
		s->tz_dev = thermal_zone_device_register( ... )
		if (IS_ERR(s->tz_dev))
			return -ENODEV;
	}

same thing in faliure/remove case ..

Quoted 59 lines
(quoted diff) +59 -0
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {

Review

Quoted 10 lines
(quoted diff) +10 -0
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np, "qcom,imem");
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+

Please see my comments in the bindings patch.

Quoted 66 lines
(quoted diff) +66 -0
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map, *field);
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0], THERMAL_DEVICE_ENABLED);
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled", i);
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);

Review

Quoted 10 lines
(quoted diff) +10 -0
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr, IRQF_TRIGGER_RISING,
+			       "tsens", &tmdev->tsens_work);

Unless I missing something,...Would it actually get an interrupt without setting the proper trip values in the hw? For example in this driver we only register from DT.. and the values from Dt never ends up writing into the hw registers.. so this would never trigger an interrupt from trip points mentioned in DT. Unless the default reg values makes sense.. still its out of sync from DT trip values.

Does the interrupt actually work? Last time when I tested it on 3.4 It never did work?..

Quoted 21 lines
(quoted diff) +21 -0
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s->tz_dev);
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev)
+{
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},

should be "qcom,msm8960-tsens"

Review

Quoted 12 lines
(quoted diff) +12 -0
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,

remove the owner as it would be set anyway byt the core.

Review

Quoted 3 lines
(quoted diff) +3 -0
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif

Please use SIMPLE_DEV_PM_OPS(...)

Review

Quoted 8 lines
(quoted diff) +7 -0
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");

--srini

#2 lore raw
Quoted 11 lines

-----Original Message-----
From: Srinivas Kandagatla [mailto:[email protected]]
Sent: Monday, January 26, 2015 11:15 PM
To: Narendran Rajan; Zhang Rui; Eduardo Valentin
Cc: Linux ARM MSM; Linux PM; Siddartha Mohanadoss; Stephen Boyd
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Thankyou for sending the patch..
here are some comments

Thanks Srini for the detail review.

Quoted 18 lines · Narendran Rajan

...

Quoted 4 lines
(quoted diff) +4 -0
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>

Review

Quoted 10 lines
(quoted diff) +10 -0
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/of_address.h>

??

Thx for the catch. Not needed, will remove.

Quoted 3 lines
Quoted 2 lines

+#include <linux/of_platform.h>

???

Required for of_find_device_by_node. If this becomes a subnode of gcc node, yes this will be removed.

Quoted 13 lines

Review

Quoted 11 lines
(quoted diff) +10 -0
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */ enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};

Do you need this if trips are actually coming from DT?

Yes, for HW driven trip points. This feature still under development in the core framework.
Please see https://patchwork.ozlabs.org/patch/364812/

I should have added this under THERMAL_TSENS8960_HWTRIPS as rest of the code. Thx

Quoted 43 lines
Quoted 43 lines
(quoted diff) +43 -0
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc

?? Why is this offset defined in the driver, Is'nt this supposed to come

via dt?

Quoted 24 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 <<

TSENS_8660_CONFIG_SHIFT)

Quoted 20 lines
(quoted diff) +20 -0
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;

Hmm.. I think you should remove this global variable and find a better way

to

Quoted 2 lines

get hold of this.

Didn't find anything simple enough. A few other drivers seems to use global as well. Will look around. If you have some quick tips let me please know.

Quoted 90 lines
Quoted 7 lines
(quoted diff) +7 -0
+
+/* Temperature on y axis and ADC-code on x-axis */ static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s) {
+	return adc_code * s->slope + s->offset; }
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map,

TSENS_INT_STATUS_ADDR, &trdy) &&

Quoted 29 lines
(quoted diff) +29 -0
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode) {
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg |

TSENS_SW_RST);

Review

Quoted 2 lines
(quoted diff) +2 -0
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA |

TSENS_EN;

Review

Quoted 2 lines
(quoted diff) +2 -0
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA |

TSENS_EN;

Review

Quoted 5 lines
(quoted diff) +5 -0
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled.

Disabling

Quoted 20 lines
(quoted diff) +20 -0
+subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
Quoted 9 lines

drivers/mfd/syscon.c static struct syscon *of_syscon_register(struct device_node *np)

Quoted 4 lines
drivers/mfd/syscon.c +4 -0
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS

Should this be a kernel config?
Or by default this should'nt be On?
Do you need this code if the trip values are actually coming from DT?

This feature need thermal core framework change as well.
This is not yet landed in upstream, but is used in some downstream Kernels (like chrome kernel). Hence under #ifdef and disabled by default.

Quoted 312 lines
Quoted 46 lines
(quoted diff) +46 -0
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor) {
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp); }
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct
+tsens_sensor *s) {
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);

Review

Quoted 10 lines
(quoted diff) +10 -0
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE,
+TSENS_THRESHOLD_MAX_CODE); }
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

Review

Quoted 6 lines
(quoted diff) +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

Review

Quoted 6 lines
(quoted diff) +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_MAX_LIMIT_SHIFT;

Quoted 8 lines
(quoted diff) +8 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE; }
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

Review

Quoted 6 lines
(quoted diff) +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

Review

Quoted 6 lines
(quoted diff) +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_MIN_LIMIT_SHIFT;

Quoted 10 lines
(quoted diff) +10 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE; }

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp,

tm_sensor);

Quoted 22 lines
(quoted diff) +22 -0
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR,

mask, code);

Quoted 21 lines
(quoted diff) +21 -0
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high) {
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work) {
+	struct tsens_device *tmdev;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR,

Review

Quoted 1 line
(quoted diff) +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR);

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

Review

Quoted 9 lines
(quoted diff) +9 -0
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status,

&code);

Quoted 15 lines
(quoted diff) +14 -0
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev-

sensor[i].tz_dev);

Review

Quoted 6 lines
(quoted diff) +6 -0
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR |

TSENS_LOWER_STATUS_CLR, mask); }

Quoted 22 lines
(quoted diff) +13 -0
+
+static irqreturn_t tsens_isr(int irq, void *data) {
+	schedule_work(data);
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int tsens_suspend(struct device *dev) {
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev-

pm_tsens_thr_data);

(quoted diff) +8 -0
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+	return 0;
+}
+
+static int tsens_resume(struct device *dev) {
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST,

TSENS_SW_RST);

Review

Quoted 2 lines
(quoted diff) +2 -0
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

Review

Quoted 1 line
(quoted diff) +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

Quoted 35 lines
(quoted diff) +7 -0
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev-

pm_tsens_thr_data);

(quoted diff) +27 -0
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev) {
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)

Review

Quoted 10 lines
(quoted diff) +10 -0
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl); }
+
+static void tsens_hw_init(struct tsens_device *tmdev) {
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR,

TSENS_SW_RST,

Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);

Can these magic shift values go some where in defines.?

Thx, will address in next version.

Quoted 58 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map,

TSENS_8960_CONFIG_ADDR,

Review

Quoted 1 line
(quoted diff) +1 -0
+				   TSENS_8960_CONFIG_MASK,

TSENS_8960_CONFIG);

Review

Quoted 5 lines
(quoted diff) +5 -0
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG <<

TSENS_8660_CONFIG_SHIFT;

drivers/mfd/syscon.c static struct syscon *of_syscon_register(struct device_node *np)

Quoted 3 lines
drivers/mfd/syscon.c +3 -0
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) <<

TSENS_SENSOR0_SHIFT;

Review

Quoted 4 lines
(quoted diff) +4 -0
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

Review

Quoted 1 line
(quoted diff) +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

Review

Quoted 1 line
(quoted diff) +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

Review

Quoted 1 line
(quoted diff) +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH <<

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |

Review

Quoted 1 line
(quoted diff) +1 -0
+		(TSENS_UPPER_LIMIT_TH <<

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |

Review

Quoted 1 line
(quoted diff) +1 -0
+		(TSENS_MIN_LIMIT_TH <<

TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |

Review

Quoted 1 line
(quoted diff) +1 -0
+		(TSENS_MAX_LIMIT_TH <<

TSENS_THRESHOLD_MAX_LIMIT_SHIFT);

Quoted 2 lines
(quoted diff) +2 -0
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr); }
+

...

Quoted 5 lines
(quoted diff) +5 -0
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap
+*map) {
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];

Review

Quoted 10 lines
(quoted diff) +10 -0
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes

seprately

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	*/
+

It looks like workaround for the problem in syscon driver. Please use this patch to fix it.

Unfortunately, this won't work. The same imem region as read as 32-bit for OPP table
chip characterization. Looks like this workaround needs to stay Please see the patch below for reference
https://chromium-review.googlesource.com/#/c/231792/6

Quoted 9 lines

--------------------->cut<----------------------
From c50ea9c1ac61d73b1edcbc5469614cdc6655a38c Mon Sep 17 00:00:00
2001
From: Srinivas Kandagatla <[email protected]>
Date: Mon, 26 Jan 2015 07:30:25 +0000
Subject: [RFC PATCH 1/2] WIP: mfd: syscon: Add register stride to DT bindings.

This patch adds register stride to dt bindings so that the consumers of

the

Quoted 1 line

syscon could change it to there need. One of the the use case for this

feature

Quoted 93 lines

is Qualcomm qfprom which needs a byte access to regmap returned from syscon.

---
  Documentation/devicetree/bindings/mfd/syscon.txt | 3 +++
  drivers/mfd/syscon.c                             | 9 +++++++++
  2 files changed, 12 insertions(+)
Documentation/devicetree/bindings/mfd/syscon.txt +3 -0
index fe8150b..7f06ec1 100644
@@ -13,6 +13,9 @@ Required properties:
  - compatible: Should contain "syscon".
  - reg: the register region can be accessed from syscon
 
+Optional properties:
+- stride : register address stride in bytes.
+
  Examples:
  gpr: iomuxc-gpr@020e0000 {
  	compatible = "fsl,imx6q-iomuxc-gpr", "syscon"; diff --git

a/drivers/mfd/syscon.c b/drivers/mfd/syscon.c index 176bf0f..98769d5 100644

drivers/mfd/syscon.c +1 -0
@@ -48,6 +48,7 @@ static struct syscon *of_syscon_register(struct
 device_node *np)
  	struct regmap *regmap;
  	void __iomem *base;
  	int ret;
+	u32 stride;
  	struct regmap_config syscon_config = syscon_regmap_config;
 

if (!of_device_is_compatible(np, "syscon")) @@ -69,6 +70,14 @@
static struct syscon *of_syscon_register(struct device_node *np)
else if (of_property_read_bool(np, "little-endian"))
syscon_config.val_format_endian =
REGMAP_ENDIAN_LITTLE;

drivers/mfd/syscon.c +8 -1
+	if (!of_property_read_u32(np, "stride", &stride)) {
+		if (stride > 4)
+			stride = 4;
+
+		syscon_config.reg_stride = stride;
+		syscon_config.val_bits = 8 * stride;
+	}
+
  	regmap = regmap_init_mmio(NULL, base, &syscon_config);
  	if (IS_ERR(regmap)) {
  		pr_err("regmap init failed\n");
--

1.9.1

--------------------->cut<----------------------

Quoted 14 lines
(quoted diff) +14 -0
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops =

{

Quoted 11 lines
(quoted diff) +11 -0
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i) {
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;

This magic values should be going in to some descriptive constants..

There is descriptive comment just above and this is not used anywhere else. Perhaps this is easier to read?

Quoted 5 lines

drivers/mfd/syscon.c static struct syscon *of_syscon_register(struct device_node *np)

Quoted 4 lines
drivers/mfd/syscon.c +4 -0
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);

Hmm where is the name actually used??

Oops, not needed in the of based registration. Will remove

Quoted 11 lines

Review

Quoted 10 lines
(quoted diff) +9 -0
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;

So does that mean that driver would not register tsens unless there is a

DT

Quoted 3 lines

node for it?
For apq8064 which has 11 sensors, its becomes necessary to add all 11 sensors in DT with trip points and cooling devices. Which IMO is not

right.

Quoted 11 lines

You could probably do something like:

	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
					&tsens_thermal_of_ops);

	if (IS_ERR(s->tz_dev)) {
		s->tz_dev = thermal_zone_device_register( ... )
		if (IS_ERR(s->tz_dev))
			return -ENODEV;
	}
Quoted 2 lines

same thing in faliure/remove case ..

Not really. If you look at further down where tsens_register is called (around ln 752), only primary register
Is mandatory. If other sensor registration fails, the code just disables those sensors. Code snipped pasted below --------------------->cut<----------------------

	/*
	 * Register sensor 0 separately. This sensor is always
	 * expected to be present and if this fails, thermal
	 * sensor probe would fail
	 * Other sensors are optional and if registration fails
	 * disable the sensor and continue
	*/
	ret = tsens_register(tmdev, 0);
	if (ret < 0) {
		dev_err(tsens_dev, "Registering failed for primary sensor");
		ret = -ENODEV;
		goto fail;
	} else {
		tsens_tz_set_mode(&tmdev->sensor[0],
THERMAL_DEVICE_ENABLED);
	}

--------------------->cut<----------------------

Quoted 64 lines
Quoted 53 lines
(quoted diff) +53 -0
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev) {
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np,

"qcom,imem");

Quoted 6 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+

Please see my comments in the bindings patch.

As mentioned above, the syscon needs to be 32-bit for other calibration data, so
I guess no option.

On base_regmap, I am good with either ways. Will
wait for your further comments and update accordingly in next revision.

Quoted 59 lines
Quoted 26 lines
(quoted diff) +25 -0
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map,

*field);

Quoted 11 lines
(quoted diff) +11 -0
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail

Review

Quoted 10 lines
(quoted diff) +10 -0
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0],

THERMAL_DEVICE_ENABLED);

Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled",

i);

Quoted 12 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr,

IRQF_TRIGGER_RISING,

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+			       "tsens", &tmdev->tsens_work);

Unless I missing something,...Would it actually get an interrupt without setting the proper trip values in the hw? For example in this driver we

only

Quoted 1 line

register from DT.. and the values from Dt never ends up writing into the

hw

Quoted 4 lines

registers.. so this would never trigger an interrupt from trip points mentioned in DT. Unless the default reg values makes sense.. still its out of sync from DT trip values.

Correct, in polling mode (which is what exists in thermal framework today), HW interrupt
do not make sense as the trip points are set to default and never updated based on Dt values.

But the code under the #ifdef THERMAL_TSENS8960_HWTRIPS supports the HW trip point mode.
This code needs the additional patch
Please see https://patchwork.ozlabs.org/patch/364812/

May be I will remove everything under HWTRIPs until it lands in the core thermal framework?

Quoted 1 line

Does the interrupt actually work? Last time when I tested it on 3.4 It

never

Quoted 25 lines

did work?..

Quoted 24 lines
(quoted diff) +11 -0
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s-

tz_dev);

(quoted diff) +12 -0
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev) {
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},

should be "qcom,msm8960-tsens"

Thx, will address in next version.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,

remove the owner as it would be set anyway byt the core.

Thx, will address in next version.

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif

Please use SIMPLE_DEV_PM_OPS(...)

Thx, will address in next version.

Quoted 9 lines

Review

Quoted 8 lines
(quoted diff) +7 -0
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");

--srini

#3 lore raw
Quoted 28 lines · Narendran Rajan
Quoted 20 lines

From: Srinivas Kandagatla [mailto:[email protected]]

Quoted 19 lines
(quoted diff) +19 -0
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;

Hmm.. I think you should remove this global variable and find a better way

Quoted 5 lines

to

Quoted 2 lines

get hold of this.

Didn't find anything simple enough. A few other drivers seems to use global as well. Will look around. If you have some quick tips let me please know.

Why do we even need those dev_dbg() printks? I'd rather see that debugging stuff get removed and this static singleton removed at the same time.

Quoted 6 lines
Quoted 1 line

Correct, in polling mode (which is what exists in thermal framework today), HW interrupt
do not make sense as the trip points are set to default and never updated based on Dt values.

Quoted 8 lines

But the code under the #ifdef THERMAL_TSENS8960_HWTRIPS supports the HW trip point mode.
This code needs the additional patch
Please see https://patchwork.ozlabs.org/patch/364812/

May be I will remove everything under HWTRIPs until it lands in the core thermal framework?

That patch is half a year old. Is aynyone still working on it? Perhaps you can pick it up and try to get it into a workable state and then port this new driver to it?

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#4 lore raw
Quoted 56 lines · Stephen Boyd
Quoted 28 lines · Narendran Rajan
Quoted 20 lines

From: Srinivas Kandagatla [mailto:[email protected]]

Quoted 19 lines
(quoted diff) +19 -0
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;

Hmm.. I think you should remove this global variable and find a better way

Quoted 5 lines

to

Quoted 2 lines

get hold of this.

Didn't find anything simple enough. A few other drivers seems to use global as well. Will look around. If you have some quick tips let me please know.

Why do we even need those dev_dbg() printks? I'd rather see that debugging stuff get removed and this static singleton removed at the same time.

Quoted 6 lines
Quoted 1 line

Correct, in polling mode (which is what exists in thermal framework today), HW interrupt
do not make sense as the trip points are set to default and never updated based on Dt values.

Quoted 10 lines

But the code under the #ifdef THERMAL_TSENS8960_HWTRIPS supports the HW trip point mode.
This code needs the additional patch
Please see https://patchwork.ozlabs.org/patch/364812/

May be I will remove everything under HWTRIPs until it lands in the core thermal framework?

Quoted 3 lines

That patch is half a year old. Is aynyone still working on it? Perhaps you can pick it up and try to get it into a workable state and then port this new driver to it?

This is preferable. The original proposal was not taken further because it added functionality that would be only in of thermal. And my concern is that of thermal must be compliant with thermal core, and not a competing API. As long as HW trips can also be used by drivers that do not use of thermal, I am more than happy to help to merge a code in that front.

Besides, other drivers would benefit too.

Quoted 4 lines
Signature · 3 lines
-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#5 lore raw
In reply to 'Stephen Boyd'
Quoted 5 lines

-----Original Message-----
From: 'Stephen Boyd' [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 3:53 PM
To: Narendran Rajan
Cc: 'Srinivas Kandagatla'; 'Narendran Rajan'; 'Zhang Rui'; 'Eduardo

Valentin';

Quoted 38 lines

'Linux ARM MSM'; 'Linux PM'; 'Siddartha Mohanadoss'
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Quoted 34 lines · Narendran Rajan
Quoted 21 lines

From: Srinivas Kandagatla [mailto:[email protected]]

Quoted 20 lines
(quoted diff) +20 -0
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;

Hmm.. I think you should remove this global variable and find a better way

to

Quoted 2 lines

get hold of this.

Didn't find anything simple enough. A few other drivers seems to use global as well. Will look around. If you have some quick tips let me

please

Quoted 6 lines

know.

Why do we even need those dev_dbg() printks? I'd rather see that debugging stuff get removed and this static singleton removed at the same time.

Agree, it was more convenience and unwanted singleton. Will change.

Quoted 11 lines
Quoted 6 lines
Quoted 1 line

Correct, in polling mode (which is what exists in thermal framework today), HW interrupt do not make sense as the trip points are set to default and never updated based on Dt values.

But the code under the #ifdef THERMAL_TSENS8960_HWTRIPS supports

the

Quoted 4 lines

HW trip point mode.
This code needs the additional patch
Please see https://patchwork.ozlabs.org/patch/364812/

Quoted 6 lines
Quoted 3 lines

May be I will remove everything under HWTRIPs until it lands in the core thermal framework?

That patch is half a year old. Is aynyone still working on it? Perhaps you can pick it up and try to get it into a workable state and

then

Quoted 2 lines

port this new driver to it?

Let me remove the dependency on this patch for now, have a simple polling mode only
Patch.
Will think through a more generic patch post this.

Quoted 3 lines

--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

#6 lore raw
In reply to Narendran Rajan
Quoted 17 lines · Narendran Rajan
Quoted 11 lines

-----Original Message-----
From: Srinivas Kandagatla [mailto:[email protected]]
Sent: Monday, January 26, 2015 11:15 PM
To: Narendran Rajan; Zhang Rui; Eduardo Valentin
Cc: Linux ARM MSM; Linux PM; Siddartha Mohanadoss; Stephen Boyd
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Thankyou for sending the patch..
here are some comments

Thanks Srini for the detail review.

Quoted 3 lines · Narendran Rajan

...

...

Quoted 72 lines
Quoted 13 lines

Review

Quoted 11 lines
(quoted diff) +10 -0
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */ enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};

Do you need this if trips are actually coming from DT?

Yes, for HW driven trip points. This feature still under development in the core framework.
Please see https://patchwork.ozlabs.org/patch/364812/

I should have added this under THERMAL_TSENS8960_HWTRIPS as rest of the code. Thx

Quoted 43 lines
Quoted 43 lines
(quoted diff) +43 -0
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc

?? Why is this offset defined in the driver, Is'nt this supposed to come

Quoted 1 line

via dt?

???

Quoted 32 lines
Quoted 24 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 <<

TSENS_8660_CONFIG_SHIFT)

Quoted 20 lines
(quoted diff) +20 -0
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;

Hmm.. I think you should remove this global variable and find a better way

Quoted 5 lines

to

Quoted 2 lines

get hold of this.

Didn't find anything simple enough. A few other drivers seems to use global as well. Will look around. If you have some quick tips let me please know.

Add this to struct tsens_device.

Quoted 1 line

...

Quoted 11 lines
Quoted 6 lines
Quoted 1 line

+#ifdef THERMAL_TSENS8960_HWTRIPS

Should this be a kernel config?
Or by default this should'nt be On?
Do you need this code if the trip values are actually coming from DT?

This feature need thermal core framework change as well.
This is not yet landed in upstream, but is used in some downstream Kernels (like chrome kernel). Hence under #ifdef and disabled by default.

Am not sure if this is right thing to do it now, the email thread you pointed to be last updated in 1st Aug 2014 .

Quoted 22 lines
Quoted 17 lines
Quoted 6 lines
(quoted diff) +5 -0
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap
+*map) {
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];

Review

Quoted 10 lines
(quoted diff) +10 -0
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes

seprately

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	*/
+

It looks like workaround for the problem in syscon driver. Please use this patch to fix it.

Quoted 5 lines

Unfortunately, this won't work. The same imem region as read as 32-bit for OPP table
chip characterization. Looks like this workaround needs to stay Please see the patch below for reference
https://chromium-review.googlesource.com/#/c/231792/6

Does'nt matter If we can read a byte we can also read a word, use regmap_read_bulk().

Again the big plan is to abstract the qfprom access this into something like http://www.spinics.net/lists/linux-arm-msm/msg13090.html

This will take care of all the code related to qfprom in the drivers. Please have a look and give us some comments so that we know whats best.

...

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8 -0
+
+static int tsens_register(struct tsens_device *tmdev, int i) {
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;

This magic values should be going in to some descriptive constants..

Quoted 2 lines

There is descriptive comment just above and this is not used anywhere else. Perhaps this is easier to read?

This seems to be like an register offset, So I would prefer it to be a nice #define

Quoted 36 lines
Quoted 6 lines

drivers/mfd/syscon.c static struct syscon *of_syscon_register(struct device_node *np)

Quoted 5 lines
drivers/mfd/syscon.c +4 -0
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);

Hmm where is the name actually used??

Oops, not needed in the of based registration. Will remove

Quoted 11 lines

Review

Quoted 10 lines
(quoted diff) +9 -0
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;

So does that mean that driver would not register tsens unless there is a

DT

Quoted 3 lines

node for it?
For apq8064 which has 11 sensors, its becomes necessary to add all 11 sensors in DT with trip points and cooling devices. Which IMO is not

right.

Quoted 11 lines

You could probably do something like:

	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
					&tsens_thermal_of_ops);

	if (IS_ERR(s->tz_dev)) {
		s->tz_dev = thermal_zone_device_register( ... )
		if (IS_ERR(s->tz_dev))
			return -ENODEV;
	}
Quoted 7 lines
Quoted 2 lines

same thing in faliure/remove case ..

Not really. If you look at further down where tsens_register is called (around ln 752), only primary register
Is mandatory. If other sensor registration fails, the code just disables those sensors. Code snipped pasted below

I think you missed my point.
Ok, I agree sensor 0 is primary one and should always be registered.

What am saying is Lets say we have 11 sensors ex: in APQ8064.

Now we have got dt entries for sensor0 and sensor6.
Your code would only register sensor0 and sensor6, because you only considered DT nodes.
This is not correct, as we are not short of any resources to populate the other 9 sensors. You should still register all the 11 sensors but 2 of them are comming from dt with there own trip point values and the rest of them are registered as regular non dt thermal sensors.

Doing this way would give the user to see all the sensors via sysfs If not you are loosing the real value of all the 9 sensors in the system.

have a look at other drivers like TI you will understand what Am saying.

Quoted 90 lines

--------------------->cut<----------------------

	/*
	 * Register sensor 0 separately. This sensor is always
	 * expected to be present and if this fails, thermal
	 * sensor probe would fail
	 * Other sensors are optional and if registration fails
	 * disable the sensor and continue
	*/
	ret = tsens_register(tmdev, 0);
	if (ret < 0) {
		dev_err(tsens_dev, "Registering failed for primary sensor");
		ret = -ENODEV;
		goto fail;
	} else {
		tsens_tz_set_mode(&tmdev->sensor[0],
THERMAL_DEVICE_ENABLED);
	}

--------------------->cut<----------------------

Quoted 64 lines
Quoted 53 lines
(quoted diff) +53 -0
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev) {
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np,

"qcom,imem");

Quoted 6 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+

Please see my comments in the bindings patch.

Quoted 6 lines

As mentioned above, the syscon needs to be 32-bit for other calibration data, so
I guess no option.

On base_regmap, I am good with either ways. Will
wait for your further comments and update accordingly in next revision.

have a look at http://www.spinics.net/lists/linux-arm-msm/msg13090.html

Quoted 101 lines
Quoted 60 lines
Quoted 27 lines
(quoted diff) +25 -0
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map,

*field);

Quoted 11 lines
(quoted diff) +11 -0
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail

Review

Quoted 10 lines
(quoted diff) +10 -0
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0],

THERMAL_DEVICE_ENABLED);

Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled",

i);

Quoted 12 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr,

IRQF_TRIGGER_RISING,

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+			       "tsens", &tmdev->tsens_work);

Unless I missing something,...Would it actually get an interrupt without setting the proper trip values in the hw? For example in this driver we

only

Quoted 1 line

register from DT.. and the values from Dt never ends up writing into the

hw

Quoted 4 lines

registers.. so this would never trigger an interrupt from trip points mentioned in DT. Unless the default reg values makes sense.. still its out of sync from DT trip values.

Correct, in polling mode (which is what exists in thermal framework today), HW interrupt
do not make sense as the trip points are set to default and never updated based on Dt values.

But the code under the #ifdef THERMAL_TSENS8960_HWTRIPS supports the HW trip point mode.
This code needs the additional patch
Please see https://patchwork.ozlabs.org/patch/364812/

Quoted 6 lines

May be I will remove everything under HWTRIPs until it lands in the core thermal framework?

Quoted 1 line

Does the interrupt actually work? Last time when I tested it on 3.4 It

never

Quoted 1 line

did work?..

???

How do you enable/disable the interrupt in the tses IP, I did not find any register to do that? Do we have one?

Quoted 70 lines
Quoted 24 lines
Quoted 24 lines
(quoted diff) +11 -0
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s-

tz_dev);

(quoted diff) +12 -0
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev) {
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},

should be "qcom,msm8960-tsens"

Thx, will address in next version.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,

remove the owner as it would be set anyway byt the core.

Thx, will address in next version.

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif

Please use SIMPLE_DEV_PM_OPS(...)

Thx, will address in next version.

Quoted 9 lines

Review

Quoted 8 lines
(quoted diff) +7 -0
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");

--srini

#7 lore raw
Quoted 103 lines

-----Original Message-----
From: Srinivas Kandagatla [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 10:06 PM
To: Narendran Rajan; 'Narendran Rajan'; 'Zhang Rui'; 'Eduardo Valentin'
Cc: 'Linux ARM MSM'; 'Linux PM'; 'Siddartha Mohanadoss'; 'Stephen Boyd'
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Quoted 17 lines · Narendran Rajan
Quoted 11 lines

-----Original Message-----
From: Srinivas Kandagatla [mailto:[email protected]]
Sent: Monday, January 26, 2015 11:15 PM
To: Narendran Rajan; Zhang Rui; Eduardo Valentin
Cc: Linux ARM MSM; Linux PM; Siddartha Mohanadoss; Stephen Boyd
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Thankyou for sending the patch..
here are some comments

Thanks Srini for the detail review.

Quoted 3 lines · Narendran Rajan

...

...

Quoted 73 lines
Quoted 13 lines

Review

Quoted 11 lines
(quoted diff) +10 -0
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */ enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};

Do you need this if trips are actually coming from DT?

Yes, for HW driven trip points. This feature still under development in the core framework.
Please see https://patchwork.ozlabs.org/patch/364812/

I should have added this under THERMAL_TSENS8960_HWTRIPS as rest of the code. Thx

Quoted 52 lines
Quoted 40 lines
(quoted diff) +40 -0
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c

Review

Quoted 10 lines
(quoted diff) +10 -0
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc

?? Why is this offset defined in the driver, Is'nt this supposed to come

Quoted 3 lines
Quoted 1 line

via dt?

???

Correct, needs to be removed.

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 <<

TSENS_8660_CONFIG_SHIFT)

Quoted 17 lines
(quoted diff) +17 -0
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;

Hmm.. I think you should remove this global variable and find a better way

Quoted 5 lines
Quoted 5 lines

to

Quoted 2 lines

get hold of this.

Didn't find anything simple enough. A few other drivers seems to use global as well. Will look around. If you have some quick tips let me

please

Quoted 4 lines

know.

Add this to struct tsens_device.

Thx, this would work.

Quoted 5 lines
Quoted 1 line

...

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

+#ifdef THERMAL_TSENS8960_HWTRIPS

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Should this be a kernel config?
Or by default this should'nt be On?
Do you need this code if the trip values are actually coming from DT?

This feature need thermal core framework change as well. This is not yet landed in upstream, but is used in some downstream Kernels (like chrome kernel). Hence under #ifdef and disabled by

default.

Quoted 3 lines
Quoted 1 line

Am not sure if this is right thing to do it now, the email thread you

pointed to

Quoted 1 line

be last updated in 1st Aug 2014 .

Agree, the both Stephen and you have pointed out not to base the code on this work.
Let me redo the patch to remove the dependency on HWTRIPs and have just a polling based
Solution.

Quoted 27 lines
Quoted 22 lines
Quoted 17 lines
Quoted 6 lines
(quoted diff) +5 -0
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap
+*map) {
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];

Review

Quoted 10 lines
(quoted diff) +10 -0
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes

seprately

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	*/
+

It looks like workaround for the problem in syscon driver. Please use this patch to fix it.

Quoted 4 lines

Unfortunately, this won't work. The same imem region as read as 32-bit for OPP table chip characterization. Looks like this workaround needs to stay Please see the patch below for reference
https://chromium-review.googlesource.com/#/c/231792/6

Quoted 4 lines

Does'nt matter If we can read a byte we can also read a word, use regmap_read_bulk().

Again the big plan is to abstract the qfprom access this into something

like

Quoted 6 lines

http://www.spinics.net/lists/linux-arm-msm/msg13090.html

This will take care of all the code related to qfprom in the drivers. Please have a look and give us some comments so that we know whats best.

...

My mistake, stride solution is indeed elegant. I just need to put a patch to change
the above patch in reference to read 4 bytes.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 5 lines
(quoted diff) +4 -0
+
+static int tsens_register(struct tsens_device *tmdev, int i) {
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;

This magic values should be going in to some descriptive constants..

Quoted 4 lines
Quoted 1 line

There is descriptive comment just above and this is not used anywhere

else.

Quoted 1 line

Perhaps this is easier to read?

This seems to be like an register offset, So I would prefer it to be a

nice

Quoted 1 line

#define

Fair enough.

Quoted 48 lines
Quoted 37 lines
Quoted 6 lines

drivers/mfd/syscon.c static struct syscon *of_syscon_register(struct device_node *np)

Quoted 5 lines
drivers/mfd/syscon.c +4 -0
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);

Hmm where is the name actually used??

Oops, not needed in the of based registration. Will remove

Quoted 12 lines

Review

Quoted 10 lines
(quoted diff) +9 -0
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;

So does that mean that driver would not register tsens unless there is a

DT

Quoted 3 lines

node for it?
For apq8064 which has 11 sensors, its becomes necessary to add all 11 sensors in DT with trip points and cooling devices. Which IMO is not

right.

Quoted 11 lines

You could probably do something like:

	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
					&tsens_thermal_of_ops);

	if (IS_ERR(s->tz_dev)) {
		s->tz_dev = thermal_zone_device_register( ... )
		if (IS_ERR(s->tz_dev))
			return -ENODEV;
	}
Quoted 7 lines
Quoted 2 lines

same thing in faliure/remove case ..

Not really. If you look at further down where tsens_register is called (around ln 752), only primary register Is mandatory. If other sensor registration fails, the code just disables those sensors. Code snipped pasted below

I think you missed my point.
Ok, I agree sensor 0 is primary one and should always be registered.

Quoted 6 lines

What am saying is Lets say we have 11 sensors ex: in APQ8064.

Now we have got dt entries for sensor0 and sensor6.
Your code would only register sensor0 and sensor6, because you only considered DT nodes.
This is not correct, as we are not short of any resources to populate the

other

Quoted 5 lines

9 sensors. You should still register all the 11 sensors but 2 of them are comming from dt with there own trip point values and the rest of them are registered as regular non dt thermal sensors.

Doing this way would give the user to see all the sensors via sysfs If not

you

Quoted 4 lines

are loosing the real value of all the 9 sensors in the system.

have a look at other drivers like TI you will understand what Am saying.

Ah, thx for the clarification.
Alternate thought :
What if the system want to leverage only say 2 sensors out of 11. The user has the flexibility
to select the sensors by specifying this in the dt node. If I do what is done in TI drivers, all sensors
get registered and polled regardless of this being used in the userspace thermal daemon.

As a data point, the user-space daemon that exists seems to use only a sub set of sensors.

Quoted 91 lines
Quoted 91 lines

--------------------->cut<----------------------

	/*
	 * Register sensor 0 separately. This sensor is always
	 * expected to be present and if this fails, thermal
	 * sensor probe would fail
	 * Other sensors are optional and if registration fails
	 * disable the sensor and continue
	*/
	ret = tsens_register(tmdev, 0);
	if (ret < 0) {
		dev_err(tsens_dev, "Registering failed for primary sensor");
		ret = -ENODEV;
		goto fail;
	} else {
		tsens_tz_set_mode(&tmdev->sensor[0],
THERMAL_DEVICE_ENABLED);
	}

--------------------->cut<----------------------

Quoted 70 lines
Quoted 53 lines
(quoted diff) +53 -0
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev) {
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np,

"qcom,imem");

Review

Quoted 5 lines
(quoted diff) +5 -0
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+

Please see my comments in the bindings patch.

Quoted 6 lines
Quoted 5 lines

As mentioned above, the syscon needs to be 32-bit for other calibration data, so I guess no option.

On base_regmap, I am good with either ways. Will wait for your further comments and update accordingly in next revision.

have a look at http://www.spinics.net/lists/linux-arm-msm/msg13090.html

Agree, my error.

Quoted 106 lines
Quoted 93 lines
Quoted 60 lines
Quoted 27 lines
(quoted diff) +25 -0
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map,

*field);

Quoted 11 lines
(quoted diff) +11 -0
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail

Review

Quoted 10 lines
(quoted diff) +10 -0
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0],

THERMAL_DEVICE_ENABLED);

Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled",

i);

Quoted 12 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr,

IRQF_TRIGGER_RISING,

Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+			       "tsens", &tmdev->tsens_work);

Unless I missing something,...Would it actually get an interrupt without setting the proper trip values in the hw? For example in this driver we

only

Quoted 2 lines

register from DT.. and the values from Dt never ends up writing into the

hw

Quoted 4 lines

registers.. so this would never trigger an interrupt from trip points mentioned in DT. Unless the default reg values makes sense.. still its out of sync from DT trip values.

Quoted 5 lines

Correct, in polling mode (which is what exists in thermal framework today), HW interrupt do not make sense as the trip points are set to default and never updated based on Dt values.

But the code under the #ifdef THERMAL_TSENS8960_HWTRIPS supports

the

Quoted 7 lines

HW trip point mode.
This code needs the additional patch
Please see https://patchwork.ozlabs.org/patch/364812/

May be I will remove everything under HWTRIPs until it lands in the core thermal framework?

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Does the interrupt actually work? Last time when I tested it on 3.4 It

never

Quoted 1 line

did work?..

???

The code was indeed running in polling mode and interrupts are not getting triggered on
3.14 as well.
I need to go back and check the older version to see if I am missing some register
setting. From a register map, it has only documented interrupt status, not enable/disable.

Quoted 74 lines

How do you enable/disable the interrupt in the tses IP, I did not find any register to do that?
Do we have one?

Quoted 70 lines
Quoted 24 lines
Quoted 24 lines
(quoted diff) +11 -0
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s-

tz_dev);

(quoted diff) +12 -0
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev) {
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},

should be "qcom,msm8960-tsens"

Thx, will address in next version.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,

remove the owner as it would be set anyway byt the core.

Thx, will address in next version.

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif

Please use SIMPLE_DEV_PM_OPS(...)

Thx, will address in next version.

Quoted 9 lines

Review

Quoted 8 lines
(quoted diff) +7 -0
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");

--srini

--Naren

#8 lore raw
In reply to Narendran Rajan
Quoted 483 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

---

drivers/thermal/Kconfig         |  14 +
drivers/thermal/Makefile        |   1 +
drivers/thermal/msm8960_tsens.c | 841 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 856 insertions(+)

create mode 100644 drivers/thermal/msm8960_tsens.c

drivers/thermal/Kconfig +14 -0
index 20915ca..a4cb2c0 100644
@@ -261,6 +261,20 @@ config INTEL_SOC_DTS_THERMAL
 	  notification methods.The other trip is a critical trip point, which
 	  was set by the driver based on the TJ MAX temperature.
 
+config THERMAL_TSENS8960
+	tristate "Qualcomm 8960 Tsens Temperature driver"
+	depends on THERMAL
+	depends on ARCH_QCOM
+	help
+	  QCOM tsens thermal driver provides support for Temperature sensor
+	  (TSENS) found on QCOM SoCs. It supports four configurable trip points
+	  and controls multiple sensors on the SOC. The four trip points are
+	  common across all sensors present in the SoC. The number of sensors
+	  present vary from chip to chip and are set through device tree entry.
+	  The driver presents as a standard thermal zone device with configurable
+	  trip points and cooling device mapping through standard thermal zone
+	  device tree
+

menu "ACPI INT340X thermal drivers"
source drivers/thermal/int340x_thermal/Kconfig
endmenu

drivers/thermal/Makefile +0 -0
index fa0dc48..23c7a34 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_TI_SOC_THERMAL)	+= ti-soc-thermal/

obj-$(CONFIG_INT340X_THERMAL) += int340x_thermal/
obj-$(CONFIG_ST_THERMAL) += st/
obj-$(CONFIG_TEGRA_SOCTHERM) += tegra_soctherm.o

drivers/thermal/Makefile +1 -0
+obj-$(CONFIG_THERMAL_TSENS8960)	+= msm8960_tsens.o
drivers/thermal/msm8960_tsens.c new file +422 -0
new file mode 100644
index 0000000..307bdc8
@@ -0,0 +1,841 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that 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/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */
+enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 << TSENS_8660_CONFIG_SHIFT)
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;
+
+/* Temperature on y axis and ADC-code on x-axis */
+static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s)
+{
+	return adc_code * s->slope + s->offset;
+}
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map, TSENS_INT_STATUS_ADDR, &trdy) &&
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg | TSENS_SW_RST);
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA | TSENS_EN;
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA | TSENS_EN;
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled. Disabling subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor)
+{
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp);
+}
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct tsens_sensor *s)
+{
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE, TSENS_THRESHOLD_MAX_CODE);
+}
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >> TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >> TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp, tm_sensor);
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR, mask, code);
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high)
+{
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work)
+{
+	struct tsens_device *tmdev;
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR);
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status, &code);
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS

drivers/thermal/msm8960_tsens.c line 432

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev->sensor[i].tz_dev);
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR | TSENS_LOWER_STATUS_CLR, mask);

Can TSENS IRQ fire before this write?

drivers/thermal/msm8960_tsens.c line 437

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+}
+
+static irqreturn_t tsens_isr(int irq, void *data)
+{
+	schedule_work(data);

You probably want to reduce the latency of interrupt notifications here. If the kernel wq gets loaded up, your IRQ handling would suffer as well.

Quoted 383 lines
drivers/thermal/msm8960_tsens.c new file +383 -0
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int tsens_suspend(struct device *dev)
+{
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev->pm_tsens_thr_data);
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+
+	return 0;
+}
+
+static int tsens_resume(struct device *dev)
+{
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST, TSENS_SW_RST);
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev->pm_tsens_thr_data);
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev)
+{
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+}
+
+static void tsens_hw_init(struct tsens_device *tmdev)
+{
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR, TSENS_SW_RST, reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map, TSENS_8960_CONFIG_ADDR,
+				   TSENS_8960_CONFIG_MASK, TSENS_8960_CONFIG);
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG << TSENS_8660_CONFIG_SHIFT;
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) << TSENS_SENSOR0_SHIFT;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH << TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |
+		(TSENS_UPPER_LIMIT_TH << TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |
+		(TSENS_MIN_LIMIT_TH << TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |
+		(TSENS_MAX_LIMIT_TH << TSENS_THRESHOLD_MAX_LIMIT_SHIFT);
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr);
+}
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap *map)
+{
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not present\n");
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap *map)
+{
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes seprately
+	*/
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops = {
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i)
+{
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np, "qcom,imem");
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map, *field);
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0], THERMAL_DEVICE_ENABLED);
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled", i);
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr, IRQF_TRIGGER_RISING,
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s->tz_dev);
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev)
+{
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},

drivers/thermal/msm8960_tsens.c line 830

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",

Curious, why specifically 8960?

Quoted 18 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
Signature · 6 lines
Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

--
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
#9 lore raw
Quoted 33 lines

-----Original Message-----
From: [email protected] [mailto:linux-arm-msm- [email protected]] On Behalf Of Lina Iyer
Sent: Tuesday, January 27, 2015 8:03 AM
To: Narendran Rajan
Cc: Zhang Rui; Eduardo Valentin; Linux ARM MSM; Linux PM; Siddartha Mohanadoss; Stephen Boyd
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Quoted 22 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled.
TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

---

drivers/thermal/Kconfig         |  14 +
drivers/thermal/Makefile        |   1 +
drivers/thermal/msm8960_tsens.c | 841
++++++++++++++++++++++++++++++++++++++++
3 files changed, 856 insertions(+)

create mode 100644 drivers/thermal/msm8960_tsens.c

Quoted 6 lines

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 261

Quoted 6 lines
drivers/thermal/Kconfig index renamed from drivers/thermal/Kconfig +0 -0

20915ca..a4cb2c0 100644

drivers/thermal/Kconfig +0 -0
@@ -261,6 +261,20 @@ config INTEL_SOC_DTS_THERMAL
 	  notification methods.The other trip is a critical trip point,

which

Quoted 483 lines

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL

Quoted 8 lines
drivers/thermal/Kconfig +6 -0
 	  was set by the driver based on the TJ MAX temperature.
 
+config THERMAL_TSENS8960
+	tristate "Qualcomm 8960 Tsens Temperature driver"
+	depends on THERMAL
+	depends on ARCH_QCOM
+	help
+	  QCOM tsens thermal driver provides support for Temperature

sensor

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 270

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  (TSENS) found on QCOM SoCs. It supports four configurable trip

points

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 272

Quoted 2 lines
drivers/thermal/Kconfig +2 -0
+	  and controls multiple sensors on the SOC. The four trip points are
+	  common across all sensors present in the SoC. The number of

sensors

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 273

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  present vary from chip to chip and are set through device tree

entry.

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 274

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  The driver presents as a standard thermal zone device with

configurable

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 275

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  trip points and cooling device mapping through standard thermal

zone

drivers/thermal/Makefile obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-

Quoted 10 lines
drivers/thermal/Kconfig +2 -0
+	  device tree
+

menu "ACPI INT340X thermal drivers"
source drivers/thermal/int340x_thermal/Kconfig
endmenu

drivers/thermal/Makefile index renamed from drivers/thermal/Makefile +0 -0

fa0dc48..23c7a34 100644

drivers/thermal/Makefile +0 -0
@@ -39,3 +39,4 @@ obj-$(CONFIG_TI_SOC_THERMAL)	+= ti-soc-

thermal/

Quoted 91 lines

obj-$(CONFIG_INT340X_THERMAL) += int340x_thermal/
obj-$(CONFIG_ST_THERMAL) += st/
obj-$(CONFIG_TEGRA_SOCTHERM) += tegra_soctherm.o

drivers/thermal/Makefile +1 -0
+obj-$(CONFIG_THERMAL_TSENS8960)	+= msm8960_tsens.o
(quoted diff) +0 -0

b/drivers/thermal/msm8960_tsens.c new file mode 100644 index 0000000..307bdc8

drivers/thermal/msm8960_tsens.c new file +81 -0
@@ -0,0 +1,841 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that 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/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */ enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)

drivers/thermal/msm8960_tsens.c line 91

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 <<

TSENS_8660_CONFIG_SHIFT)

Quoted 34 lines
drivers/thermal/msm8960_tsens.c new file +34 -0
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;
+
+/* Temperature on y axis and ADC-code on x-axis */ static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s) {
+	return adc_code * s->slope + s->offset; }
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map,

TSENS_INT_STATUS_ADDR, &trdy) &&

Quoted 29 lines
drivers/thermal/msm8960_tsens.c new file +29 -0
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode) {
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg |

TSENS_SW_RST);

drivers/thermal/msm8960_tsens.c line 176

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA |

TSENS_EN;

drivers/thermal/msm8960_tsens.c line 178

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA |

TSENS_EN;

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled.

Disabling

Quoted 70 lines
drivers/thermal/msm8960_tsens.c new file +70 -0
+subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor) {
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp); }
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct tsens_sensor
+*s) {
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE,
+TSENS_THRESHOLD_MAX_CODE); }
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_MAX_LIMIT_SHIFT;

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE; }
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_MIN_LIMIT_SHIFT;

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE; }

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp,

tm_sensor);

Quoted 22 lines
drivers/thermal/msm8960_tsens.c new file +22 -0
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR,

mask, code);

Quoted 21 lines
drivers/thermal/msm8960_tsens.c new file +21 -0
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high) {
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work) {
+	struct tsens_device *tmdev;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR,

drivers/thermal/msm8960_tsens.c line 389

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR);

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +9 -0
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status,

&code);

Quoted 15 lines
drivers/thermal/msm8960_tsens.c new file +14 -0
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev-

sensor[i].tz_dev);

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR |

TSENS_LOWER_STATUS_CLR, mask);

Quoted 1 line

Can TSENS IRQ fire before this write?

Yes it can as per document, let me run further experiments and update.

Quoted 7 lines

drivers/thermal/msm8960_tsens.c line 437

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+}
+
+static irqreturn_t tsens_isr(int irq, void *data) {
+	schedule_work(data);

You probably want to reduce the latency of interrupt notifications here. If the kernel wq gets loaded up, your IRQ handling would suffer as well.

Good point. Thx. Let me run further experiments on interrupt firing to confirm the behavior on interrupt frequency and load.
PS: By default thermal framework uses a polling mode and default thresholds set are very high.

Quoted 349 lines
Quoted 20 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int tsens_suspend(struct device *dev) {
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev-

pm_tsens_thr_data);

drivers/thermal/msm8960_tsens.c new file +8 -0
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	return 0;
+}
+
+static int tsens_resume(struct device *dev) {
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST,

TSENS_SW_RST);

drivers/thermal/msm8960_tsens.c

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

Quoted 35 lines
drivers/thermal/msm8960_tsens.c new file +7 -0
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev-

pm_tsens_thr_data);

drivers/thermal/msm8960_tsens.c new file +27 -0
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev) {
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl); }
+
+static void tsens_hw_init(struct tsens_device *tmdev) {
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR,

TSENS_SW_RST,

drivers/thermal/msm8960_tsens.c

Quoted 7 lines
drivers/thermal/msm8960_tsens.c new file +7 -0
+reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map,

TSENS_8960_CONFIG_ADDR,

drivers/thermal/msm8960_tsens.c line 474

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+				   TSENS_8960_CONFIG_MASK,

TSENS_8960_CONFIG);

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG <<

TSENS_8660_CONFIG_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) <<

TSENS_SENSOR0_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH <<

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_UPPER_LIMIT_TH <<

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_MIN_LIMIT_TH <<

TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_MAX_LIMIT_TH <<

TSENS_THRESHOLD_MAX_LIMIT_SHIFT);

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr); }
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap
+*map) {

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not

present\n");

Quoted 13 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap
+*map) {
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes

seprately

Quoted 16 lines
drivers/thermal/msm8960_tsens.c new file +16 -0
+	*/
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops =

{

Quoted 83 lines
drivers/thermal/msm8960_tsens.c new file +83 -0
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i) {
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev) {
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np,

"qcom,imem");

Quoted 30 lines
drivers/thermal/msm8960_tsens.c new file +30 -0
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map,

*field);

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0],

THERMAL_DEVICE_ENABLED);

Quoted 8 lines

drivers/thermal/msm8960_tsens.c

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled",

i);

Quoted 49 lines

drivers/thermal/msm8960_tsens.c

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr,

IRQF_TRIGGER_RISING,

Quoted 37 lines
drivers/thermal/msm8960_tsens.c new file +12 -0
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s-

tz_dev);

drivers/thermal/msm8960_tsens.c new file +24 -0
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev) {
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 830

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",

Curious, why specifically 8960?

I guess your point is why not tsens-thermal ?
There are different versions of tsens controller used within QCOM SoCs. May need a different driver for newer versions of this controller. Picked 8960 as I guess this was the earliest chip where this controller was used.

Quoted 17 lines
Quoted 17 lines
drivers/thermal/msm8960_tsens.c new file +11 -1
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
--

Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

Quoted 7 lines
Quoted 3 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to [email protected] More majordomo

info

Quoted 1 line

at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm"

in

Quoted 2 lines

the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--Naren

#10 lore raw
Quoted 26 lines · Narendran Rajan
Quoted 26 lines

-----Original Message-----
From: [email protected] [mailto:linux-arm-msm- [email protected]] On Behalf Of Lina Iyer
Sent: Tuesday, January 27, 2015 8:03 AM
To: Narendran Rajan
Cc: Zhang Rui; Eduardo Valentin; Linux ARM MSM; Linux PM; Siddartha Mohanadoss; Stephen Boyd
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Quoted 15 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled.
TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

---

[...]

Quoted 8 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR |

TSENS_LOWER_STATUS_CLR, mask);

Can TSENS IRQ fire before this write?

Yes it can as per document, let me run further experiments and update.

If the IRQ fires before this work-fn() is complete, schedule_work() would not reschedule.

Quoted 8 lines
Quoted 7 lines

drivers/thermal/msm8960_tsens.c line 437

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+}
+
+static irqreturn_t tsens_isr(int irq, void *data) {
+	schedule_work(data);

You probably want to reduce the latency of interrupt notifications here. If the kernel wq gets loaded up, your IRQ handling would suffer as well.

Quoted 5 lines

Good point. Thx. Let me run further experiments on interrupt firing to confirm the behavior on interrupt frequency and load.
PS: By default thermal framework uses a polling mode and default thresholds set are very high.

Yes, sure. But thermal ramps are better handled immediately. IRQs are the most preferred on production devices.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+	return IRQ_HANDLED;
+}
+

[...]

Quoted 16 lines
Quoted 15 lines

+static struct of_device_id tsens_match_table[] = {

drivers/thermal/msm8960_tsens.c line 830

Quoted 12 lines
drivers/thermal/msm8960_tsens.c new file +12 -0
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",

Curious, why specifically 8960?

Quoted 5 lines

I guess your point is why not tsens-thermal ?
There are different versions of tsens controller used within QCOM SoCs. May need a different driver for newer versions of this controller. Picked 8960 as I guess this was the earliest chip where this controller was used.

It would be nice if you keep this generic, as it will be applicable to a slew of QCOM SoC's. I havent seen it change much in the past few SoCs. You could also use the compatible flag for any small revisional changes.

Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
drivers/thermal/msm8960_tsens.c new file +11 -1
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
--

Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

Quoted 7 lines
Quoted 3 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to [email protected] More majordomo

info

Quoted 1 line

at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm"

in

Quoted 2 lines

the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--Naren

#11 lore raw
In reply to Narendran Rajan
Quoted 89 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

---

drivers/thermal/Kconfig         |  14 +
drivers/thermal/Makefile        |   1 +
drivers/thermal/msm8960_tsens.c | 841 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 856 insertions(+)

create mode 100644 drivers/thermal/msm8960_tsens.c

drivers/thermal/Kconfig +14 -0
index 20915ca..a4cb2c0 100644
@@ -261,6 +261,20 @@ config INTEL_SOC_DTS_THERMAL
 	  notification methods.The other trip is a critical trip point, which
 	  was set by the driver based on the TJ MAX temperature.
 
+config THERMAL_TSENS8960
+	tristate "Qualcomm 8960 Tsens Temperature driver"
+	depends on THERMAL
+	depends on ARCH_QCOM
+	help
+	  QCOM tsens thermal driver provides support for Temperature sensor
+	  (TSENS) found on QCOM SoCs. It supports four configurable trip points
+	  and controls multiple sensors on the SOC. The four trip points are
+	  common across all sensors present in the SoC. The number of sensors
+	  present vary from chip to chip and are set through device tree entry.
+	  The driver presents as a standard thermal zone device with configurable
+	  trip points and cooling device mapping through standard thermal zone
+	  device tree
+

menu "ACPI INT340X thermal drivers"
source drivers/thermal/int340x_thermal/Kconfig
endmenu

drivers/thermal/Makefile +0 -0
index fa0dc48..23c7a34 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_TI_SOC_THERMAL)	+= ti-soc-thermal/

obj-$(CONFIG_INT340X_THERMAL) += int340x_thermal/
obj-$(CONFIG_ST_THERMAL) += st/
obj-$(CONFIG_TEGRA_SOCTHERM) += tegra_soctherm.o

drivers/thermal/Makefile +1 -0
+obj-$(CONFIG_THERMAL_TSENS8960)	+= msm8960_tsens.o
drivers/thermal/msm8960_tsens.c new file +28 -0
new file mode 100644
index 0000000..307bdc8
@@ -0,0 +1,841 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that 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/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>

drivers/thermal/msm8960_tsens.c line 38

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */
+enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};

Care to explain why is this different from the trip_types defined in linux/thermal.h ?

Quoted 43 lines
drivers/thermal/msm8960_tsens.c new file +43 -0
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)

drivers/thermal/msm8960_tsens.c line 91

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 << TSENS_8660_CONFIG_SHIFT)

Could you align these?

Quoted 53 lines
drivers/thermal/msm8960_tsens.c new file +53 -0
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;
+
+/* Temperature on y axis and ADC-code on x-axis */
+static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s)
+{
+	return adc_code * s->slope + s->offset;
+}
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map, TSENS_INT_STATUS_ADDR, &trdy) &&
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);

drivers/thermal/msm8960_tsens.c line 154

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and

/s/disable/disabled

Quoted 18 lines
drivers/thermal/msm8960_tsens.c new file +18 -0
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {

drivers/thermal/msm8960_tsens.c line 182

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg | TSENS_SW_RST);
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA | TSENS_EN;
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA | TSENS_EN;

Instead of doing this check everytime to decide between 8660 and 8960, at init you could set the right mask value in the tsens_device.

Quoted 259 lines
drivers/thermal/msm8960_tsens.c new file +259 -0
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled. Disabling subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor)
+{
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp);
+}
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct tsens_sensor *s)
+{
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE, TSENS_THRESHOLD_MAX_CODE);
+}
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >> TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >> TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp, tm_sensor);
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR, mask, code);
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high)
+{
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work)
+{
+	struct tsens_device *tmdev;
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR);
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status, &code);
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev->sensor[i].tz_dev);
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR | TSENS_LOWER_STATUS_CLR, mask);
+}
+
+static irqreturn_t tsens_isr(int irq, void *data)
+{
+	schedule_work(data);
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM

drivers/thermal/msm8960_tsens.c line 451

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+static int tsens_suspend(struct device *dev)
+{
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev->pm_tsens_thr_data);
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);

No check for num_sensors here?

Quoted 14 lines
drivers/thermal/msm8960_tsens.c new file +14 -0
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+
+	return 0;
+}
+
+static int tsens_resume(struct device *dev)
+{
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;

drivers/thermal/msm8960_tsens.c line 475

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST, TSENS_SW_RST);
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);

What about 8660?

Quoted 193 lines
drivers/thermal/msm8960_tsens.c new file +193 -0
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev->pm_tsens_thr_data);
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev)
+{
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+}
+
+static void tsens_hw_init(struct tsens_device *tmdev)
+{
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR, TSENS_SW_RST, reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map, TSENS_8960_CONFIG_ADDR,
+				   TSENS_8960_CONFIG_MASK, TSENS_8960_CONFIG);
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG << TSENS_8660_CONFIG_SHIFT;
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) << TSENS_SENSOR0_SHIFT;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH << TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |
+		(TSENS_UPPER_LIMIT_TH << TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |
+		(TSENS_MIN_LIMIT_TH << TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |
+		(TSENS_MAX_LIMIT_TH << TSENS_THRESHOLD_MAX_LIMIT_SHIFT);
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr);
+}
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap *map)
+{
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not present\n");
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap *map)
+{
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes seprately
+	*/
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops = {
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i)
+{
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+

drivers/thermal/msm8960_tsens.c line 678

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);

Any particular reason, the sensors are appended to the tmdev, instead of allocating separately?

Quoted 170 lines
drivers/thermal/msm8960_tsens.c new file +163 -0
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np, "qcom,imem");
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map, *field);
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0], THERMAL_DEVICE_ENABLED);
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled", i);
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr, IRQF_TRIGGER_RISING,
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s->tz_dev);
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev)
+{
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
Signature · 6 lines
Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

--
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
#12 lore raw

Thanks for the detail look.

Quoted 31 lines

-----Original Message-----
From: Lina Iyer [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 9:01 AM
To: Narendran Rajan
Cc: Zhang Rui; Eduardo Valentin; Linux ARM MSM; Linux PM; Siddartha Mohanadoss; Stephen Boyd
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Quoted 22 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled.
TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

---

drivers/thermal/Kconfig         |  14 +
drivers/thermal/Makefile        |   1 +
drivers/thermal/msm8960_tsens.c | 841
++++++++++++++++++++++++++++++++++++++++
3 files changed, 856 insertions(+)

create mode 100644 drivers/thermal/msm8960_tsens.c

Quoted 6 lines

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 261

Quoted 6 lines
drivers/thermal/Kconfig index renamed from drivers/thermal/Kconfig +0 -0

20915ca..a4cb2c0 100644

drivers/thermal/Kconfig +0 -0
@@ -261,6 +261,20 @@ config INTEL_SOC_DTS_THERMAL
 	  notification methods.The other trip is a critical trip point,

which

Quoted 74 lines

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL

Quoted 8 lines
drivers/thermal/Kconfig +6 -0
 	  was set by the driver based on the TJ MAX temperature.
 
+config THERMAL_TSENS8960
+	tristate "Qualcomm 8960 Tsens Temperature driver"
+	depends on THERMAL
+	depends on ARCH_QCOM
+	help
+	  QCOM tsens thermal driver provides support for Temperature

sensor

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 270

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  (TSENS) found on QCOM SoCs. It supports four configurable trip

points

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 272

Quoted 2 lines
drivers/thermal/Kconfig +2 -0
+	  and controls multiple sensors on the SOC. The four trip points are
+	  common across all sensors present in the SoC. The number of

sensors

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 273

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  present vary from chip to chip and are set through device tree

entry.

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 274

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  The driver presents as a standard thermal zone device with

configurable

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 275

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  trip points and cooling device mapping through standard thermal

zone

drivers/thermal/Makefile obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-

Quoted 10 lines
drivers/thermal/Kconfig +2 -0
+	  device tree
+

menu "ACPI INT340X thermal drivers"
source drivers/thermal/int340x_thermal/Kconfig
endmenu

drivers/thermal/Makefile index renamed from drivers/thermal/Makefile +0 -0

fa0dc48..23c7a34 100644

drivers/thermal/Makefile +0 -0
@@ -39,3 +39,4 @@ obj-$(CONFIG_TI_SOC_THERMAL)	+= ti-soc-

thermal/

Quoted 43 lines

obj-$(CONFIG_INT340X_THERMAL) += int340x_thermal/
obj-$(CONFIG_ST_THERMAL) += st/
obj-$(CONFIG_TEGRA_SOCTHERM) += tegra_soctherm.o

drivers/thermal/Makefile +1 -0
+obj-$(CONFIG_THERMAL_TSENS8960)	+= msm8960_tsens.o
(quoted diff) +0 -0

b/drivers/thermal/msm8960_tsens.c new file mode 100644 index 0000000..307bdc8

drivers/thermal/msm8960_tsens.c new file +33 -0
@@ -0,0 +1,841 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that 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/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */ enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
Quoted 9 lines

drivers/thermal/msm8960_tsens.c line 38

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};

Care to explain why is this different from the trip_types defined in linux/thermal.h ?

The internal trip points have a different meaning (configurable trips, which could moved around
based on userspace thermal daemon algorithm) than the one defined in thermal.h.

Given all your feedbacks, for now I am going to remove HW TRIP dependencies and will have this removed as well

Quoted 55 lines
Quoted 43 lines
drivers/thermal/msm8960_tsens.c new file +43 -0
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 <<

TSENS_8660_CONFIG_SHIFT)

Quoted 1 line

Could you align these?

Didn't notice. Thx, will do.

Quoted 55 lines
Quoted 34 lines
drivers/thermal/msm8960_tsens.c new file +34 -0
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;
+
+/* Temperature on y axis and ADC-code on x-axis */ static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s) {
+	return adc_code * s->slope + s->offset; }
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map,

TSENS_INT_STATUS_ADDR, &trdy) &&

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
Quoted 9 lines

drivers/thermal/msm8960_tsens.c line 154

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and

/s/disable/disabled

Thanks. Will fix

Quoted 27 lines
Quoted 13 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode) {
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg |

TSENS_SW_RST);

drivers/thermal/msm8960_tsens.c line 176

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA |

TSENS_EN;

Quoted 7 lines

drivers/thermal/msm8960_tsens.c line 178

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA |

TSENS_EN;

Instead of doing this check everytime to decide between 8660 and 8960, at init you could set the right mask value in the tsens_device.

Agree, its cleaner and scalable that way. Will do.

Quoted 269 lines

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled.

Disabling

Quoted 70 lines
drivers/thermal/msm8960_tsens.c new file +70 -0
+subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor) {
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp); }
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct tsens_sensor
+*s) {
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE,
+TSENS_THRESHOLD_MAX_CODE); }
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_MAX_LIMIT_SHIFT;

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE; }
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_MIN_LIMIT_SHIFT;

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE; }

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp,

tm_sensor);

Quoted 22 lines
drivers/thermal/msm8960_tsens.c new file +22 -0
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR,

mask, code);

Quoted 21 lines
drivers/thermal/msm8960_tsens.c new file +21 -0
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high) {
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work) {
+	struct tsens_device *tmdev;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR,

drivers/thermal/msm8960_tsens.c line 389

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR);

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +9 -0
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status,

&code);

Quoted 15 lines
drivers/thermal/msm8960_tsens.c new file +14 -0
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev-

sensor[i].tz_dev);

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR |

TSENS_LOWER_STATUS_CLR, mask); }

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+
+static irqreturn_t tsens_isr(int irq, void *data) {
+	schedule_work(data);
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int tsens_suspend(struct device *dev) {
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 451

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev-

pm_tsens_thr_data);

drivers/thermal/msm8960_tsens.c new file +3 -0
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);

No check for num_sensors here?

Good catch. Thx. Will fix

Quoted 21 lines
Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	return 0;
+}
+
+static int tsens_resume(struct device *dev) {
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST,

TSENS_SW_RST);

drivers/thermal/msm8960_tsens.c

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 475

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);

What about 8660?

Thx, will fix.

Quoted 210 lines
Quoted 30 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev-

pm_tsens_thr_data);

drivers/thermal/msm8960_tsens.c new file +27 -0
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev) {
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl); }
+
+static void tsens_hw_init(struct tsens_device *tmdev) {
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR,

TSENS_SW_RST,

drivers/thermal/msm8960_tsens.c

Quoted 7 lines
drivers/thermal/msm8960_tsens.c new file +7 -0
+reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map,

TSENS_8960_CONFIG_ADDR,

drivers/thermal/msm8960_tsens.c line 474

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+				   TSENS_8960_CONFIG_MASK,

TSENS_8960_CONFIG);

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG <<

TSENS_8660_CONFIG_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) <<

TSENS_SENSOR0_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH <<

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_UPPER_LIMIT_TH <<

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_MIN_LIMIT_TH <<

TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_MAX_LIMIT_TH <<

TSENS_THRESHOLD_MAX_LIMIT_SHIFT);

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr); }
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap
+*map) {

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not

present\n");

Quoted 13 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap
+*map) {
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes

seprately

Quoted 16 lines
drivers/thermal/msm8960_tsens.c new file +16 -0
+	*/
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops =

{

Quoted 59 lines
drivers/thermal/msm8960_tsens.c new file +59 -0
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i) {
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev) {
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
Quoted 9 lines

drivers/thermal/msm8960_tsens.c line 678

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);

Any particular reason, the sensors are appended to the tmdev, instead of allocating separately?

Avoid two separate allocs, so less fragmentation and do not see a downside as such.
(original idea is from Stephen and carried forwarded as it looked elegant)

Quoted 93 lines
Quoted 19 lines
drivers/thermal/msm8960_tsens.c new file +19 -0
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np,

"qcom,imem");

Quoted 30 lines
drivers/thermal/msm8960_tsens.c new file +30 -0
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map,

*field);

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0],

THERMAL_DEVICE_ENABLED);

Quoted 8 lines

drivers/thermal/msm8960_tsens.c

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled",

i);

Quoted 71 lines

drivers/thermal/msm8960_tsens.c

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr,

IRQF_TRIGGER_RISING,

Quoted 59 lines
drivers/thermal/msm8960_tsens.c new file +12 -0
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s-

tz_dev);

drivers/thermal/msm8960_tsens.c new file +41 -1
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev) {
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
--

Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

Quoted 5 lines
Quoted 3 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to [email protected] More majordomo

info

Quoted 1 line

at http://vger.kernel.org/majordomo-info.html

-- Naren

#13 lore raw
In reply to Narendran Rajan
Quoted 10 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

So as far as I can tell you removed my loop TODOs, added debug prints and the device singleton, put the calibration data offset into DT (another TODO), used regmap on the qfprom, made this into a standalone device instead of a child of GCC, and moved to OF thermal zones (forcing that THERMAL_TSENS8960_HWTRIPS ifdef I suppose?) Anything else?

Quoted 4 lines
Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

Something is wrong with your git. There should only be one space between your name and the email id.

Quoted 60 lines

---

drivers/thermal/msm8960_tsens.c new file +54 -0
new file mode 100644
index 0000000..307bdc8
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >> TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >> TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+			break;

drivers/thermal/msm8960_tsens.c line 324

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE;
+}

I wonder if these can be loops, or something on top of find_next_bit()? That's what my TODO was about, hoping to reduce these functions even further.

Quoted 37 lines
drivers/thermal/msm8960_tsens.c new file +37 -0
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp, tm_sensor);
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);

drivers/thermal/msm8960_tsens.c line 371

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR, mask, code);
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high)

Is this function even used?

Quoted 44 lines
drivers/thermal/msm8960_tsens.c new file +44 -0
+{
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work)
+{
+	struct tsens_device *tmdev;
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR);
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status, &code);
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+

drivers/thermal/msm8960_tsens.c line 425

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);

This looks like debug noise. Please remove.

drivers/thermal/msm8960_tsens.c line 437

Quoted 12 lines
drivers/thermal/msm8960_tsens.c new file +12 -0
+			thermal_zone_device_update(tmdev->sensor[i].tz_dev);
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR | TSENS_LOWER_STATUS_CLR, mask);
+}
+
+static irqreturn_t tsens_isr(int irq, void *data)
+{
+	schedule_work(data);

I was going to move this to threaded irqs, please do that.

Quoted 21 lines
drivers/thermal/msm8960_tsens.c new file +21 -0
+	return IRQ_HANDLED;
+}
+
+static void tsens_disable_mode(const struct tsens_device *tmdev)
+{
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+}
+

drivers/thermal/msm8960_tsens.c line 521

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+static void tsens_hw_init(struct tsens_device *tmdev)
+{
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR, TSENS_SW_RST, reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);

Unnecessary ()?

drivers/thermal/msm8960_tsens.c line 527

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map, TSENS_8960_CONFIG_ADDR,
+				   TSENS_8960_CONFIG_MASK, TSENS_8960_CONFIG);
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);

Unnecessary ()?

Quoted 26 lines
drivers/thermal/msm8960_tsens.c new file +26 -0
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG << TSENS_8660_CONFIG_SHIFT;
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) << TSENS_SENSOR0_SHIFT;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH << TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |
+		(TSENS_UPPER_LIMIT_TH << TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |
+		(TSENS_MIN_LIMIT_TH << TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |
+		(TSENS_MAX_LIMIT_TH << TSENS_THRESHOLD_MAX_LIMIT_SHIFT);
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr);
+}
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap *map)
+{

drivers/thermal/msm8960_tsens.c line 563

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not present\n");

Ah we do use it for an error here. It isn't hard to put a struct device inside tmdev...

Quoted 14 lines
drivers/thermal/msm8960_tsens.c new file +14 -0
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap *map)
+{
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];
+	u8 *byte_data;

drivers/thermal/msm8960_tsens.c line 587

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes seprately
+	*/

Weird comment style. Please do it like

/*
* This because this isn't kernel-doc.
*/

Quoted 13 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}

drivers/thermal/msm8960_tsens.c line 610

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	byte_data = (u8 *)temp_data;
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}

This is a lot of hoops to jump through just to use a syscon regmap (does this even work in big-endian mode?). Given your point about consumers wanting to read with different strides makes me think that using a regmap is not a good idea. It would be better if we had an API that could be used to read an arbitrary number of bytes from an eeprom.

drivers/thermal/msm8960_tsens.c line 616

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops = {
+	.get_temp = tsens_tz_get_temp,
+};
+
+

Nitpick: Two newlines instead of one.

Quoted 17 lines
drivers/thermal/msm8960_tsens.c new file +17 -0
+static int tsens_register(struct tsens_device *tmdev, int i)
+{
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+

drivers/thermal/msm8960_tsens.c line 643

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;

So if it's an error pointer we leave it there? And then if an interrupt arrives and this sensor has a new temperature to report we'll update the thermal framework with an invalid pointer? When I wrote this code I didn't allow probe to continue if any of these zones failed to register. Why change that because we're using the sensor API?

Quoted 50 lines
drivers/thermal/msm8960_tsens.c new file +50 -0
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,

drivers/thermal/msm8960_tsens.c line 703

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");

Why make any noise at all? A backup is here.

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np, "qcom,imem");
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);

drivers/thermal/msm8960_tsens.c line 724

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {

Kernel style is typically

if (!base_node)

Quoted 42 lines
drivers/thermal/msm8960_tsens.c new file +42 -0
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map, *field);
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;

drivers/thermal/msm8960_tsens.c line 776

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0], THERMAL_DEVICE_ENABLED);
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled", i);

Missing newline...

drivers/thermal/msm8960_tsens.c line 778

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);

Does this do anything? The mode is already THERMAL_DEVICE_DISABLED so I imagine it just bails out immediately?

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+

drivers/thermal/msm8960_tsens.c line 794

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr, IRQF_TRIGGER_RISING,
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");

Please remove.

drivers/thermal/msm8960_tsens.c line 798

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)

I should have made this simpler like:

for (s = tmdev->sensor; s < tmdev->sensor + tmdev->num_sensors; s++)

drivers/thermal/msm8960_tsens.c line 807

Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +9 -0
+		thermal_zone_of_sensor_unregister(&pdev->dev, s->tz_dev);
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev)
+{
+	int i;

Because then we can save a whole variable here.

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);

drivers/thermal/msm8960_tsens.c line 823

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);

I would prefer we leave it as a child of GCC like I had done in my original patch. The one ugly part was passing the DT node to the virtual tsens device so that it could figure out the number of sensors, etc. We should be able to fix that by creating the device, assigning the of_node, and then registering the device with the platform bus.

drivers/thermal/msm8960_tsens.c line 830

Quoted 7 lines
drivers/thermal/msm8960_tsens.c new file +7 -0
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",

A better name may be tsens-tm or qcom-tsens. The same version of the hardware exists in so many different SoCs there really isn't anything SoC specific about the hardware. The SoC difference is almost entirely in the qfprom layout, except for when the hw engineers decide to put a different number of sensors on the chip. The same comment applies to the file name. Probably qcom-tsens.c or something.

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#14 lore raw

Thank you Stephen for the detail comments.

Quoted 19 lines

-----Original Message-----
From: [email protected] [mailto:linux-arm-msm- [email protected]] On Behalf Of Stephen Boyd
Sent: Wednesday, January 28, 2015 5:46 PM
To: Narendran Rajan
Cc: Zhang Rui; Eduardo Valentin; Linux ARM MSM; Linux PM; Siddartha Mohanadoss
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Quoted 9 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Quoted 4 lines
Quoted 1 line

Based on code by Siddartha Mohanadoss and Stephen Boyd.

So as far as I can tell you removed my loop TODOs, added debug prints and the device singleton, put the calibration data offset into DT (another

TODO),

Quoted 5 lines

used regmap on the qfprom, made this into a standalone device instead of a child of GCC, and moved to OF thermal zones (forcing that THERMAL_TSENS8960_HWTRIPS ifdef I
suppose?) Anything else?

Let me please know if you prefer a different attribution, would be glad to add.

Quoted 8 lines
Quoted 4 lines
Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

Something is wrong with your git. There should only be one space between your name and the email id.

Good eyes :-)
Thx, I did notice just after a I sent the mail. Fixed it internally, will be corrected in next patch.

Quoted 62 lines
Quoted 6 lines

---

(quoted diff) +0 -0

b/drivers/thermal/msm8960_tsens.c new file mode 100644 index 0000000..307bdc8

drivers/thermal/msm8960_tsens.c

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_MAX_LIMIT_SHIFT;

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE; }
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_MIN_LIMIT_SHIFT;

Quoted 15 lines

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE; }

I wonder if these can be loops, or something on top of find_next_bit()? That's what my TODO was about, hoping to reduce these functions even further.

This looked pretty readable enough. Let me see find_next_bit helps. Thx

Quoted 44 lines

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp,

tm_sensor);

Quoted 22 lines
drivers/thermal/msm8960_tsens.c new file +22 -0
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR,

mask, code);

Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 371

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high)

Is this function even used?

Yes, only when HW trip path is present. Let me remove this as you suggested in previous comment
And have only polling mode.

Quoted 44 lines
Quoted 17 lines
drivers/thermal/msm8960_tsens.c new file +17 -0
+{
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work) {
+	struct tsens_device *tmdev;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR,

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR);

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +9 -0
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status,

&code);

Quoted 15 lines

drivers/thermal/msm8960_tsens.c line 425

Quoted 13 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);

This looks like debug noise. Please remove.

Will do (in fact plan to remove interrupt mode completely)

Quoted 10 lines

drivers/thermal/msm8960_tsens.c

Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +1 -0
+			thermal_zone_device_update(tmdev-

sensor[i].tz_dev);

drivers/thermal/msm8960_tsens.c new file +6 -0
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR |

TSENS_LOWER_STATUS_CLR, mask); }

Quoted 6 lines

drivers/thermal/msm8960_tsens.c line 437

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+
+static irqreturn_t tsens_isr(int irq, void *data) {
+	schedule_work(data);

I was going to move this to threaded irqs, please do that.

I guess in a polling mode, this will not be needed. Will remove this completely.

Quoted 25 lines
Quoted 14 lines
drivers/thermal/msm8960_tsens.c new file +14 -0
+	return IRQ_HANDLED;
+}
+
+static void tsens_disable_mode(const struct tsens_device *tmdev) {
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl); }
+
+static void tsens_hw_init(struct tsens_device *tmdev) {
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR,

TSENS_SW_RST,

Quoted 7 lines

drivers/thermal/msm8960_tsens.c line 521

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);

Unnecessary ()?

Ok, Srini also wanted not to hardcode '18' (and '16' below). Will redefine TSENS_MEASURE_PERIOD_8960 as BIT(18)

Quoted 11 lines

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map,

TSENS_8960_CONFIG_ADDR,

drivers/thermal/msm8960_tsens.c line 474

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+				   TSENS_8960_CONFIG_MASK,

TSENS_8960_CONFIG);

drivers/thermal/msm8960_tsens.c line 527

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);

Unnecessary ()?

Ok, Srini also wanted not to hardcode '18' (and '16' below). Will redefine TSENS_MEASURE_PERIOD_8960 as BIT(16)

Quoted 48 lines

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG <<

TSENS_8660_CONFIG_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) <<

TSENS_SENSOR0_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH <<

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_UPPER_LIMIT_TH <<

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_MIN_LIMIT_TH <<

TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_MAX_LIMIT_TH <<

TSENS_THRESHOLD_MAX_LIMIT_SHIFT);

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr); }
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap
+*map) {

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not

present\n");

Quoted 1 line

Ah we do use it for an error here. It isn't hard to put a struct device

inside

Quoted 1 line

tmdev...

Agree (did look around some drivers and they seem to have used). Anyways, I agree
It's a bad practice. Will correct

Quoted 25 lines
Quoted 14 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap
+*map) {
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes

seprately

Quoted 4 lines

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+	*/

Weird comment style. Please do it like

Thx. Will correct.

Quoted 21 lines

/*
* This because this isn't kernel-doc.
*/

Quoted 17 lines
drivers/thermal/msm8960_tsens.c new file +17 -0
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 610

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}

This is a lot of hoops to jump through just to use a syscon regmap (does

this

Quoted 3 lines

even work in big-endian mode?). Given your point about consumers wanting to read with different strides makes me think that using a regmap is not a good idea. It would be better if we had an API that could be used to read

an

Quoted 2 lines

arbitrary number of bytes from an eeprom.

I guess Srini's solution is elegant enough. It allows to set stride values and then helper
Functions to read the number of bytes needed.

Quoted 30 lines

drivers/thermal/msm8960_tsens.c

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops =

{

drivers/thermal/msm8960_tsens.c line 616

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+	.get_temp = tsens_tz_get_temp,
+};
+
+

Nitpick: Two newlines instead of one.

Quoted 20 lines
drivers/thermal/msm8960_tsens.c new file +20 -0
+static int tsens_register(struct tsens_device *tmdev, int i) {
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 643

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;

So if it's an error pointer we leave it there? And then if an interrupt

arrives

Quoted 3 lines

and this sensor has a new temperature to report we'll update the thermal framework with an invalid pointer? When I wrote this code I didn't allow probe to continue if any of these zones failed to register. Why change

that

Quoted 2 lines

because we're using the sensor API?

When the registration fails, the sensor is not enabled (its explicitly disabled in the code).
The interrupts by design is common across all sensors. Primary sensor registration fail means
Probe fail and the code deregisters from thermal framework.

The registration of other sensors can fail as there may not a dt entry for that sensor.
This is valid in mind as the system integrator may prefer (based on thermal profiling) to enable
only a few sensors out of total N and monitor only them.

Quoted 53 lines
Quoted 53 lines
drivers/thermal/msm8960_tsens.c new file +53 -0
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev) {
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;
Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 703

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");

Why make any noise at all? A backup is here.

Ok, will move to dbg.

Quoted 17 lines

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np,

"qcom,imem");

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
Quoted 9 lines

drivers/thermal/msm8960_tsens.c line 724

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {

Kernel style is typically

Thx, will correct

Quoted 48 lines

if (!base_node)

Quoted 13 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map,

*field);

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0],

THERMAL_DEVICE_ENABLED);

Quoted 8 lines

drivers/thermal/msm8960_tsens.c

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled",

i);

Quoted 2 lines

Missing newline...

Thx, will fix

Quoted 6 lines

drivers/thermal/msm8960_tsens.c line 778

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);

Does this do anything? The mode is already THERMAL_DEVICE_DISABLED so I imagine it just bails out immediately?

Didn't want to rely on default. As noted above, the registration for non Primary sensors are allowed to fail and the driver just mark those sensors As disabled and continue.

Quoted 11 lines

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +9 -0
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr,

IRQF_TRIGGER_RISING,

Quoted 10 lines

drivers/thermal/msm8960_tsens.c line 794

Quoted 7 lines
drivers/thermal/msm8960_tsens.c new file +7 -0
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");

Please remove.

Less boot up noise ? Ok.

Quoted 31 lines

drivers/thermal/msm8960_tsens.c line 798

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)

I should have made this simpler like:

for (s = tmdev->sensor; s < tmdev->sensor + tmdev->num_sensors; s++)

drivers/thermal/msm8960_tsens.c line 807

Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +1 -0
+		thermal_zone_of_sensor_unregister(&pdev->dev, s-

tz_dev);

drivers/thermal/msm8960_tsens.c new file +7 -0
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev) {
+	int i;

Because then we can save a whole variable here.

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 823

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);

I would prefer we leave it as a child of GCC like I had done in my

original

Quoted 1 line

patch. The one ugly part was passing the DT node to the virtual tsens

device

Quoted 1 line

so that it could figure out the number of sensors, etc. We should be able

to

Quoted 1 line

fix that by creating the device, assigning the of_node, and then

registering

Quoted 2 lines

the device with the platform bus.

Ok - personally the existing one looked cleaner, but I agree with both you and Srini.
Will change to GCC child node.

Quoted 6 lines
Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
Quoted 9 lines

drivers/thermal/msm8960_tsens.c line 830

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		.name = "tsens8960-thermal",

A better name may be tsens-tm or qcom-tsens. The same version of the hardware exists in so many different SoCs there really isn't anything SoC specific about the hardware. The SoC difference is almost entirely in the qfprom layout, except for when the hw engineers decide to put a different number of sensors on the chip. The same comment applies to the file name. Probably qcom-tsens.c or something.

Looks like this is the consensus, so will change to qcom-tsens.

Quoted 9 lines
Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +9 -0
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
Quoted 8 lines

drivers/thermal/msm8960_tsens.c line 841

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");

--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm"

in

Quoted 2 lines

the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw
In reply to Narendran Rajan
Quoted 9 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Quoted 5 lines

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

I know this might not be completely related, but since you are sending QCOM code, can you please check my comments on your PMIC thermal code?
https://patchwork.kernel.org/patch/5013021/

Ivan seams to have disappeared.

Quoted 891 lines
---
 drivers/thermal/Kconfig         |  14 +
 drivers/thermal/Makefile        |   1 +
 drivers/thermal/msm8960_tsens.c | 841 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 856 insertions(+)
 create mode 100644 drivers/thermal/msm8960_tsens.c
drivers/thermal/Kconfig +14 -0
index 20915ca..a4cb2c0 100644
@@ -261,6 +261,20 @@ config INTEL_SOC_DTS_THERMAL
 	  notification methods.The other trip is a critical trip point, which
 	  was set by the driver based on the TJ MAX temperature.
 
+config THERMAL_TSENS8960
+	tristate "Qualcomm 8960 Tsens Temperature driver"
+	depends on THERMAL
+	depends on ARCH_QCOM
+	help
+	  QCOM tsens thermal driver provides support for Temperature sensor
+	  (TSENS) found on QCOM SoCs. It supports four configurable trip points
+	  and controls multiple sensors on the SOC. The four trip points are
+	  common across all sensors present in the SoC. The number of sensors
+	  present vary from chip to chip and are set through device tree entry.
+	  The driver presents as a standard thermal zone device with configurable
+	  trip points and cooling device mapping through standard thermal zone
+	  device tree
+
 menu "ACPI INT340X thermal drivers"
 source drivers/thermal/int340x_thermal/Kconfig
 endmenu
drivers/thermal/Makefile +1 -0
index fa0dc48..23c7a34 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_TI_SOC_THERMAL)	+= ti-soc-thermal/
 obj-$(CONFIG_INT340X_THERMAL)  += int340x_thermal/
 obj-$(CONFIG_ST_THERMAL)	+= st/
 obj-$(CONFIG_TEGRA_SOCTHERM)	+= tegra_soctherm.o
+obj-$(CONFIG_THERMAL_TSENS8960)	+= msm8960_tsens.o
drivers/thermal/msm8960_tsens.c new file +841 -0
new file mode 100644
index 0000000..307bdc8
@@ -0,0 +1,841 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that 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/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+
+/* Trips: from very hot to very cold */
+enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 << TSENS_8660_CONFIG_SHIFT)
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;
+
+/* Temperature on y axis and ADC-code on x-axis */
+static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s)
+{
+	return adc_code * s->slope + s->offset;
+}
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map, TSENS_INT_STATUS_ADDR, &trdy) &&
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg | TSENS_SW_RST);
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA | TSENS_EN;
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA | TSENS_EN;
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled. Disabling subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor)
+{
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp);
+}
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct tsens_sensor *s)
+{
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE, TSENS_THRESHOLD_MAX_CODE);
+}
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >> TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >> TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp, tm_sensor);
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR, mask, code);
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high)
+{
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif
+
+static void tsens_scheduler_fn(struct work_struct *work)
+{
+	struct tsens_device *tmdev;
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR);
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status, &code);
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev->sensor[i].tz_dev);
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR | TSENS_LOWER_STATUS_CLR, mask);
+}
+
+static irqreturn_t tsens_isr(int irq, void *data)
+{
+	schedule_work(data);
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int tsens_suspend(struct device *dev)
+{
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev->pm_tsens_thr_data);
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+
+	return 0;
+}
+
+static int tsens_resume(struct device *dev)
+{
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST, TSENS_SW_RST);
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev->pm_tsens_thr_data);
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev)
+{
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+}
+
+static void tsens_hw_init(struct tsens_device *tmdev)
+{
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR, TSENS_SW_RST, reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map, TSENS_8960_CONFIG_ADDR,
+				   TSENS_8960_CONFIG_MASK, TSENS_8960_CONFIG);
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG << TSENS_8660_CONFIG_SHIFT;
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) << TSENS_SENSOR0_SHIFT;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH << TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |
+		(TSENS_UPPER_LIMIT_TH << TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |
+		(TSENS_MIN_LIMIT_TH << TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |
+		(TSENS_MAX_LIMIT_TH << TSENS_THRESHOLD_MAX_LIMIT_SHIFT);
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr);
+}
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap *map)
+{
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not present\n");
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap *map)
+{
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes seprately
+	*/
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops = {
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i)
+{
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np, "qcom,imem");
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map, *field);
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0], THERMAL_DEVICE_ENABLED);
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled", i);
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr, IRQF_TRIGGER_RISING,
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s->tz_dev);
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev)
+{
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
Signature · 1 line
Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
#16 lore raw
Quoted 16 lines · Eduardo Valentin
Quoted 9 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Quoted 5 lines

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>
Quoted 5 lines

I know this might not be completely related, but since you are sending QCOM code, can you please check my comments on your PMIC thermal code?
https://patchwork.kernel.org/patch/5013021/

Ivan seams to have disappeared.

Mm no, sorry, just distracted by other tasks. PMIC thermal driver is in the road map. I will try to accelerate rework.

Regards,
Ivan

#17 lore raw
Quoted 22 lines · Ivan T. Ivanov
Quoted 20 lines · Eduardo Valentin
Quoted 9 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Quoted 5 lines

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>

I know this might not be completely related, but since you are sending QCOM code, can you please check my comments on your PMIC thermal code?
https://patchwork.kernel.org/patch/5013021/

Quoted 6 lines
Quoted 1 line

Ivan seams to have disappeared.

Mm no, sorry, just distracted by other tasks. PMIC thermal driver is in the road map. I will try to accelerate rework.

Great news Ivan!

Let me know if you have doubts or need any support / extension on the OF thermal API in order to get the PMIC thermal properly implemented.

Quoted 3 lines

Regards,
Ivan

Eduardo Valentin

#18 lore raw
In reply to Narendran Rajan
Quoted 80 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>
---
 drivers/thermal/Kconfig         |  14 +
 drivers/thermal/Makefile        |   1 +
 drivers/thermal/msm8960_tsens.c | 841 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 856 insertions(+)
 create mode 100644 drivers/thermal/msm8960_tsens.c
drivers/thermal/Kconfig +14 -0
index 20915ca..a4cb2c0 100644
@@ -261,6 +261,20 @@ config INTEL_SOC_DTS_THERMAL
 	  notification methods.The other trip is a critical trip point, which
 	  was set by the driver based on the TJ MAX temperature.
 
+config THERMAL_TSENS8960
+	tristate "Qualcomm 8960 Tsens Temperature driver"
+	depends on THERMAL
+	depends on ARCH_QCOM
+	help
+	  QCOM tsens thermal driver provides support for Temperature sensor
+	  (TSENS) found on QCOM SoCs. It supports four configurable trip points
+	  and controls multiple sensors on the SOC. The four trip points are
+	  common across all sensors present in the SoC. The number of sensors
+	  present vary from chip to chip and are set through device tree entry.
+	  The driver presents as a standard thermal zone device with configurable
+	  trip points and cooling device mapping through standard thermal zone
+	  device tree
+
 menu "ACPI INT340X thermal drivers"
 source drivers/thermal/int340x_thermal/Kconfig
 endmenu
drivers/thermal/Makefile +1 -0
index fa0dc48..23c7a34 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_TI_SOC_THERMAL)	+= ti-soc-thermal/
 obj-$(CONFIG_INT340X_THERMAL)  += int340x_thermal/
 obj-$(CONFIG_ST_THERMAL)	+= st/
 obj-$(CONFIG_TEGRA_SOCTHERM)	+= tegra_soctherm.o
+obj-$(CONFIG_THERMAL_TSENS8960)	+= msm8960_tsens.o
drivers/thermal/msm8960_tsens.c new file +19 -0
new file mode 100644
index 0000000..307bdc8
@@ -0,0 +1,841 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that 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/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>

drivers/thermal/msm8960_tsens.c line 29

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>

Please review your includes, remove those that are not needed.

drivers/thermal/msm8960_tsens.c line 38

Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +9 -0
+
+/* Trips: from very hot to very cold */
+enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};

Why is the above needed?

Quoted 337 lines
drivers/thermal/msm8960_tsens.c new file +337 -0
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 << TSENS_8660_CONFIG_SHIFT)
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;
+
+/* Temperature on y axis and ADC-code on x-axis */
+static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s)
+{
+	return adc_code * s->slope + s->offset;
+}
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map, TSENS_INT_STATUS_ADDR, &trdy) &&
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg | TSENS_SW_RST);
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA | TSENS_EN;
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA | TSENS_EN;
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled. Disabling subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor)
+{
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp);
+}
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct tsens_sensor *s)
+{
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE, TSENS_THRESHOLD_MAX_CODE);
+}
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >> TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh)
+{
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >> TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE;
+}
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp, tm_sensor);
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR, mask, code);
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high)
+{
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);

drivers/thermal/msm8960_tsens.c line 385

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif

This whole chunk of code seams to be unused. Should we remove it?

Looks like this driver brings again the HW trip discussion again. Do you have any specific needs to extend of thermal?

Quoted 459 lines
drivers/thermal/msm8960_tsens.c new file +456 -0
+
+static void tsens_scheduler_fn(struct work_struct *work)
+{
+	struct tsens_device *tmdev;
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR);
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >> TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status, &code);
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev->sensor[i].tz_dev);
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR | TSENS_LOWER_STATUS_CLR, mask);
+}
+
+static irqreturn_t tsens_isr(int irq, void *data)
+{
+	schedule_work(data);
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int tsens_suspend(struct device *dev)
+{
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev->pm_tsens_thr_data);
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+
+	return 0;
+}
+
+static int tsens_resume(struct device *dev)
+{
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST, TSENS_SW_RST);
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev->pm_tsens_thr_data);
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev)
+{
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+}
+
+static void tsens_hw_init(struct tsens_device *tmdev)
+{
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR, TSENS_SW_RST, reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map, TSENS_8960_CONFIG_ADDR,
+				   TSENS_8960_CONFIG_MASK, TSENS_8960_CONFIG);
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG << TSENS_8660_CONFIG_SHIFT;
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) << TSENS_SENSOR0_SHIFT;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK,
+			TSENS_LOWER_STATUS_CLR | TSENS_UPPER_STATUS_CLR |
+			TSENS_MIN_STATUS_MASK | TSENS_MAX_STATUS_MASK);
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH << TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |
+		(TSENS_UPPER_LIMIT_TH << TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |
+		(TSENS_MIN_LIMIT_TH << TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |
+		(TSENS_MAX_LIMIT_TH << TSENS_THRESHOLD_MAX_LIMIT_SHIFT);
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr);
+}
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap *map)
+{
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not present\n");
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap *map)
+{
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes seprately
+	*/
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops = {
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i)
+{
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np, "qcom,imem");
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map, *field);
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0], THERMAL_DEVICE_ENABLED);
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled", i);
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr, IRQF_TRIGGER_RISING,
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s->tz_dev);
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev)
+{
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
Signature · 1 line
Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
#19 lore raw
Quoted 32 lines

-----Original Message-----
From: Eduardo Valentin [mailto:[email protected]]
Sent: Thursday, January 29, 2015 2:49 PM
To: Narendran Rajan
Cc: Zhang Rui; Linux ARM MSM; Linux PM; Siddartha Mohanadoss; Stephen Boyd
Subject: Re: [PATCH] thermal: Add msm tsens thermal sensor driver

Quoted 22 lines · Narendran Rajan

TSENS supports reading temperature from multiple thermal sensors present in QCOM SOCs.
TSENS HW is enabled only when the main sensor is requested. The TSENS block is disabled if the main senors is disabled irrespective of any other sensors that are being enabled. TSENS driver supports configurable threshold for temperature monitoring in which case it can generate an interrupt when specific thresholds are reached

Based on code by Siddartha Mohanadoss and Stephen Boyd.

Cc Siddartha Mohanadoss <[email protected]>
Cc Stephen Boyd <[email protected]>
---
 drivers/thermal/Kconfig         |  14 +
 drivers/thermal/Makefile        |   1 +
 drivers/thermal/msm8960_tsens.c | 841
++++++++++++++++++++++++++++++++++++++++
 3 files changed, 856 insertions(+)
 create mode 100644 drivers/thermal/msm8960_tsens.c
Quoted 6 lines

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 261

Quoted 6 lines
drivers/thermal/Kconfig index renamed from drivers/thermal/Kconfig +0 -0

20915ca..a4cb2c0 100644

drivers/thermal/Kconfig +0 -0
@@ -261,6 +261,20 @@ config INTEL_SOC_DTS_THERMAL
 	  notification methods.The other trip is a critical trip point,

which

Quoted 66 lines

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL

Quoted 8 lines
drivers/thermal/Kconfig +6 -0
 	  was set by the driver based on the TJ MAX temperature.
 
+config THERMAL_TSENS8960
+	tristate "Qualcomm 8960 Tsens Temperature driver"
+	depends on THERMAL
+	depends on ARCH_QCOM
+	help
+	  QCOM tsens thermal driver provides support for Temperature

sensor

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 270

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  (TSENS) found on QCOM SoCs. It supports four configurable trip

points

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 272

Quoted 2 lines
drivers/thermal/Kconfig +2 -0
+	  and controls multiple sensors on the SOC. The four trip points are
+	  common across all sensors present in the SoC. The number of

sensors

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 273

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  present vary from chip to chip and are set through device tree

entry.

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 274

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  The driver presents as a standard thermal zone device with

configurable

drivers/thermal/Kconfig config INTEL_SOC_DTS_THERMAL · line 275

Quoted 1 line
drivers/thermal/Kconfig +1 -0
+	  trip points and cooling device mapping through standard thermal

zone

drivers/thermal/Makefile obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-

Quoted 10 lines
drivers/thermal/Kconfig +2 -0
+	  device tree
+
 menu "ACPI INT340X thermal drivers"
 source drivers/thermal/int340x_thermal/Kconfig
 endmenu
drivers/thermal/Makefile index renamed from drivers/thermal/Makefile +0 -0

fa0dc48..23c7a34 100644

drivers/thermal/Makefile +0 -0
@@ -39,3 +39,4 @@ obj-$(CONFIG_TI_SOC_THERMAL)	+= ti-soc-

thermal/

Quoted 35 lines
drivers/thermal/Makefile +1 -0
 obj-$(CONFIG_INT340X_THERMAL)  += int340x_thermal/
 obj-$(CONFIG_ST_THERMAL)	+= st/
 obj-$(CONFIG_TEGRA_SOCTHERM)	+= tegra_soctherm.o
+obj-$(CONFIG_THERMAL_TSENS8960)	+= msm8960_tsens.o
(quoted diff) +0 -0

b/drivers/thermal/msm8960_tsens.c new file mode 100644 index 0000000..307bdc8

drivers/thermal/msm8960_tsens.c new file +25 -0
@@ -0,0 +1,841 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that 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/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/thermal.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/pm.h>
+#include <linux/bitops.h>
+#include <linux/regmap.h>
Quoted 9 lines

drivers/thermal/msm8960_tsens.c line 29

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>

Please review your includes, remove those that are not needed.

Will do.

Quoted 365 lines

drivers/thermal/msm8960_tsens.c line 38

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+
+/* Trips: from very hot to very cold */ enum tsens_trip_type {
+	TSENS_TRIP_STAGE3,
+	TSENS_TRIP_STAGE2,
+	TSENS_TRIP_STAGE1,
+	TSENS_TRIP_STAGE0,
+	TSENS_TRIP_NUM,
+};

Why is the above needed?

Quoted 43 lines
drivers/thermal/msm8960_tsens.c new file +43 -0
+
+#define TSENS_CAL_MDEGC				30000
+
+#define TSENS_MAX_SENSORS			11
+
+#define TSENS_8960_CONFIG_ADDR			0x3640
+#define TSENS_8960_CONFIG			0x9b
+#define TSENS_8960_CONFIG_MASK			0xf
+
+#define TSENS_CNTL_ADDR				0x3620
+#define TSENS_CNTL_RESUME_MASK			0xfffffff9
+#define TSENS_EN				BIT(0)
+#define TSENS_SW_RST				BIT(1)
+#define SENSOR0_EN				BIT(3)
+#define TSENS_MIN_STATUS_MASK			BIT(0)
+#define TSENS_LOWER_STATUS_CLR			BIT(1)
+#define TSENS_UPPER_STATUS_CLR			BIT(2)
+#define TSENS_MAX_STATUS_MASK			BIT(3)
+#define TSENS_MEASURE_PERIOD			1
+#define TSENS_8960_SLP_CLK_ENA			BIT(26)
+#define TSENS_8660_SLP_CLK_ENA			BIT(24)
+#define TSENS_8064_STATUS_CNTL			0x3660
+
+#define TSENS_THRESHOLD_ADDR			0x3624
+#define TSENS_THRESHOLD_MAX_CODE		0xff
+#define TSENS_THRESHOLD_MIN_CODE		0
+#define TSENS_THRESHOLD_MAX_LIMIT_SHIFT		24
+#define TSENS_THRESHOLD_MIN_LIMIT_SHIFT		16
+#define TSENS_THRESHOLD_UPPER_LIMIT_SHIFT	8
+#define TSENS_THRESHOLD_LOWER_LIMIT_SHIFT	0
+
+/* Initial temperature threshold values */
+#define TSENS_LOWER_LIMIT_TH			0x50
+#define TSENS_UPPER_LIMIT_TH			0xdf
+#define TSENS_MIN_LIMIT_TH			0x0
+#define TSENS_MAX_LIMIT_TH			0xff
+
+#define TSENS_S0_STATUS_ADDR			0x3628
+
+#define TSENS_INT_STATUS_ADDR			0x363c
+#define TSENS_LOWER_INT_MASK			BIT(1)
+#define TSENS_UPPER_INT_MASK			BIT(2)
+#define TSENS_MAX_INT_MASK			BIT(3)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+#define TSENS_TRDY_MASK				BIT(7)
+
+#define TSENS_SENSOR_SHIFT			16
+#define TSENS_REDUND_SHIFT			24
+#define TSENS_SENSOR0_SHIFT			3
+
+#define TSENS_8660_QFPROM_ADDR			0x00bc
+#define TSENS_8660_CONFIG			1
+#define TSENS_8660_CONFIG_SHIFT			28
+#define TSENS_8660_CONFIG_MASK			(3 <<

TSENS_8660_CONFIG_SHIFT)

Quoted 34 lines
drivers/thermal/msm8960_tsens.c new file +34 -0
+
+struct tsens_device;
+
+struct tsens_sensor {
+	struct thermal_zone_device	*tz_dev;
+	enum thermal_device_mode	mode;
+	unsigned int			sensor_num;
+	int				offset;
+	u32				slope;
+	struct tsens_device		*tmdev;
+	u32                             status;
+};
+
+struct tsens_device {
+	bool			prev_reading_avail;
+	unsigned int		num_sensors;
+	int			pm_tsens_thr_data;
+	int			pm_tsens_cntl;
+	unsigned int            calib_offset;
+	unsigned int            backup_calib_offset;
+	struct work_struct	tsens_work;
+	struct regmap		*map;
+	struct regmap_field	*status_field;
+	struct tsens_sensor	sensor[0];
+};
+
+static struct device *tsens_dev;
+
+/* Temperature on y axis and ADC-code on x-axis */ static int
+tsens_tz_code_to_mdegC(u32 adc_code, const struct tsens_sensor *s) {
+	return adc_code * s->slope + s->offset; }
+
+static int tsens_tz_get_temp(void *_sensor,
+			     long *temp)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+{
+	const struct tsens_sensor *tm_sensor = _sensor;
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 code, trdy;
+
+	if (tm_sensor->mode != THERMAL_DEVICE_ENABLED)
+		return -EINVAL;
+
+	if (!tmdev->prev_reading_avail) {
+		while (!regmap_read(tmdev->map,

TSENS_INT_STATUS_ADDR, &trdy) &&

Quoted 29 lines
drivers/thermal/msm8960_tsens.c new file +29 -0
+		       !(trdy & TSENS_TRDY_MASK))
+			usleep_range(1000, 1100);
+		tmdev->prev_reading_avail = true;
+	}
+
+	regmap_read(tmdev->map, tm_sensor->status, &code);
+	*temp = tsens_tz_code_to_mdegC(code, tm_sensor);
+
+	dev_dbg(tsens_dev, "Temperature for sensor %d is: %ld",
+		tm_sensor->sensor_num, *temp);
+
+	return 0;
+}
+
+/*
+ * If the main sensor is disabled all the sensors are disable and
+ * the clock is disabled.
+ * If the main sensor is disabled and a sub-sensor is enabled
+ * return with an error.
+ */
+static int tsens_tz_set_mode(struct tsens_sensor *tm_sensor,
+			      enum thermal_device_mode mode) {
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	unsigned int i, n = tmdev->num_sensors;
+	u32 reg, mask;
+
+	if (mode == tm_sensor->mode)
+		return 0;
+

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg);
+
+	mask = BIT(tm_sensor->sensor_num + TSENS_SENSOR0_SHIFT);
+	if (mode == THERMAL_DEVICE_ENABLED) {
+		if (!(reg & SENSOR0_EN) && mask != SENSOR0_EN) {
+			pr_err("Main sensor not enabled\n");
+			return -EINVAL;
+		}
+
+		regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg |

TSENS_SW_RST);

drivers/thermal/msm8960_tsens.c line 176

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+		if (tmdev->num_sensors > 1)
+			reg |= mask | TSENS_8960_SLP_CLK_ENA |

TSENS_EN;

drivers/thermal/msm8960_tsens.c line 178

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+		else
+			reg |= mask | TSENS_8660_SLP_CLK_ENA |

TSENS_EN;

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+		tmdev->prev_reading_avail = false;
+	} else {
+		reg &= ~mask;
+		if (!(reg & SENSOR0_EN)) {
+			dev_warn(tsens_dev, "Main sensor not enabled.

Disabling

Quoted 70 lines
drivers/thermal/msm8960_tsens.c new file +70 -0
+subsensors\n");
+
+			reg &= ~GENMASK(n + TSENS_SENSOR0_SHIFT - 1,
+					TSENS_SENSOR0_SHIFT);
+			reg &= ~TSENS_EN;
+
+			if (tmdev->num_sensors > 1)
+				reg &= ~TSENS_8960_SLP_CLK_ENA;
+			else
+				reg &= ~TSENS_8660_SLP_CLK_ENA;
+
+			/* Disable all sub-sensors */
+			for (i = 1; i < n; i++)
+				tmdev->sensor[i].mode = mode;
+		}
+	}
+
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg);
+	tm_sensor->mode = mode;
+
+	return 0;
+}
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+static int tsens_get_trip_temp(struct tsens_sensor *tm_sensor,
+				   int trip, int *temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	u32 reg;
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &reg);
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		reg >>= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		reg >>= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		reg >>= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		reg >>= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:
+		return -EINVAL;
+	}
+	reg &= TSENS_THRESHOLD_MAX_CODE;
+
+	temp = tsens_tz_code_to_mdegC(reg, tm_sensor);
+
+	dev_dbg(tsens_dev, "Sensor %d, trip %d temp is :%d",
+		tm_sensor->sensor_num, trip, *temp);
+
+	return 0;
+}
+
+static void tsens_print_trip_temp(struct tsens_sensor *tm_sensor) {
+	int temp;
+
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE0, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, &temp);
+	tsens_get_trip_temp(tm_sensor, TSENS_TRIP_STAGE3, &temp); }
+
+static int tsens_tz_mdegC_to_code(int mdegC, const struct
+tsens_sensor *s) {
+	int code;
+
+	code = DIV_ROUND_CLOSEST(mdegC - s->offset, s->slope);

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	return clamp(code, TSENS_THRESHOLD_MIN_CODE,
+TSENS_THRESHOLD_MAX_CODE); }
+
+static u32 tsens_hi_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 hi_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE0:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_MAX_STATUS_MASK)) {
+			hi_code = thresh >>

TSENS_THRESHOLD_MAX_LIMIT_SHIFT;

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE3:
+	default:
+		hi_code = TSENS_THRESHOLD_MAX_CODE;
+		break;
+	}

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	return hi_code & TSENS_THRESHOLD_MAX_CODE; }
+
+static u32 tsens_lo_code(int trip, u32 reg_cntl, u32 thresh) {
+	u32 lo_code;
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		if (!(reg_cntl & TSENS_UPPER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE2:
+		if (!(reg_cntl & TSENS_LOWER_STATUS_CLR)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE1:
+		if (!(reg_cntl & TSENS_MIN_STATUS_MASK)) {
+			lo_code = thresh >>

TSENS_THRESHOLD_MIN_LIMIT_SHIFT;

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+			break;
+		}
+		/* else fall through */
+	case TSENS_TRIP_STAGE0:
+	default:
+		lo_code = TSENS_THRESHOLD_MIN_CODE;
+		break;
+	}
+
+	return lo_code & TSENS_THRESHOLD_MAX_CODE; }

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+static int tsens_tz_set_trip_temp(struct tsens_sensor *tm_sensor,
+	int trip, unsigned long temp)
+{
+	struct tsens_device *tmdev = tm_sensor->tmdev;
+	struct regmap_field *status = tmdev->status_field;
+	u32 thresh, reg_cntl, mask = TSENS_THRESHOLD_MAX_CODE;
+	u32 code, hi_code, lo_code, code_err_chk;
+
+	code_err_chk = code = tsens_tz_mdegC_to_code(temp,

tm_sensor);

Quoted 22 lines
drivers/thermal/msm8960_tsens.c new file +22 -0
+
+	regmap_field_read(status, &reg_cntl);
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &thresh);
+
+	switch (trip) {
+	case TSENS_TRIP_STAGE3:
+		code <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MAX_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE2:
+		code <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE1:
+		code <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;
+		break;
+	case TSENS_TRIP_STAGE0:
+		code <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		mask <<= TSENS_THRESHOLD_MIN_LIMIT_SHIFT;
+		break;
+	default:

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		return -EINVAL;
+	}
+
+	hi_code = tsens_hi_code(trip, reg_cntl, thresh);
+	lo_code = tsens_lo_code(trip, reg_cntl, thresh);
+
+	if (code_err_chk < lo_code || code_err_chk > hi_code)
+		return -EINVAL;
+
+	regmap_update_bits(tmdev->map, TSENS_THRESHOLD_ADDR,

mask, code);

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+
+	return 0;
+}
+
+static int tsens_set_trips(void *_sensor, long low, long high) {
+	struct tsens_sensor *tm_sensor = _sensor;
+
+	tsens_print_trip_temp(tm_sensor);

drivers/thermal/msm8960_tsens.c line 385

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE2, high))
+		return -EINVAL;
+
+	if (tsens_tz_set_trip_temp(tm_sensor, TSENS_TRIP_STAGE1, low))
+		return -EINVAL;
+
+	return 0;
+}
+#endif

This whole chunk of code seams to be unused. Should we remove it?

Quoted 1 line

Looks like this driver brings again the HW trip discussion again. Do you

have

Quoted 3 lines

any specific needs to extend of thermal?

Is not used when compiled with upstream kernel.
Wanted to keep a common code base with a downstream kernel which has this support.
But in my tests, yet to observe any noticeable advantage for this driver with HW trips.
May be I need to run more tests, collect data points.

Let me revise my patch with this code removed completely.

Quoted 404 lines
Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+
+static void tsens_scheduler_fn(struct work_struct *work) {
+	struct tsens_device *tmdev;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	struct regmap_field *status;
+	unsigned int threshold, threshold_low, i, code, bits, mask = 0;
+	unsigned long sensor;
+	bool upper_th_x, lower_th_x;
+
+	tmdev = container_of(work, struct tsens_device, tsens_work);
+	status = tmdev->status_field;
+
+	regmap_field_update_bits(status,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR,

drivers/thermal/msm8960_tsens.c line 389

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR);

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+
+	regmap_read(tmdev->map, TSENS_THRESHOLD_ADDR, &threshold);
+	threshold_low = threshold >>

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 9 lines
drivers/thermal/msm8960_tsens.c new file +9 -0
+	threshold_low &= TSENS_THRESHOLD_MAX_CODE;
+	threshold = threshold >> TSENS_THRESHOLD_UPPER_LIMIT_SHIFT;
+	threshold &= TSENS_THRESHOLD_MAX_CODE;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &bits);
+	sensor = bits;
+	sensor >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &sensor, tmdev->num_sensors) {
+		regmap_read(tmdev->map, tmdev->sensor[i].status,

&code);

Quoted 15 lines
drivers/thermal/msm8960_tsens.c new file +14 -0
+		upper_th_x = code >= threshold;
+		lower_th_x = code <= threshold_low;
+
+		if (upper_th_x)
+			mask |= TSENS_UPPER_STATUS_CLR;
+
+		if (lower_th_x)
+			mask |= TSENS_LOWER_STATUS_CLR;
+
+#ifdef THERMAL_TSENS8960_HWTRIPS
+		if (upper_th_x || lower_th_x) {
+			dev_info(tsens_dev,
+				"Threshold reached for sensor(%d)\n", i);
+			thermal_zone_device_update(tmdev-

sensor[i].tz_dev);

drivers/thermal/msm8960_tsens.c

Quoted 6 lines
drivers/thermal/msm8960_tsens.c new file +6 -0
+		}
+#endif
+	}
+
+	regmap_field_update_bits(status,
+			TSENS_UPPER_STATUS_CLR |

TSENS_LOWER_STATUS_CLR, mask); }

Quoted 22 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+
+static irqreturn_t tsens_isr(int irq, void *data) {
+	schedule_work(data);
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int tsens_suspend(struct device *dev) {
+	int i;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_read(map, TSENS_THRESHOLD_ADDR, &tmdev-

pm_tsens_thr_data);

drivers/thermal/msm8960_tsens.c new file +8 -0
+	regmap_read(map, TSENS_CNTL_ADDR, &tmdev->pm_tsens_cntl);
+	regmap_update_bits(map, TSENS_CNTL_ADDR,
+			   TSENS_8960_SLP_CLK_ENA | TSENS_EN, 0);
+
+	tmdev->prev_reading_avail = 0;
+	for (i = 0; i < tmdev->num_sensors; i++)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_DISABLED;
+

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	return 0;
+}
+
+static int tsens_resume(struct device *dev) {
+	int i;
+	unsigned long reg_cntl;
+	struct tsens_device *tmdev = dev_get_drvdata(dev);
+	struct regmap *map = tmdev->map;
+
+	regmap_update_bits(map, TSENS_CNTL_ADDR, TSENS_SW_RST,

TSENS_SW_RST);

drivers/thermal/msm8960_tsens.c

Quoted 2 lines
drivers/thermal/msm8960_tsens.c new file +2 -0
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

Quoted 35 lines
drivers/thermal/msm8960_tsens.c new file +7 -0
+
+	if (tmdev->num_sensors > 1)
+		regmap_update_bits(map, TSENS_8960_CONFIG_ADDR,
+				TSENS_8960_CONFIG_MASK,
+				TSENS_8960_CONFIG);
+
+	regmap_write(map, TSENS_THRESHOLD_ADDR, tmdev-

pm_tsens_thr_data);

drivers/thermal/msm8960_tsens.c new file +27 -0
+	regmap_write(map, TSENS_CNTL_ADDR, tmdev->pm_tsens_cntl);
+
+	reg_cntl = tmdev->pm_tsens_cntl;
+	reg_cntl >>= TSENS_SENSOR0_SHIFT;
+	for_each_set_bit(i, &reg_cntl, tmdev->num_sensors)
+		tmdev->sensor[i].mode = THERMAL_DEVICE_ENABLED;
+
+	return 0;
+}
+
+static const struct dev_pm_ops tsens_pm_ops = {
+	.suspend	= tsens_suspend,
+	.resume		= tsens_resume,
+};
+#endif
+
+static void tsens_disable_mode(const struct tsens_device *tmdev) {
+	u32 reg_cntl;
+	u32 mask;
+
+	mask = GENMASK(tmdev->num_sensors - 1, 0);
+	mask <<= TSENS_SENSOR0_SHIFT;
+	mask |= TSENS_EN;
+
+	regmap_read(tmdev->map, TSENS_CNTL_ADDR, &reg_cntl);
+	reg_cntl &= ~mask;
+	if (tmdev->num_sensors > 1)

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		reg_cntl &= ~TSENS_8960_SLP_CLK_ENA;
+	else
+		reg_cntl &= ~TSENS_8660_SLP_CLK_ENA;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl); }
+
+static void tsens_hw_init(struct tsens_device *tmdev) {
+	u32 reg_cntl, reg_thr;
+
+	reg_cntl = TSENS_SW_RST;
+	regmap_update_bits(tmdev->map, TSENS_CNTL_ADDR,

TSENS_SW_RST,

drivers/thermal/msm8960_tsens.c

Quoted 7 lines
drivers/thermal/msm8960_tsens.c new file +7 -0
+reg_cntl);
+
+	if (tmdev->num_sensors > 1) {
+		reg_cntl |= TSENS_8960_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 18);
+		reg_cntl &= ~TSENS_SW_RST;
+		regmap_update_bits(tmdev->map,

TSENS_8960_CONFIG_ADDR,

drivers/thermal/msm8960_tsens.c line 474

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+				   TSENS_8960_CONFIG_MASK,

TSENS_8960_CONFIG);

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+	} else {
+		reg_cntl |= TSENS_8660_SLP_CLK_ENA |
+			(TSENS_MEASURE_PERIOD << 16);
+		reg_cntl &= ~TSENS_8660_CONFIG_MASK;
+		reg_cntl |= TSENS_8660_CONFIG <<

TSENS_8660_CONFIG_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 3 lines
drivers/thermal/msm8960_tsens.c new file +3 -0
+	}
+
+	reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) <<

TSENS_SENSOR0_SHIFT;

drivers/thermal/msm8960_tsens.c

Quoted 4 lines
drivers/thermal/msm8960_tsens.c new file +4 -0
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	regmap_field_update_bits(tmdev->status_field,
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK,

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_LOWER_STATUS_CLR |

TSENS_UPPER_STATUS_CLR |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+			TSENS_MIN_STATUS_MASK |

TSENS_MAX_STATUS_MASK);

drivers/thermal/msm8960_tsens.c

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+
+	reg_cntl |= TSENS_EN;
+	regmap_write(tmdev->map, TSENS_CNTL_ADDR, reg_cntl);
+
+	reg_thr = (TSENS_LOWER_LIMIT_TH <<

TSENS_THRESHOLD_LOWER_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_UPPER_LIMIT_TH <<

TSENS_THRESHOLD_UPPER_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_MIN_LIMIT_TH <<

TSENS_THRESHOLD_MIN_LIMIT_SHIFT) |

drivers/thermal/msm8960_tsens.c

Quoted 1 line
drivers/thermal/msm8960_tsens.c new file +1 -0
+		(TSENS_MAX_LIMIT_TH <<

TSENS_THRESHOLD_MAX_LIMIT_SHIFT);

Quoted 5 lines
drivers/thermal/msm8960_tsens.c new file +5 -0
+	regmap_write(tmdev->map, TSENS_THRESHOLD_ADDR, reg_thr); }
+
+static int
+tsens_calib_sensors8660(struct tsens_device *tmdev, struct regmap
+*map) {

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u32 temp_data, data;
+	struct tsens_sensor *s = &tmdev->sensor[0];
+
+	if (regmap_read(map, tmdev->calib_offset, &temp_data))
+		return -EINVAL;
+
+	data = (temp_data >> 24) & 0xff;
+
+	if (!data) {
+		dev_err(tsens_dev, "QFPROM TSENS calibration data not

present\n");

Quoted 13 lines
drivers/thermal/msm8960_tsens.c new file +13 -0
+		return -EINVAL;
+	}
+
+	s->offset = TSENS_CAL_MDEGC - s->slope * data;
+
+	return 0;
+}
+
+static int
+tsens_calib_sensors8960(struct tsens_device *tmdev, struct regmap
+*map) {
+	int i;
+	u32 temp_data[TSENS_MAX_SENSORS];

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	u8 *byte_data;
+	u32 fuse, redun, num_read;
+	struct tsens_sensor *s = tmdev->sensor;
+
+	fuse = tmdev->calib_offset;
+	redun = tmdev->backup_calib_offset;
+
+	/**
+	* syscon regmap is 32-bit data, but calibration data is 8-bit.
+	* read 4 bytes from regmap in a loop and then extract bytes

seprately

Quoted 16 lines
drivers/thermal/msm8960_tsens.c new file +16 -0
+	*/
+
+	num_read = DIV_ROUND_UP(tmdev->num_sensors, 4);
+
+	for (i = 0; i < num_read; i++) {
+		if (regmap_read(map, (redun + i*4), &temp_data[i]))
+			return -EINVAL;
+
+		if (!temp_data[i]) {
+			dev_dbg(tsens_dev, "Main calib data not valid\n");
+			if (regmap_read(map, (fuse + i*4), &temp_data[i]))
+				return -EINVAL;
+		}
+	}
+
+	byte_data = (u8 *)temp_data;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+
+	for (i = 0; i < tmdev->num_sensors; i++, s++) {
+		s->offset = TSENS_CAL_MDEGC - s->slope * byte_data[i];
+		dev_dbg(tsens_dev, "calib data %d is %c", i, byte_data[i]);
+	}
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops tsens_thermal_of_ops =

{

Quoted 83 lines
drivers/thermal/msm8960_tsens.c new file +83 -0
+	.get_temp = tsens_tz_get_temp,
+};
+
+
+static int tsens_register(struct tsens_device *tmdev, int i) {
+	char name[18];
+	u32 addr = TSENS_S0_STATUS_ADDR;
+	struct tsens_sensor *s = &tmdev->sensor[i];
+
+	/*
+	* The status registers for each sensor are discontiguous
+	* because some SoCs have 5 sensors while others have more
+	* but the control registers stay in the same place, i.e.
+	* directly after the first 5 status registers.
+	*/
+	if (i >= 5)
+		addr += 40;
+
+	addr += i * 4;
+
+	snprintf(name, sizeof(name), "tsens_tz_sensor%d", i);
+	s->mode = THERMAL_DEVICE_ENABLED;
+	s->sensor_num = i;
+	s->status = addr;
+	s->tmdev = tmdev;
+	s->tz_dev = thermal_zone_of_sensor_register(tsens_dev, i, s,
+						&tsens_thermal_of_ops);
+
+	if (IS_ERR(s->tz_dev))
+		return -ENODEV;
+
+	return 0;
+}
+
+static int tsens_probe(struct platform_device *pdev) {
+	struct device_node *np = pdev->dev.of_node;
+	struct device_node *base_node;
+	struct platform_device *base_pdev;
+	int ret, i, irq, num;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev;
+	struct regmap *map, *imem_regmap;
+	struct reg_field *field;
+	static struct reg_field status_0 = {
+		.reg = TSENS_8064_STATUS_CNTL,
+		.lsb = 0,
+		.msb = 3,
+	};
+	static struct reg_field status_8 = {
+		.reg = TSENS_CNTL_ADDR,
+		.lsb = 8,
+		.msb = 11,
+	};
+
+	tsens_dev = &pdev->dev;
+
+	num = of_property_count_u32_elems(np, "qcom,tsens-slopes");
+	if (num <= 0) {
+		dev_err(tsens_dev, "invalid tsens slopes\n");
+		return -EINVAL;
+	}
+
+	tmdev = devm_kzalloc(tsens_dev, sizeof(*tmdev) +
+			     num * sizeof(struct tsens_sensor), GFP_KERNEL);
+	if (tmdev == NULL)
+		return -ENOMEM;
+
+	tmdev->num_sensors = num;
+	for (i = 0, s = tmdev->sensor; i < num; i++, s++)
+		of_property_read_u32_index(np, "qcom,tsens-slopes", i,
+					   &s->slope);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(tsens_dev,  "no irq resource?\n");
+		return  -EINVAL;
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 0,
+					   &tmdev->calib_offset);
+	if (ret != 0) {
+		dev_err(tsens_dev,  "No calibration offset set\n");
+		return  -EINVAL;

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	}
+
+	ret = of_property_read_u32_index(np, "qcom,calib-offsets", 1,
+					   &tmdev->backup_calib_offset);
+	if (ret) {
+		dev_info(tsens_dev, "Missing backup calibration offset\n");
+		tmdev->backup_calib_offset = tmdev->calib_offset;
+	}
+
+	imem_regmap = syscon_regmap_lookup_by_phandle(np,

"qcom,imem");

Quoted 30 lines
drivers/thermal/msm8960_tsens.c new file +30 -0
+	if (IS_ERR(imem_regmap)) {
+		dev_err(tsens_dev, "syscon regmap look up error\n");
+		return PTR_ERR(imem_regmap);
+	}
+
+	if (num == 1)
+		ret = tsens_calib_sensors8660(tmdev, imem_regmap);
+	else
+		ret = tsens_calib_sensors8960(tmdev, imem_regmap);
+
+	if (ret < 0) {
+		dev_err(tsens_dev, "tsense calibration failed\n");
+		return ret;
+	}
+
+	base_node = of_parse_phandle(np, "qcom,tsens-base", 0);
+	if (base_node == NULL) {
+		dev_err(tsens_dev, "no base node present\n");
+		return  -EINVAL;
+	}
+
+	base_pdev = of_find_device_by_node(base_node);
+	if (base_pdev == NULL) {
+		dev_err(tsens_dev, "no base pdev node\n");
+		return  -ENODEV;
+	}
+
+	tmdev->map = map = dev_get_regmap(&base_pdev->dev, NULL);
+	if (map == NULL) {
+		dev_err(tsens_dev, "base regmap get failed\n");

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+		return  -ENODEV;
+	}
+
+	/* Status bits move when the sensor bits next to them overlap */
+	if (num > 5)
+		field = &status_0;
+	else
+		field = &status_8;
+
+	tmdev->status_field = devm_regmap_field_alloc(tsens_dev, map,

*field);

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+	if (IS_ERR(tmdev->status_field)) {
+		dev_err(tsens_dev, "regmap alloc failed\n");
+		return PTR_ERR(tmdev->status_field);
+	}
+
+	tsens_hw_init(tmdev);
+
+	/*
+	 * Register sensor 0 separately. This sensor is always
+	 * expected to be present and if this fails, thermal
+	 * sensor probe would fail

drivers/thermal/msm8960_tsens.c

Quoted 10 lines
drivers/thermal/msm8960_tsens.c new file +10 -0
+	 * Other sensors are optional and if registration fails
+	 * disable the sensor and continue
+	*/
+	ret = tsens_register(tmdev, 0);
+	if (ret < 0) {
+		dev_err(tsens_dev, "Registering failed for primary sensor");
+		ret = -ENODEV;
+		goto fail;
+	} else {
+		tsens_tz_set_mode(&tmdev->sensor[0],

THERMAL_DEVICE_ENABLED);

Quoted 8 lines

drivers/thermal/msm8960_tsens.c

Quoted 8 lines
drivers/thermal/msm8960_tsens.c new file +8 -0
+	}
+
+	for (i = 1;  i < tmdev->num_sensors; i++) {
+		ret = tsens_register(tmdev, i);
+
+		if (ret < 0) {
+			dev_err(tsens_dev,
+				"Registering failed. Sensor(%i), disabled",

i);

Quoted 71 lines

drivers/thermal/msm8960_tsens.c

Quoted 11 lines
drivers/thermal/msm8960_tsens.c new file +11 -0
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_DISABLED);
+		} else {
+			tsens_tz_set_mode(&tmdev->sensor[i],
+				THERMAL_DEVICE_ENABLED);
+		}
+	}
+
+	INIT_WORK(&tmdev->tsens_work, tsens_scheduler_fn);
+
+	ret = devm_request_irq(tsens_dev, irq, tsens_isr,

IRQF_TRIGGER_RISING,

Quoted 59 lines
drivers/thermal/msm8960_tsens.c new file +12 -0
+			       "tsens", &tmdev->tsens_work);
+	if (ret < 0)
+		goto err_irq;
+
+	platform_set_drvdata(pdev, tmdev);
+
+	dev_info(tsens_dev, "Tsens driver initialized\n");
+
+	return 0;
+err_irq:
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_of_sensor_unregister(&pdev->dev, s-

tz_dev);

drivers/thermal/msm8960_tsens.c new file +41 -1
+fail:
+	tsens_disable_mode(tmdev);
+	return ret;
+}
+
+static int tsens_remove(struct platform_device *pdev) {
+	int i;
+	struct tsens_sensor *s;
+	struct tsens_device *tmdev = platform_get_drvdata(pdev);
+
+	tsens_disable_mode(tmdev);
+	for (i = 0, s = tmdev->sensor; i < tmdev->num_sensors; i++, s++)
+		thermal_zone_device_unregister(s->tz_dev);
+
+	return 0;
+}
+
+static struct of_device_id tsens_match_table[] = {
+	{.compatible = "qcom,ipq806x-tsens"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, tsens_match_table);
+
+static struct platform_driver tsens_driver = {
+	.probe = tsens_probe,
+	.remove = tsens_remove,
+	.driver = {
+		.of_match_table = tsens_match_table,
+		.name = "tsens8960-thermal",
+		.owner = THIS_MODULE,
+#ifdef CONFIG_PM
+		.pm	= &tsens_pm_ops,
+#endif
+	},
+};
+module_platform_driver(tsens_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Temperature Sensor driver");
+MODULE_ALIAS("platform:tsens8960-tm");
--

Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

#20 lore raw

On Thu, Jan 29, 2015 at 05:42:53PM -0800, Narendran Rajan wrote:

<cut>

Quoted 6 lines
Quoted 2 lines

Is not used when compiled with upstream kernel.
Wanted to keep a common code base with a downstream kernel which has this support.

Please, keep in mind that vanilla kernels at kernel.org never maintains compatibility to code outside the tree. You either remove the unused code, or port the full feature, if that makes sense.

Quoted 2 lines

But in my tests, yet to observe any noticeable advantage for this driver with HW trips.

I see. That seams to be the final common conclusion of developers that write code for chips with such feature, once they port to the linux kernel thermal framework. We may need a good use case to get that ported.

Quoted 2 lines

May be I need to run more tests, collect data points.

OK. Let me know your results or if you have any insights regarding HW trip points.

Quoted 2 lines

Let me revise my patch with this code removed completely.

sounds good.

BR,

Eduardo Valentin