// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2015 MediaTek Inc.
* Author: Hanyi Wu <hanyi.wu@mediatek.com>
* Sascha Hauer <s.hauer@pengutronix.de>
* Dawei Chien <dawei.chien@mediatek.com>
* Louis Yu <louis.yu@mediatek.com>
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/nvmem-consumer.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/thermal.h>
#include <linux/reset.h>
#include <linux/types.h>
#include "../thermal_hwmon.h"
/* AUXADC Registers */
#define AUXADC_CON1_SET_V 0x008
#define AUXADC_CON1_CLR_V 0x00c
#define AUXADC_CON2_V 0x010
#define AUXADC_DATA(channel) (0x14 + (channel) * 4)
#define APMIXED_SYS_TS_CON0 0x600
#define APMIXED_SYS_TS_CON1 0x604
/* Thermal Controller Registers */
#define TEMP_MONCTL0 0x000
#define TEMP_MONCTL1 0x004
#define TEMP_MONCTL2 0x008
#define TEMP_MONIDET0 0x014
#define TEMP_MONIDET1 0x018
#define TEMP_MSRCTL0 0x038
#define TEMP_MSRCTL1 0x03c
#define TEMP_AHBPOLL 0x040
#define TEMP_AHBTO 0x044
#define TEMP_ADCPNP0 0x048
#define TEMP_ADCPNP1 0x04c
#define TEMP_ADCPNP2 0x050
#define TEMP_ADCPNP3 0x0b4
#define TEMP_ADCMUX 0x054
#define TEMP_ADCEN 0x060
#define TEMP_PNPMUXADDR 0x064
#define TEMP_ADCMUXADDR 0x068
#define TEMP_ADCENADDR 0x074
#define TEMP_ADCVALIDADDR 0x078
#define TEMP_ADCVOLTADDR 0x07c
#define TEMP_RDCTRL 0x080
#define TEMP_ADCVALIDMASK 0x084
#define TEMP_ADCVOLTAGESHIFT 0x088
#define TEMP_ADCWRITECTRL 0x08c
#define TEMP_MSR0 0x090
#define TEMP_MSR1 0x094
#define TEMP_MSR2 0x098
#define TEMP_MSR3 0x0B8
#define TEMP_SPARE0 0x0f0
#define TEMP_ADCPNP0_1 0x148
#define TEMP_ADCPNP1_1 0x14c
#define TEMP_ADCPNP2_1 0x150
#define TEMP_MSR0_1 0x190
#define TEMP_MSR1_1 0x194
#define TEMP_MSR2_1 0x198
#define TEMP_ADCPNP3_1 0x1b4
#define TEMP_MSR3_1 0x1B8
#define PTPCORESEL 0x400
#define TEMP_MONCTL1_PERIOD_UNIT(x) ((x) & 0x3ff)
#define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16)
#define TEMP_MONCTL2_SENSOR_INTERVAL(x) ((x) & 0x3ff)
#define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x) (x)
#define TEMP_ADCWRITECTRL_ADC_PNP_WRITE BIT(0)
#define TEMP_ADCWRITECTRL_ADC_MUX_WRITE BIT(1)
#define TEMP_ADCVALIDMASK_VALID_HIGH BIT(5)
#define TEMP_ADCVALIDMASK_VALID_POS(bit) (bit)
/* MT8173 thermal sensors */
#define MT8173_TS1 0
#define MT8173_TS2 1
#define MT8173_TS3 2
#define MT8173_TS4 3
#define MT8173_TSABB 4
/* AUXADC channel 11 is used for the temperature sensors */
#define MT8173_TEMP_AUXADC_CHANNEL 11
/* The total number of temperature sensors in the MT8173 */
#define MT8173_NUM_SENSORS 5
/* The number of banks in the MT8173 */
#define MT8173_NUM_ZONES 4
/* The number of sensing points per bank */
#define MT8173_NUM_SENSORS_PER_ZONE 4
/* The number of controller in the MT8173 */
#define MT8173_NUM_CONTROLLER 1
/* The calibration coefficient of sensor */
#define MT8173_CALIBRATION 165
/* Valid temperatures range */
#define MT8173_TEMP_MIN -20000
#define MT8173_TEMP_MAX 150000
/*
* Layout of the fuses providing the calibration data
* These macros could be used for MT8183, MT8173, MT2701, and MT2712.
* MT8183 has 6 sensors and needs 6 VTS calibration data.
* MT8173 has 5 sensors and needs 5 VTS calibration data.
* MT2701 has 3 sensors and needs 3 VTS calibration data.
* MT2712 has 4 sensors and needs 4 VTS calibration data.
*/
#define CALIB_BUF0_VALID_V1 BIT(0)
#define CALIB_BUF1_ADC_GE_V1(x) (((x) >> 22) & 0x3ff)
#define CALIB_BUF0_VTS_TS1_V1(x) (((x) >> 17) & 0x1ff)
#define CALIB_BUF0_VTS_TS2_V1(x) (((x) >> 8) & 0x1ff)
#define CALIB_BUF1_VTS_TS3_V1(x) (((x) >> 0) & 0x1ff)
#define CALIB_BUF2_VTS_TS4_V1(x) (((x) >> 23) & 0x1ff)
#define CALIB_BUF2_VTS_TS5_V1(x) (((x) >> 5) & 0x1ff)
#define CALIB_BUF2_VTS_TSABB_V1(x) (((x) >> 14) & 0x1ff)
#define CALIB_BUF0_DEGC_CALI_V1(x) (((x) >> 1) & 0x3f)
#define CALIB_BUF0_O_SLOPE_V1(x) (((x) >> 26) & 0x3f)
#define CALIB_BUF0_O_SLOPE_SIGN_V1(x) (((x) >> 7) & 0x1)
#define CALIB_BUF1_ID_V1(x) (((x) >> 9) & 0x1)
/*
* Layout of the fuses providing the calibration data
* These macros could be used for MT7622.
*/
#define CALIB_BUF0_ADC_OE_V2(x) (((x) >> 22) & 0x3ff)
#define CALIB_BUF0_ADC_GE_V2(x) (((x) >> 12) & 0x3ff)
#define CALIB_BUF0_DEGC_CALI_V2(x) (((x) >> 6) & 0x3f)
#define CALIB_BUF0_O_SLOPE_V2(x) (((x) >> 0) & 0x3f)
#define CALIB_BUF1_VTS_TS1_V2(x) (((x) >> 23) & 0x1ff)
#define CALIB_BUF1_VTS_TS2_V2(x) (((x) >> 14) & 0x1ff)
#define CALIB_BUF1_VTS_TSABB_V2(x) (((x) >> 5) & 0x1ff)
#define CALIB_BUF1_VALID_V2(x) (((x) >> 4) & 0x1)
#define CALIB_BUF1_O_SLOPE_SIGN_V2(x) (((x) >> 3) & 0x1)
/*
* Layout of the fuses providing the calibration data
* These macros can be used for MT7981 and MT7986.
*/
#define CALIB_BUF0_ADC_GE_V3(x) (((x) >> 0) & 0x3ff)
#define CALIB_BUF0_DEGC_CALI_V3(x) (((x) >> 20) & 0x3f)
#define CALIB_BUF0_O_SLOPE_V3(x) (((x) >> 26) & 0x3f)
#define CALIB_BUF1_VTS_TS1_V3(x) (((x) >> 0) & 0x1ff)
#define CALIB_BUF1_VTS_TS2_V3(x) (((x) >> 21) & 0x1ff)
#define CALIB_BUF1_VTS_TSABB_V3(x) (((x) >> 9) & 0x1ff)
#define CALIB_BUF1_VALID_V3(x) (((x) >> 18) & 0x1)
#define CALIB_BUF1_O_SLOPE_SIGN_V3(x) (((x) >> 19) & 0x1)
#define CALIB_BUF1_ID_V3(x) (((x) >> 20) & 0x1)
enum {
VTS1,
VTS2,
VTS3,
VTS4,
VTS5,
VTSABB,
MAX_NUM_VTS,
};
enum mtk_thermal_version {
MTK_THERMAL_V1 = 1,
MTK_THERMAL_V2,
MTK_THERMAL_V3,
};
/* MT2701 thermal sensors */
#define MT2701_TS1 0