Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
0004  *
0005  * Baikal-T1 Process, Voltage, Temperature sensor driver
0006  */
0007 #ifndef __HWMON_BT1_PVT_H__
0008 #define __HWMON_BT1_PVT_H__
0009 
0010 #include <linux/completion.h>
0011 #include <linux/hwmon.h>
0012 #include <linux/kernel.h>
0013 #include <linux/ktime.h>
0014 #include <linux/mutex.h>
0015 #include <linux/seqlock.h>
0016 
0017 /* Baikal-T1 PVT registers and their bitfields */
0018 #define PVT_CTRL            0x00
0019 #define PVT_CTRL_EN         BIT(0)
0020 #define PVT_CTRL_MODE_FLD       1
0021 #define PVT_CTRL_MODE_MASK      GENMASK(3, PVT_CTRL_MODE_FLD)
0022 #define PVT_CTRL_MODE_TEMP      0x0
0023 #define PVT_CTRL_MODE_VOLT      0x1
0024 #define PVT_CTRL_MODE_LVT       0x2
0025 #define PVT_CTRL_MODE_HVT       0x4
0026 #define PVT_CTRL_MODE_SVT       0x6
0027 #define PVT_CTRL_TRIM_FLD       4
0028 #define PVT_CTRL_TRIM_MASK      GENMASK(8, PVT_CTRL_TRIM_FLD)
0029 #define PVT_DATA            0x04
0030 #define PVT_DATA_VALID          BIT(10)
0031 #define PVT_DATA_DATA_FLD       0
0032 #define PVT_DATA_DATA_MASK      GENMASK(9, PVT_DATA_DATA_FLD)
0033 #define PVT_TTHRES          0x08
0034 #define PVT_VTHRES          0x0C
0035 #define PVT_LTHRES          0x10
0036 #define PVT_HTHRES          0x14
0037 #define PVT_STHRES          0x18
0038 #define PVT_THRES_LO_FLD        0
0039 #define PVT_THRES_LO_MASK       GENMASK(9, PVT_THRES_LO_FLD)
0040 #define PVT_THRES_HI_FLD        10
0041 #define PVT_THRES_HI_MASK       GENMASK(19, PVT_THRES_HI_FLD)
0042 #define PVT_TTIMEOUT            0x1C
0043 #define PVT_INTR_STAT           0x20
0044 #define PVT_INTR_MASK           0x24
0045 #define PVT_RAW_INTR_STAT       0x28
0046 #define PVT_INTR_DVALID         BIT(0)
0047 #define PVT_INTR_TTHRES_LO      BIT(1)
0048 #define PVT_INTR_TTHRES_HI      BIT(2)
0049 #define PVT_INTR_VTHRES_LO      BIT(3)
0050 #define PVT_INTR_VTHRES_HI      BIT(4)
0051 #define PVT_INTR_LTHRES_LO      BIT(5)
0052 #define PVT_INTR_LTHRES_HI      BIT(6)
0053 #define PVT_INTR_HTHRES_LO      BIT(7)
0054 #define PVT_INTR_HTHRES_HI      BIT(8)
0055 #define PVT_INTR_STHRES_LO      BIT(9)
0056 #define PVT_INTR_STHRES_HI      BIT(10)
0057 #define PVT_INTR_ALL            GENMASK(10, 0)
0058 #define PVT_CLR_INTR            0x2C
0059 
0060 /*
0061  * PVT sensors-related limits and default values
0062  * @PVT_TEMP_MIN: Minimal temperature in millidegrees of Celsius.
0063  * @PVT_TEMP_MAX: Maximal temperature in millidegrees of Celsius.
0064  * @PVT_TEMP_CHS: Number of temperature hwmon channels.
0065  * @PVT_VOLT_MIN: Minimal voltage in mV.
0066  * @PVT_VOLT_MAX: Maximal voltage in mV.
0067  * @PVT_VOLT_CHS: Number of voltage hwmon channels.
0068  * @PVT_DATA_MIN: Minimal PVT raw data value.
0069  * @PVT_DATA_MAX: Maximal PVT raw data value.
0070  * @PVT_TRIM_MIN: Minimal temperature sensor trim value.
0071  * @PVT_TRIM_MAX: Maximal temperature sensor trim value.
0072  * @PVT_TRIM_DEF: Default temperature sensor trim value (set a proper value
0073  *        when one is determined for Baikal-T1 SoC).
0074  * @PVT_TRIM_TEMP: Maximum temperature encoded by the trim factor.
0075  * @PVT_TRIM_STEP: Temperature stride corresponding to the trim value.
0076  * @PVT_TOUT_MIN: Minimal timeout between samples in nanoseconds.
0077  * @PVT_TOUT_DEF: Default data measurements timeout. In case if alarms are
0078  *        activated the PVT IRQ is enabled to be raised after each
0079  *        conversion in order to have the thresholds checked and the
0080  *        converted value cached. Too frequent conversions may cause
0081  *        the system CPU overload. Lets set the 50ms delay between
0082  *        them by default to prevent this.
0083  */
0084 #define PVT_TEMP_MIN        -48380L
0085 #define PVT_TEMP_MAX        147438L
0086 #define PVT_TEMP_CHS        1
0087 #define PVT_VOLT_MIN        620L
0088 #define PVT_VOLT_MAX        1168L
0089 #define PVT_VOLT_CHS        4
0090 #define PVT_DATA_MIN        0
0091 #define PVT_DATA_MAX        (PVT_DATA_DATA_MASK >> PVT_DATA_DATA_FLD)
0092 #define PVT_TRIM_MIN        0
0093 #define PVT_TRIM_MAX        (PVT_CTRL_TRIM_MASK >> PVT_CTRL_TRIM_FLD)
0094 #define PVT_TRIM_TEMP       7130
0095 #define PVT_TRIM_STEP       (PVT_TRIM_TEMP / PVT_TRIM_MAX)
0096 #define PVT_TRIM_DEF        0
0097 #define PVT_TOUT_MIN        (NSEC_PER_SEC / 3000)
0098 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0099 # define PVT_TOUT_DEF       60000
0100 #else
0101 # define PVT_TOUT_DEF       0
0102 #endif
0103 
0104 /*
0105  * enum pvt_sensor_type - Baikal-T1 PVT sensor types (correspond to each PVT
0106  *            sampling mode)
0107  * @PVT_SENSOR*: helpers to traverse the sensors in loops.
0108  * @PVT_TEMP: PVT Temperature sensor.
0109  * @PVT_VOLT: PVT Voltage sensor.
0110  * @PVT_LVT: PVT Low-Voltage threshold sensor.
0111  * @PVT_HVT: PVT High-Voltage threshold sensor.
0112  * @PVT_SVT: PVT Standard-Voltage threshold sensor.
0113  */
0114 enum pvt_sensor_type {
0115     PVT_SENSOR_FIRST,
0116     PVT_TEMP = PVT_SENSOR_FIRST,
0117     PVT_VOLT,
0118     PVT_LVT,
0119     PVT_HVT,
0120     PVT_SVT,
0121     PVT_SENSOR_LAST = PVT_SVT,
0122     PVT_SENSORS_NUM
0123 };
0124 
0125 /*
0126  * enum pvt_clock_type - Baikal-T1 PVT clocks.
0127  * @PVT_CLOCK_APB: APB clock.
0128  * @PVT_CLOCK_REF: PVT reference clock.
0129  */
0130 enum pvt_clock_type {
0131     PVT_CLOCK_APB,
0132     PVT_CLOCK_REF,
0133     PVT_CLOCK_NUM
0134 };
0135 
0136 /*
0137  * struct pvt_sensor_info - Baikal-T1 PVT sensor informational structure
0138  * @channel: Sensor channel ID.
0139  * @label: hwmon sensor label.
0140  * @mode: PVT mode corresponding to the channel.
0141  * @thres_base: upper and lower threshold values of the sensor.
0142  * @thres_sts_lo: low threshold status bitfield.
0143  * @thres_sts_hi: high threshold status bitfield.
0144  * @type: Sensor type.
0145  * @attr_min_alarm: Min alarm attribute ID.
0146  * @attr_min_alarm: Max alarm attribute ID.
0147  */
0148 struct pvt_sensor_info {
0149     int channel;
0150     const char *label;
0151     u32 mode;
0152     unsigned long thres_base;
0153     u32 thres_sts_lo;
0154     u32 thres_sts_hi;
0155     enum hwmon_sensor_types type;
0156     u32 attr_min_alarm;
0157     u32 attr_max_alarm;
0158 };
0159 
0160 #define PVT_SENSOR_INFO(_ch, _label, _type, _mode, _thres)  \
0161     {                           \
0162         .channel = _ch,                 \
0163         .label = _label,                \
0164         .mode = PVT_CTRL_MODE_ ##_mode,         \
0165         .thres_base = PVT_ ##_thres,            \
0166         .thres_sts_lo = PVT_INTR_ ##_thres## _LO,   \
0167         .thres_sts_hi = PVT_INTR_ ##_thres## _HI,   \
0168         .type = _type,                  \
0169         .attr_min_alarm = _type## _min,         \
0170         .attr_max_alarm = _type## _max,         \
0171     }
0172 
0173 /*
0174  * struct pvt_cache - PVT sensors data cache
0175  * @data: data cache in raw format.
0176  * @thres_sts_lo: low threshold status saved on the previous data conversion.
0177  * @thres_sts_hi: high threshold status saved on the previous data conversion.
0178  * @data_seqlock: cached data seq-lock.
0179  * @conversion: data conversion completion.
0180  */
0181 struct pvt_cache {
0182     u32 data;
0183 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0184     seqlock_t data_seqlock;
0185     u32 thres_sts_lo;
0186     u32 thres_sts_hi;
0187 #else
0188     struct completion conversion;
0189 #endif
0190 };
0191 
0192 /*
0193  * struct pvt_hwmon - Baikal-T1 PVT private data
0194  * @dev: device structure of the PVT platform device.
0195  * @hwmon: hwmon device structure.
0196  * @regs: pointer to the Baikal-T1 PVT registers region.
0197  * @irq: PVT events IRQ number.
0198  * @clks: Array of the PVT clocks descriptor (APB/ref clocks).
0199  * @ref_clk: Pointer to the reference clocks descriptor.
0200  * @iface_mtx: Generic interface mutex (used to lock the alarm registers
0201  *         when the alarms enabled, or the data conversion interface
0202  *         if alarms are disabled).
0203  * @sensor: current PVT sensor the data conversion is being performed for.
0204  * @cache: data cache descriptor.
0205  * @timeout: conversion timeout cache.
0206  */
0207 struct pvt_hwmon {
0208     struct device *dev;
0209     struct device *hwmon;
0210 
0211     void __iomem *regs;
0212     int irq;
0213 
0214     struct clk_bulk_data clks[PVT_CLOCK_NUM];
0215 
0216     struct mutex iface_mtx;
0217     enum pvt_sensor_type sensor;
0218     struct pvt_cache cache[PVT_SENSORS_NUM];
0219     ktime_t timeout;
0220 };
0221 
0222 /*
0223  * struct pvt_poly_term - a term descriptor of the PVT data translation
0224  *            polynomial
0225  * @deg: degree of the term.
0226  * @coef: multiplication factor of the term.
0227  * @divider: distributed divider per each degree.
0228  * @divider_leftover: divider leftover, which couldn't be redistributed.
0229  */
0230 struct pvt_poly_term {
0231     unsigned int deg;
0232     long coef;
0233     long divider;
0234     long divider_leftover;
0235 };
0236 
0237 /*
0238  * struct pvt_poly - PVT data translation polynomial descriptor
0239  * @total_divider: total data divider.
0240  * @terms: polynomial terms up to a free one.
0241  */
0242 struct pvt_poly {
0243     long total_divider;
0244     struct pvt_poly_term terms[];
0245 };
0246 
0247 #endif /* __HWMON_BT1_PVT_H__ */