12d71d8deSAmit Kucheria // SPDX-License-Identifier: GPL-2.0 29066073cSRajendra Nayak /* 39066073cSRajendra Nayak * Copyright (c) 2015, The Linux Foundation. All rights reserved. 4a7ff8297SAmit Kucheria * Copyright (c) 2019, 2020, Linaro Ltd. 59066073cSRajendra Nayak */ 69066073cSRajendra Nayak 77c938f48SAmit Kucheria #include <linux/debugfs.h> 89066073cSRajendra Nayak #include <linux/err.h> 9a7ff8297SAmit Kucheria #include <linux/io.h> 109066073cSRajendra Nayak #include <linux/module.h> 11a7ff8297SAmit Kucheria #include <linux/nvmem-consumer.h> 129066073cSRajendra Nayak #include <linux/of.h> 13a7ff8297SAmit Kucheria #include <linux/of_address.h> 14634e11d5SAmit Kucheria #include <linux/of_platform.h> 1553e2a20eSAnsuel Smith #include <linux/mfd/syscon.h> 169066073cSRajendra Nayak #include <linux/platform_device.h> 179066073cSRajendra Nayak #include <linux/pm.h> 18a7ff8297SAmit Kucheria #include <linux/regmap.h> 199066073cSRajendra Nayak #include <linux/slab.h> 209066073cSRajendra Nayak #include <linux/thermal.h> 218556e19dSDmitry Baryshkov #include "../thermal_hwmon.h" 229066073cSRajendra Nayak #include "tsens.h" 239066073cSRajendra Nayak 24a7ff8297SAmit Kucheria /** 25a7ff8297SAmit Kucheria * struct tsens_irq_data - IRQ status and temperature violations 26a7ff8297SAmit Kucheria * @up_viol: upper threshold violated 27a7ff8297SAmit Kucheria * @up_thresh: upper threshold temperature value 28a7ff8297SAmit Kucheria * @up_irq_mask: mask register for upper threshold irqs 29a7ff8297SAmit Kucheria * @up_irq_clear: clear register for uppper threshold irqs 30a7ff8297SAmit Kucheria * @low_viol: lower threshold violated 31a7ff8297SAmit Kucheria * @low_thresh: lower threshold temperature value 32a7ff8297SAmit Kucheria * @low_irq_mask: mask register for lower threshold irqs 33a7ff8297SAmit Kucheria * @low_irq_clear: clear register for lower threshold irqs 34a7ff8297SAmit Kucheria * @crit_viol: critical threshold violated 35a7ff8297SAmit Kucheria * @crit_thresh: critical threshold temperature value 36a7ff8297SAmit Kucheria * @crit_irq_mask: mask register for critical threshold irqs 37a7ff8297SAmit Kucheria * @crit_irq_clear: clear register for critical threshold irqs 38a7ff8297SAmit Kucheria * 39a7ff8297SAmit Kucheria * Structure containing data about temperature threshold settings and 40a7ff8297SAmit Kucheria * irq status if they were violated. 41a7ff8297SAmit Kucheria */ 42a7ff8297SAmit Kucheria struct tsens_irq_data { 43a7ff8297SAmit Kucheria u32 up_viol; 44a7ff8297SAmit Kucheria int up_thresh; 45a7ff8297SAmit Kucheria u32 up_irq_mask; 46a7ff8297SAmit Kucheria u32 up_irq_clear; 47a7ff8297SAmit Kucheria u32 low_viol; 48a7ff8297SAmit Kucheria int low_thresh; 49a7ff8297SAmit Kucheria u32 low_irq_mask; 50a7ff8297SAmit Kucheria u32 low_irq_clear; 51a7ff8297SAmit Kucheria u32 crit_viol; 52a7ff8297SAmit Kucheria u32 crit_thresh; 53a7ff8297SAmit Kucheria u32 crit_irq_mask; 54a7ff8297SAmit Kucheria u32 crit_irq_clear; 55a7ff8297SAmit Kucheria }; 56a7ff8297SAmit Kucheria 57a7ff8297SAmit Kucheria char *qfprom_read(struct device *dev, const char *cname) 58a7ff8297SAmit Kucheria { 59a7ff8297SAmit Kucheria struct nvmem_cell *cell; 60a7ff8297SAmit Kucheria ssize_t data; 61a7ff8297SAmit Kucheria char *ret; 62a7ff8297SAmit Kucheria 63a7ff8297SAmit Kucheria cell = nvmem_cell_get(dev, cname); 64a7ff8297SAmit Kucheria if (IS_ERR(cell)) 65a7ff8297SAmit Kucheria return ERR_CAST(cell); 66a7ff8297SAmit Kucheria 67a7ff8297SAmit Kucheria ret = nvmem_cell_read(cell, &data); 68a7ff8297SAmit Kucheria nvmem_cell_put(cell); 69a7ff8297SAmit Kucheria 70a7ff8297SAmit Kucheria return ret; 71a7ff8297SAmit Kucheria } 72a7ff8297SAmit Kucheria 73*498d2457SDmitry Baryshkov int tsens_calibrate_nvmem(struct tsens_priv *priv, int shift) 74*498d2457SDmitry Baryshkov { 75*498d2457SDmitry Baryshkov u32 mode; 76*498d2457SDmitry Baryshkov u32 base1, base2; 77*498d2457SDmitry Baryshkov u32 p1[MAX_SENSORS], p2[MAX_SENSORS]; 78*498d2457SDmitry Baryshkov char name[] = "sXX_pY"; /* s10_p1 */ 79*498d2457SDmitry Baryshkov int i, ret; 80*498d2457SDmitry Baryshkov 81*498d2457SDmitry Baryshkov if (priv->num_sensors > MAX_SENSORS) 82*498d2457SDmitry Baryshkov return -EINVAL; 83*498d2457SDmitry Baryshkov 84*498d2457SDmitry Baryshkov ret = nvmem_cell_read_variable_le_u32(priv->dev, "mode", &mode); 85*498d2457SDmitry Baryshkov if (ret == -ENOENT) 86*498d2457SDmitry Baryshkov dev_warn(priv->dev, "Please migrate to separate nvmem cells for calibration data\n"); 87*498d2457SDmitry Baryshkov if (ret < 0) 88*498d2457SDmitry Baryshkov return ret; 89*498d2457SDmitry Baryshkov 90*498d2457SDmitry Baryshkov dev_dbg(priv->dev, "calibration mode is %d\n", mode); 91*498d2457SDmitry Baryshkov 92*498d2457SDmitry Baryshkov ret = nvmem_cell_read_variable_le_u32(priv->dev, "base1", &base1); 93*498d2457SDmitry Baryshkov if (ret < 0) 94*498d2457SDmitry Baryshkov return ret; 95*498d2457SDmitry Baryshkov 96*498d2457SDmitry Baryshkov ret = nvmem_cell_read_variable_le_u32(priv->dev, "base2", &base2); 97*498d2457SDmitry Baryshkov if (ret < 0) 98*498d2457SDmitry Baryshkov return ret; 99*498d2457SDmitry Baryshkov 100*498d2457SDmitry Baryshkov for (i = 0; i < priv->num_sensors; i++) { 101*498d2457SDmitry Baryshkov ret = snprintf(name, sizeof(name), "s%d_p1", priv->sensor[i].hw_id); 102*498d2457SDmitry Baryshkov if (ret < 0) 103*498d2457SDmitry Baryshkov return ret; 104*498d2457SDmitry Baryshkov 105*498d2457SDmitry Baryshkov ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &p1[i]); 106*498d2457SDmitry Baryshkov if (ret) 107*498d2457SDmitry Baryshkov return ret; 108*498d2457SDmitry Baryshkov 109*498d2457SDmitry Baryshkov ret = snprintf(name, sizeof(name), "s%d_p2", priv->sensor[i].hw_id); 110*498d2457SDmitry Baryshkov if (ret < 0) 111*498d2457SDmitry Baryshkov return ret; 112*498d2457SDmitry Baryshkov 113*498d2457SDmitry Baryshkov ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &p2[i]); 114*498d2457SDmitry Baryshkov if (ret) 115*498d2457SDmitry Baryshkov return ret; 116*498d2457SDmitry Baryshkov } 117*498d2457SDmitry Baryshkov 118*498d2457SDmitry Baryshkov switch (mode) { 119*498d2457SDmitry Baryshkov case ONE_PT_CALIB: 120*498d2457SDmitry Baryshkov for (i = 0; i < priv->num_sensors; i++) 121*498d2457SDmitry Baryshkov p1[i] = p1[i] + (base1 << shift); 122*498d2457SDmitry Baryshkov break; 123*498d2457SDmitry Baryshkov case TWO_PT_CALIB: 124*498d2457SDmitry Baryshkov for (i = 0; i < priv->num_sensors; i++) 125*498d2457SDmitry Baryshkov p2[i] = (p2[i] + base2) << shift; 126*498d2457SDmitry Baryshkov fallthrough; 127*498d2457SDmitry Baryshkov case ONE_PT_CALIB2: 128*498d2457SDmitry Baryshkov for (i = 0; i < priv->num_sensors; i++) 129*498d2457SDmitry Baryshkov p1[i] = (p1[i] + base1) << shift; 130*498d2457SDmitry Baryshkov break; 131*498d2457SDmitry Baryshkov default: 132*498d2457SDmitry Baryshkov dev_dbg(priv->dev, "calibrationless mode\n"); 133*498d2457SDmitry Baryshkov for (i = 0; i < priv->num_sensors; i++) { 134*498d2457SDmitry Baryshkov p1[i] = 500; 135*498d2457SDmitry Baryshkov p2[i] = 780; 136*498d2457SDmitry Baryshkov } 137*498d2457SDmitry Baryshkov } 138*498d2457SDmitry Baryshkov 139*498d2457SDmitry Baryshkov compute_intercept_slope(priv, p1, p2, mode); 140*498d2457SDmitry Baryshkov 141*498d2457SDmitry Baryshkov return 0; 142*498d2457SDmitry Baryshkov } 143*498d2457SDmitry Baryshkov 144*498d2457SDmitry Baryshkov int tsens_calibrate_common(struct tsens_priv *priv) 145*498d2457SDmitry Baryshkov { 146*498d2457SDmitry Baryshkov return tsens_calibrate_nvmem(priv, 2); 147*498d2457SDmitry Baryshkov } 148*498d2457SDmitry Baryshkov 149a7ff8297SAmit Kucheria /* 150a7ff8297SAmit Kucheria * Use this function on devices where slope and offset calculations 151a7ff8297SAmit Kucheria * depend on calibration data read from qfprom. On others the slope 152a7ff8297SAmit Kucheria * and offset values are derived from tz->tzp->slope and tz->tzp->offset 153a7ff8297SAmit Kucheria * resp. 154a7ff8297SAmit Kucheria */ 155a7ff8297SAmit Kucheria void compute_intercept_slope(struct tsens_priv *priv, u32 *p1, 156a7ff8297SAmit Kucheria u32 *p2, u32 mode) 157a7ff8297SAmit Kucheria { 158a7ff8297SAmit Kucheria int i; 159a7ff8297SAmit Kucheria int num, den; 160a7ff8297SAmit Kucheria 161a7ff8297SAmit Kucheria for (i = 0; i < priv->num_sensors; i++) { 162a7ff8297SAmit Kucheria dev_dbg(priv->dev, 163a7ff8297SAmit Kucheria "%s: sensor%d - data_point1:%#x data_point2:%#x\n", 164a7ff8297SAmit Kucheria __func__, i, p1[i], p2[i]); 165a7ff8297SAmit Kucheria 1669d51769bSAnsuel Smith if (!priv->sensor[i].slope) 167a7ff8297SAmit Kucheria priv->sensor[i].slope = SLOPE_DEFAULT; 168a7ff8297SAmit Kucheria if (mode == TWO_PT_CALIB) { 169a7ff8297SAmit Kucheria /* 170a7ff8297SAmit Kucheria * slope (m) = adc_code2 - adc_code1 (y2 - y1)/ 171a7ff8297SAmit Kucheria * temp_120_degc - temp_30_degc (x2 - x1) 172a7ff8297SAmit Kucheria */ 173a7ff8297SAmit Kucheria num = p2[i] - p1[i]; 174a7ff8297SAmit Kucheria num *= SLOPE_FACTOR; 175a7ff8297SAmit Kucheria den = CAL_DEGC_PT2 - CAL_DEGC_PT1; 176a7ff8297SAmit Kucheria priv->sensor[i].slope = num / den; 177a7ff8297SAmit Kucheria } 178a7ff8297SAmit Kucheria 179a7ff8297SAmit Kucheria priv->sensor[i].offset = (p1[i] * SLOPE_FACTOR) - 180a7ff8297SAmit Kucheria (CAL_DEGC_PT1 * 181a7ff8297SAmit Kucheria priv->sensor[i].slope); 182a7ff8297SAmit Kucheria dev_dbg(priv->dev, "%s: offset:%d\n", __func__, 183a7ff8297SAmit Kucheria priv->sensor[i].offset); 184a7ff8297SAmit Kucheria } 185a7ff8297SAmit Kucheria } 186a7ff8297SAmit Kucheria 187a7ff8297SAmit Kucheria static inline u32 degc_to_code(int degc, const struct tsens_sensor *s) 188a7ff8297SAmit Kucheria { 189a7ff8297SAmit Kucheria u64 code = div_u64(((u64)degc * s->slope + s->offset), SLOPE_FACTOR); 190a7ff8297SAmit Kucheria 191a7ff8297SAmit Kucheria pr_debug("%s: raw_code: 0x%llx, degc:%d\n", __func__, code, degc); 192a7ff8297SAmit Kucheria return clamp_val(code, THRESHOLD_MIN_ADC_CODE, THRESHOLD_MAX_ADC_CODE); 193a7ff8297SAmit Kucheria } 194a7ff8297SAmit Kucheria 195a7ff8297SAmit Kucheria static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s) 196a7ff8297SAmit Kucheria { 197a7ff8297SAmit Kucheria int degc, num, den; 198a7ff8297SAmit Kucheria 199a7ff8297SAmit Kucheria num = (adc_code * SLOPE_FACTOR) - s->offset; 200a7ff8297SAmit Kucheria den = s->slope; 201a7ff8297SAmit Kucheria 202a7ff8297SAmit Kucheria if (num > 0) 203a7ff8297SAmit Kucheria degc = num + (den / 2); 204a7ff8297SAmit Kucheria else if (num < 0) 205a7ff8297SAmit Kucheria degc = num - (den / 2); 206a7ff8297SAmit Kucheria else 207a7ff8297SAmit Kucheria degc = num; 208a7ff8297SAmit Kucheria 209a7ff8297SAmit Kucheria degc /= den; 210a7ff8297SAmit Kucheria 211a7ff8297SAmit Kucheria return degc; 212a7ff8297SAmit Kucheria } 213a7ff8297SAmit Kucheria 214a7ff8297SAmit Kucheria /** 215a7ff8297SAmit Kucheria * tsens_hw_to_mC - Return sign-extended temperature in mCelsius. 216a7ff8297SAmit Kucheria * @s: Pointer to sensor struct 217a7ff8297SAmit Kucheria * @field: Index into regmap_field array pointing to temperature data 218a7ff8297SAmit Kucheria * 219a7ff8297SAmit Kucheria * This function handles temperature returned in ADC code or deciCelsius 220a7ff8297SAmit Kucheria * depending on IP version. 221a7ff8297SAmit Kucheria * 222a7ff8297SAmit Kucheria * Return: Temperature in milliCelsius on success, a negative errno will 223a7ff8297SAmit Kucheria * be returned in error cases 224a7ff8297SAmit Kucheria */ 225a7ff8297SAmit Kucheria static int tsens_hw_to_mC(const struct tsens_sensor *s, int field) 226a7ff8297SAmit Kucheria { 227a7ff8297SAmit Kucheria struct tsens_priv *priv = s->priv; 228a7ff8297SAmit Kucheria u32 resolution; 229a7ff8297SAmit Kucheria u32 temp = 0; 230a7ff8297SAmit Kucheria int ret; 231a7ff8297SAmit Kucheria 232a7ff8297SAmit Kucheria resolution = priv->fields[LAST_TEMP_0].msb - 233a7ff8297SAmit Kucheria priv->fields[LAST_TEMP_0].lsb; 234a7ff8297SAmit Kucheria 235a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[field], &temp); 236a7ff8297SAmit Kucheria if (ret) 237a7ff8297SAmit Kucheria return ret; 238a7ff8297SAmit Kucheria 239a7ff8297SAmit Kucheria /* Convert temperature from ADC code to milliCelsius */ 240a7ff8297SAmit Kucheria if (priv->feat->adc) 241a7ff8297SAmit Kucheria return code_to_degc(temp, s) * 1000; 242a7ff8297SAmit Kucheria 243a7ff8297SAmit Kucheria /* deciCelsius -> milliCelsius along with sign extension */ 244a7ff8297SAmit Kucheria return sign_extend32(temp, resolution) * 100; 245a7ff8297SAmit Kucheria } 246a7ff8297SAmit Kucheria 247a7ff8297SAmit Kucheria /** 248a7ff8297SAmit Kucheria * tsens_mC_to_hw - Convert temperature to hardware register value 249a7ff8297SAmit Kucheria * @s: Pointer to sensor struct 250a7ff8297SAmit Kucheria * @temp: temperature in milliCelsius to be programmed to hardware 251a7ff8297SAmit Kucheria * 252a7ff8297SAmit Kucheria * This function outputs the value to be written to hardware in ADC code 253a7ff8297SAmit Kucheria * or deciCelsius depending on IP version. 254a7ff8297SAmit Kucheria * 255a7ff8297SAmit Kucheria * Return: ADC code or temperature in deciCelsius. 256a7ff8297SAmit Kucheria */ 257a7ff8297SAmit Kucheria static int tsens_mC_to_hw(const struct tsens_sensor *s, int temp) 258a7ff8297SAmit Kucheria { 259a7ff8297SAmit Kucheria struct tsens_priv *priv = s->priv; 260a7ff8297SAmit Kucheria 261a7ff8297SAmit Kucheria /* milliC to adc code */ 262a7ff8297SAmit Kucheria if (priv->feat->adc) 263a7ff8297SAmit Kucheria return degc_to_code(temp / 1000, s); 264a7ff8297SAmit Kucheria 265a7ff8297SAmit Kucheria /* milliC to deciC */ 266a7ff8297SAmit Kucheria return temp / 100; 267a7ff8297SAmit Kucheria } 268a7ff8297SAmit Kucheria 269a7ff8297SAmit Kucheria static inline enum tsens_ver tsens_version(struct tsens_priv *priv) 270a7ff8297SAmit Kucheria { 271a7ff8297SAmit Kucheria return priv->feat->ver_major; 272a7ff8297SAmit Kucheria } 273a7ff8297SAmit Kucheria 274a7ff8297SAmit Kucheria static void tsens_set_interrupt_v1(struct tsens_priv *priv, u32 hw_id, 275a7ff8297SAmit Kucheria enum tsens_irq_type irq_type, bool enable) 276a7ff8297SAmit Kucheria { 277a7ff8297SAmit Kucheria u32 index = 0; 278a7ff8297SAmit Kucheria 279a7ff8297SAmit Kucheria switch (irq_type) { 280a7ff8297SAmit Kucheria case UPPER: 281a7ff8297SAmit Kucheria index = UP_INT_CLEAR_0 + hw_id; 282a7ff8297SAmit Kucheria break; 283a7ff8297SAmit Kucheria case LOWER: 284a7ff8297SAmit Kucheria index = LOW_INT_CLEAR_0 + hw_id; 285a7ff8297SAmit Kucheria break; 286a7ff8297SAmit Kucheria case CRITICAL: 287a7ff8297SAmit Kucheria /* No critical interrupts before v2 */ 288a7ff8297SAmit Kucheria return; 289a7ff8297SAmit Kucheria } 290a7ff8297SAmit Kucheria regmap_field_write(priv->rf[index], enable ? 0 : 1); 291a7ff8297SAmit Kucheria } 292a7ff8297SAmit Kucheria 293a7ff8297SAmit Kucheria static void tsens_set_interrupt_v2(struct tsens_priv *priv, u32 hw_id, 294a7ff8297SAmit Kucheria enum tsens_irq_type irq_type, bool enable) 295a7ff8297SAmit Kucheria { 296a7ff8297SAmit Kucheria u32 index_mask = 0, index_clear = 0; 297a7ff8297SAmit Kucheria 298a7ff8297SAmit Kucheria /* 299a7ff8297SAmit Kucheria * To enable the interrupt flag for a sensor: 300a7ff8297SAmit Kucheria * - clear the mask bit 301a7ff8297SAmit Kucheria * To disable the interrupt flag for a sensor: 302a7ff8297SAmit Kucheria * - Mask further interrupts for this sensor 303a7ff8297SAmit Kucheria * - Write 1 followed by 0 to clear the interrupt 304a7ff8297SAmit Kucheria */ 305a7ff8297SAmit Kucheria switch (irq_type) { 306a7ff8297SAmit Kucheria case UPPER: 307a7ff8297SAmit Kucheria index_mask = UP_INT_MASK_0 + hw_id; 308a7ff8297SAmit Kucheria index_clear = UP_INT_CLEAR_0 + hw_id; 309a7ff8297SAmit Kucheria break; 310a7ff8297SAmit Kucheria case LOWER: 311a7ff8297SAmit Kucheria index_mask = LOW_INT_MASK_0 + hw_id; 312a7ff8297SAmit Kucheria index_clear = LOW_INT_CLEAR_0 + hw_id; 313a7ff8297SAmit Kucheria break; 314a7ff8297SAmit Kucheria case CRITICAL: 315a7ff8297SAmit Kucheria index_mask = CRIT_INT_MASK_0 + hw_id; 316a7ff8297SAmit Kucheria index_clear = CRIT_INT_CLEAR_0 + hw_id; 317a7ff8297SAmit Kucheria break; 318a7ff8297SAmit Kucheria } 319a7ff8297SAmit Kucheria 320a7ff8297SAmit Kucheria if (enable) { 321a7ff8297SAmit Kucheria regmap_field_write(priv->rf[index_mask], 0); 322a7ff8297SAmit Kucheria } else { 323a7ff8297SAmit Kucheria regmap_field_write(priv->rf[index_mask], 1); 324a7ff8297SAmit Kucheria regmap_field_write(priv->rf[index_clear], 1); 325a7ff8297SAmit Kucheria regmap_field_write(priv->rf[index_clear], 0); 326a7ff8297SAmit Kucheria } 327a7ff8297SAmit Kucheria } 328a7ff8297SAmit Kucheria 329a7ff8297SAmit Kucheria /** 330a7ff8297SAmit Kucheria * tsens_set_interrupt - Set state of an interrupt 331a7ff8297SAmit Kucheria * @priv: Pointer to tsens controller private data 332a7ff8297SAmit Kucheria * @hw_id: Hardware ID aka. sensor number 333a7ff8297SAmit Kucheria * @irq_type: irq_type from enum tsens_irq_type 334a7ff8297SAmit Kucheria * @enable: false = disable, true = enable 335a7ff8297SAmit Kucheria * 336a7ff8297SAmit Kucheria * Call IP-specific function to set state of an interrupt 337a7ff8297SAmit Kucheria * 338a7ff8297SAmit Kucheria * Return: void 339a7ff8297SAmit Kucheria */ 340a7ff8297SAmit Kucheria static void tsens_set_interrupt(struct tsens_priv *priv, u32 hw_id, 341a7ff8297SAmit Kucheria enum tsens_irq_type irq_type, bool enable) 342a7ff8297SAmit Kucheria { 343a7ff8297SAmit Kucheria dev_dbg(priv->dev, "[%u] %s: %s -> %s\n", hw_id, __func__, 344a7ff8297SAmit Kucheria irq_type ? ((irq_type == 1) ? "UP" : "CRITICAL") : "LOW", 345a7ff8297SAmit Kucheria enable ? "en" : "dis"); 346a7ff8297SAmit Kucheria if (tsens_version(priv) > VER_1_X) 347a7ff8297SAmit Kucheria tsens_set_interrupt_v2(priv, hw_id, irq_type, enable); 348a7ff8297SAmit Kucheria else 349a7ff8297SAmit Kucheria tsens_set_interrupt_v1(priv, hw_id, irq_type, enable); 350a7ff8297SAmit Kucheria } 351a7ff8297SAmit Kucheria 352a7ff8297SAmit Kucheria /** 353a7ff8297SAmit Kucheria * tsens_threshold_violated - Check if a sensor temperature violated a preset threshold 354a7ff8297SAmit Kucheria * @priv: Pointer to tsens controller private data 355a7ff8297SAmit Kucheria * @hw_id: Hardware ID aka. sensor number 356a7ff8297SAmit Kucheria * @d: Pointer to irq state data 357a7ff8297SAmit Kucheria * 358a7ff8297SAmit Kucheria * Return: 0 if threshold was not violated, 1 if it was violated and negative 359a7ff8297SAmit Kucheria * errno in case of errors 360a7ff8297SAmit Kucheria */ 361a7ff8297SAmit Kucheria static int tsens_threshold_violated(struct tsens_priv *priv, u32 hw_id, 362a7ff8297SAmit Kucheria struct tsens_irq_data *d) 363a7ff8297SAmit Kucheria { 364a7ff8297SAmit Kucheria int ret; 365a7ff8297SAmit Kucheria 366a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[UPPER_STATUS_0 + hw_id], &d->up_viol); 367a7ff8297SAmit Kucheria if (ret) 368a7ff8297SAmit Kucheria return ret; 369a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[LOWER_STATUS_0 + hw_id], &d->low_viol); 370a7ff8297SAmit Kucheria if (ret) 371a7ff8297SAmit Kucheria return ret; 372a7ff8297SAmit Kucheria 373a7ff8297SAmit Kucheria if (priv->feat->crit_int) { 374a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[CRITICAL_STATUS_0 + hw_id], 375a7ff8297SAmit Kucheria &d->crit_viol); 376a7ff8297SAmit Kucheria if (ret) 377a7ff8297SAmit Kucheria return ret; 378a7ff8297SAmit Kucheria } 379a7ff8297SAmit Kucheria 380a7ff8297SAmit Kucheria if (d->up_viol || d->low_viol || d->crit_viol) 381a7ff8297SAmit Kucheria return 1; 382a7ff8297SAmit Kucheria 383a7ff8297SAmit Kucheria return 0; 384a7ff8297SAmit Kucheria } 385a7ff8297SAmit Kucheria 386a7ff8297SAmit Kucheria static int tsens_read_irq_state(struct tsens_priv *priv, u32 hw_id, 387a7ff8297SAmit Kucheria const struct tsens_sensor *s, 388a7ff8297SAmit Kucheria struct tsens_irq_data *d) 389a7ff8297SAmit Kucheria { 390a7ff8297SAmit Kucheria int ret; 391a7ff8297SAmit Kucheria 392a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[UP_INT_CLEAR_0 + hw_id], &d->up_irq_clear); 393a7ff8297SAmit Kucheria if (ret) 394a7ff8297SAmit Kucheria return ret; 395a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[LOW_INT_CLEAR_0 + hw_id], &d->low_irq_clear); 396a7ff8297SAmit Kucheria if (ret) 397a7ff8297SAmit Kucheria return ret; 398a7ff8297SAmit Kucheria if (tsens_version(priv) > VER_1_X) { 399a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[UP_INT_MASK_0 + hw_id], &d->up_irq_mask); 400a7ff8297SAmit Kucheria if (ret) 401a7ff8297SAmit Kucheria return ret; 402a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[LOW_INT_MASK_0 + hw_id], &d->low_irq_mask); 403a7ff8297SAmit Kucheria if (ret) 404a7ff8297SAmit Kucheria return ret; 405a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[CRIT_INT_CLEAR_0 + hw_id], 406a7ff8297SAmit Kucheria &d->crit_irq_clear); 407a7ff8297SAmit Kucheria if (ret) 408a7ff8297SAmit Kucheria return ret; 409a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[CRIT_INT_MASK_0 + hw_id], 410a7ff8297SAmit Kucheria &d->crit_irq_mask); 411a7ff8297SAmit Kucheria if (ret) 412a7ff8297SAmit Kucheria return ret; 413a7ff8297SAmit Kucheria 414a7ff8297SAmit Kucheria d->crit_thresh = tsens_hw_to_mC(s, CRIT_THRESH_0 + hw_id); 415a7ff8297SAmit Kucheria } else { 416a7ff8297SAmit Kucheria /* No mask register on older TSENS */ 417a7ff8297SAmit Kucheria d->up_irq_mask = 0; 418a7ff8297SAmit Kucheria d->low_irq_mask = 0; 419a7ff8297SAmit Kucheria d->crit_irq_clear = 0; 420a7ff8297SAmit Kucheria d->crit_irq_mask = 0; 421a7ff8297SAmit Kucheria d->crit_thresh = 0; 422a7ff8297SAmit Kucheria } 423a7ff8297SAmit Kucheria 424a7ff8297SAmit Kucheria d->up_thresh = tsens_hw_to_mC(s, UP_THRESH_0 + hw_id); 425a7ff8297SAmit Kucheria d->low_thresh = tsens_hw_to_mC(s, LOW_THRESH_0 + hw_id); 426a7ff8297SAmit Kucheria 427a7ff8297SAmit Kucheria dev_dbg(priv->dev, "[%u] %s%s: status(%u|%u|%u) | clr(%u|%u|%u) | mask(%u|%u|%u)\n", 428a7ff8297SAmit Kucheria hw_id, __func__, 429a7ff8297SAmit Kucheria (d->up_viol || d->low_viol || d->crit_viol) ? "(V)" : "", 430a7ff8297SAmit Kucheria d->low_viol, d->up_viol, d->crit_viol, 431a7ff8297SAmit Kucheria d->low_irq_clear, d->up_irq_clear, d->crit_irq_clear, 432a7ff8297SAmit Kucheria d->low_irq_mask, d->up_irq_mask, d->crit_irq_mask); 433a7ff8297SAmit Kucheria dev_dbg(priv->dev, "[%u] %s%s: thresh: (%d:%d:%d)\n", hw_id, __func__, 434a7ff8297SAmit Kucheria (d->up_viol || d->low_viol || d->crit_viol) ? "(V)" : "", 435a7ff8297SAmit Kucheria d->low_thresh, d->up_thresh, d->crit_thresh); 436a7ff8297SAmit Kucheria 437a7ff8297SAmit Kucheria return 0; 438a7ff8297SAmit Kucheria } 439a7ff8297SAmit Kucheria 440a7ff8297SAmit Kucheria static inline u32 masked_irq(u32 hw_id, u32 mask, enum tsens_ver ver) 441a7ff8297SAmit Kucheria { 442a7ff8297SAmit Kucheria if (ver > VER_1_X) 443a7ff8297SAmit Kucheria return mask & (1 << hw_id); 444a7ff8297SAmit Kucheria 445a7ff8297SAmit Kucheria /* v1, v0.1 don't have a irq mask register */ 446a7ff8297SAmit Kucheria return 0; 447a7ff8297SAmit Kucheria } 448a7ff8297SAmit Kucheria 449a7ff8297SAmit Kucheria /** 450a7ff8297SAmit Kucheria * tsens_critical_irq_thread() - Threaded handler for critical interrupts 451a7ff8297SAmit Kucheria * @irq: irq number 452a7ff8297SAmit Kucheria * @data: tsens controller private data 453a7ff8297SAmit Kucheria * 454a7ff8297SAmit Kucheria * Check FSM watchdog bark status and clear if needed. 455a7ff8297SAmit Kucheria * Check all sensors to find ones that violated their critical threshold limits. 456a7ff8297SAmit Kucheria * Clear and then re-enable the interrupt. 457a7ff8297SAmit Kucheria * 458a7ff8297SAmit Kucheria * The level-triggered interrupt might deassert if the temperature returned to 459a7ff8297SAmit Kucheria * within the threshold limits by the time the handler got scheduled. We 460a7ff8297SAmit Kucheria * consider the irq to have been handled in that case. 461a7ff8297SAmit Kucheria * 462a7ff8297SAmit Kucheria * Return: IRQ_HANDLED 463a7ff8297SAmit Kucheria */ 4643ecc8292SAmit Kucheria static irqreturn_t tsens_critical_irq_thread(int irq, void *data) 465a7ff8297SAmit Kucheria { 466a7ff8297SAmit Kucheria struct tsens_priv *priv = data; 467a7ff8297SAmit Kucheria struct tsens_irq_data d; 468a7ff8297SAmit Kucheria int temp, ret, i; 469a7ff8297SAmit Kucheria u32 wdog_status, wdog_count; 470a7ff8297SAmit Kucheria 471a7ff8297SAmit Kucheria if (priv->feat->has_watchdog) { 472a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[WDOG_BARK_STATUS], 473a7ff8297SAmit Kucheria &wdog_status); 474a7ff8297SAmit Kucheria if (ret) 475a7ff8297SAmit Kucheria return ret; 476a7ff8297SAmit Kucheria 477a7ff8297SAmit Kucheria if (wdog_status) { 478a7ff8297SAmit Kucheria /* Clear WDOG interrupt */ 479a7ff8297SAmit Kucheria regmap_field_write(priv->rf[WDOG_BARK_CLEAR], 1); 480a7ff8297SAmit Kucheria regmap_field_write(priv->rf[WDOG_BARK_CLEAR], 0); 481a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[WDOG_BARK_COUNT], 482a7ff8297SAmit Kucheria &wdog_count); 483a7ff8297SAmit Kucheria if (ret) 484a7ff8297SAmit Kucheria return ret; 485a7ff8297SAmit Kucheria if (wdog_count) 486a7ff8297SAmit Kucheria dev_dbg(priv->dev, "%s: watchdog count: %d\n", 487a7ff8297SAmit Kucheria __func__, wdog_count); 488a7ff8297SAmit Kucheria 489a7ff8297SAmit Kucheria /* Fall through to handle critical interrupts if any */ 490a7ff8297SAmit Kucheria } 491a7ff8297SAmit Kucheria } 492a7ff8297SAmit Kucheria 493a7ff8297SAmit Kucheria for (i = 0; i < priv->num_sensors; i++) { 494a7ff8297SAmit Kucheria const struct tsens_sensor *s = &priv->sensor[i]; 495a7ff8297SAmit Kucheria u32 hw_id = s->hw_id; 496a7ff8297SAmit Kucheria 497cf969218SAnsuel Smith if (!s->tzd) 498a7ff8297SAmit Kucheria continue; 499a7ff8297SAmit Kucheria if (!tsens_threshold_violated(priv, hw_id, &d)) 500a7ff8297SAmit Kucheria continue; 501a7ff8297SAmit Kucheria ret = get_temp_tsens_valid(s, &temp); 502a7ff8297SAmit Kucheria if (ret) { 503a7ff8297SAmit Kucheria dev_err(priv->dev, "[%u] %s: error reading sensor\n", 504a7ff8297SAmit Kucheria hw_id, __func__); 505a7ff8297SAmit Kucheria continue; 506a7ff8297SAmit Kucheria } 507a7ff8297SAmit Kucheria 508a7ff8297SAmit Kucheria tsens_read_irq_state(priv, hw_id, s, &d); 509a7ff8297SAmit Kucheria if (d.crit_viol && 510a7ff8297SAmit Kucheria !masked_irq(hw_id, d.crit_irq_mask, tsens_version(priv))) { 511a7ff8297SAmit Kucheria /* Mask critical interrupts, unused on Linux */ 512a7ff8297SAmit Kucheria tsens_set_interrupt(priv, hw_id, CRITICAL, false); 513a7ff8297SAmit Kucheria } 514a7ff8297SAmit Kucheria } 515a7ff8297SAmit Kucheria 516a7ff8297SAmit Kucheria return IRQ_HANDLED; 517a7ff8297SAmit Kucheria } 518a7ff8297SAmit Kucheria 519a7ff8297SAmit Kucheria /** 520a7ff8297SAmit Kucheria * tsens_irq_thread - Threaded interrupt handler for uplow interrupts 521a7ff8297SAmit Kucheria * @irq: irq number 522a7ff8297SAmit Kucheria * @data: tsens controller private data 523a7ff8297SAmit Kucheria * 524a7ff8297SAmit Kucheria * Check all sensors to find ones that violated their threshold limits. If the 525a7ff8297SAmit Kucheria * temperature is still outside the limits, call thermal_zone_device_update() to 526a7ff8297SAmit Kucheria * update the thresholds, else re-enable the interrupts. 527a7ff8297SAmit Kucheria * 528a7ff8297SAmit Kucheria * The level-triggered interrupt might deassert if the temperature returned to 529a7ff8297SAmit Kucheria * within the threshold limits by the time the handler got scheduled. We 530a7ff8297SAmit Kucheria * consider the irq to have been handled in that case. 531a7ff8297SAmit Kucheria * 532a7ff8297SAmit Kucheria * Return: IRQ_HANDLED 533a7ff8297SAmit Kucheria */ 5343ecc8292SAmit Kucheria static irqreturn_t tsens_irq_thread(int irq, void *data) 535a7ff8297SAmit Kucheria { 536a7ff8297SAmit Kucheria struct tsens_priv *priv = data; 537a7ff8297SAmit Kucheria struct tsens_irq_data d; 538a7ff8297SAmit Kucheria bool enable = true, disable = false; 539a7ff8297SAmit Kucheria unsigned long flags; 540a7ff8297SAmit Kucheria int temp, ret, i; 541a7ff8297SAmit Kucheria 542a7ff8297SAmit Kucheria for (i = 0; i < priv->num_sensors; i++) { 543a7ff8297SAmit Kucheria bool trigger = false; 544a7ff8297SAmit Kucheria const struct tsens_sensor *s = &priv->sensor[i]; 545a7ff8297SAmit Kucheria u32 hw_id = s->hw_id; 546a7ff8297SAmit Kucheria 547cf969218SAnsuel Smith if (!s->tzd) 548a7ff8297SAmit Kucheria continue; 549a7ff8297SAmit Kucheria if (!tsens_threshold_violated(priv, hw_id, &d)) 550a7ff8297SAmit Kucheria continue; 551a7ff8297SAmit Kucheria ret = get_temp_tsens_valid(s, &temp); 552a7ff8297SAmit Kucheria if (ret) { 553a7ff8297SAmit Kucheria dev_err(priv->dev, "[%u] %s: error reading sensor\n", 554a7ff8297SAmit Kucheria hw_id, __func__); 555a7ff8297SAmit Kucheria continue; 556a7ff8297SAmit Kucheria } 557a7ff8297SAmit Kucheria 558a7ff8297SAmit Kucheria spin_lock_irqsave(&priv->ul_lock, flags); 559a7ff8297SAmit Kucheria 560a7ff8297SAmit Kucheria tsens_read_irq_state(priv, hw_id, s, &d); 561a7ff8297SAmit Kucheria 562a7ff8297SAmit Kucheria if (d.up_viol && 563a7ff8297SAmit Kucheria !masked_irq(hw_id, d.up_irq_mask, tsens_version(priv))) { 564a7ff8297SAmit Kucheria tsens_set_interrupt(priv, hw_id, UPPER, disable); 565a7ff8297SAmit Kucheria if (d.up_thresh > temp) { 566a7ff8297SAmit Kucheria dev_dbg(priv->dev, "[%u] %s: re-arm upper\n", 567a7ff8297SAmit Kucheria hw_id, __func__); 568a7ff8297SAmit Kucheria tsens_set_interrupt(priv, hw_id, UPPER, enable); 569a7ff8297SAmit Kucheria } else { 570a7ff8297SAmit Kucheria trigger = true; 571a7ff8297SAmit Kucheria /* Keep irq masked */ 572a7ff8297SAmit Kucheria } 573a7ff8297SAmit Kucheria } else if (d.low_viol && 574a7ff8297SAmit Kucheria !masked_irq(hw_id, d.low_irq_mask, tsens_version(priv))) { 575a7ff8297SAmit Kucheria tsens_set_interrupt(priv, hw_id, LOWER, disable); 576a7ff8297SAmit Kucheria if (d.low_thresh < temp) { 577a7ff8297SAmit Kucheria dev_dbg(priv->dev, "[%u] %s: re-arm low\n", 578a7ff8297SAmit Kucheria hw_id, __func__); 579a7ff8297SAmit Kucheria tsens_set_interrupt(priv, hw_id, LOWER, enable); 580a7ff8297SAmit Kucheria } else { 581a7ff8297SAmit Kucheria trigger = true; 582a7ff8297SAmit Kucheria /* Keep irq masked */ 583a7ff8297SAmit Kucheria } 584a7ff8297SAmit Kucheria } 585a7ff8297SAmit Kucheria 586a7ff8297SAmit Kucheria spin_unlock_irqrestore(&priv->ul_lock, flags); 587a7ff8297SAmit Kucheria 588a7ff8297SAmit Kucheria if (trigger) { 589a7ff8297SAmit Kucheria dev_dbg(priv->dev, "[%u] %s: TZ update trigger (%d mC)\n", 590a7ff8297SAmit Kucheria hw_id, __func__, temp); 591a7ff8297SAmit Kucheria thermal_zone_device_update(s->tzd, 592a7ff8297SAmit Kucheria THERMAL_EVENT_UNSPECIFIED); 593a7ff8297SAmit Kucheria } else { 594a7ff8297SAmit Kucheria dev_dbg(priv->dev, "[%u] %s: no violation: %d\n", 595a7ff8297SAmit Kucheria hw_id, __func__, temp); 596a7ff8297SAmit Kucheria } 59753e2a20eSAnsuel Smith 59853e2a20eSAnsuel Smith if (tsens_version(priv) < VER_0_1) { 59953e2a20eSAnsuel Smith /* Constraint: There is only 1 interrupt control register for all 60053e2a20eSAnsuel Smith * 11 temperature sensor. So monitoring more than 1 sensor based 60153e2a20eSAnsuel Smith * on interrupts will yield inconsistent result. To overcome this 60253e2a20eSAnsuel Smith * issue we will monitor only sensor 0 which is the master sensor. 60353e2a20eSAnsuel Smith */ 60453e2a20eSAnsuel Smith break; 60553e2a20eSAnsuel Smith } 606a7ff8297SAmit Kucheria } 607a7ff8297SAmit Kucheria 608a7ff8297SAmit Kucheria return IRQ_HANDLED; 609a7ff8297SAmit Kucheria } 610a7ff8297SAmit Kucheria 6114360af35SRobert Marko /** 6124360af35SRobert Marko * tsens_combined_irq_thread() - Threaded interrupt handler for combined interrupts 6134360af35SRobert Marko * @irq: irq number 6144360af35SRobert Marko * @data: tsens controller private data 6154360af35SRobert Marko * 6164360af35SRobert Marko * Handle the combined interrupt as if it were 2 separate interrupts, so call the 6174360af35SRobert Marko * critical handler first and then the up/low one. 6184360af35SRobert Marko * 6194360af35SRobert Marko * Return: IRQ_HANDLED 6204360af35SRobert Marko */ 6214360af35SRobert Marko static irqreturn_t tsens_combined_irq_thread(int irq, void *data) 6224360af35SRobert Marko { 6234360af35SRobert Marko irqreturn_t ret; 6244360af35SRobert Marko 6254360af35SRobert Marko ret = tsens_critical_irq_thread(irq, data); 6264360af35SRobert Marko if (ret != IRQ_HANDLED) 6274360af35SRobert Marko return ret; 6284360af35SRobert Marko 6294360af35SRobert Marko return tsens_irq_thread(irq, data); 6304360af35SRobert Marko } 6314360af35SRobert Marko 632ca1b9a9eSDaniel Lezcano static int tsens_set_trips(struct thermal_zone_device *tz, int low, int high) 633a7ff8297SAmit Kucheria { 634ca1b9a9eSDaniel Lezcano struct tsens_sensor *s = tz->devdata; 635a7ff8297SAmit Kucheria struct tsens_priv *priv = s->priv; 636a7ff8297SAmit Kucheria struct device *dev = priv->dev; 637a7ff8297SAmit Kucheria struct tsens_irq_data d; 638a7ff8297SAmit Kucheria unsigned long flags; 639a7ff8297SAmit Kucheria int high_val, low_val, cl_high, cl_low; 640a7ff8297SAmit Kucheria u32 hw_id = s->hw_id; 641a7ff8297SAmit Kucheria 64253e2a20eSAnsuel Smith if (tsens_version(priv) < VER_0_1) { 64353e2a20eSAnsuel Smith /* Pre v0.1 IP had a single register for each type of interrupt 64453e2a20eSAnsuel Smith * and thresholds 64553e2a20eSAnsuel Smith */ 64653e2a20eSAnsuel Smith hw_id = 0; 64753e2a20eSAnsuel Smith } 64853e2a20eSAnsuel Smith 649a7ff8297SAmit Kucheria dev_dbg(dev, "[%u] %s: proposed thresholds: (%d:%d)\n", 650a7ff8297SAmit Kucheria hw_id, __func__, low, high); 651a7ff8297SAmit Kucheria 652f63bacedSRobert Marko cl_high = clamp_val(high, priv->feat->trip_min_temp, priv->feat->trip_max_temp); 653f63bacedSRobert Marko cl_low = clamp_val(low, priv->feat->trip_min_temp, priv->feat->trip_max_temp); 654a7ff8297SAmit Kucheria 655a7ff8297SAmit Kucheria high_val = tsens_mC_to_hw(s, cl_high); 656a7ff8297SAmit Kucheria low_val = tsens_mC_to_hw(s, cl_low); 657a7ff8297SAmit Kucheria 658a7ff8297SAmit Kucheria spin_lock_irqsave(&priv->ul_lock, flags); 659a7ff8297SAmit Kucheria 660a7ff8297SAmit Kucheria tsens_read_irq_state(priv, hw_id, s, &d); 661a7ff8297SAmit Kucheria 662a7ff8297SAmit Kucheria /* Write the new thresholds and clear the status */ 663a7ff8297SAmit Kucheria regmap_field_write(priv->rf[LOW_THRESH_0 + hw_id], low_val); 664a7ff8297SAmit Kucheria regmap_field_write(priv->rf[UP_THRESH_0 + hw_id], high_val); 665a7ff8297SAmit Kucheria tsens_set_interrupt(priv, hw_id, LOWER, true); 666a7ff8297SAmit Kucheria tsens_set_interrupt(priv, hw_id, UPPER, true); 667a7ff8297SAmit Kucheria 668a7ff8297SAmit Kucheria spin_unlock_irqrestore(&priv->ul_lock, flags); 669a7ff8297SAmit Kucheria 670a7ff8297SAmit Kucheria dev_dbg(dev, "[%u] %s: (%d:%d)->(%d:%d)\n", 671a7ff8297SAmit Kucheria hw_id, __func__, d.low_thresh, d.up_thresh, cl_low, cl_high); 672a7ff8297SAmit Kucheria 673a7ff8297SAmit Kucheria return 0; 674a7ff8297SAmit Kucheria } 675a7ff8297SAmit Kucheria 6763ecc8292SAmit Kucheria static int tsens_enable_irq(struct tsens_priv *priv) 677a7ff8297SAmit Kucheria { 678a7ff8297SAmit Kucheria int ret; 679a7ff8297SAmit Kucheria int val = tsens_version(priv) > VER_1_X ? 7 : 1; 680a7ff8297SAmit Kucheria 681a7ff8297SAmit Kucheria ret = regmap_field_write(priv->rf[INT_EN], val); 682a7ff8297SAmit Kucheria if (ret < 0) 683a7ff8297SAmit Kucheria dev_err(priv->dev, "%s: failed to enable interrupts\n", 684a7ff8297SAmit Kucheria __func__); 685a7ff8297SAmit Kucheria 686a7ff8297SAmit Kucheria return ret; 687a7ff8297SAmit Kucheria } 688a7ff8297SAmit Kucheria 6893ecc8292SAmit Kucheria static void tsens_disable_irq(struct tsens_priv *priv) 690a7ff8297SAmit Kucheria { 691a7ff8297SAmit Kucheria regmap_field_write(priv->rf[INT_EN], 0); 692a7ff8297SAmit Kucheria } 693a7ff8297SAmit Kucheria 694a7ff8297SAmit Kucheria int get_temp_tsens_valid(const struct tsens_sensor *s, int *temp) 695a7ff8297SAmit Kucheria { 696a7ff8297SAmit Kucheria struct tsens_priv *priv = s->priv; 697a7ff8297SAmit Kucheria int hw_id = s->hw_id; 698a7ff8297SAmit Kucheria u32 temp_idx = LAST_TEMP_0 + hw_id; 699a7ff8297SAmit Kucheria u32 valid_idx = VALID_0 + hw_id; 700a7ff8297SAmit Kucheria u32 valid; 701a7ff8297SAmit Kucheria int ret; 702a7ff8297SAmit Kucheria 70353e2a20eSAnsuel Smith /* VER_0 doesn't have VALID bit */ 704d012f918SAnsuel Smith if (tsens_version(priv) == VER_0) 705d012f918SAnsuel Smith goto get_temp; 706d012f918SAnsuel Smith 707a7ff8297SAmit Kucheria /* Valid bit is 0 for 6 AHB clock cycles. 708a7ff8297SAmit Kucheria * At 19.2MHz, 1 AHB clock is ~60ns. 709a7ff8297SAmit Kucheria * We should enter this loop very, very rarely. 710d012f918SAnsuel Smith * Wait 1 us since it's the min of poll_timeout macro. 711d012f918SAnsuel Smith * Old value was 400 ns. 712a7ff8297SAmit Kucheria */ 713d012f918SAnsuel Smith ret = regmap_field_read_poll_timeout(priv->rf[valid_idx], valid, 714d012f918SAnsuel Smith valid, 1, 20 * USEC_PER_MSEC); 715a7ff8297SAmit Kucheria if (ret) 716a7ff8297SAmit Kucheria return ret; 717a7ff8297SAmit Kucheria 718d012f918SAnsuel Smith get_temp: 719a7ff8297SAmit Kucheria /* Valid bit is set, OK to read the temperature */ 720a7ff8297SAmit Kucheria *temp = tsens_hw_to_mC(s, temp_idx); 721a7ff8297SAmit Kucheria 722a7ff8297SAmit Kucheria return 0; 723a7ff8297SAmit Kucheria } 724a7ff8297SAmit Kucheria 725a7ff8297SAmit Kucheria int get_temp_common(const struct tsens_sensor *s, int *temp) 726a7ff8297SAmit Kucheria { 727a7ff8297SAmit Kucheria struct tsens_priv *priv = s->priv; 728a7ff8297SAmit Kucheria int hw_id = s->hw_id; 72953e2a20eSAnsuel Smith int last_temp = 0, ret, trdy; 73053e2a20eSAnsuel Smith unsigned long timeout; 73153e2a20eSAnsuel Smith 73253e2a20eSAnsuel Smith timeout = jiffies + usecs_to_jiffies(TIMEOUT_US); 73353e2a20eSAnsuel Smith do { 73453e2a20eSAnsuel Smith if (tsens_version(priv) == VER_0) { 73553e2a20eSAnsuel Smith ret = regmap_field_read(priv->rf[TRDY], &trdy); 73653e2a20eSAnsuel Smith if (ret) 73753e2a20eSAnsuel Smith return ret; 73853e2a20eSAnsuel Smith if (!trdy) 73953e2a20eSAnsuel Smith continue; 74053e2a20eSAnsuel Smith } 741a7ff8297SAmit Kucheria 742a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[LAST_TEMP_0 + hw_id], &last_temp); 743a7ff8297SAmit Kucheria if (ret) 744a7ff8297SAmit Kucheria return ret; 745a7ff8297SAmit Kucheria 746a7ff8297SAmit Kucheria *temp = code_to_degc(last_temp, s) * 1000; 747a7ff8297SAmit Kucheria 748a7ff8297SAmit Kucheria return 0; 74953e2a20eSAnsuel Smith } while (time_before(jiffies, timeout)); 75053e2a20eSAnsuel Smith 75153e2a20eSAnsuel Smith return -ETIMEDOUT; 752a7ff8297SAmit Kucheria } 753a7ff8297SAmit Kucheria 754a7ff8297SAmit Kucheria #ifdef CONFIG_DEBUG_FS 755a7ff8297SAmit Kucheria static int dbg_sensors_show(struct seq_file *s, void *data) 756a7ff8297SAmit Kucheria { 757a7ff8297SAmit Kucheria struct platform_device *pdev = s->private; 758a7ff8297SAmit Kucheria struct tsens_priv *priv = platform_get_drvdata(pdev); 759a7ff8297SAmit Kucheria int i; 760a7ff8297SAmit Kucheria 761a7ff8297SAmit Kucheria seq_printf(s, "max: %2d\nnum: %2d\n\n", 762a7ff8297SAmit Kucheria priv->feat->max_sensors, priv->num_sensors); 763a7ff8297SAmit Kucheria 764a7ff8297SAmit Kucheria seq_puts(s, " id slope offset\n--------------------------\n"); 765a7ff8297SAmit Kucheria for (i = 0; i < priv->num_sensors; i++) { 766a7ff8297SAmit Kucheria seq_printf(s, "%8d %8d %8d\n", priv->sensor[i].hw_id, 767a7ff8297SAmit Kucheria priv->sensor[i].slope, priv->sensor[i].offset); 768a7ff8297SAmit Kucheria } 769a7ff8297SAmit Kucheria 770a7ff8297SAmit Kucheria return 0; 771a7ff8297SAmit Kucheria } 772a7ff8297SAmit Kucheria 773a7ff8297SAmit Kucheria static int dbg_version_show(struct seq_file *s, void *data) 774a7ff8297SAmit Kucheria { 775a7ff8297SAmit Kucheria struct platform_device *pdev = s->private; 776a7ff8297SAmit Kucheria struct tsens_priv *priv = platform_get_drvdata(pdev); 777a7ff8297SAmit Kucheria u32 maj_ver, min_ver, step_ver; 778a7ff8297SAmit Kucheria int ret; 779a7ff8297SAmit Kucheria 780a7ff8297SAmit Kucheria if (tsens_version(priv) > VER_0_1) { 781a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[VER_MAJOR], &maj_ver); 782a7ff8297SAmit Kucheria if (ret) 783a7ff8297SAmit Kucheria return ret; 784a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[VER_MINOR], &min_ver); 785a7ff8297SAmit Kucheria if (ret) 786a7ff8297SAmit Kucheria return ret; 787a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[VER_STEP], &step_ver); 788a7ff8297SAmit Kucheria if (ret) 789a7ff8297SAmit Kucheria return ret; 790a7ff8297SAmit Kucheria seq_printf(s, "%d.%d.%d\n", maj_ver, min_ver, step_ver); 791a7ff8297SAmit Kucheria } else { 792c7e077e9SChristian Marangi seq_printf(s, "0.%d.0\n", priv->feat->ver_major); 793a7ff8297SAmit Kucheria } 794a7ff8297SAmit Kucheria 795a7ff8297SAmit Kucheria return 0; 796a7ff8297SAmit Kucheria } 797a7ff8297SAmit Kucheria 798a7ff8297SAmit Kucheria DEFINE_SHOW_ATTRIBUTE(dbg_version); 799a7ff8297SAmit Kucheria DEFINE_SHOW_ATTRIBUTE(dbg_sensors); 800a7ff8297SAmit Kucheria 801a7ff8297SAmit Kucheria static void tsens_debug_init(struct platform_device *pdev) 802a7ff8297SAmit Kucheria { 803a7ff8297SAmit Kucheria struct tsens_priv *priv = platform_get_drvdata(pdev); 804a7ff8297SAmit Kucheria 80589992d95SChristian Marangi priv->debug_root = debugfs_lookup("tsens", NULL); 80689992d95SChristian Marangi if (!priv->debug_root) 807a7ff8297SAmit Kucheria priv->debug_root = debugfs_create_dir("tsens", NULL); 808a7ff8297SAmit Kucheria 809a7ff8297SAmit Kucheria /* A directory for each instance of the TSENS IP */ 810a7ff8297SAmit Kucheria priv->debug = debugfs_create_dir(dev_name(&pdev->dev), priv->debug_root); 81189992d95SChristian Marangi debugfs_create_file("version", 0444, priv->debug, pdev, &dbg_version_fops); 812a7ff8297SAmit Kucheria debugfs_create_file("sensors", 0444, priv->debug, pdev, &dbg_sensors_fops); 813a7ff8297SAmit Kucheria } 814a7ff8297SAmit Kucheria #else 815a7ff8297SAmit Kucheria static inline void tsens_debug_init(struct platform_device *pdev) {} 816a7ff8297SAmit Kucheria #endif 817a7ff8297SAmit Kucheria 818a7ff8297SAmit Kucheria static const struct regmap_config tsens_config = { 819a7ff8297SAmit Kucheria .name = "tm", 820a7ff8297SAmit Kucheria .reg_bits = 32, 821a7ff8297SAmit Kucheria .val_bits = 32, 822a7ff8297SAmit Kucheria .reg_stride = 4, 823a7ff8297SAmit Kucheria }; 824a7ff8297SAmit Kucheria 825a7ff8297SAmit Kucheria static const struct regmap_config tsens_srot_config = { 826a7ff8297SAmit Kucheria .name = "srot", 827a7ff8297SAmit Kucheria .reg_bits = 32, 828a7ff8297SAmit Kucheria .val_bits = 32, 829a7ff8297SAmit Kucheria .reg_stride = 4, 830a7ff8297SAmit Kucheria }; 831a7ff8297SAmit Kucheria 832a7ff8297SAmit Kucheria int __init init_common(struct tsens_priv *priv) 833a7ff8297SAmit Kucheria { 834a7ff8297SAmit Kucheria void __iomem *tm_base, *srot_base; 835a7ff8297SAmit Kucheria struct device *dev = priv->dev; 836a7ff8297SAmit Kucheria u32 ver_minor; 837a7ff8297SAmit Kucheria struct resource *res; 838a7ff8297SAmit Kucheria u32 enabled; 839a7ff8297SAmit Kucheria int ret, i, j; 840a7ff8297SAmit Kucheria struct platform_device *op = of_find_device_by_node(priv->dev->of_node); 841a7ff8297SAmit Kucheria 842a7ff8297SAmit Kucheria if (!op) 843a7ff8297SAmit Kucheria return -EINVAL; 844a7ff8297SAmit Kucheria 845a7ff8297SAmit Kucheria if (op->num_resources > 1) { 846a7ff8297SAmit Kucheria /* DT with separate SROT and TM address space */ 847a7ff8297SAmit Kucheria priv->tm_offset = 0; 848a7ff8297SAmit Kucheria res = platform_get_resource(op, IORESOURCE_MEM, 1); 849a7ff8297SAmit Kucheria srot_base = devm_ioremap_resource(dev, res); 850a7ff8297SAmit Kucheria if (IS_ERR(srot_base)) { 851a7ff8297SAmit Kucheria ret = PTR_ERR(srot_base); 852a7ff8297SAmit Kucheria goto err_put_device; 853a7ff8297SAmit Kucheria } 854a7ff8297SAmit Kucheria 855a7ff8297SAmit Kucheria priv->srot_map = devm_regmap_init_mmio(dev, srot_base, 856a7ff8297SAmit Kucheria &tsens_srot_config); 857a7ff8297SAmit Kucheria if (IS_ERR(priv->srot_map)) { 858a7ff8297SAmit Kucheria ret = PTR_ERR(priv->srot_map); 859a7ff8297SAmit Kucheria goto err_put_device; 860a7ff8297SAmit Kucheria } 861a7ff8297SAmit Kucheria } else { 862a7ff8297SAmit Kucheria /* old DTs where SROT and TM were in a contiguous 2K block */ 863a7ff8297SAmit Kucheria priv->tm_offset = 0x1000; 864a7ff8297SAmit Kucheria } 865a7ff8297SAmit Kucheria 86653e2a20eSAnsuel Smith if (tsens_version(priv) >= VER_0_1) { 867a7ff8297SAmit Kucheria res = platform_get_resource(op, IORESOURCE_MEM, 0); 868a7ff8297SAmit Kucheria tm_base = devm_ioremap_resource(dev, res); 869a7ff8297SAmit Kucheria if (IS_ERR(tm_base)) { 870a7ff8297SAmit Kucheria ret = PTR_ERR(tm_base); 871a7ff8297SAmit Kucheria goto err_put_device; 872a7ff8297SAmit Kucheria } 873a7ff8297SAmit Kucheria 874a7ff8297SAmit Kucheria priv->tm_map = devm_regmap_init_mmio(dev, tm_base, &tsens_config); 87553e2a20eSAnsuel Smith } else { /* VER_0 share the same gcc regs using a syscon */ 87653e2a20eSAnsuel Smith struct device *parent = priv->dev->parent; 87753e2a20eSAnsuel Smith 87853e2a20eSAnsuel Smith if (parent) 87953e2a20eSAnsuel Smith priv->tm_map = syscon_node_to_regmap(parent->of_node); 88053e2a20eSAnsuel Smith } 88153e2a20eSAnsuel Smith 88253e2a20eSAnsuel Smith if (IS_ERR_OR_NULL(priv->tm_map)) { 88353e2a20eSAnsuel Smith if (!priv->tm_map) 88453e2a20eSAnsuel Smith ret = -ENODEV; 88553e2a20eSAnsuel Smith else 886a7ff8297SAmit Kucheria ret = PTR_ERR(priv->tm_map); 887a7ff8297SAmit Kucheria goto err_put_device; 888a7ff8297SAmit Kucheria } 889a7ff8297SAmit Kucheria 89053e2a20eSAnsuel Smith /* VER_0 have only tm_map */ 89153e2a20eSAnsuel Smith if (!priv->srot_map) 89253e2a20eSAnsuel Smith priv->srot_map = priv->tm_map; 89353e2a20eSAnsuel Smith 894a7ff8297SAmit Kucheria if (tsens_version(priv) > VER_0_1) { 895a7ff8297SAmit Kucheria for (i = VER_MAJOR; i <= VER_STEP; i++) { 896a7ff8297SAmit Kucheria priv->rf[i] = devm_regmap_field_alloc(dev, priv->srot_map, 897a7ff8297SAmit Kucheria priv->fields[i]); 898f4136863SGuangqing Zhu if (IS_ERR(priv->rf[i])) { 899f4136863SGuangqing Zhu ret = PTR_ERR(priv->rf[i]); 900f4136863SGuangqing Zhu goto err_put_device; 901f4136863SGuangqing Zhu } 902a7ff8297SAmit Kucheria } 903a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[VER_MINOR], &ver_minor); 904a7ff8297SAmit Kucheria if (ret) 905a7ff8297SAmit Kucheria goto err_put_device; 906a7ff8297SAmit Kucheria } 907a7ff8297SAmit Kucheria 908a7ff8297SAmit Kucheria priv->rf[TSENS_EN] = devm_regmap_field_alloc(dev, priv->srot_map, 909a7ff8297SAmit Kucheria priv->fields[TSENS_EN]); 910a7ff8297SAmit Kucheria if (IS_ERR(priv->rf[TSENS_EN])) { 911a7ff8297SAmit Kucheria ret = PTR_ERR(priv->rf[TSENS_EN]); 912a7ff8297SAmit Kucheria goto err_put_device; 913a7ff8297SAmit Kucheria } 91453e2a20eSAnsuel Smith /* in VER_0 TSENS need to be explicitly enabled */ 91553e2a20eSAnsuel Smith if (tsens_version(priv) == VER_0) 91653e2a20eSAnsuel Smith regmap_field_write(priv->rf[TSENS_EN], 1); 91753e2a20eSAnsuel Smith 918a7ff8297SAmit Kucheria ret = regmap_field_read(priv->rf[TSENS_EN], &enabled); 919a7ff8297SAmit Kucheria if (ret) 920a7ff8297SAmit Kucheria goto err_put_device; 921a7ff8297SAmit Kucheria if (!enabled) { 922a7ff8297SAmit Kucheria dev_err(dev, "%s: device not enabled\n", __func__); 923a7ff8297SAmit Kucheria ret = -ENODEV; 924a7ff8297SAmit Kucheria goto err_put_device; 925a7ff8297SAmit Kucheria } 926a7ff8297SAmit Kucheria 927a7ff8297SAmit Kucheria priv->rf[SENSOR_EN] = devm_regmap_field_alloc(dev, priv->srot_map, 928a7ff8297SAmit Kucheria priv->fields[SENSOR_EN]); 929a7ff8297SAmit Kucheria if (IS_ERR(priv->rf[SENSOR_EN])) { 930a7ff8297SAmit Kucheria ret = PTR_ERR(priv->rf[SENSOR_EN]); 931a7ff8297SAmit Kucheria goto err_put_device; 932a7ff8297SAmit Kucheria } 933a7ff8297SAmit Kucheria priv->rf[INT_EN] = devm_regmap_field_alloc(dev, priv->tm_map, 934a7ff8297SAmit Kucheria priv->fields[INT_EN]); 935a7ff8297SAmit Kucheria if (IS_ERR(priv->rf[INT_EN])) { 936a7ff8297SAmit Kucheria ret = PTR_ERR(priv->rf[INT_EN]); 937a7ff8297SAmit Kucheria goto err_put_device; 938a7ff8297SAmit Kucheria } 939a7ff8297SAmit Kucheria 94053e2a20eSAnsuel Smith priv->rf[TSENS_SW_RST] = 94153e2a20eSAnsuel Smith devm_regmap_field_alloc(dev, priv->srot_map, priv->fields[TSENS_SW_RST]); 94253e2a20eSAnsuel Smith if (IS_ERR(priv->rf[TSENS_SW_RST])) { 94353e2a20eSAnsuel Smith ret = PTR_ERR(priv->rf[TSENS_SW_RST]); 94453e2a20eSAnsuel Smith goto err_put_device; 94553e2a20eSAnsuel Smith } 94653e2a20eSAnsuel Smith 94753e2a20eSAnsuel Smith priv->rf[TRDY] = devm_regmap_field_alloc(dev, priv->tm_map, priv->fields[TRDY]); 94853e2a20eSAnsuel Smith if (IS_ERR(priv->rf[TRDY])) { 94953e2a20eSAnsuel Smith ret = PTR_ERR(priv->rf[TRDY]); 95053e2a20eSAnsuel Smith goto err_put_device; 95153e2a20eSAnsuel Smith } 95253e2a20eSAnsuel Smith 953a7ff8297SAmit Kucheria /* This loop might need changes if enum regfield_ids is reordered */ 954a7ff8297SAmit Kucheria for (j = LAST_TEMP_0; j <= UP_THRESH_15; j += 16) { 955a7ff8297SAmit Kucheria for (i = 0; i < priv->feat->max_sensors; i++) { 956a7ff8297SAmit Kucheria int idx = j + i; 957a7ff8297SAmit Kucheria 958a7ff8297SAmit Kucheria priv->rf[idx] = devm_regmap_field_alloc(dev, 959a7ff8297SAmit Kucheria priv->tm_map, 960a7ff8297SAmit Kucheria priv->fields[idx]); 961a7ff8297SAmit Kucheria if (IS_ERR(priv->rf[idx])) { 962a7ff8297SAmit Kucheria ret = PTR_ERR(priv->rf[idx]); 963a7ff8297SAmit Kucheria goto err_put_device; 964a7ff8297SAmit Kucheria } 965a7ff8297SAmit Kucheria } 966a7ff8297SAmit Kucheria } 967a7ff8297SAmit Kucheria 96853e2a20eSAnsuel Smith if (priv->feat->crit_int || tsens_version(priv) < VER_0_1) { 969a7ff8297SAmit Kucheria /* Loop might need changes if enum regfield_ids is reordered */ 970a7ff8297SAmit Kucheria for (j = CRITICAL_STATUS_0; j <= CRIT_THRESH_15; j += 16) { 971a7ff8297SAmit Kucheria for (i = 0; i < priv->feat->max_sensors; i++) { 972a7ff8297SAmit Kucheria int idx = j + i; 973a7ff8297SAmit Kucheria 974a7ff8297SAmit Kucheria priv->rf[idx] = 975a7ff8297SAmit Kucheria devm_regmap_field_alloc(dev, 976a7ff8297SAmit Kucheria priv->tm_map, 977a7ff8297SAmit Kucheria priv->fields[idx]); 978a7ff8297SAmit Kucheria if (IS_ERR(priv->rf[idx])) { 979a7ff8297SAmit Kucheria ret = PTR_ERR(priv->rf[idx]); 980a7ff8297SAmit Kucheria goto err_put_device; 981a7ff8297SAmit Kucheria } 982a7ff8297SAmit Kucheria } 983a7ff8297SAmit Kucheria } 984a7ff8297SAmit Kucheria } 985a7ff8297SAmit Kucheria 986a7ff8297SAmit Kucheria if (tsens_version(priv) > VER_1_X && ver_minor > 2) { 987a7ff8297SAmit Kucheria /* Watchdog is present only on v2.3+ */ 988a7ff8297SAmit Kucheria priv->feat->has_watchdog = 1; 989a7ff8297SAmit Kucheria for (i = WDOG_BARK_STATUS; i <= CC_MON_MASK; i++) { 990a7ff8297SAmit Kucheria priv->rf[i] = devm_regmap_field_alloc(dev, priv->tm_map, 991a7ff8297SAmit Kucheria priv->fields[i]); 992a7ff8297SAmit Kucheria if (IS_ERR(priv->rf[i])) { 993a7ff8297SAmit Kucheria ret = PTR_ERR(priv->rf[i]); 994a7ff8297SAmit Kucheria goto err_put_device; 995a7ff8297SAmit Kucheria } 996a7ff8297SAmit Kucheria } 997a7ff8297SAmit Kucheria /* 998a7ff8297SAmit Kucheria * Watchdog is already enabled, unmask the bark. 999a7ff8297SAmit Kucheria * Disable cycle completion monitoring 1000a7ff8297SAmit Kucheria */ 1001a7ff8297SAmit Kucheria regmap_field_write(priv->rf[WDOG_BARK_MASK], 0); 1002a7ff8297SAmit Kucheria regmap_field_write(priv->rf[CC_MON_MASK], 1); 1003a7ff8297SAmit Kucheria } 1004a7ff8297SAmit Kucheria 1005a7ff8297SAmit Kucheria spin_lock_init(&priv->ul_lock); 100653e2a20eSAnsuel Smith 100753e2a20eSAnsuel Smith /* VER_0 interrupt doesn't need to be enabled */ 100853e2a20eSAnsuel Smith if (tsens_version(priv) >= VER_0_1) 1009a7ff8297SAmit Kucheria tsens_enable_irq(priv); 101053e2a20eSAnsuel Smith 1011a7ff8297SAmit Kucheria err_put_device: 1012a7ff8297SAmit Kucheria put_device(&op->dev); 1013a7ff8297SAmit Kucheria return ret; 1014a7ff8297SAmit Kucheria } 1015a7ff8297SAmit Kucheria 1016ca1b9a9eSDaniel Lezcano static int tsens_get_temp(struct thermal_zone_device *tz, int *temp) 10179066073cSRajendra Nayak { 1018ca1b9a9eSDaniel Lezcano struct tsens_sensor *s = tz->devdata; 101969b628acSAmit Kucheria struct tsens_priv *priv = s->priv; 10209066073cSRajendra Nayak 10218b71bce4SAmit Kucheria return priv->ops->get_temp(s, temp); 10229066073cSRajendra Nayak } 10239066073cSRajendra Nayak 10245b97469aSArnd Bergmann static int __maybe_unused tsens_suspend(struct device *dev) 10259066073cSRajendra Nayak { 102669b628acSAmit Kucheria struct tsens_priv *priv = dev_get_drvdata(dev); 10279066073cSRajendra Nayak 102869b628acSAmit Kucheria if (priv->ops && priv->ops->suspend) 102969b628acSAmit Kucheria return priv->ops->suspend(priv); 10309066073cSRajendra Nayak 10319066073cSRajendra Nayak return 0; 10329066073cSRajendra Nayak } 10339066073cSRajendra Nayak 10345b97469aSArnd Bergmann static int __maybe_unused tsens_resume(struct device *dev) 10359066073cSRajendra Nayak { 103669b628acSAmit Kucheria struct tsens_priv *priv = dev_get_drvdata(dev); 10379066073cSRajendra Nayak 103869b628acSAmit Kucheria if (priv->ops && priv->ops->resume) 103969b628acSAmit Kucheria return priv->ops->resume(priv); 10409066073cSRajendra Nayak 10419066073cSRajendra Nayak return 0; 10429066073cSRajendra Nayak } 10439066073cSRajendra Nayak 10449066073cSRajendra Nayak static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume); 10459066073cSRajendra Nayak 10469066073cSRajendra Nayak static const struct of_device_id tsens_table[] = { 10479066073cSRajendra Nayak { 10486b3aeafbSAnsuel Smith .compatible = "qcom,ipq8064-tsens", 10496b3aeafbSAnsuel Smith .data = &data_8960, 10506b3aeafbSAnsuel Smith }, { 10516840455dSRobert Marko .compatible = "qcom,ipq8074-tsens", 10526840455dSRobert Marko .data = &data_ipq8074, 10536840455dSRobert Marko }, { 1054a2149ab8SKonrad Dybcio .compatible = "qcom,mdm9607-tsens", 1055a2149ab8SKonrad Dybcio .data = &data_9607, 1056a2149ab8SKonrad Dybcio }, { 10579066073cSRajendra Nayak .compatible = "qcom,msm8916-tsens", 1058840a5bd3SRajendra Nayak .data = &data_8916, 10599066073cSRajendra Nayak }, { 1060332bc8ebSShawn Guo .compatible = "qcom,msm8939-tsens", 1061332bc8ebSShawn Guo .data = &data_8939, 1062332bc8ebSShawn Guo }, { 1063a7d3006bSDmitry Baryshkov .compatible = "qcom,msm8956-tsens", 1064a7d3006bSDmitry Baryshkov .data = &data_8956, 1065a7d3006bSDmitry Baryshkov }, { 10662caf7396SDmitry Baryshkov .compatible = "qcom,msm8960-tsens", 10672caf7396SDmitry Baryshkov .data = &data_8960, 10682caf7396SDmitry Baryshkov }, { 10699066073cSRajendra Nayak .compatible = "qcom,msm8974-tsens", 10705e6703bdSRajendra Nayak .data = &data_8974, 1071d059c739SRajendra Nayak }, { 10720e580290SAngeloGioacchino Del Regno .compatible = "qcom,msm8976-tsens", 10730e580290SAngeloGioacchino Del Regno .data = &data_8976, 10740e580290SAngeloGioacchino Del Regno }, { 1075d059c739SRajendra Nayak .compatible = "qcom,msm8996-tsens", 1076d059c739SRajendra Nayak .data = &data_8996, 1077191dc74bSAmit Kucheria }, { 1078e8c24c6fSAmit Kucheria .compatible = "qcom,tsens-v1", 1079e8c24c6fSAmit Kucheria .data = &data_tsens_v1, 1080e8c24c6fSAmit Kucheria }, { 1081191dc74bSAmit Kucheria .compatible = "qcom,tsens-v2", 1082191dc74bSAmit Kucheria .data = &data_tsens_v2, 10839066073cSRajendra Nayak }, 10849066073cSRajendra Nayak {} 10859066073cSRajendra Nayak }; 10869066073cSRajendra Nayak MODULE_DEVICE_TABLE(of, tsens_table); 10879066073cSRajendra Nayak 1088ca1b9a9eSDaniel Lezcano static const struct thermal_zone_device_ops tsens_of_ops = { 10899066073cSRajendra Nayak .get_temp = tsens_get_temp, 1090634e11d5SAmit Kucheria .set_trips = tsens_set_trips, 10919066073cSRajendra Nayak }; 10929066073cSRajendra Nayak 109379125e03SAmit Kucheria static int tsens_register_irq(struct tsens_priv *priv, char *irqname, 109479125e03SAmit Kucheria irq_handler_t thread_fn) 109579125e03SAmit Kucheria { 109679125e03SAmit Kucheria struct platform_device *pdev; 109779125e03SAmit Kucheria int ret, irq; 109879125e03SAmit Kucheria 109979125e03SAmit Kucheria pdev = of_find_device_by_node(priv->dev->of_node); 110079125e03SAmit Kucheria if (!pdev) 110179125e03SAmit Kucheria return -ENODEV; 110279125e03SAmit Kucheria 110379125e03SAmit Kucheria irq = platform_get_irq_byname(pdev, irqname); 110479125e03SAmit Kucheria if (irq < 0) { 110579125e03SAmit Kucheria ret = irq; 110679125e03SAmit Kucheria /* For old DTs with no IRQ defined */ 110779125e03SAmit Kucheria if (irq == -ENXIO) 110879125e03SAmit Kucheria ret = 0; 110979125e03SAmit Kucheria } else { 111053e2a20eSAnsuel Smith /* VER_0 interrupt is TRIGGER_RISING, VER_0_1 and up is ONESHOT */ 111153e2a20eSAnsuel Smith if (tsens_version(priv) == VER_0) 111279125e03SAmit Kucheria ret = devm_request_threaded_irq(&pdev->dev, irq, 111353e2a20eSAnsuel Smith thread_fn, NULL, 111453e2a20eSAnsuel Smith IRQF_TRIGGER_RISING, 111553e2a20eSAnsuel Smith dev_name(&pdev->dev), 111653e2a20eSAnsuel Smith priv); 111753e2a20eSAnsuel Smith else 111853e2a20eSAnsuel Smith ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 111953e2a20eSAnsuel Smith thread_fn, IRQF_ONESHOT, 112053e2a20eSAnsuel Smith dev_name(&pdev->dev), 112153e2a20eSAnsuel Smith priv); 112253e2a20eSAnsuel Smith 112379125e03SAmit Kucheria if (ret) 112479125e03SAmit Kucheria dev_err(&pdev->dev, "%s: failed to get irq\n", 112579125e03SAmit Kucheria __func__); 112679125e03SAmit Kucheria else 112779125e03SAmit Kucheria enable_irq_wake(irq); 112879125e03SAmit Kucheria } 112979125e03SAmit Kucheria 113079125e03SAmit Kucheria put_device(&pdev->dev); 113179125e03SAmit Kucheria return ret; 113279125e03SAmit Kucheria } 113379125e03SAmit Kucheria 113469b628acSAmit Kucheria static int tsens_register(struct tsens_priv *priv) 11359066073cSRajendra Nayak { 113679125e03SAmit Kucheria int i, ret; 11379066073cSRajendra Nayak struct thermal_zone_device *tzd; 11389066073cSRajendra Nayak 113969b628acSAmit Kucheria for (i = 0; i < priv->num_sensors; i++) { 114069b628acSAmit Kucheria priv->sensor[i].priv = priv; 1141ca1b9a9eSDaniel Lezcano tzd = devm_thermal_of_zone_register(priv->dev, priv->sensor[i].hw_id, 114269b628acSAmit Kucheria &priv->sensor[i], 11439066073cSRajendra Nayak &tsens_of_ops); 11449066073cSRajendra Nayak if (IS_ERR(tzd)) 11459066073cSRajendra Nayak continue; 114669b628acSAmit Kucheria priv->sensor[i].tzd = tzd; 114769b628acSAmit Kucheria if (priv->ops->enable) 114869b628acSAmit Kucheria priv->ops->enable(priv, i); 11498556e19dSDmitry Baryshkov 11508556e19dSDmitry Baryshkov if (devm_thermal_add_hwmon_sysfs(tzd)) 11518556e19dSDmitry Baryshkov dev_warn(priv->dev, 11528556e19dSDmitry Baryshkov "Failed to add hwmon sysfs attributes\n"); 11539066073cSRajendra Nayak } 1154634e11d5SAmit Kucheria 115553e2a20eSAnsuel Smith /* VER_0 require to set MIN and MAX THRESH 115653e2a20eSAnsuel Smith * These 2 regs are set using the: 115753e2a20eSAnsuel Smith * - CRIT_THRESH_0 for MAX THRESH hardcoded to 120°C 115853e2a20eSAnsuel Smith * - CRIT_THRESH_1 for MIN THRESH hardcoded to 0°C 115953e2a20eSAnsuel Smith */ 116053e2a20eSAnsuel Smith if (tsens_version(priv) < VER_0_1) { 116153e2a20eSAnsuel Smith regmap_field_write(priv->rf[CRIT_THRESH_0], 116253e2a20eSAnsuel Smith tsens_mC_to_hw(priv->sensor, 120000)); 116353e2a20eSAnsuel Smith 116453e2a20eSAnsuel Smith regmap_field_write(priv->rf[CRIT_THRESH_1], 116553e2a20eSAnsuel Smith tsens_mC_to_hw(priv->sensor, 0)); 116653e2a20eSAnsuel Smith } 116753e2a20eSAnsuel Smith 11684360af35SRobert Marko if (priv->feat->combo_int) { 11694360af35SRobert Marko ret = tsens_register_irq(priv, "combined", 11704360af35SRobert Marko tsens_combined_irq_thread); 11714360af35SRobert Marko } else { 117279125e03SAmit Kucheria ret = tsens_register_irq(priv, "uplow", tsens_irq_thread); 117379125e03SAmit Kucheria if (ret < 0) 117479125e03SAmit Kucheria return ret; 1175634e11d5SAmit Kucheria 117679125e03SAmit Kucheria if (priv->feat->crit_int) 117779125e03SAmit Kucheria ret = tsens_register_irq(priv, "critical", 117879125e03SAmit Kucheria tsens_critical_irq_thread); 11794360af35SRobert Marko } 1180634e11d5SAmit Kucheria 1181634e11d5SAmit Kucheria return ret; 11829066073cSRajendra Nayak } 11839066073cSRajendra Nayak 11849066073cSRajendra Nayak static int tsens_probe(struct platform_device *pdev) 11859066073cSRajendra Nayak { 11869066073cSRajendra Nayak int ret, i; 11879066073cSRajendra Nayak struct device *dev; 11889066073cSRajendra Nayak struct device_node *np; 118969b628acSAmit Kucheria struct tsens_priv *priv; 11903c040ce0SAmit Kucheria const struct tsens_plat_data *data; 11919066073cSRajendra Nayak const struct of_device_id *id; 11926d7c70d1SBjorn Andersson u32 num_sensors; 11939066073cSRajendra Nayak 11949066073cSRajendra Nayak if (pdev->dev.of_node) 11959066073cSRajendra Nayak dev = &pdev->dev; 11969066073cSRajendra Nayak else 11979066073cSRajendra Nayak dev = pdev->dev.parent; 11989066073cSRajendra Nayak 11999066073cSRajendra Nayak np = dev->of_node; 12009066073cSRajendra Nayak 12019066073cSRajendra Nayak id = of_match_node(tsens_table, np); 120220d4fd84SRajendra Nayak if (id) 12039066073cSRajendra Nayak data = id->data; 120420d4fd84SRajendra Nayak else 120520d4fd84SRajendra Nayak data = &data_8960; 12069066073cSRajendra Nayak 12076d7c70d1SBjorn Andersson num_sensors = data->num_sensors; 12086d7c70d1SBjorn Andersson 12096d7c70d1SBjorn Andersson if (np) 12106d7c70d1SBjorn Andersson of_property_read_u32(np, "#qcom,sensors", &num_sensors); 12116d7c70d1SBjorn Andersson 12126d7c70d1SBjorn Andersson if (num_sensors <= 0) { 12133795ad5eSAmit Kucheria dev_err(dev, "%s: invalid number of sensors\n", __func__); 12149066073cSRajendra Nayak return -EINVAL; 12159066073cSRajendra Nayak } 12169066073cSRajendra Nayak 121769b628acSAmit Kucheria priv = devm_kzalloc(dev, 121869b628acSAmit Kucheria struct_size(priv, sensor, num_sensors), 12190ed2dd03SKees Cook GFP_KERNEL); 122069b628acSAmit Kucheria if (!priv) 12219066073cSRajendra Nayak return -ENOMEM; 12229066073cSRajendra Nayak 122369b628acSAmit Kucheria priv->dev = dev; 122469b628acSAmit Kucheria priv->num_sensors = num_sensors; 122569b628acSAmit Kucheria priv->ops = data->ops; 122669b628acSAmit Kucheria for (i = 0; i < priv->num_sensors; i++) { 12279066073cSRajendra Nayak if (data->hw_ids) 122869b628acSAmit Kucheria priv->sensor[i].hw_id = data->hw_ids[i]; 12299066073cSRajendra Nayak else 123069b628acSAmit Kucheria priv->sensor[i].hw_id = i; 12319066073cSRajendra Nayak } 1232c1997054SAmit Kucheria priv->feat = data->feat; 1233c1997054SAmit Kucheria priv->fields = data->fields; 12349066073cSRajendra Nayak 12350e9c0bc7SAmit Kucheria platform_set_drvdata(pdev, priv); 12360e9c0bc7SAmit Kucheria 123769b628acSAmit Kucheria if (!priv->ops || !priv->ops->init || !priv->ops->get_temp) 12389066073cSRajendra Nayak return -EINVAL; 12399066073cSRajendra Nayak 124069b628acSAmit Kucheria ret = priv->ops->init(priv); 12419066073cSRajendra Nayak if (ret < 0) { 12423795ad5eSAmit Kucheria dev_err(dev, "%s: init failed\n", __func__); 12439066073cSRajendra Nayak return ret; 12449066073cSRajendra Nayak } 12459066073cSRajendra Nayak 124669b628acSAmit Kucheria if (priv->ops->calibrate) { 124769b628acSAmit Kucheria ret = priv->ops->calibrate(priv); 12489066073cSRajendra Nayak if (ret < 0) { 1249fc7d18cfSAmit Kucheria if (ret != -EPROBE_DEFER) 12503795ad5eSAmit Kucheria dev_err(dev, "%s: calibration failed\n", __func__); 12519066073cSRajendra Nayak return ret; 12529066073cSRajendra Nayak } 12539066073cSRajendra Nayak } 12549066073cSRajendra Nayak 1255de48d876SChristian Marangi ret = tsens_register(priv); 1256de48d876SChristian Marangi if (!ret) 1257de48d876SChristian Marangi tsens_debug_init(pdev); 1258de48d876SChristian Marangi 1259de48d876SChristian Marangi return ret; 12609066073cSRajendra Nayak } 12619066073cSRajendra Nayak 12629066073cSRajendra Nayak static int tsens_remove(struct platform_device *pdev) 12639066073cSRajendra Nayak { 126469b628acSAmit Kucheria struct tsens_priv *priv = platform_get_drvdata(pdev); 12659066073cSRajendra Nayak 12667c938f48SAmit Kucheria debugfs_remove_recursive(priv->debug_root); 1267634e11d5SAmit Kucheria tsens_disable_irq(priv); 126869b628acSAmit Kucheria if (priv->ops->disable) 126969b628acSAmit Kucheria priv->ops->disable(priv); 12709066073cSRajendra Nayak 12719066073cSRajendra Nayak return 0; 12729066073cSRajendra Nayak } 12739066073cSRajendra Nayak 12749066073cSRajendra Nayak static struct platform_driver tsens_driver = { 12759066073cSRajendra Nayak .probe = tsens_probe, 12769066073cSRajendra Nayak .remove = tsens_remove, 12779066073cSRajendra Nayak .driver = { 12789066073cSRajendra Nayak .name = "qcom-tsens", 12799066073cSRajendra Nayak .pm = &tsens_pm_ops, 12809066073cSRajendra Nayak .of_match_table = tsens_table, 12819066073cSRajendra Nayak }, 12829066073cSRajendra Nayak }; 12839066073cSRajendra Nayak module_platform_driver(tsens_driver); 12849066073cSRajendra Nayak 12859066073cSRajendra Nayak MODULE_LICENSE("GPL v2"); 12869066073cSRajendra Nayak MODULE_DESCRIPTION("QCOM Temperature Sensor driver"); 12879066073cSRajendra Nayak MODULE_ALIAS("platform:qcom-tsens"); 1288