197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 272e9baf9SAmit Kucheria /* 30ffdab6fSVeera Vegivada * Copyright (c) 2011-2015, 2017, 2020, The Linux Foundation. All rights reserved. 472e9baf9SAmit Kucheria */ 572e9baf9SAmit Kucheria 672e9baf9SAmit Kucheria #include <linux/bitops.h> 772e9baf9SAmit Kucheria #include <linux/delay.h> 872e9baf9SAmit Kucheria #include <linux/err.h> 972e9baf9SAmit Kucheria #include <linux/iio/consumer.h> 1072e9baf9SAmit Kucheria #include <linux/interrupt.h> 1172e9baf9SAmit Kucheria #include <linux/module.h> 1272e9baf9SAmit Kucheria #include <linux/of.h> 1372e9baf9SAmit Kucheria #include <linux/of_device.h> 1472e9baf9SAmit Kucheria #include <linux/platform_device.h> 1572e9baf9SAmit Kucheria #include <linux/regmap.h> 1672e9baf9SAmit Kucheria #include <linux/thermal.h> 1772e9baf9SAmit Kucheria 1872e9baf9SAmit Kucheria #include "../thermal_core.h" 19fb55b012SDmitry Baryshkov #include "../thermal_hwmon.h" 2072e9baf9SAmit Kucheria 21aa92b331SDavid Collins #define QPNP_TM_REG_DIG_MAJOR 0x01 2272e9baf9SAmit Kucheria #define QPNP_TM_REG_TYPE 0x04 2372e9baf9SAmit Kucheria #define QPNP_TM_REG_SUBTYPE 0x05 2472e9baf9SAmit Kucheria #define QPNP_TM_REG_STATUS 0x08 2572e9baf9SAmit Kucheria #define QPNP_TM_REG_SHUTDOWN_CTRL1 0x40 2672e9baf9SAmit Kucheria #define QPNP_TM_REG_ALARM_CTRL 0x46 2772e9baf9SAmit Kucheria 2872e9baf9SAmit Kucheria #define QPNP_TM_TYPE 0x09 2972e9baf9SAmit Kucheria #define QPNP_TM_SUBTYPE_GEN1 0x08 3072e9baf9SAmit Kucheria #define QPNP_TM_SUBTYPE_GEN2 0x09 3172e9baf9SAmit Kucheria 3272e9baf9SAmit Kucheria #define STATUS_GEN1_STAGE_MASK GENMASK(1, 0) 3372e9baf9SAmit Kucheria #define STATUS_GEN2_STATE_MASK GENMASK(6, 4) 3472e9baf9SAmit Kucheria #define STATUS_GEN2_STATE_SHIFT 4 3572e9baf9SAmit Kucheria 3672e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_OVERRIDE_S2 BIT(6) 3772e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0) 3872e9baf9SAmit Kucheria 3972e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_RATE_25HZ BIT(3) 4072e9baf9SAmit Kucheria 4172e9baf9SAmit Kucheria #define ALARM_CTRL_FORCE_ENABLE BIT(7) 4272e9baf9SAmit Kucheria 43aa92b331SDavid Collins #define THRESH_COUNT 4 44aa92b331SDavid Collins #define STAGE_COUNT 3 4572e9baf9SAmit Kucheria 46aa92b331SDavid Collins /* Over-temperature trip point values in mC */ 47aa92b331SDavid Collins static const long temp_map_gen1[THRESH_COUNT][STAGE_COUNT] = { 48aa92b331SDavid Collins { 105000, 125000, 145000 }, 49aa92b331SDavid Collins { 110000, 130000, 150000 }, 50aa92b331SDavid Collins { 115000, 135000, 155000 }, 51aa92b331SDavid Collins { 120000, 140000, 160000 }, 52aa92b331SDavid Collins }; 53aa92b331SDavid Collins 54aa92b331SDavid Collins static const long temp_map_gen2_v1[THRESH_COUNT][STAGE_COUNT] = { 55aa92b331SDavid Collins { 90000, 110000, 140000 }, 56aa92b331SDavid Collins { 95000, 115000, 145000 }, 57aa92b331SDavid Collins { 100000, 120000, 150000 }, 58aa92b331SDavid Collins { 105000, 125000, 155000 }, 59aa92b331SDavid Collins }; 60aa92b331SDavid Collins 6172e9baf9SAmit Kucheria #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */ 6272e9baf9SAmit Kucheria 6372e9baf9SAmit Kucheria #define THRESH_MIN 0 6472e9baf9SAmit Kucheria #define THRESH_MAX 3 6572e9baf9SAmit Kucheria 66aa92b331SDavid Collins #define TEMP_STAGE_HYSTERESIS 2000 6772e9baf9SAmit Kucheria 6872e9baf9SAmit Kucheria /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */ 6972e9baf9SAmit Kucheria #define DEFAULT_TEMP 37000 7072e9baf9SAmit Kucheria 7172e9baf9SAmit Kucheria struct qpnp_tm_chip { 7272e9baf9SAmit Kucheria struct regmap *map; 7372e9baf9SAmit Kucheria struct device *dev; 7472e9baf9SAmit Kucheria struct thermal_zone_device *tz_dev; 7572e9baf9SAmit Kucheria unsigned int subtype; 7672e9baf9SAmit Kucheria long temp; 7772e9baf9SAmit Kucheria unsigned int thresh; 7872e9baf9SAmit Kucheria unsigned int stage; 7972e9baf9SAmit Kucheria unsigned int prev_stage; 8072e9baf9SAmit Kucheria unsigned int base; 8172e9baf9SAmit Kucheria /* protects .thresh, .stage and chip registers */ 8272e9baf9SAmit Kucheria struct mutex lock; 8372e9baf9SAmit Kucheria bool initialized; 8472e9baf9SAmit Kucheria 8572e9baf9SAmit Kucheria struct iio_channel *adc; 86aa92b331SDavid Collins const long (*temp_map)[THRESH_COUNT][STAGE_COUNT]; 8772e9baf9SAmit Kucheria }; 8872e9baf9SAmit Kucheria 8972e9baf9SAmit Kucheria /* This array maps from GEN2 alarm state to GEN1 alarm stage */ 9072e9baf9SAmit Kucheria static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3}; 9172e9baf9SAmit Kucheria 9272e9baf9SAmit Kucheria static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data) 9372e9baf9SAmit Kucheria { 9472e9baf9SAmit Kucheria unsigned int val; 9572e9baf9SAmit Kucheria int ret; 9672e9baf9SAmit Kucheria 9772e9baf9SAmit Kucheria ret = regmap_read(chip->map, chip->base + addr, &val); 9872e9baf9SAmit Kucheria if (ret < 0) 9972e9baf9SAmit Kucheria return ret; 10072e9baf9SAmit Kucheria 10172e9baf9SAmit Kucheria *data = val; 10272e9baf9SAmit Kucheria return 0; 10372e9baf9SAmit Kucheria } 10472e9baf9SAmit Kucheria 10572e9baf9SAmit Kucheria static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data) 10672e9baf9SAmit Kucheria { 10772e9baf9SAmit Kucheria return regmap_write(chip->map, chip->base + addr, data); 10872e9baf9SAmit Kucheria } 10972e9baf9SAmit Kucheria 11072e9baf9SAmit Kucheria /** 111aa92b331SDavid Collins * qpnp_tm_decode_temp() - return temperature in mC corresponding to the 112aa92b331SDavid Collins * specified over-temperature stage 113aa92b331SDavid Collins * @chip: Pointer to the qpnp_tm chip 114aa92b331SDavid Collins * @stage: Over-temperature stage 115aa92b331SDavid Collins * 116aa92b331SDavid Collins * Return: temperature in mC 117aa92b331SDavid Collins */ 118aa92b331SDavid Collins static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage) 119aa92b331SDavid Collins { 120aa92b331SDavid Collins if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 || 121aa92b331SDavid Collins stage > STAGE_COUNT) 122aa92b331SDavid Collins return 0; 123aa92b331SDavid Collins 124aa92b331SDavid Collins return (*chip->temp_map)[chip->thresh][stage - 1]; 125aa92b331SDavid Collins } 126aa92b331SDavid Collins 127aa92b331SDavid Collins /** 12872e9baf9SAmit Kucheria * qpnp_tm_get_temp_stage() - return over-temperature stage 12972e9baf9SAmit Kucheria * @chip: Pointer to the qpnp_tm chip 13072e9baf9SAmit Kucheria * 13172e9baf9SAmit Kucheria * Return: stage (GEN1) or state (GEN2) on success, or errno on failure. 13272e9baf9SAmit Kucheria */ 13372e9baf9SAmit Kucheria static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip) 13472e9baf9SAmit Kucheria { 13572e9baf9SAmit Kucheria int ret; 13672e9baf9SAmit Kucheria u8 reg = 0; 13772e9baf9SAmit Kucheria 13872e9baf9SAmit Kucheria ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, ®); 13972e9baf9SAmit Kucheria if (ret < 0) 14072e9baf9SAmit Kucheria return ret; 14172e9baf9SAmit Kucheria 14272e9baf9SAmit Kucheria if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) 14372e9baf9SAmit Kucheria ret = reg & STATUS_GEN1_STAGE_MASK; 14472e9baf9SAmit Kucheria else 14572e9baf9SAmit Kucheria ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT; 14672e9baf9SAmit Kucheria 14772e9baf9SAmit Kucheria return ret; 14872e9baf9SAmit Kucheria } 14972e9baf9SAmit Kucheria 15072e9baf9SAmit Kucheria /* 15172e9baf9SAmit Kucheria * This function updates the internal temp value based on the 15272e9baf9SAmit Kucheria * current thermal stage and threshold as well as the previous stage 15372e9baf9SAmit Kucheria */ 15472e9baf9SAmit Kucheria static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip) 15572e9baf9SAmit Kucheria { 15672e9baf9SAmit Kucheria unsigned int stage, stage_new, stage_old; 15772e9baf9SAmit Kucheria int ret; 15872e9baf9SAmit Kucheria 15972e9baf9SAmit Kucheria WARN_ON(!mutex_is_locked(&chip->lock)); 16072e9baf9SAmit Kucheria 16172e9baf9SAmit Kucheria ret = qpnp_tm_get_temp_stage(chip); 16272e9baf9SAmit Kucheria if (ret < 0) 16372e9baf9SAmit Kucheria return ret; 16472e9baf9SAmit Kucheria stage = ret; 16572e9baf9SAmit Kucheria 16672e9baf9SAmit Kucheria if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) { 16772e9baf9SAmit Kucheria stage_new = stage; 16872e9baf9SAmit Kucheria stage_old = chip->stage; 16972e9baf9SAmit Kucheria } else { 17072e9baf9SAmit Kucheria stage_new = alarm_state_map[stage]; 17172e9baf9SAmit Kucheria stage_old = alarm_state_map[chip->stage]; 17272e9baf9SAmit Kucheria } 17372e9baf9SAmit Kucheria 17472e9baf9SAmit Kucheria if (stage_new > stage_old) { 17572e9baf9SAmit Kucheria /* increasing stage, use lower bound */ 176aa92b331SDavid Collins chip->temp = qpnp_tm_decode_temp(chip, stage_new) 177aa92b331SDavid Collins + TEMP_STAGE_HYSTERESIS; 17872e9baf9SAmit Kucheria } else if (stage_new < stage_old) { 17972e9baf9SAmit Kucheria /* decreasing stage, use upper bound */ 180aa92b331SDavid Collins chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1) 181aa92b331SDavid Collins - TEMP_STAGE_HYSTERESIS; 18272e9baf9SAmit Kucheria } 18372e9baf9SAmit Kucheria 18472e9baf9SAmit Kucheria chip->stage = stage; 18572e9baf9SAmit Kucheria 18672e9baf9SAmit Kucheria return 0; 18772e9baf9SAmit Kucheria } 18872e9baf9SAmit Kucheria 189ca1b9a9eSDaniel Lezcano static int qpnp_tm_get_temp(struct thermal_zone_device *tz, int *temp) 19072e9baf9SAmit Kucheria { 191ca1b9a9eSDaniel Lezcano struct qpnp_tm_chip *chip = tz->devdata; 19272e9baf9SAmit Kucheria int ret, mili_celsius; 19372e9baf9SAmit Kucheria 19472e9baf9SAmit Kucheria if (!temp) 19572e9baf9SAmit Kucheria return -EINVAL; 19672e9baf9SAmit Kucheria 19772e9baf9SAmit Kucheria if (!chip->initialized) { 19872e9baf9SAmit Kucheria *temp = DEFAULT_TEMP; 19972e9baf9SAmit Kucheria return 0; 20072e9baf9SAmit Kucheria } 20172e9baf9SAmit Kucheria 20272e9baf9SAmit Kucheria if (!chip->adc) { 20372e9baf9SAmit Kucheria mutex_lock(&chip->lock); 20472e9baf9SAmit Kucheria ret = qpnp_tm_update_temp_no_adc(chip); 20572e9baf9SAmit Kucheria mutex_unlock(&chip->lock); 20672e9baf9SAmit Kucheria if (ret < 0) 20772e9baf9SAmit Kucheria return ret; 20872e9baf9SAmit Kucheria } else { 20972e9baf9SAmit Kucheria ret = iio_read_channel_processed(chip->adc, &mili_celsius); 21072e9baf9SAmit Kucheria if (ret < 0) 21172e9baf9SAmit Kucheria return ret; 21272e9baf9SAmit Kucheria 21372e9baf9SAmit Kucheria chip->temp = mili_celsius; 21472e9baf9SAmit Kucheria } 21572e9baf9SAmit Kucheria 2160ffdab6fSVeera Vegivada *temp = chip->temp; 21772e9baf9SAmit Kucheria 21872e9baf9SAmit Kucheria return 0; 21972e9baf9SAmit Kucheria } 22072e9baf9SAmit Kucheria 22172e9baf9SAmit Kucheria static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip, 22272e9baf9SAmit Kucheria int temp) 22372e9baf9SAmit Kucheria { 224aa92b331SDavid Collins long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1]; 225aa92b331SDavid Collins long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1]; 22672e9baf9SAmit Kucheria bool disable_s2_shutdown = false; 227aa92b331SDavid Collins u8 reg; 22872e9baf9SAmit Kucheria 22972e9baf9SAmit Kucheria WARN_ON(!mutex_is_locked(&chip->lock)); 23072e9baf9SAmit Kucheria 23172e9baf9SAmit Kucheria /* 23272e9baf9SAmit Kucheria * Default: S2 and S3 shutdown enabled, thresholds at 233aa92b331SDavid Collins * lowest threshold set, monitoring at 25Hz 23472e9baf9SAmit Kucheria */ 23572e9baf9SAmit Kucheria reg = SHUTDOWN_CTRL1_RATE_25HZ; 23672e9baf9SAmit Kucheria 23772e9baf9SAmit Kucheria if (temp == THERMAL_TEMP_INVALID || 238aa92b331SDavid Collins temp < stage2_threshold_min) { 23972e9baf9SAmit Kucheria chip->thresh = THRESH_MIN; 24072e9baf9SAmit Kucheria goto skip; 24172e9baf9SAmit Kucheria } 24272e9baf9SAmit Kucheria 243aa92b331SDavid Collins if (temp <= stage2_threshold_max) { 24472e9baf9SAmit Kucheria chip->thresh = THRESH_MAX - 245aa92b331SDavid Collins ((stage2_threshold_max - temp) / 24672e9baf9SAmit Kucheria TEMP_THRESH_STEP); 24772e9baf9SAmit Kucheria disable_s2_shutdown = true; 24872e9baf9SAmit Kucheria } else { 24972e9baf9SAmit Kucheria chip->thresh = THRESH_MAX; 25072e9baf9SAmit Kucheria 25172e9baf9SAmit Kucheria if (chip->adc) 25272e9baf9SAmit Kucheria disable_s2_shutdown = true; 25372e9baf9SAmit Kucheria else 25472e9baf9SAmit Kucheria dev_warn(chip->dev, 2558763f8acSLuca Weiss "No ADC is configured and critical temperature %d mC is above the maximum stage 2 threshold of %ld mC! Configuring stage 2 shutdown at %ld mC.\n", 2568763f8acSLuca Weiss temp, stage2_threshold_max, stage2_threshold_max); 25772e9baf9SAmit Kucheria } 25872e9baf9SAmit Kucheria 25972e9baf9SAmit Kucheria skip: 26072e9baf9SAmit Kucheria reg |= chip->thresh; 26172e9baf9SAmit Kucheria if (disable_s2_shutdown) 26272e9baf9SAmit Kucheria reg |= SHUTDOWN_CTRL1_OVERRIDE_S2; 26372e9baf9SAmit Kucheria 26472e9baf9SAmit Kucheria return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg); 26572e9baf9SAmit Kucheria } 26672e9baf9SAmit Kucheria 2671fa86b0aSDaniel Lezcano static int qpnp_tm_set_trip_temp(struct thermal_zone_device *tz, int trip_id, int temp) 26872e9baf9SAmit Kucheria { 269ca1b9a9eSDaniel Lezcano struct qpnp_tm_chip *chip = tz->devdata; 2701fa86b0aSDaniel Lezcano struct thermal_trip trip; 27172e9baf9SAmit Kucheria int ret; 27272e9baf9SAmit Kucheria 273e6ec64f8SJohan Hovold ret = __thermal_zone_get_trip(chip->tz_dev, trip_id, &trip); 2741fa86b0aSDaniel Lezcano if (ret) 2751fa86b0aSDaniel Lezcano return ret; 27672e9baf9SAmit Kucheria 2771fa86b0aSDaniel Lezcano if (trip.type != THERMAL_TRIP_CRITICAL) 27872e9baf9SAmit Kucheria return 0; 27972e9baf9SAmit Kucheria 28072e9baf9SAmit Kucheria mutex_lock(&chip->lock); 28172e9baf9SAmit Kucheria ret = qpnp_tm_update_critical_trip_temp(chip, temp); 28272e9baf9SAmit Kucheria mutex_unlock(&chip->lock); 28372e9baf9SAmit Kucheria 28472e9baf9SAmit Kucheria return ret; 28572e9baf9SAmit Kucheria } 28672e9baf9SAmit Kucheria 287ca1b9a9eSDaniel Lezcano static const struct thermal_zone_device_ops qpnp_tm_sensor_ops = { 28872e9baf9SAmit Kucheria .get_temp = qpnp_tm_get_temp, 28972e9baf9SAmit Kucheria .set_trip_temp = qpnp_tm_set_trip_temp, 29072e9baf9SAmit Kucheria }; 29172e9baf9SAmit Kucheria 29272e9baf9SAmit Kucheria static irqreturn_t qpnp_tm_isr(int irq, void *data) 29372e9baf9SAmit Kucheria { 29472e9baf9SAmit Kucheria struct qpnp_tm_chip *chip = data; 29572e9baf9SAmit Kucheria 29672e9baf9SAmit Kucheria thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); 29772e9baf9SAmit Kucheria 29872e9baf9SAmit Kucheria return IRQ_HANDLED; 29972e9baf9SAmit Kucheria } 30072e9baf9SAmit Kucheria 30172e9baf9SAmit Kucheria static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip) 30272e9baf9SAmit Kucheria { 3031fa86b0aSDaniel Lezcano struct thermal_trip trip; 3041fa86b0aSDaniel Lezcano int i, ret; 30572e9baf9SAmit Kucheria 3061fa86b0aSDaniel Lezcano for (i = 0; i < thermal_zone_get_num_trips(chip->tz_dev); i++) { 30772e9baf9SAmit Kucheria 3081fa86b0aSDaniel Lezcano ret = thermal_zone_get_trip(chip->tz_dev, i, &trip); 3091fa86b0aSDaniel Lezcano if (ret) 3101fa86b0aSDaniel Lezcano continue; 31172e9baf9SAmit Kucheria 3121fa86b0aSDaniel Lezcano if (trip.type == THERMAL_TRIP_CRITICAL) 3131fa86b0aSDaniel Lezcano return trip.temperature; 31472e9baf9SAmit Kucheria } 31572e9baf9SAmit Kucheria 31672e9baf9SAmit Kucheria return THERMAL_TEMP_INVALID; 31772e9baf9SAmit Kucheria } 31872e9baf9SAmit Kucheria 31972e9baf9SAmit Kucheria /* 32072e9baf9SAmit Kucheria * This function initializes the internal temp value based on only the 32172e9baf9SAmit Kucheria * current thermal stage and threshold. Setup threshold control and 32272e9baf9SAmit Kucheria * disable shutdown override. 32372e9baf9SAmit Kucheria */ 32472e9baf9SAmit Kucheria static int qpnp_tm_init(struct qpnp_tm_chip *chip) 32572e9baf9SAmit Kucheria { 32672e9baf9SAmit Kucheria unsigned int stage; 32772e9baf9SAmit Kucheria int ret; 32872e9baf9SAmit Kucheria u8 reg = 0; 32972e9baf9SAmit Kucheria int crit_temp; 33072e9baf9SAmit Kucheria 33172e9baf9SAmit Kucheria mutex_lock(&chip->lock); 33272e9baf9SAmit Kucheria 33372e9baf9SAmit Kucheria ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, ®); 33472e9baf9SAmit Kucheria if (ret < 0) 33572e9baf9SAmit Kucheria goto out; 33672e9baf9SAmit Kucheria 33772e9baf9SAmit Kucheria chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK; 33872e9baf9SAmit Kucheria chip->temp = DEFAULT_TEMP; 33972e9baf9SAmit Kucheria 34072e9baf9SAmit Kucheria ret = qpnp_tm_get_temp_stage(chip); 34172e9baf9SAmit Kucheria if (ret < 0) 34272e9baf9SAmit Kucheria goto out; 34372e9baf9SAmit Kucheria chip->stage = ret; 34472e9baf9SAmit Kucheria 34572e9baf9SAmit Kucheria stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1 34672e9baf9SAmit Kucheria ? chip->stage : alarm_state_map[chip->stage]; 34772e9baf9SAmit Kucheria 34872e9baf9SAmit Kucheria if (stage) 349aa92b331SDavid Collins chip->temp = qpnp_tm_decode_temp(chip, stage); 35072e9baf9SAmit Kucheria 351*59edcd91SJohan Hovold mutex_unlock(&chip->lock); 352*59edcd91SJohan Hovold 35372e9baf9SAmit Kucheria crit_temp = qpnp_tm_get_critical_trip_temp(chip); 354*59edcd91SJohan Hovold 355*59edcd91SJohan Hovold mutex_lock(&chip->lock); 356*59edcd91SJohan Hovold 35772e9baf9SAmit Kucheria ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp); 35872e9baf9SAmit Kucheria if (ret < 0) 35972e9baf9SAmit Kucheria goto out; 36072e9baf9SAmit Kucheria 36172e9baf9SAmit Kucheria /* Enable the thermal alarm PMIC module in always-on mode. */ 36272e9baf9SAmit Kucheria reg = ALARM_CTRL_FORCE_ENABLE; 36372e9baf9SAmit Kucheria ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg); 36472e9baf9SAmit Kucheria 36572e9baf9SAmit Kucheria chip->initialized = true; 36672e9baf9SAmit Kucheria 36772e9baf9SAmit Kucheria out: 36872e9baf9SAmit Kucheria mutex_unlock(&chip->lock); 36972e9baf9SAmit Kucheria return ret; 37072e9baf9SAmit Kucheria } 37172e9baf9SAmit Kucheria 37272e9baf9SAmit Kucheria static int qpnp_tm_probe(struct platform_device *pdev) 37372e9baf9SAmit Kucheria { 37472e9baf9SAmit Kucheria struct qpnp_tm_chip *chip; 37572e9baf9SAmit Kucheria struct device_node *node; 376aa92b331SDavid Collins u8 type, subtype, dig_major; 37772e9baf9SAmit Kucheria u32 res; 37872e9baf9SAmit Kucheria int ret, irq; 37972e9baf9SAmit Kucheria 38072e9baf9SAmit Kucheria node = pdev->dev.of_node; 38172e9baf9SAmit Kucheria 38272e9baf9SAmit Kucheria chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); 38372e9baf9SAmit Kucheria if (!chip) 38472e9baf9SAmit Kucheria return -ENOMEM; 38572e9baf9SAmit Kucheria 38672e9baf9SAmit Kucheria dev_set_drvdata(&pdev->dev, chip); 38772e9baf9SAmit Kucheria chip->dev = &pdev->dev; 38872e9baf9SAmit Kucheria 38972e9baf9SAmit Kucheria mutex_init(&chip->lock); 39072e9baf9SAmit Kucheria 39172e9baf9SAmit Kucheria chip->map = dev_get_regmap(pdev->dev.parent, NULL); 39272e9baf9SAmit Kucheria if (!chip->map) 39372e9baf9SAmit Kucheria return -ENXIO; 39472e9baf9SAmit Kucheria 39572e9baf9SAmit Kucheria ret = of_property_read_u32(node, "reg", &res); 39672e9baf9SAmit Kucheria if (ret < 0) 39772e9baf9SAmit Kucheria return ret; 39872e9baf9SAmit Kucheria 39972e9baf9SAmit Kucheria irq = platform_get_irq(pdev, 0); 40072e9baf9SAmit Kucheria if (irq < 0) 40172e9baf9SAmit Kucheria return irq; 40272e9baf9SAmit Kucheria 40372e9baf9SAmit Kucheria /* ADC based measurements are optional */ 40472e9baf9SAmit Kucheria chip->adc = devm_iio_channel_get(&pdev->dev, "thermal"); 40572e9baf9SAmit Kucheria if (IS_ERR(chip->adc)) { 40672e9baf9SAmit Kucheria ret = PTR_ERR(chip->adc); 40772e9baf9SAmit Kucheria chip->adc = NULL; 40872e9baf9SAmit Kucheria if (ret == -EPROBE_DEFER) 40972e9baf9SAmit Kucheria return ret; 41072e9baf9SAmit Kucheria } 41172e9baf9SAmit Kucheria 41272e9baf9SAmit Kucheria chip->base = res; 41372e9baf9SAmit Kucheria 41472e9baf9SAmit Kucheria ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type); 41572e9baf9SAmit Kucheria if (ret < 0) { 41672e9baf9SAmit Kucheria dev_err(&pdev->dev, "could not read type\n"); 41772e9baf9SAmit Kucheria return ret; 41872e9baf9SAmit Kucheria } 41972e9baf9SAmit Kucheria 42072e9baf9SAmit Kucheria ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype); 42172e9baf9SAmit Kucheria if (ret < 0) { 42272e9baf9SAmit Kucheria dev_err(&pdev->dev, "could not read subtype\n"); 42372e9baf9SAmit Kucheria return ret; 42472e9baf9SAmit Kucheria } 42572e9baf9SAmit Kucheria 426aa92b331SDavid Collins ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major); 427aa92b331SDavid Collins if (ret < 0) { 428aa92b331SDavid Collins dev_err(&pdev->dev, "could not read dig_major\n"); 429aa92b331SDavid Collins return ret; 430aa92b331SDavid Collins } 431aa92b331SDavid Collins 43272e9baf9SAmit Kucheria if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1 43372e9baf9SAmit Kucheria && subtype != QPNP_TM_SUBTYPE_GEN2)) { 43472e9baf9SAmit Kucheria dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n", 43572e9baf9SAmit Kucheria type, subtype); 43672e9baf9SAmit Kucheria return -ENODEV; 43772e9baf9SAmit Kucheria } 43872e9baf9SAmit Kucheria 43972e9baf9SAmit Kucheria chip->subtype = subtype; 440aa92b331SDavid Collins if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 1) 441aa92b331SDavid Collins chip->temp_map = &temp_map_gen2_v1; 442aa92b331SDavid Collins else 443aa92b331SDavid Collins chip->temp_map = &temp_map_gen1; 44472e9baf9SAmit Kucheria 44572e9baf9SAmit Kucheria /* 44672e9baf9SAmit Kucheria * Register the sensor before initializing the hardware to be able to 44772e9baf9SAmit Kucheria * read the trip points. get_temp() returns the default temperature 44872e9baf9SAmit Kucheria * before the hardware initialization is completed. 44972e9baf9SAmit Kucheria */ 450ca1b9a9eSDaniel Lezcano chip->tz_dev = devm_thermal_of_zone_register( 45172e9baf9SAmit Kucheria &pdev->dev, 0, chip, &qpnp_tm_sensor_ops); 45272e9baf9SAmit Kucheria if (IS_ERR(chip->tz_dev)) { 45372e9baf9SAmit Kucheria dev_err(&pdev->dev, "failed to register sensor\n"); 45472e9baf9SAmit Kucheria return PTR_ERR(chip->tz_dev); 45572e9baf9SAmit Kucheria } 45672e9baf9SAmit Kucheria 45772e9baf9SAmit Kucheria ret = qpnp_tm_init(chip); 45872e9baf9SAmit Kucheria if (ret < 0) { 45972e9baf9SAmit Kucheria dev_err(&pdev->dev, "init failed\n"); 46072e9baf9SAmit Kucheria return ret; 46172e9baf9SAmit Kucheria } 46272e9baf9SAmit Kucheria 463fb55b012SDmitry Baryshkov if (devm_thermal_add_hwmon_sysfs(chip->tz_dev)) 464fb55b012SDmitry Baryshkov dev_warn(&pdev->dev, 465fb55b012SDmitry Baryshkov "Failed to add hwmon sysfs attributes\n"); 466fb55b012SDmitry Baryshkov 46772e9baf9SAmit Kucheria ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr, 46872e9baf9SAmit Kucheria IRQF_ONESHOT, node->name, chip); 46972e9baf9SAmit Kucheria if (ret < 0) 47072e9baf9SAmit Kucheria return ret; 47172e9baf9SAmit Kucheria 47272e9baf9SAmit Kucheria thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); 47372e9baf9SAmit Kucheria 47472e9baf9SAmit Kucheria return 0; 47572e9baf9SAmit Kucheria } 47672e9baf9SAmit Kucheria 47772e9baf9SAmit Kucheria static const struct of_device_id qpnp_tm_match_table[] = { 47872e9baf9SAmit Kucheria { .compatible = "qcom,spmi-temp-alarm" }, 47972e9baf9SAmit Kucheria { } 48072e9baf9SAmit Kucheria }; 48172e9baf9SAmit Kucheria MODULE_DEVICE_TABLE(of, qpnp_tm_match_table); 48272e9baf9SAmit Kucheria 48372e9baf9SAmit Kucheria static struct platform_driver qpnp_tm_driver = { 48472e9baf9SAmit Kucheria .driver = { 48572e9baf9SAmit Kucheria .name = "spmi-temp-alarm", 48672e9baf9SAmit Kucheria .of_match_table = qpnp_tm_match_table, 48772e9baf9SAmit Kucheria }, 48872e9baf9SAmit Kucheria .probe = qpnp_tm_probe, 48972e9baf9SAmit Kucheria }; 49072e9baf9SAmit Kucheria module_platform_driver(qpnp_tm_driver); 49172e9baf9SAmit Kucheria 49272e9baf9SAmit Kucheria MODULE_ALIAS("platform:spmi-temp-alarm"); 49372e9baf9SAmit Kucheria MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver"); 49472e9baf9SAmit Kucheria MODULE_LICENSE("GPL v2"); 495