Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
0004  *
0005  * Authors:
0006  *   Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>
0007  *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
0008  *
0009  * Baikal-T1 Process, Voltage, Temperature sensor driver
0010  */
0011 
0012 #include <linux/bitfield.h>
0013 #include <linux/bitops.h>
0014 #include <linux/clk.h>
0015 #include <linux/completion.h>
0016 #include <linux/delay.h>
0017 #include <linux/device.h>
0018 #include <linux/hwmon-sysfs.h>
0019 #include <linux/hwmon.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/io.h>
0022 #include <linux/kernel.h>
0023 #include <linux/ktime.h>
0024 #include <linux/limits.h>
0025 #include <linux/module.h>
0026 #include <linux/mutex.h>
0027 #include <linux/of.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/polynomial.h>
0030 #include <linux/seqlock.h>
0031 #include <linux/sysfs.h>
0032 #include <linux/types.h>
0033 
0034 #include "bt1-pvt.h"
0035 
0036 /*
0037  * For the sake of the code simplification we created the sensors info table
0038  * with the sensor names, activation modes, threshold registers base address
0039  * and the thresholds bit fields.
0040  */
0041 static const struct pvt_sensor_info pvt_info[] = {
0042     PVT_SENSOR_INFO(0, "CPU Core Temperature", hwmon_temp, TEMP, TTHRES),
0043     PVT_SENSOR_INFO(0, "CPU Core Voltage", hwmon_in, VOLT, VTHRES),
0044     PVT_SENSOR_INFO(1, "CPU Core Low-Vt", hwmon_in, LVT, LTHRES),
0045     PVT_SENSOR_INFO(2, "CPU Core High-Vt", hwmon_in, HVT, HTHRES),
0046     PVT_SENSOR_INFO(3, "CPU Core Standard-Vt", hwmon_in, SVT, STHRES),
0047 };
0048 
0049 /*
0050  * The original translation formulae of the temperature (in degrees of Celsius)
0051  * to PVT data and vice-versa are following:
0052  * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) +
0053  *     1.7204e2,
0054  * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) +
0055  *     3.1020e-1*(N^1) - 4.838e1,
0056  * where T = [-48.380, 147.438]C and N = [0, 1023].
0057  * They must be accordingly altered to be suitable for the integer arithmetics.
0058  * The technique is called 'factor redistribution', which just makes sure the
0059  * multiplications and divisions are made so to have a result of the operations
0060  * within the integer numbers limit. In addition we need to translate the
0061  * formulae to accept millidegrees of Celsius. Here what they look like after
0062  * the alterations:
0063  * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T +
0064  *     17204e2) / 1e4,
0065  * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D -
0066  *     48380,
0067  * where T = [-48380, 147438] mC and N = [0, 1023].
0068  */
0069 static const struct polynomial __maybe_unused poly_temp_to_N = {
0070     .total_divider = 10000,
0071     .terms = {
0072         {4, 18322, 10000, 10000},
0073         {3, 2343, 10000, 10},
0074         {2, 87018, 10000, 10},
0075         {1, 39269, 1000, 1},
0076         {0, 1720400, 1, 1}
0077     }
0078 };
0079 
0080 static const struct polynomial poly_N_to_temp = {
0081     .total_divider = 1,
0082     .terms = {
0083         {4, -16743, 1000, 1},
0084         {3, 81542, 1000, 1},
0085         {2, -182010, 1000, 1},
0086         {1, 310200, 1000, 1},
0087         {0, -48380, 1, 1}
0088     }
0089 };
0090 
0091 /*
0092  * Similar alterations are performed for the voltage conversion equations.
0093  * The original formulae are:
0094  * N = 1.8658e3*V - 1.1572e3,
0095  * V = (N + 1.1572e3) / 1.8658e3,
0096  * where V = [0.620, 1.168] V and N = [0, 1023].
0097  * After the optimization they looks as follows:
0098  * N = (18658e-3*V - 11572) / 10,
0099  * V = N * 10^5 / 18658 + 11572 * 10^4 / 18658.
0100  */
0101 static const struct polynomial __maybe_unused poly_volt_to_N = {
0102     .total_divider = 10,
0103     .terms = {
0104         {1, 18658, 1000, 1},
0105         {0, -11572, 1, 1}
0106     }
0107 };
0108 
0109 static const struct polynomial poly_N_to_volt = {
0110     .total_divider = 10,
0111     .terms = {
0112         {1, 100000, 18658, 1},
0113         {0, 115720000, 1, 18658}
0114     }
0115 };
0116 
0117 static inline u32 pvt_update(void __iomem *reg, u32 mask, u32 data)
0118 {
0119     u32 old;
0120 
0121     old = readl_relaxed(reg);
0122     writel((old & ~mask) | (data & mask), reg);
0123 
0124     return old & mask;
0125 }
0126 
0127 /*
0128  * Baikal-T1 PVT mode can be updated only when the controller is disabled.
0129  * So first we disable it, then set the new mode together with the controller
0130  * getting back enabled. The same concerns the temperature trim and
0131  * measurements timeout. If it is necessary the interface mutex is supposed
0132  * to be locked at the time the operations are performed.
0133  */
0134 static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
0135 {
0136     u32 old;
0137 
0138     mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
0139 
0140     old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0141     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN,
0142            mode | old);
0143 }
0144 
0145 static inline u32 pvt_calc_trim(long temp)
0146 {
0147     temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
0148 
0149     return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
0150 }
0151 
0152 static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
0153 {
0154     u32 old;
0155 
0156     trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
0157 
0158     old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0159     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN,
0160            trim | old);
0161 }
0162 
0163 static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
0164 {
0165     u32 old;
0166 
0167     old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0168     writel(tout, pvt->regs + PVT_TTIMEOUT);
0169     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, old);
0170 }
0171 
0172 /*
0173  * This driver can optionally provide the hwmon alarms for each sensor the PVT
0174  * controller supports. The alarms functionality is made compile-time
0175  * configurable due to the hardware interface implementation peculiarity
0176  * described further in this comment. So in case if alarms are unnecessary in
0177  * your system design it's recommended to have them disabled to prevent the PVT
0178  * IRQs being periodically raised to get the data cache/alarms status up to
0179  * date.
0180  *
0181  * Baikal-T1 PVT embedded controller is based on the Analog Bits PVT sensor,
0182  * but is equipped with a dedicated control wrapper. It exposes the PVT
0183  * sub-block registers space via the APB3 bus. In addition the wrapper provides
0184  * a common interrupt vector of the sensors conversion completion events and
0185  * threshold value alarms. Alas the wrapper interface hasn't been fully thought
0186  * through. There is only one sensor can be activated at a time, for which the
0187  * thresholds comparator is enabled right after the data conversion is
0188  * completed. Due to this if alarms need to be implemented for all available
0189  * sensors we can't just set the thresholds and enable the interrupts. We need
0190  * to enable the sensors one after another and let the controller to detect
0191  * the alarms by itself at each conversion. This also makes pointless to handle
0192  * the alarms interrupts, since in occasion they happen synchronously with
0193  * data conversion completion. The best driver design would be to have the
0194  * completion interrupts enabled only and keep the converted value in the
0195  * driver data cache. This solution is implemented if hwmon alarms are enabled
0196  * in this driver. In case if the alarms are disabled, the conversion is
0197  * performed on demand at the time a sensors input file is read.
0198  */
0199 
0200 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0201 
0202 #define pvt_hard_isr NULL
0203 
0204 static irqreturn_t pvt_soft_isr(int irq, void *data)
0205 {
0206     const struct pvt_sensor_info *info;
0207     struct pvt_hwmon *pvt = data;
0208     struct pvt_cache *cache;
0209     u32 val, thres_sts, old;
0210 
0211     /*
0212      * DVALID bit will be cleared by reading the data. We need to save the
0213      * status before the next conversion happens. Threshold events will be
0214      * handled a bit later.
0215      */
0216     thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
0217 
0218     /*
0219      * Then lets recharge the PVT interface with the next sampling mode.
0220      * Lock the interface mutex to serialize trim, timeouts and alarm
0221      * thresholds settings.
0222      */
0223     cache = &pvt->cache[pvt->sensor];
0224     info = &pvt_info[pvt->sensor];
0225     pvt->sensor = (pvt->sensor == PVT_SENSOR_LAST) ?
0226               PVT_SENSOR_FIRST : (pvt->sensor + 1);
0227 
0228     /*
0229      * For some reason we have to mask the interrupt before changing the
0230      * mode, otherwise sometimes the temperature mode doesn't get
0231      * activated even though the actual mode in the ctrl register
0232      * corresponds to one. Then we read the data. By doing so we also
0233      * recharge the data conversion. After this the mode corresponding
0234      * to the next sensor in the row is set. Finally we enable the
0235      * interrupts back.
0236      */
0237     mutex_lock(&pvt->iface_mtx);
0238 
0239     old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
0240              PVT_INTR_DVALID);
0241 
0242     val = readl(pvt->regs + PVT_DATA);
0243 
0244     pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
0245 
0246     pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
0247 
0248     mutex_unlock(&pvt->iface_mtx);
0249 
0250     /*
0251      * We can now update the data cache with data just retrieved from the
0252      * sensor. Lock write-seqlock to make sure the reader has a coherent
0253      * data.
0254      */
0255     write_seqlock(&cache->data_seqlock);
0256 
0257     cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
0258 
0259     write_sequnlock(&cache->data_seqlock);
0260 
0261     /*
0262      * While PVT core is doing the next mode data conversion, we'll check
0263      * whether the alarms were triggered for the current sensor. Note that
0264      * according to the documentation only one threshold IRQ status can be
0265      * set at a time, that's why if-else statement is utilized.
0266      */
0267     if ((thres_sts & info->thres_sts_lo) ^ cache->thres_sts_lo) {
0268         WRITE_ONCE(cache->thres_sts_lo, thres_sts & info->thres_sts_lo);
0269         hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
0270                    info->channel);
0271     } else if ((thres_sts & info->thres_sts_hi) ^ cache->thres_sts_hi) {
0272         WRITE_ONCE(cache->thres_sts_hi, thres_sts & info->thres_sts_hi);
0273         hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
0274                    info->channel);
0275     }
0276 
0277     return IRQ_HANDLED;
0278 }
0279 
0280 static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
0281 {
0282     return 0644;
0283 }
0284 
0285 static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
0286 {
0287     return 0444;
0288 }
0289 
0290 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0291              long *val)
0292 {
0293     struct pvt_cache *cache = &pvt->cache[type];
0294     unsigned int seq;
0295     u32 data;
0296 
0297     do {
0298         seq = read_seqbegin(&cache->data_seqlock);
0299         data = cache->data;
0300     } while (read_seqretry(&cache->data_seqlock, seq));
0301 
0302     if (type == PVT_TEMP)
0303         *val = polynomial_calc(&poly_N_to_temp, data);
0304     else
0305         *val = polynomial_calc(&poly_N_to_volt, data);
0306 
0307     return 0;
0308 }
0309 
0310 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0311               bool is_low, long *val)
0312 {
0313     u32 data;
0314 
0315     /* No need in serialization, since it is just read from MMIO. */
0316     data = readl(pvt->regs + pvt_info[type].thres_base);
0317 
0318     if (is_low)
0319         data = FIELD_GET(PVT_THRES_LO_MASK, data);
0320     else
0321         data = FIELD_GET(PVT_THRES_HI_MASK, data);
0322 
0323     if (type == PVT_TEMP)
0324         *val = polynomial_calc(&poly_N_to_temp, data);
0325     else
0326         *val = polynomial_calc(&poly_N_to_volt, data);
0327 
0328     return 0;
0329 }
0330 
0331 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0332                bool is_low, long val)
0333 {
0334     u32 data, limit, mask;
0335     int ret;
0336 
0337     if (type == PVT_TEMP) {
0338         val = clamp(val, PVT_TEMP_MIN, PVT_TEMP_MAX);
0339         data = polynomial_calc(&poly_temp_to_N, val);
0340     } else {
0341         val = clamp(val, PVT_VOLT_MIN, PVT_VOLT_MAX);
0342         data = polynomial_calc(&poly_volt_to_N, val);
0343     }
0344 
0345     /* Serialize limit update, since a part of the register is changed. */
0346     ret = mutex_lock_interruptible(&pvt->iface_mtx);
0347     if (ret)
0348         return ret;
0349 
0350     /* Make sure the upper and lower ranges don't intersect. */
0351     limit = readl(pvt->regs + pvt_info[type].thres_base);
0352     if (is_low) {
0353         limit = FIELD_GET(PVT_THRES_HI_MASK, limit);
0354         data = clamp_val(data, PVT_DATA_MIN, limit);
0355         data = FIELD_PREP(PVT_THRES_LO_MASK, data);
0356         mask = PVT_THRES_LO_MASK;
0357     } else {
0358         limit = FIELD_GET(PVT_THRES_LO_MASK, limit);
0359         data = clamp_val(data, limit, PVT_DATA_MAX);
0360         data = FIELD_PREP(PVT_THRES_HI_MASK, data);
0361         mask = PVT_THRES_HI_MASK;
0362     }
0363 
0364     pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
0365 
0366     mutex_unlock(&pvt->iface_mtx);
0367 
0368     return 0;
0369 }
0370 
0371 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0372               bool is_low, long *val)
0373 {
0374     if (is_low)
0375         *val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
0376     else
0377         *val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
0378 
0379     return 0;
0380 }
0381 
0382 static const struct hwmon_channel_info *pvt_channel_info[] = {
0383     HWMON_CHANNEL_INFO(chip,
0384                HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
0385     HWMON_CHANNEL_INFO(temp,
0386                HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
0387                HWMON_T_MIN | HWMON_T_MIN_ALARM |
0388                HWMON_T_MAX | HWMON_T_MAX_ALARM |
0389                HWMON_T_OFFSET),
0390     HWMON_CHANNEL_INFO(in,
0391                HWMON_I_INPUT | HWMON_I_LABEL |
0392                HWMON_I_MIN | HWMON_I_MIN_ALARM |
0393                HWMON_I_MAX | HWMON_I_MAX_ALARM,
0394                HWMON_I_INPUT | HWMON_I_LABEL |
0395                HWMON_I_MIN | HWMON_I_MIN_ALARM |
0396                HWMON_I_MAX | HWMON_I_MAX_ALARM,
0397                HWMON_I_INPUT | HWMON_I_LABEL |
0398                HWMON_I_MIN | HWMON_I_MIN_ALARM |
0399                HWMON_I_MAX | HWMON_I_MAX_ALARM,
0400                HWMON_I_INPUT | HWMON_I_LABEL |
0401                HWMON_I_MIN | HWMON_I_MIN_ALARM |
0402                HWMON_I_MAX | HWMON_I_MAX_ALARM),
0403     NULL
0404 };
0405 
0406 #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
0407 
0408 static irqreturn_t pvt_hard_isr(int irq, void *data)
0409 {
0410     struct pvt_hwmon *pvt = data;
0411     struct pvt_cache *cache;
0412     u32 val;
0413 
0414     /*
0415      * Mask the DVALID interrupt so after exiting from the handler a
0416      * repeated conversion wouldn't happen.
0417      */
0418     pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
0419            PVT_INTR_DVALID);
0420 
0421     /*
0422      * Nothing special for alarm-less driver. Just read the data, update
0423      * the cache and notify a waiter of this event.
0424      */
0425     val = readl(pvt->regs + PVT_DATA);
0426     if (!(val & PVT_DATA_VALID)) {
0427         dev_err(pvt->dev, "Got IRQ when data isn't valid\n");
0428         return IRQ_HANDLED;
0429     }
0430 
0431     cache = &pvt->cache[pvt->sensor];
0432 
0433     WRITE_ONCE(cache->data, FIELD_GET(PVT_DATA_DATA_MASK, val));
0434 
0435     complete(&cache->conversion);
0436 
0437     return IRQ_HANDLED;
0438 }
0439 
0440 #define pvt_soft_isr NULL
0441 
0442 static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
0443 {
0444     return 0;
0445 }
0446 
0447 static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
0448 {
0449     return 0;
0450 }
0451 
0452 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0453              long *val)
0454 {
0455     struct pvt_cache *cache = &pvt->cache[type];
0456     unsigned long timeout;
0457     u32 data;
0458     int ret;
0459 
0460     /*
0461      * Lock PVT conversion interface until data cache is updated. The
0462      * data read procedure is following: set the requested PVT sensor
0463      * mode, enable IRQ and conversion, wait until conversion is finished,
0464      * then disable conversion and IRQ, and read the cached data.
0465      */
0466     ret = mutex_lock_interruptible(&pvt->iface_mtx);
0467     if (ret)
0468         return ret;
0469 
0470     pvt->sensor = type;
0471     pvt_set_mode(pvt, pvt_info[type].mode);
0472 
0473     /*
0474      * Unmask the DVALID interrupt and enable the sensors conversions.
0475      * Do the reverse procedure when conversion is done.
0476      */
0477     pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
0478     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
0479 
0480     /*
0481      * Wait with timeout since in case if the sensor is suddenly powered
0482      * down the request won't be completed and the caller will hang up on
0483      * this procedure until the power is back up again. Multiply the
0484      * timeout by the factor of two to prevent a false timeout.
0485      */
0486     timeout = 2 * usecs_to_jiffies(ktime_to_us(pvt->timeout));
0487     ret = wait_for_completion_timeout(&cache->conversion, timeout);
0488 
0489     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0490     pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
0491            PVT_INTR_DVALID);
0492 
0493     data = READ_ONCE(cache->data);
0494 
0495     mutex_unlock(&pvt->iface_mtx);
0496 
0497     if (!ret)
0498         return -ETIMEDOUT;
0499 
0500     if (type == PVT_TEMP)
0501         *val = polynomial_calc(&poly_N_to_temp, data);
0502     else
0503         *val = polynomial_calc(&poly_N_to_volt, data);
0504 
0505     return 0;
0506 }
0507 
0508 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0509               bool is_low, long *val)
0510 {
0511     return -EOPNOTSUPP;
0512 }
0513 
0514 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0515                bool is_low, long val)
0516 {
0517     return -EOPNOTSUPP;
0518 }
0519 
0520 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0521               bool is_low, long *val)
0522 {
0523     return -EOPNOTSUPP;
0524 }
0525 
0526 static const struct hwmon_channel_info *pvt_channel_info[] = {
0527     HWMON_CHANNEL_INFO(chip,
0528                HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
0529     HWMON_CHANNEL_INFO(temp,
0530                HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
0531                HWMON_T_OFFSET),
0532     HWMON_CHANNEL_INFO(in,
0533                HWMON_I_INPUT | HWMON_I_LABEL,
0534                HWMON_I_INPUT | HWMON_I_LABEL,
0535                HWMON_I_INPUT | HWMON_I_LABEL,
0536                HWMON_I_INPUT | HWMON_I_LABEL),
0537     NULL
0538 };
0539 
0540 #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
0541 
0542 static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type,
0543                           int ch)
0544 {
0545     switch (type) {
0546     case hwmon_temp:
0547         if (ch < 0 || ch >= PVT_TEMP_CHS)
0548             return false;
0549         break;
0550     case hwmon_in:
0551         if (ch < 0 || ch >= PVT_VOLT_CHS)
0552             return false;
0553         break;
0554     default:
0555         break;
0556     }
0557 
0558     /* The rest of the types are independent from the channel number. */
0559     return true;
0560 }
0561 
0562 static umode_t pvt_hwmon_is_visible(const void *data,
0563                     enum hwmon_sensor_types type,
0564                     u32 attr, int ch)
0565 {
0566     if (!pvt_hwmon_channel_is_valid(type, ch))
0567         return 0;
0568 
0569     switch (type) {
0570     case hwmon_chip:
0571         switch (attr) {
0572         case hwmon_chip_update_interval:
0573             return 0644;
0574         }
0575         break;
0576     case hwmon_temp:
0577         switch (attr) {
0578         case hwmon_temp_input:
0579         case hwmon_temp_type:
0580         case hwmon_temp_label:
0581             return 0444;
0582         case hwmon_temp_min:
0583         case hwmon_temp_max:
0584             return pvt_limit_is_visible(ch);
0585         case hwmon_temp_min_alarm:
0586         case hwmon_temp_max_alarm:
0587             return pvt_alarm_is_visible(ch);
0588         case hwmon_temp_offset:
0589             return 0644;
0590         }
0591         break;
0592     case hwmon_in:
0593         switch (attr) {
0594         case hwmon_in_input:
0595         case hwmon_in_label:
0596             return 0444;
0597         case hwmon_in_min:
0598         case hwmon_in_max:
0599             return pvt_limit_is_visible(PVT_VOLT + ch);
0600         case hwmon_in_min_alarm:
0601         case hwmon_in_max_alarm:
0602             return pvt_alarm_is_visible(PVT_VOLT + ch);
0603         }
0604         break;
0605     default:
0606         break;
0607     }
0608 
0609     return 0;
0610 }
0611 
0612 static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
0613 {
0614     u32 data;
0615 
0616     data = readl(pvt->regs + PVT_CTRL);
0617     *val = FIELD_GET(PVT_CTRL_TRIM_MASK, data) * PVT_TRIM_STEP;
0618 
0619     return 0;
0620 }
0621 
0622 static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
0623 {
0624     u32 trim;
0625     int ret;
0626 
0627     /*
0628      * Serialize trim update, since a part of the register is changed and
0629      * the controller is supposed to be disabled during this operation.
0630      */
0631     ret = mutex_lock_interruptible(&pvt->iface_mtx);
0632     if (ret)
0633         return ret;
0634 
0635     trim = pvt_calc_trim(val);
0636     pvt_set_trim(pvt, trim);
0637 
0638     mutex_unlock(&pvt->iface_mtx);
0639 
0640     return 0;
0641 }
0642 
0643 static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
0644 {
0645     int ret;
0646 
0647     ret = mutex_lock_interruptible(&pvt->iface_mtx);
0648     if (ret)
0649         return ret;
0650 
0651     /* Return the result in msec as hwmon sysfs interface requires. */
0652     *val = ktime_to_ms(pvt->timeout);
0653 
0654     mutex_unlock(&pvt->iface_mtx);
0655 
0656     return 0;
0657 }
0658 
0659 static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
0660 {
0661     unsigned long rate;
0662     ktime_t kt, cache;
0663     u32 data;
0664     int ret;
0665 
0666     rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
0667     if (!rate)
0668         return -ENODEV;
0669 
0670     /*
0671      * If alarms are enabled, the requested timeout must be divided
0672      * between all available sensors to have the requested delay
0673      * applicable to each individual sensor.
0674      */
0675     cache = kt = ms_to_ktime(val);
0676 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0677     kt = ktime_divns(kt, PVT_SENSORS_NUM);
0678 #endif
0679 
0680     /*
0681      * Subtract a constant lag, which always persists due to the limited
0682      * PVT sampling rate. Make sure the timeout is not negative.
0683      */
0684     kt = ktime_sub_ns(kt, PVT_TOUT_MIN);
0685     if (ktime_to_ns(kt) < 0)
0686         kt = ktime_set(0, 0);
0687 
0688     /*
0689      * Finally recalculate the timeout in terms of the reference clock
0690      * period.
0691      */
0692     data = ktime_divns(kt * rate, NSEC_PER_SEC);
0693 
0694     /*
0695      * Update the measurements delay, but lock the interface first, since
0696      * we have to disable PVT in order to have the new delay actually
0697      * updated.
0698      */
0699     ret = mutex_lock_interruptible(&pvt->iface_mtx);
0700     if (ret)
0701         return ret;
0702 
0703     pvt_set_tout(pvt, data);
0704     pvt->timeout = cache;
0705 
0706     mutex_unlock(&pvt->iface_mtx);
0707 
0708     return 0;
0709 }
0710 
0711 static int pvt_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
0712               u32 attr, int ch, long *val)
0713 {
0714     struct pvt_hwmon *pvt = dev_get_drvdata(dev);
0715 
0716     if (!pvt_hwmon_channel_is_valid(type, ch))
0717         return -EINVAL;
0718 
0719     switch (type) {
0720     case hwmon_chip:
0721         switch (attr) {
0722         case hwmon_chip_update_interval:
0723             return pvt_read_timeout(pvt, val);
0724         }
0725         break;
0726     case hwmon_temp:
0727         switch (attr) {
0728         case hwmon_temp_input:
0729             return pvt_read_data(pvt, ch, val);
0730         case hwmon_temp_type:
0731             *val = 1;
0732             return 0;
0733         case hwmon_temp_min:
0734             return pvt_read_limit(pvt, ch, true, val);
0735         case hwmon_temp_max:
0736             return pvt_read_limit(pvt, ch, false, val);
0737         case hwmon_temp_min_alarm:
0738             return pvt_read_alarm(pvt, ch, true, val);
0739         case hwmon_temp_max_alarm:
0740             return pvt_read_alarm(pvt, ch, false, val);
0741         case hwmon_temp_offset:
0742             return pvt_read_trim(pvt, val);
0743         }
0744         break;
0745     case hwmon_in:
0746         switch (attr) {
0747         case hwmon_in_input:
0748             return pvt_read_data(pvt, PVT_VOLT + ch, val);
0749         case hwmon_in_min:
0750             return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
0751         case hwmon_in_max:
0752             return pvt_read_limit(pvt, PVT_VOLT + ch, false, val);
0753         case hwmon_in_min_alarm:
0754             return pvt_read_alarm(pvt, PVT_VOLT + ch, true, val);
0755         case hwmon_in_max_alarm:
0756             return pvt_read_alarm(pvt, PVT_VOLT + ch, false, val);
0757         }
0758         break;
0759     default:
0760         break;
0761     }
0762 
0763     return -EOPNOTSUPP;
0764 }
0765 
0766 static int pvt_hwmon_read_string(struct device *dev,
0767                  enum hwmon_sensor_types type,
0768                  u32 attr, int ch, const char **str)
0769 {
0770     if (!pvt_hwmon_channel_is_valid(type, ch))
0771         return -EINVAL;
0772 
0773     switch (type) {
0774     case hwmon_temp:
0775         switch (attr) {
0776         case hwmon_temp_label:
0777             *str = pvt_info[ch].label;
0778             return 0;
0779         }
0780         break;
0781     case hwmon_in:
0782         switch (attr) {
0783         case hwmon_in_label:
0784             *str = pvt_info[PVT_VOLT + ch].label;
0785             return 0;
0786         }
0787         break;
0788     default:
0789         break;
0790     }
0791 
0792     return -EOPNOTSUPP;
0793 }
0794 
0795 static int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
0796                u32 attr, int ch, long val)
0797 {
0798     struct pvt_hwmon *pvt = dev_get_drvdata(dev);
0799 
0800     if (!pvt_hwmon_channel_is_valid(type, ch))
0801         return -EINVAL;
0802 
0803     switch (type) {
0804     case hwmon_chip:
0805         switch (attr) {
0806         case hwmon_chip_update_interval:
0807             return pvt_write_timeout(pvt, val);
0808         }
0809         break;
0810     case hwmon_temp:
0811         switch (attr) {
0812         case hwmon_temp_min:
0813             return pvt_write_limit(pvt, ch, true, val);
0814         case hwmon_temp_max:
0815             return pvt_write_limit(pvt, ch, false, val);
0816         case hwmon_temp_offset:
0817             return pvt_write_trim(pvt, val);
0818         }
0819         break;
0820     case hwmon_in:
0821         switch (attr) {
0822         case hwmon_in_min:
0823             return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
0824         case hwmon_in_max:
0825             return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
0826         }
0827         break;
0828     default:
0829         break;
0830     }
0831 
0832     return -EOPNOTSUPP;
0833 }
0834 
0835 static const struct hwmon_ops pvt_hwmon_ops = {
0836     .is_visible = pvt_hwmon_is_visible,
0837     .read = pvt_hwmon_read,
0838     .read_string = pvt_hwmon_read_string,
0839     .write = pvt_hwmon_write
0840 };
0841 
0842 static const struct hwmon_chip_info pvt_hwmon_info = {
0843     .ops = &pvt_hwmon_ops,
0844     .info = pvt_channel_info
0845 };
0846 
0847 static void pvt_clear_data(void *data)
0848 {
0849     struct pvt_hwmon *pvt = data;
0850 #if !defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0851     int idx;
0852 
0853     for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
0854         complete_all(&pvt->cache[idx].conversion);
0855 #endif
0856 
0857     mutex_destroy(&pvt->iface_mtx);
0858 }
0859 
0860 static struct pvt_hwmon *pvt_create_data(struct platform_device *pdev)
0861 {
0862     struct device *dev = &pdev->dev;
0863     struct pvt_hwmon *pvt;
0864     int ret, idx;
0865 
0866     pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
0867     if (!pvt)
0868         return ERR_PTR(-ENOMEM);
0869 
0870     ret = devm_add_action(dev, pvt_clear_data, pvt);
0871     if (ret) {
0872         dev_err(dev, "Can't add PVT data clear action\n");
0873         return ERR_PTR(ret);
0874     }
0875 
0876     pvt->dev = dev;
0877     pvt->sensor = PVT_SENSOR_FIRST;
0878     mutex_init(&pvt->iface_mtx);
0879 
0880 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0881     for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
0882         seqlock_init(&pvt->cache[idx].data_seqlock);
0883 #else
0884     for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
0885         init_completion(&pvt->cache[idx].conversion);
0886 #endif
0887 
0888     return pvt;
0889 }
0890 
0891 static int pvt_request_regs(struct pvt_hwmon *pvt)
0892 {
0893     struct platform_device *pdev = to_platform_device(pvt->dev);
0894     struct resource *res;
0895 
0896     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0897     if (!res) {
0898         dev_err(pvt->dev, "Couldn't find PVT memresource\n");
0899         return -EINVAL;
0900     }
0901 
0902     pvt->regs = devm_ioremap_resource(pvt->dev, res);
0903     if (IS_ERR(pvt->regs))
0904         return PTR_ERR(pvt->regs);
0905 
0906     return 0;
0907 }
0908 
0909 static void pvt_disable_clks(void *data)
0910 {
0911     struct pvt_hwmon *pvt = data;
0912 
0913     clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
0914 }
0915 
0916 static int pvt_request_clks(struct pvt_hwmon *pvt)
0917 {
0918     int ret;
0919 
0920     pvt->clks[PVT_CLOCK_APB].id = "pclk";
0921     pvt->clks[PVT_CLOCK_REF].id = "ref";
0922 
0923     ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
0924     if (ret) {
0925         dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
0926         return ret;
0927     }
0928 
0929     ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
0930     if (ret) {
0931         dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
0932         return ret;
0933     }
0934 
0935     ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
0936     if (ret) {
0937         dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
0938         return ret;
0939     }
0940 
0941     return 0;
0942 }
0943 
0944 static int pvt_check_pwr(struct pvt_hwmon *pvt)
0945 {
0946     unsigned long tout;
0947     int ret = 0;
0948     u32 data;
0949 
0950     /*
0951      * Test out the sensor conversion functionality. If it is not done on
0952      * time then the domain must have been unpowered and we won't be able
0953      * to use the device later in this driver.
0954      * Note If the power source is lost during the normal driver work the
0955      * data read procedure will either return -ETIMEDOUT (for the
0956      * alarm-less driver configuration) or just stop the repeated
0957      * conversion. In the later case alas we won't be able to detect the
0958      * problem.
0959      */
0960     pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
0961     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
0962     pvt_set_tout(pvt, 0);
0963     readl(pvt->regs + PVT_DATA);
0964 
0965     tout = PVT_TOUT_MIN / NSEC_PER_USEC;
0966     usleep_range(tout, 2 * tout);
0967 
0968     data = readl(pvt->regs + PVT_DATA);
0969     if (!(data & PVT_DATA_VALID)) {
0970         ret = -ENODEV;
0971         dev_err(pvt->dev, "Sensor is powered down\n");
0972     }
0973 
0974     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0975 
0976     return ret;
0977 }
0978 
0979 static int pvt_init_iface(struct pvt_hwmon *pvt)
0980 {
0981     unsigned long rate;
0982     u32 trim, temp;
0983 
0984     rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
0985     if (!rate) {
0986         dev_err(pvt->dev, "Invalid reference clock rate\n");
0987         return -ENODEV;
0988     }
0989 
0990     /*
0991      * Make sure all interrupts and controller are disabled so not to
0992      * accidentally have ISR executed before the driver data is fully
0993      * initialized. Clear the IRQ status as well.
0994      */
0995     pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
0996     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0997     readl(pvt->regs + PVT_CLR_INTR);
0998     readl(pvt->regs + PVT_DATA);
0999 
1000     /* Setup default sensor mode, timeout and temperature trim. */
1001     pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
1002     pvt_set_tout(pvt, PVT_TOUT_DEF);
1003 
1004     /*
1005      * Preserve the current ref-clock based delay (Ttotal) between the
1006      * sensors data samples in the driver data so not to recalculate it
1007      * each time on the data requests and timeout reads. It consists of the
1008      * delay introduced by the internal ref-clock timer (N / Fclk) and the
1009      * constant timeout caused by each conversion latency (Tmin):
1010      *   Ttotal = N / Fclk + Tmin
1011      * If alarms are enabled the sensors are polled one after another and
1012      * in order to get the next measurement of a particular sensor the
1013      * caller will have to wait for at most until all the others are
1014      * polled. In that case the formulae will look a bit different:
1015      *   Ttotal = 5 * (N / Fclk + Tmin)
1016      */
1017 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1018     pvt->timeout = ktime_set(PVT_SENSORS_NUM * PVT_TOUT_DEF, 0);
1019     pvt->timeout = ktime_divns(pvt->timeout, rate);
1020     pvt->timeout = ktime_add_ns(pvt->timeout, PVT_SENSORS_NUM * PVT_TOUT_MIN);
1021 #else
1022     pvt->timeout = ktime_set(PVT_TOUT_DEF, 0);
1023     pvt->timeout = ktime_divns(pvt->timeout, rate);
1024     pvt->timeout = ktime_add_ns(pvt->timeout, PVT_TOUT_MIN);
1025 #endif
1026 
1027     trim = PVT_TRIM_DEF;
1028     if (!of_property_read_u32(pvt->dev->of_node,
1029          "baikal,pvt-temp-offset-millicelsius", &temp))
1030         trim = pvt_calc_trim(temp);
1031 
1032     pvt_set_trim(pvt, trim);
1033 
1034     return 0;
1035 }
1036 
1037 static int pvt_request_irq(struct pvt_hwmon *pvt)
1038 {
1039     struct platform_device *pdev = to_platform_device(pvt->dev);
1040     int ret;
1041 
1042     pvt->irq = platform_get_irq(pdev, 0);
1043     if (pvt->irq < 0)
1044         return pvt->irq;
1045 
1046     ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
1047                     pvt_hard_isr, pvt_soft_isr,
1048 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1049                     IRQF_SHARED | IRQF_TRIGGER_HIGH |
1050                     IRQF_ONESHOT,
1051 #else
1052                     IRQF_SHARED | IRQF_TRIGGER_HIGH,
1053 #endif
1054                     "pvt", pvt);
1055     if (ret) {
1056         dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
1057         return ret;
1058     }
1059 
1060     return 0;
1061 }
1062 
1063 static int pvt_create_hwmon(struct pvt_hwmon *pvt)
1064 {
1065     pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
1066         &pvt_hwmon_info, NULL);
1067     if (IS_ERR(pvt->hwmon)) {
1068         dev_err(pvt->dev, "Couldn't create hwmon device\n");
1069         return PTR_ERR(pvt->hwmon);
1070     }
1071 
1072     return 0;
1073 }
1074 
1075 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1076 
1077 static void pvt_disable_iface(void *data)
1078 {
1079     struct pvt_hwmon *pvt = data;
1080 
1081     mutex_lock(&pvt->iface_mtx);
1082     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1083     pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
1084            PVT_INTR_DVALID);
1085     mutex_unlock(&pvt->iface_mtx);
1086 }
1087 
1088 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1089 {
1090     int ret;
1091 
1092     ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
1093     if (ret) {
1094         dev_err(pvt->dev, "Can't add PVT disable interface action\n");
1095         return ret;
1096     }
1097 
1098     /*
1099      * Enable sensors data conversion and IRQ. We need to lock the
1100      * interface mutex since hwmon has just been created and the
1101      * corresponding sysfs files are accessible from user-space,
1102      * which theoretically may cause races.
1103      */
1104     mutex_lock(&pvt->iface_mtx);
1105     pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
1106     pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
1107     mutex_unlock(&pvt->iface_mtx);
1108 
1109     return 0;
1110 }
1111 
1112 #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1113 
1114 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1115 {
1116     return 0;
1117 }
1118 
1119 #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1120 
1121 static int pvt_probe(struct platform_device *pdev)
1122 {
1123     struct pvt_hwmon *pvt;
1124     int ret;
1125 
1126     pvt = pvt_create_data(pdev);
1127     if (IS_ERR(pvt))
1128         return PTR_ERR(pvt);
1129 
1130     ret = pvt_request_regs(pvt);
1131     if (ret)
1132         return ret;
1133 
1134     ret = pvt_request_clks(pvt);
1135     if (ret)
1136         return ret;
1137 
1138     ret = pvt_check_pwr(pvt);
1139     if (ret)
1140         return ret;
1141 
1142     ret = pvt_init_iface(pvt);
1143     if (ret)
1144         return ret;
1145 
1146     ret = pvt_request_irq(pvt);
1147     if (ret)
1148         return ret;
1149 
1150     ret = pvt_create_hwmon(pvt);
1151     if (ret)
1152         return ret;
1153 
1154     ret = pvt_enable_iface(pvt);
1155     if (ret)
1156         return ret;
1157 
1158     return 0;
1159 }
1160 
1161 static const struct of_device_id pvt_of_match[] = {
1162     { .compatible = "baikal,bt1-pvt" },
1163     { }
1164 };
1165 MODULE_DEVICE_TABLE(of, pvt_of_match);
1166 
1167 static struct platform_driver pvt_driver = {
1168     .probe = pvt_probe,
1169     .driver = {
1170         .name = "bt1-pvt",
1171         .of_match_table = pvt_of_match
1172     }
1173 };
1174 module_platform_driver(pvt_driver);
1175 
1176 MODULE_AUTHOR("Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>");
1177 MODULE_DESCRIPTION("Baikal-T1 PVT driver");
1178 MODULE_LICENSE("GPL v2");