^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: David Hernandez Sanchez <david.hernandezsanchez@st.com> for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * STMicroelectronics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/clk-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/iopoll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "../thermal_core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "../thermal_hwmon.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* DTS register offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define DTS_CFGR1_OFFSET 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define DTS_T0VALR1_OFFSET 0x8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define DTS_RAMPVALR_OFFSET 0X10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define DTS_ITR1_OFFSET 0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define DTS_DR_OFFSET 0x1C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define DTS_SR_OFFSET 0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define DTS_ITENR_OFFSET 0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define DTS_ICIFR_OFFSET 0x28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* DTS_CFGR1 register mask definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define HSREF_CLK_DIV_MASK GENMASK(30, 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define TS1_SMP_TIME_MASK GENMASK(19, 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define TS1_INTRIG_SEL_MASK GENMASK(11, 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* DTS_T0VALR1 register mask definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define TS1_T0_MASK GENMASK(17, 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define TS1_FMT0_MASK GENMASK(15, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* DTS_RAMPVALR register mask definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define TS1_RAMP_COEFF_MASK GENMASK(15, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* DTS_ITR1 register mask definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define TS1_HITTHD_MASK GENMASK(31, 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define TS1_LITTHD_MASK GENMASK(15, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* DTS_DR register mask definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define TS1_MFREQ_MASK GENMASK(15, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* DTS_ITENR register mask definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define ITENR_MASK (GENMASK(2, 0) | GENMASK(6, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* DTS_ICIFR register mask definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define ICIFR_MASK (GENMASK(2, 0) | GENMASK(6, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* Less significant bit position definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define TS1_T0_POS 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define TS1_HITTHD_POS 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define TS1_LITTHD_POS 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define HSREF_CLK_DIV_POS 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* DTS_CFGR1 bit definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define TS1_EN BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define TS1_START BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define REFCLK_SEL BIT(20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define REFCLK_LSE REFCLK_SEL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define Q_MEAS_OPT BIT(21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define CALIBRATION_CONTROL Q_MEAS_OPT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* DTS_SR bit definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define TS_RDY BIT(15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* Bit definitions below are common for DTS_SR, DTS_ITENR and DTS_CIFR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define HIGH_THRESHOLD BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define LOW_THRESHOLD BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* Constants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define ADJUST 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define ONE_MHZ 1000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define POLL_TIMEOUT 5000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define STARTUP_TIME 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define TS1_T0_VAL0 30000 /* 30 celsius */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define TS1_T0_VAL1 130000 /* 130 celsius */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define NO_HW_TRIG 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #define SAMPLING_TIME 15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct stm_thermal_sensor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct thermal_zone_device *th_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) enum thermal_device_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) unsigned int low_temp_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned int high_temp_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int t0, fmt0, ramp_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int stm_enable_irq(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dev_dbg(sensor->dev, "low:%d high:%d\n", sensor->low_temp_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) sensor->high_temp_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Disable IT generation for low and high thresholds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) value = readl_relaxed(sensor->base + DTS_ITENR_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) value &= ~(LOW_THRESHOLD | HIGH_THRESHOLD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (sensor->low_temp_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) value |= HIGH_THRESHOLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (sensor->high_temp_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) value |= LOW_THRESHOLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Enable interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) writel_relaxed(value, sensor->base + DTS_ITENR_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static irqreturn_t stm_thermal_irq_handler(int irq, void *sdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct stm_thermal_sensor *sensor = sdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) dev_dbg(sensor->dev, "sr:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) readl_relaxed(sensor->base + DTS_SR_OFFSET));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) thermal_zone_device_update(sensor->th_dev, THERMAL_EVENT_UNSPECIFIED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) stm_enable_irq(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* Acknoledge all DTS irqs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) writel_relaxed(ICIFR_MASK, sensor->base + DTS_ICIFR_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int stm_sensor_power_on(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Enable sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) value |= TS1_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * The DTS block can be enabled by setting TSx_EN bit in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * DTS_CFGRx register. It requires a startup time of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * 40μs. Use 5 ms as arbitrary timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ret = readl_poll_timeout(sensor->base + DTS_SR_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) value, (value & TS_RDY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) STARTUP_TIME, POLL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Start continuous measuring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) value = readl_relaxed(sensor->base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) value |= TS1_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) writel_relaxed(value, sensor->base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) sensor->mode = THERMAL_DEVICE_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int stm_sensor_power_off(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) sensor->mode = THERMAL_DEVICE_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* Stop measuring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) value &= ~TS1_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* Ensure stop is taken into account */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) usleep_range(STARTUP_TIME, POLL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* Disable sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) value &= ~TS1_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* Ensure disable is taken into account */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return readl_poll_timeout(sensor->base + DTS_SR_OFFSET, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) !(value & TS_RDY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) STARTUP_TIME, POLL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int stm_thermal_calibration(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) u32 value, clk_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) u32 prescaler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Figure out prescaler value for PCLK during calibration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) clk_freq = clk_get_rate(sensor->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (!clk_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) prescaler = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) clk_freq /= ONE_MHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (clk_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) while (prescaler <= clk_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) prescaler++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* Clear prescaler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) value &= ~HSREF_CLK_DIV_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /* Set prescaler. pclk_freq/prescaler < 1MHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) value |= (prescaler << HSREF_CLK_DIV_POS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* Select PCLK as reference clock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) value &= ~REFCLK_SEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* Set maximal sampling time for better precision */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) value |= TS1_SMP_TIME_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /* Measure with calibration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) value &= ~CALIBRATION_CONTROL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* select trigger */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) value &= ~TS1_INTRIG_SEL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) value |= NO_HW_TRIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* Fill in DTS structure with factory sensor values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int stm_thermal_read_factory_settings(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* Retrieve engineering calibration temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) sensor->t0 = readl_relaxed(sensor->base + DTS_T0VALR1_OFFSET) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) TS1_T0_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (!sensor->t0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) sensor->t0 = TS1_T0_VAL0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) sensor->t0 = TS1_T0_VAL1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /* Retrieve fmt0 and put it on Hz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) sensor->fmt0 = ADJUST * (readl_relaxed(sensor->base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) DTS_T0VALR1_OFFSET) & TS1_FMT0_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* Retrieve ramp coefficient */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) sensor->ramp_coeff = readl_relaxed(sensor->base + DTS_RAMPVALR_OFFSET) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) TS1_RAMP_COEFF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (!sensor->fmt0 || !sensor->ramp_coeff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) dev_err(sensor->dev, "%s: wrong setting\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dev_dbg(sensor->dev, "%s: T0 = %doC, FMT0 = %dHz, RAMP_COEFF = %dHz/oC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) __func__, sensor->t0, sensor->fmt0, sensor->ramp_coeff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int stm_thermal_calculate_threshold(struct stm_thermal_sensor *sensor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int temp, u32 *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int freqM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /* Figure out the CLK_PTAT frequency for a given temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) freqM = ((temp - sensor->t0) * sensor->ramp_coeff) / 1000 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) sensor->fmt0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /* Figure out the threshold sample number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) *th = clk_get_rate(sensor->clk) * SAMPLING_TIME / freqM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (!*th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dev_dbg(sensor->dev, "freqM=%d Hz, threshold=0x%x", freqM, *th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /* Disable temperature interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static int stm_disable_irq(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* Disable IT generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) value = readl_relaxed(sensor->base + DTS_ITENR_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) value &= ~ITENR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) writel_relaxed(value, sensor->base + DTS_ITENR_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int stm_thermal_set_trips(void *data, int low, int high)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct stm_thermal_sensor *sensor = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) u32 itr1, th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) dev_dbg(sensor->dev, "set trips %d <--> %d\n", low, high);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* Erase threshold content */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) itr1 = readl_relaxed(sensor->base + DTS_ITR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) itr1 &= ~(TS1_LITTHD_MASK | TS1_HITTHD_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * Disable low-temp if "low" is too small. As per thermal framework
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * API, we use -INT_MAX rather than INT_MIN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (low > -INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) sensor->low_temp_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /* add 0.5 of hysteresis due to measurement error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) ret = stm_thermal_calculate_threshold(sensor, low - 500, &th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) itr1 |= (TS1_HITTHD_MASK & (th << TS1_HITTHD_POS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) sensor->low_temp_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Disable high-temp if "high" is too big. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (high < INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) sensor->high_temp_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) ret = stm_thermal_calculate_threshold(sensor, high, &th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) itr1 |= (TS1_LITTHD_MASK & (th << TS1_LITTHD_POS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) sensor->high_temp_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* Write new threshod values*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) writel_relaxed(itr1, sensor->base + DTS_ITR1_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* Callback to get temperature from HW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static int stm_thermal_get_temp(void *data, int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct stm_thermal_sensor *sensor = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) u32 periods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) int freqM, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (sensor->mode != THERMAL_DEVICE_ENABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* Retrieve the number of periods sampled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) ret = readl_relaxed_poll_timeout(sensor->base + DTS_DR_OFFSET, periods,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) (periods & TS1_MFREQ_MASK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) STARTUP_TIME, POLL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* Figure out the CLK_PTAT frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) freqM = (clk_get_rate(sensor->clk) * SAMPLING_TIME) / periods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!freqM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /* Figure out the temperature in mili celsius */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) *temp = (freqM - sensor->fmt0) * 1000 / sensor->ramp_coeff + sensor->t0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* Registers DTS irq to be visible by GIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static int stm_register_irq(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct device *dev = sensor->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) sensor->irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (sensor->irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return sensor->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ret = devm_request_threaded_irq(dev, sensor->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) stm_thermal_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) dev->driver->name, sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) dev_err(dev, "%s: Failed to register IRQ %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) sensor->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) dev_dbg(dev, "%s: thermal IRQ registered", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static int stm_thermal_sensor_off(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) stm_disable_irq(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) ret = stm_sensor_power_off(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) clk_disable_unprepare(sensor->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static int stm_thermal_prepare(struct stm_thermal_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) ret = clk_prepare_enable(sensor->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) ret = stm_thermal_read_factory_settings(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) goto thermal_unprepare;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) ret = stm_thermal_calibration(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) goto thermal_unprepare;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) thermal_unprepare:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) clk_disable_unprepare(sensor->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static int stm_thermal_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct stm_thermal_sensor *sensor = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return stm_thermal_sensor_off(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static int stm_thermal_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct stm_thermal_sensor *sensor = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) ret = stm_thermal_prepare(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ret = stm_sensor_power_on(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) thermal_zone_device_update(sensor->th_dev, THERMAL_EVENT_UNSPECIFIED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) stm_enable_irq(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) #endif /* CONFIG_PM_SLEEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static SIMPLE_DEV_PM_OPS(stm_thermal_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) stm_thermal_suspend, stm_thermal_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static const struct thermal_zone_of_device_ops stm_tz_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) .get_temp = stm_thermal_get_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) .set_trips = stm_thermal_set_trips,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static const struct of_device_id stm_thermal_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) { .compatible = "st,stm32-thermal"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) MODULE_DEVICE_TABLE(of, stm_thermal_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static int stm_thermal_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) struct stm_thermal_sensor *sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (!pdev->dev.of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) dev_err(&pdev->dev, "%s: device tree node not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (!sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) platform_set_drvdata(pdev, sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) sensor->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) base = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (IS_ERR(base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) return PTR_ERR(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /* Populate sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) sensor->base = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) sensor->clk = devm_clk_get(&pdev->dev, "pclk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (IS_ERR(sensor->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) dev_err(&pdev->dev, "%s: failed to fetch PCLK clock\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return PTR_ERR(sensor->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) stm_disable_irq(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) /* Clear irq flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) writel_relaxed(ICIFR_MASK, sensor->base + DTS_ICIFR_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* Configure and enable HW sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) ret = stm_thermal_prepare(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) dev_err(&pdev->dev, "Error prepare sensor: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) ret = stm_sensor_power_on(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) dev_err(&pdev->dev, "Error power on sensor: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) sensor->th_dev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) sensor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) &stm_tz_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (IS_ERR(sensor->th_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) dev_err(&pdev->dev, "%s: thermal zone sensor registering KO\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) ret = PTR_ERR(sensor->th_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /* Register IRQ into GIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) ret = stm_register_irq(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) goto err_tz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) stm_enable_irq(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * Thermal_zone doesn't enable hwmon as default,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * enable it here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) sensor->th_dev->tzp->no_hwmon = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) ret = thermal_add_hwmon_sysfs(sensor->th_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) goto err_tz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) dev_info(&pdev->dev, "%s: Driver initialized successfully\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) err_tz:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) thermal_zone_of_sensor_unregister(&pdev->dev, sensor->th_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static int stm_thermal_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct stm_thermal_sensor *sensor = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) stm_thermal_sensor_off(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) thermal_remove_hwmon_sysfs(sensor->th_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) thermal_zone_of_sensor_unregister(&pdev->dev, sensor->th_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) static struct platform_driver stm_thermal_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) .name = "stm_thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) .pm = &stm_thermal_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) .of_match_table = stm_thermal_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) .probe = stm_thermal_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) .remove = stm_thermal_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) module_platform_driver(stm_thermal_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) MODULE_DESCRIPTION("STMicroelectronics STM32 Thermal Sensor Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) MODULE_AUTHOR("David Hernandez Sanchez <david.hernandezsanchez@st.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) MODULE_ALIAS("platform:stm_thermal");