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 18fb55b012SDmitry Baryshkov #include "../thermal_hwmon.h" 1972e9baf9SAmit Kucheria 20aa92b331SDavid Collins #define QPNP_TM_REG_DIG_MAJOR 0x01 2172e9baf9SAmit Kucheria #define QPNP_TM_REG_TYPE 0x04 2272e9baf9SAmit Kucheria #define QPNP_TM_REG_SUBTYPE 0x05 2372e9baf9SAmit Kucheria #define QPNP_TM_REG_STATUS 0x08 2472e9baf9SAmit Kucheria #define QPNP_TM_REG_SHUTDOWN_CTRL1 0x40 2572e9baf9SAmit Kucheria #define QPNP_TM_REG_ALARM_CTRL 0x46 2672e9baf9SAmit Kucheria 2772e9baf9SAmit Kucheria #define QPNP_TM_TYPE 0x09 2872e9baf9SAmit Kucheria #define QPNP_TM_SUBTYPE_GEN1 0x08 2972e9baf9SAmit Kucheria #define QPNP_TM_SUBTYPE_GEN2 0x09 3072e9baf9SAmit Kucheria 3172e9baf9SAmit Kucheria #define STATUS_GEN1_STAGE_MASK GENMASK(1, 0) 3272e9baf9SAmit Kucheria #define STATUS_GEN2_STATE_MASK GENMASK(6, 4) 3372e9baf9SAmit Kucheria #define STATUS_GEN2_STATE_SHIFT 4 3472e9baf9SAmit Kucheria 3572e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_OVERRIDE_S2 BIT(6) 3672e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0) 3772e9baf9SAmit Kucheria 3872e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_RATE_25HZ BIT(3) 3972e9baf9SAmit Kucheria 4072e9baf9SAmit Kucheria #define ALARM_CTRL_FORCE_ENABLE BIT(7) 4172e9baf9SAmit Kucheria 42aa92b331SDavid Collins #define THRESH_COUNT 4 43aa92b331SDavid Collins #define STAGE_COUNT 3 4472e9baf9SAmit Kucheria 45aa92b331SDavid Collins /* Over-temperature trip point values in mC */ 46aa92b331SDavid Collins static const long temp_map_gen1[THRESH_COUNT][STAGE_COUNT] = { 47aa92b331SDavid Collins { 105000, 125000, 145000 }, 48aa92b331SDavid Collins { 110000, 130000, 150000 }, 49aa92b331SDavid Collins { 115000, 135000, 155000 }, 50aa92b331SDavid Collins { 120000, 140000, 160000 }, 51aa92b331SDavid Collins }; 52aa92b331SDavid Collins 53aa92b331SDavid Collins static const long temp_map_gen2_v1[THRESH_COUNT][STAGE_COUNT] = { 54aa92b331SDavid Collins { 90000, 110000, 140000 }, 55aa92b331SDavid Collins { 95000, 115000, 145000 }, 56aa92b331SDavid Collins { 100000, 120000, 150000 }, 57aa92b331SDavid Collins { 105000, 125000, 155000 }, 58aa92b331SDavid Collins }; 59aa92b331SDavid Collins 6072e9baf9SAmit Kucheria #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */ 6172e9baf9SAmit Kucheria 6272e9baf9SAmit Kucheria #define THRESH_MIN 0 6372e9baf9SAmit Kucheria #define THRESH_MAX 3 6472e9baf9SAmit Kucheria 65aa92b331SDavid Collins #define TEMP_STAGE_HYSTERESIS 2000 6672e9baf9SAmit Kucheria 6772e9baf9SAmit Kucheria /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */ 6872e9baf9SAmit Kucheria #define DEFAULT_TEMP 37000 6972e9baf9SAmit Kucheria 7072e9baf9SAmit Kucheria struct qpnp_tm_chip { 7172e9baf9SAmit Kucheria struct regmap *map; 7272e9baf9SAmit Kucheria struct device *dev; 7372e9baf9SAmit Kucheria struct thermal_zone_device *tz_dev; 7472e9baf9SAmit Kucheria unsigned int subtype; 7572e9baf9SAmit Kucheria long temp; 7672e9baf9SAmit Kucheria unsigned int thresh; 7772e9baf9SAmit Kucheria unsigned int stage; 7872e9baf9SAmit Kucheria unsigned int prev_stage; 7972e9baf9SAmit Kucheria unsigned int base; 8072e9baf9SAmit Kucheria /* protects .thresh, .stage and chip registers */ 8172e9baf9SAmit Kucheria struct mutex lock; 8272e9baf9SAmit Kucheria bool initialized; 8372e9baf9SAmit Kucheria 8472e9baf9SAmit Kucheria struct iio_channel *adc; 85aa92b331SDavid Collins const long (*temp_map)[THRESH_COUNT][STAGE_COUNT]; 8672e9baf9SAmit Kucheria }; 8772e9baf9SAmit Kucheria 8872e9baf9SAmit Kucheria /* This array maps from GEN2 alarm state to GEN1 alarm stage */ 8972e9baf9SAmit Kucheria static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3}; 9072e9baf9SAmit Kucheria 9172e9baf9SAmit Kucheria static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data) 9272e9baf9SAmit Kucheria { 9372e9baf9SAmit Kucheria unsigned int val; 9472e9baf9SAmit Kucheria int ret; 9572e9baf9SAmit Kucheria 9672e9baf9SAmit Kucheria ret = regmap_read(chip->map, chip->base + addr, &val); 9772e9baf9SAmit Kucheria if (ret < 0) 9872e9baf9SAmit Kucheria return ret; 9972e9baf9SAmit Kucheria 10072e9baf9SAmit Kucheria *data = val; 10172e9baf9SAmit Kucheria return 0; 10272e9baf9SAmit Kucheria } 10372e9baf9SAmit Kucheria 10472e9baf9SAmit Kucheria static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data) 10572e9baf9SAmit Kucheria { 10672e9baf9SAmit Kucheria return regmap_write(chip->map, chip->base + addr, data); 10772e9baf9SAmit Kucheria } 10872e9baf9SAmit Kucheria 10972e9baf9SAmit Kucheria /** 110aa92b331SDavid Collins * qpnp_tm_decode_temp() - return temperature in mC corresponding to the 111aa92b331SDavid Collins * specified over-temperature stage 112aa92b331SDavid Collins * @chip: Pointer to the qpnp_tm chip 113aa92b331SDavid Collins * @stage: Over-temperature stage 114aa92b331SDavid Collins * 115aa92b331SDavid Collins * Return: temperature in mC 116aa92b331SDavid Collins */ 117aa92b331SDavid Collins static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage) 118aa92b331SDavid Collins { 119aa92b331SDavid Collins if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 || 120aa92b331SDavid Collins stage > STAGE_COUNT) 121aa92b331SDavid Collins return 0; 122aa92b331SDavid Collins 123aa92b331SDavid Collins return (*chip->temp_map)[chip->thresh][stage - 1]; 124aa92b331SDavid Collins } 125aa92b331SDavid Collins 126aa92b331SDavid Collins /** 12772e9baf9SAmit Kucheria * qpnp_tm_get_temp_stage() - return over-temperature stage 12872e9baf9SAmit Kucheria * @chip: Pointer to the qpnp_tm chip 12972e9baf9SAmit Kucheria * 13072e9baf9SAmit Kucheria * Return: stage (GEN1) or state (GEN2) on success, or errno on failure. 13172e9baf9SAmit Kucheria */ 13272e9baf9SAmit Kucheria static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip) 13372e9baf9SAmit Kucheria { 13472e9baf9SAmit Kucheria int ret; 13572e9baf9SAmit Kucheria u8 reg = 0; 13672e9baf9SAmit Kucheria 13772e9baf9SAmit Kucheria ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, ®); 13872e9baf9SAmit Kucheria if (ret < 0) 13972e9baf9SAmit Kucheria return ret; 14072e9baf9SAmit Kucheria 14172e9baf9SAmit Kucheria if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) 14272e9baf9SAmit Kucheria ret = reg & STATUS_GEN1_STAGE_MASK; 14372e9baf9SAmit Kucheria else 14472e9baf9SAmit Kucheria ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT; 14572e9baf9SAmit Kucheria 14672e9baf9SAmit Kucheria return ret; 14772e9baf9SAmit Kucheria } 14872e9baf9SAmit Kucheria 14972e9baf9SAmit Kucheria /* 15072e9baf9SAmit Kucheria * This function updates the internal temp value based on the 15172e9baf9SAmit Kucheria * current thermal stage and threshold as well as the previous stage 15272e9baf9SAmit Kucheria */ 15372e9baf9SAmit Kucheria static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip) 15472e9baf9SAmit Kucheria { 15572e9baf9SAmit Kucheria unsigned int stage, stage_new, stage_old; 15672e9baf9SAmit Kucheria int ret; 15772e9baf9SAmit Kucheria 15872e9baf9SAmit Kucheria WARN_ON(!mutex_is_locked(&chip->lock)); 15972e9baf9SAmit Kucheria 16072e9baf9SAmit Kucheria ret = qpnp_tm_get_temp_stage(chip); 16172e9baf9SAmit Kucheria if (ret < 0) 16272e9baf9SAmit Kucheria return ret; 16372e9baf9SAmit Kucheria stage = ret; 16472e9baf9SAmit Kucheria 16572e9baf9SAmit Kucheria if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) { 16672e9baf9SAmit Kucheria stage_new = stage; 16772e9baf9SAmit Kucheria stage_old = chip->stage; 16872e9baf9SAmit Kucheria } else { 16972e9baf9SAmit Kucheria stage_new = alarm_state_map[stage]; 17072e9baf9SAmit Kucheria stage_old = alarm_state_map[chip->stage]; 17172e9baf9SAmit Kucheria } 17272e9baf9SAmit Kucheria 17372e9baf9SAmit Kucheria if (stage_new > stage_old) { 17472e9baf9SAmit Kucheria /* increasing stage, use lower bound */ 175aa92b331SDavid Collins chip->temp = qpnp_tm_decode_temp(chip, stage_new) 176aa92b331SDavid Collins + TEMP_STAGE_HYSTERESIS; 17772e9baf9SAmit Kucheria } else if (stage_new < stage_old) { 17872e9baf9SAmit Kucheria /* decreasing stage, use upper bound */ 179aa92b331SDavid Collins chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1) 180aa92b331SDavid Collins - TEMP_STAGE_HYSTERESIS; 18172e9baf9SAmit Kucheria } 18272e9baf9SAmit Kucheria 18372e9baf9SAmit Kucheria chip->stage = stage; 18472e9baf9SAmit Kucheria 18572e9baf9SAmit Kucheria return 0; 18672e9baf9SAmit Kucheria } 18772e9baf9SAmit Kucheria 188ca1b9a9eSDaniel Lezcano static int qpnp_tm_get_temp(struct thermal_zone_device *tz, int *temp) 18972e9baf9SAmit Kucheria { 190*5f68d078SDaniel Lezcano struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz); 19172e9baf9SAmit Kucheria int ret, mili_celsius; 19272e9baf9SAmit Kucheria 19372e9baf9SAmit Kucheria if (!temp) 19472e9baf9SAmit Kucheria return -EINVAL; 19572e9baf9SAmit Kucheria 19672e9baf9SAmit Kucheria if (!chip->initialized) { 19772e9baf9SAmit Kucheria *temp = DEFAULT_TEMP; 19872e9baf9SAmit Kucheria return 0; 19972e9baf9SAmit Kucheria } 20072e9baf9SAmit Kucheria 20172e9baf9SAmit Kucheria if (!chip->adc) { 20272e9baf9SAmit Kucheria mutex_lock(&chip->lock); 20372e9baf9SAmit Kucheria ret = qpnp_tm_update_temp_no_adc(chip); 20472e9baf9SAmit Kucheria mutex_unlock(&chip->lock); 20572e9baf9SAmit Kucheria if (ret < 0) 20672e9baf9SAmit Kucheria return ret; 20772e9baf9SAmit Kucheria } else { 20872e9baf9SAmit Kucheria ret = iio_read_channel_processed(chip->adc, &mili_celsius); 20972e9baf9SAmit Kucheria if (ret < 0) 21072e9baf9SAmit Kucheria return ret; 21172e9baf9SAmit Kucheria 21272e9baf9SAmit Kucheria chip->temp = mili_celsius; 21372e9baf9SAmit Kucheria } 21472e9baf9SAmit Kucheria 2150ffdab6fSVeera Vegivada *temp = chip->temp; 21672e9baf9SAmit Kucheria 21772e9baf9SAmit Kucheria return 0; 21872e9baf9SAmit Kucheria } 21972e9baf9SAmit Kucheria 22072e9baf9SAmit Kucheria static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip, 22172e9baf9SAmit Kucheria int temp) 22272e9baf9SAmit Kucheria { 223aa92b331SDavid Collins long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1]; 224aa92b331SDavid Collins long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1]; 22572e9baf9SAmit Kucheria bool disable_s2_shutdown = false; 226aa92b331SDavid Collins u8 reg; 22772e9baf9SAmit Kucheria 22872e9baf9SAmit Kucheria WARN_ON(!mutex_is_locked(&chip->lock)); 22972e9baf9SAmit Kucheria 23072e9baf9SAmit Kucheria /* 23172e9baf9SAmit Kucheria * Default: S2 and S3 shutdown enabled, thresholds at 232aa92b331SDavid Collins * lowest threshold set, monitoring at 25Hz 23372e9baf9SAmit Kucheria */ 23472e9baf9SAmit Kucheria reg = SHUTDOWN_CTRL1_RATE_25HZ; 23572e9baf9SAmit Kucheria 23672e9baf9SAmit Kucheria if (temp == THERMAL_TEMP_INVALID || 237aa92b331SDavid Collins temp < stage2_threshold_min) { 23872e9baf9SAmit Kucheria chip->thresh = THRESH_MIN; 23972e9baf9SAmit Kucheria goto skip; 24072e9baf9SAmit Kucheria } 24172e9baf9SAmit Kucheria 242aa92b331SDavid Collins if (temp <= stage2_threshold_max) { 24372e9baf9SAmit Kucheria chip->thresh = THRESH_MAX - 244aa92b331SDavid Collins ((stage2_threshold_max - temp) / 24572e9baf9SAmit Kucheria TEMP_THRESH_STEP); 24672e9baf9SAmit Kucheria disable_s2_shutdown = true; 24772e9baf9SAmit Kucheria } else { 24872e9baf9SAmit Kucheria chip->thresh = THRESH_MAX; 24972e9baf9SAmit Kucheria 25072e9baf9SAmit Kucheria if (chip->adc) 25172e9baf9SAmit Kucheria disable_s2_shutdown = true; 25272e9baf9SAmit Kucheria else 25372e9baf9SAmit Kucheria dev_warn(chip->dev, 2548763f8acSLuca 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", 2558763f8acSLuca Weiss temp, stage2_threshold_max, stage2_threshold_max); 25672e9baf9SAmit Kucheria } 25772e9baf9SAmit Kucheria 25872e9baf9SAmit Kucheria skip: 25972e9baf9SAmit Kucheria reg |= chip->thresh; 26072e9baf9SAmit Kucheria if (disable_s2_shutdown) 26172e9baf9SAmit Kucheria reg |= SHUTDOWN_CTRL1_OVERRIDE_S2; 26272e9baf9SAmit Kucheria 26372e9baf9SAmit Kucheria return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg); 26472e9baf9SAmit Kucheria } 26572e9baf9SAmit Kucheria 2661fa86b0aSDaniel Lezcano static int qpnp_tm_set_trip_temp(struct thermal_zone_device *tz, int trip_id, int temp) 26772e9baf9SAmit Kucheria { 268*5f68d078SDaniel Lezcano struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz); 2691fa86b0aSDaniel Lezcano struct thermal_trip trip; 27072e9baf9SAmit Kucheria int ret; 27172e9baf9SAmit Kucheria 272e6ec64f8SJohan Hovold ret = __thermal_zone_get_trip(chip->tz_dev, trip_id, &trip); 2731fa86b0aSDaniel Lezcano if (ret) 2741fa86b0aSDaniel Lezcano return ret; 27572e9baf9SAmit Kucheria 2761fa86b0aSDaniel Lezcano if (trip.type != THERMAL_TRIP_CRITICAL) 27772e9baf9SAmit Kucheria return 0; 27872e9baf9SAmit Kucheria 27972e9baf9SAmit Kucheria mutex_lock(&chip->lock); 28072e9baf9SAmit Kucheria ret = qpnp_tm_update_critical_trip_temp(chip, temp); 28172e9baf9SAmit Kucheria mutex_unlock(&chip->lock); 28272e9baf9SAmit Kucheria 28372e9baf9SAmit Kucheria return ret; 28472e9baf9SAmit Kucheria } 28572e9baf9SAmit Kucheria 286ca1b9a9eSDaniel Lezcano static const struct thermal_zone_device_ops qpnp_tm_sensor_ops = { 28772e9baf9SAmit Kucheria .get_temp = qpnp_tm_get_temp, 28872e9baf9SAmit Kucheria .set_trip_temp = qpnp_tm_set_trip_temp, 28972e9baf9SAmit Kucheria }; 29072e9baf9SAmit Kucheria 29172e9baf9SAmit Kucheria static irqreturn_t qpnp_tm_isr(int irq, void *data) 29272e9baf9SAmit Kucheria { 29372e9baf9SAmit Kucheria struct qpnp_tm_chip *chip = data; 29472e9baf9SAmit Kucheria 29572e9baf9SAmit Kucheria thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); 29672e9baf9SAmit Kucheria 29772e9baf9SAmit Kucheria return IRQ_HANDLED; 29872e9baf9SAmit Kucheria } 29972e9baf9SAmit Kucheria 30072e9baf9SAmit Kucheria static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip) 30172e9baf9SAmit Kucheria { 3021fa86b0aSDaniel Lezcano struct thermal_trip trip; 3031fa86b0aSDaniel Lezcano int i, ret; 30472e9baf9SAmit Kucheria 3051fa86b0aSDaniel Lezcano for (i = 0; i < thermal_zone_get_num_trips(chip->tz_dev); i++) { 30672e9baf9SAmit Kucheria 3071fa86b0aSDaniel Lezcano ret = thermal_zone_get_trip(chip->tz_dev, i, &trip); 3081fa86b0aSDaniel Lezcano if (ret) 3091fa86b0aSDaniel Lezcano continue; 31072e9baf9SAmit Kucheria 3111fa86b0aSDaniel Lezcano if (trip.type == THERMAL_TRIP_CRITICAL) 3121fa86b0aSDaniel Lezcano return trip.temperature; 31372e9baf9SAmit Kucheria } 31472e9baf9SAmit Kucheria 31572e9baf9SAmit Kucheria return THERMAL_TEMP_INVALID; 31672e9baf9SAmit Kucheria } 31772e9baf9SAmit Kucheria 31872e9baf9SAmit Kucheria /* 31972e9baf9SAmit Kucheria * This function initializes the internal temp value based on only the 32072e9baf9SAmit Kucheria * current thermal stage and threshold. Setup threshold control and 32172e9baf9SAmit Kucheria * disable shutdown override. 32272e9baf9SAmit Kucheria */ 32372e9baf9SAmit Kucheria static int qpnp_tm_init(struct qpnp_tm_chip *chip) 32472e9baf9SAmit Kucheria { 32572e9baf9SAmit Kucheria unsigned int stage; 32672e9baf9SAmit Kucheria int ret; 32772e9baf9SAmit Kucheria u8 reg = 0; 32872e9baf9SAmit Kucheria int crit_temp; 32972e9baf9SAmit Kucheria 33072e9baf9SAmit Kucheria mutex_lock(&chip->lock); 33172e9baf9SAmit Kucheria 33272e9baf9SAmit Kucheria ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, ®); 33372e9baf9SAmit Kucheria if (ret < 0) 33472e9baf9SAmit Kucheria goto out; 33572e9baf9SAmit Kucheria 33672e9baf9SAmit Kucheria chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK; 33772e9baf9SAmit Kucheria chip->temp = DEFAULT_TEMP; 33872e9baf9SAmit Kucheria 33972e9baf9SAmit Kucheria ret = qpnp_tm_get_temp_stage(chip); 34072e9baf9SAmit Kucheria if (ret < 0) 34172e9baf9SAmit Kucheria goto out; 34272e9baf9SAmit Kucheria chip->stage = ret; 34372e9baf9SAmit Kucheria 34472e9baf9SAmit Kucheria stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1 34572e9baf9SAmit Kucheria ? chip->stage : alarm_state_map[chip->stage]; 34672e9baf9SAmit Kucheria 34772e9baf9SAmit Kucheria if (stage) 348aa92b331SDavid Collins chip->temp = qpnp_tm_decode_temp(chip, stage); 34972e9baf9SAmit Kucheria 35059edcd91SJohan Hovold mutex_unlock(&chip->lock); 35159edcd91SJohan Hovold 35272e9baf9SAmit Kucheria crit_temp = qpnp_tm_get_critical_trip_temp(chip); 35359edcd91SJohan Hovold 35459edcd91SJohan Hovold mutex_lock(&chip->lock); 35559edcd91SJohan Hovold 35672e9baf9SAmit Kucheria ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp); 35772e9baf9SAmit Kucheria if (ret < 0) 35872e9baf9SAmit Kucheria goto out; 35972e9baf9SAmit Kucheria 36072e9baf9SAmit Kucheria /* Enable the thermal alarm PMIC module in always-on mode. */ 36172e9baf9SAmit Kucheria reg = ALARM_CTRL_FORCE_ENABLE; 36272e9baf9SAmit Kucheria ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg); 36372e9baf9SAmit Kucheria 36472e9baf9SAmit Kucheria chip->initialized = true; 36572e9baf9SAmit Kucheria 36672e9baf9SAmit Kucheria out: 36772e9baf9SAmit Kucheria mutex_unlock(&chip->lock); 36872e9baf9SAmit Kucheria return ret; 36972e9baf9SAmit Kucheria } 37072e9baf9SAmit Kucheria 37172e9baf9SAmit Kucheria static int qpnp_tm_probe(struct platform_device *pdev) 37272e9baf9SAmit Kucheria { 37372e9baf9SAmit Kucheria struct qpnp_tm_chip *chip; 37472e9baf9SAmit Kucheria struct device_node *node; 375aa92b331SDavid Collins u8 type, subtype, dig_major; 37672e9baf9SAmit Kucheria u32 res; 37772e9baf9SAmit Kucheria int ret, irq; 37872e9baf9SAmit Kucheria 37972e9baf9SAmit Kucheria node = pdev->dev.of_node; 38072e9baf9SAmit Kucheria 38172e9baf9SAmit Kucheria chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); 38272e9baf9SAmit Kucheria if (!chip) 38372e9baf9SAmit Kucheria return -ENOMEM; 38472e9baf9SAmit Kucheria 38572e9baf9SAmit Kucheria dev_set_drvdata(&pdev->dev, chip); 38672e9baf9SAmit Kucheria chip->dev = &pdev->dev; 38772e9baf9SAmit Kucheria 38872e9baf9SAmit Kucheria mutex_init(&chip->lock); 38972e9baf9SAmit Kucheria 39072e9baf9SAmit Kucheria chip->map = dev_get_regmap(pdev->dev.parent, NULL); 39172e9baf9SAmit Kucheria if (!chip->map) 39272e9baf9SAmit Kucheria return -ENXIO; 39372e9baf9SAmit Kucheria 39472e9baf9SAmit Kucheria ret = of_property_read_u32(node, "reg", &res); 39572e9baf9SAmit Kucheria if (ret < 0) 39672e9baf9SAmit Kucheria return ret; 39772e9baf9SAmit Kucheria 39872e9baf9SAmit Kucheria irq = platform_get_irq(pdev, 0); 39972e9baf9SAmit Kucheria if (irq < 0) 40072e9baf9SAmit Kucheria return irq; 40172e9baf9SAmit Kucheria 40272e9baf9SAmit Kucheria /* ADC based measurements are optional */ 40372e9baf9SAmit Kucheria chip->adc = devm_iio_channel_get(&pdev->dev, "thermal"); 40472e9baf9SAmit Kucheria if (IS_ERR(chip->adc)) { 40572e9baf9SAmit Kucheria ret = PTR_ERR(chip->adc); 40672e9baf9SAmit Kucheria chip->adc = NULL; 40772e9baf9SAmit Kucheria if (ret == -EPROBE_DEFER) 40872e9baf9SAmit Kucheria return ret; 40972e9baf9SAmit Kucheria } 41072e9baf9SAmit Kucheria 41172e9baf9SAmit Kucheria chip->base = res; 41272e9baf9SAmit Kucheria 41372e9baf9SAmit Kucheria ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type); 41472e9baf9SAmit Kucheria if (ret < 0) { 41572e9baf9SAmit Kucheria dev_err(&pdev->dev, "could not read type\n"); 41672e9baf9SAmit Kucheria return ret; 41772e9baf9SAmit Kucheria } 41872e9baf9SAmit Kucheria 41972e9baf9SAmit Kucheria ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype); 42072e9baf9SAmit Kucheria if (ret < 0) { 42172e9baf9SAmit Kucheria dev_err(&pdev->dev, "could not read subtype\n"); 42272e9baf9SAmit Kucheria return ret; 42372e9baf9SAmit Kucheria } 42472e9baf9SAmit Kucheria 425aa92b331SDavid Collins ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major); 426aa92b331SDavid Collins if (ret < 0) { 427aa92b331SDavid Collins dev_err(&pdev->dev, "could not read dig_major\n"); 428aa92b331SDavid Collins return ret; 429aa92b331SDavid Collins } 430aa92b331SDavid Collins 43172e9baf9SAmit Kucheria if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1 43272e9baf9SAmit Kucheria && subtype != QPNP_TM_SUBTYPE_GEN2)) { 43372e9baf9SAmit Kucheria dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n", 43472e9baf9SAmit Kucheria type, subtype); 43572e9baf9SAmit Kucheria return -ENODEV; 43672e9baf9SAmit Kucheria } 43772e9baf9SAmit Kucheria 43872e9baf9SAmit Kucheria chip->subtype = subtype; 439aa92b331SDavid Collins if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 1) 440aa92b331SDavid Collins chip->temp_map = &temp_map_gen2_v1; 441aa92b331SDavid Collins else 442aa92b331SDavid Collins chip->temp_map = &temp_map_gen1; 44372e9baf9SAmit Kucheria 44472e9baf9SAmit Kucheria /* 44572e9baf9SAmit Kucheria * Register the sensor before initializing the hardware to be able to 44672e9baf9SAmit Kucheria * read the trip points. get_temp() returns the default temperature 44772e9baf9SAmit Kucheria * before the hardware initialization is completed. 44872e9baf9SAmit Kucheria */ 449ca1b9a9eSDaniel Lezcano chip->tz_dev = devm_thermal_of_zone_register( 45072e9baf9SAmit Kucheria &pdev->dev, 0, chip, &qpnp_tm_sensor_ops); 45172e9baf9SAmit Kucheria if (IS_ERR(chip->tz_dev)) { 45272e9baf9SAmit Kucheria dev_err(&pdev->dev, "failed to register sensor\n"); 45372e9baf9SAmit Kucheria return PTR_ERR(chip->tz_dev); 45472e9baf9SAmit Kucheria } 45572e9baf9SAmit Kucheria 45672e9baf9SAmit Kucheria ret = qpnp_tm_init(chip); 45772e9baf9SAmit Kucheria if (ret < 0) { 45872e9baf9SAmit Kucheria dev_err(&pdev->dev, "init failed\n"); 45972e9baf9SAmit Kucheria return ret; 46072e9baf9SAmit Kucheria } 46172e9baf9SAmit Kucheria 462fb55b012SDmitry Baryshkov if (devm_thermal_add_hwmon_sysfs(chip->tz_dev)) 463fb55b012SDmitry Baryshkov dev_warn(&pdev->dev, 464fb55b012SDmitry Baryshkov "Failed to add hwmon sysfs attributes\n"); 465fb55b012SDmitry Baryshkov 46672e9baf9SAmit Kucheria ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr, 46772e9baf9SAmit Kucheria IRQF_ONESHOT, node->name, chip); 46872e9baf9SAmit Kucheria if (ret < 0) 46972e9baf9SAmit Kucheria return ret; 47072e9baf9SAmit Kucheria 47172e9baf9SAmit Kucheria thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); 47272e9baf9SAmit Kucheria 47372e9baf9SAmit Kucheria return 0; 47472e9baf9SAmit Kucheria } 47572e9baf9SAmit Kucheria 47672e9baf9SAmit Kucheria static const struct of_device_id qpnp_tm_match_table[] = { 47772e9baf9SAmit Kucheria { .compatible = "qcom,spmi-temp-alarm" }, 47872e9baf9SAmit Kucheria { } 47972e9baf9SAmit Kucheria }; 48072e9baf9SAmit Kucheria MODULE_DEVICE_TABLE(of, qpnp_tm_match_table); 48172e9baf9SAmit Kucheria 48272e9baf9SAmit Kucheria static struct platform_driver qpnp_tm_driver = { 48372e9baf9SAmit Kucheria .driver = { 48472e9baf9SAmit Kucheria .name = "spmi-temp-alarm", 48572e9baf9SAmit Kucheria .of_match_table = qpnp_tm_match_table, 48672e9baf9SAmit Kucheria }, 48772e9baf9SAmit Kucheria .probe = qpnp_tm_probe, 48872e9baf9SAmit Kucheria }; 48972e9baf9SAmit Kucheria module_platform_driver(qpnp_tm_driver); 49072e9baf9SAmit Kucheria 49172e9baf9SAmit Kucheria MODULE_ALIAS("platform:spmi-temp-alarm"); 49272e9baf9SAmit Kucheria MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver"); 49372e9baf9SAmit Kucheria MODULE_LICENSE("GPL v2"); 494