1ffcb2fc8SKeerthy // SPDX-License-Identifier: GPL-2.0 2ffcb2fc8SKeerthy /* 3ffcb2fc8SKeerthy * TI Bandgap temperature sensor driver for J72XX SoC Family 4ffcb2fc8SKeerthy * 5ffcb2fc8SKeerthy * Copyright (C) 2021 Texas Instruments Incorporated - http://www.ti.com/ 6ffcb2fc8SKeerthy */ 7ffcb2fc8SKeerthy 8ffcb2fc8SKeerthy #include <linux/math.h> 9ffcb2fc8SKeerthy #include <linux/math64.h> 10ffcb2fc8SKeerthy #include <linux/module.h> 11ffcb2fc8SKeerthy #include <linux/init.h> 12ffcb2fc8SKeerthy #include <linux/kernel.h> 13ffcb2fc8SKeerthy #include <linux/pm_runtime.h> 14ffcb2fc8SKeerthy #include <linux/err.h> 15ffcb2fc8SKeerthy #include <linux/types.h> 16ffcb2fc8SKeerthy #include <linux/of_platform.h> 17ffcb2fc8SKeerthy #include <linux/io.h> 18ffcb2fc8SKeerthy #include <linux/thermal.h> 19ffcb2fc8SKeerthy #include <linux/of.h> 20ffcb2fc8SKeerthy #include <linux/delay.h> 21ffcb2fc8SKeerthy #include <linux/slab.h> 22ffcb2fc8SKeerthy 23ffcb2fc8SKeerthy #define K3_VTM_DEVINFO_PWR0_OFFSET 0x4 24ffcb2fc8SKeerthy #define K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK 0xf0 25ffcb2fc8SKeerthy #define K3_VTM_TMPSENS0_CTRL_OFFSET 0x300 26ffcb2fc8SKeerthy #define K3_VTM_MISC_CTRL_OFFSET 0xc 27ffcb2fc8SKeerthy #define K3_VTM_TMPSENS_STAT_OFFSET 0x8 28ffcb2fc8SKeerthy #define K3_VTM_ANYMAXT_OUTRG_ALERT_EN 0x1 29ffcb2fc8SKeerthy #define K3_VTM_MISC_CTRL2_OFFSET 0x10 30ffcb2fc8SKeerthy #define K3_VTM_TS_STAT_DTEMP_MASK 0x3ff 31ffcb2fc8SKeerthy #define K3_VTM_MAX_NUM_TS 8 32ffcb2fc8SKeerthy #define K3_VTM_TMPSENS_CTRL_SOC BIT(5) 33ffcb2fc8SKeerthy #define K3_VTM_TMPSENS_CTRL_CLRZ BIT(6) 34ffcb2fc8SKeerthy #define K3_VTM_TMPSENS_CTRL_CLKON_REQ BIT(7) 35ffcb2fc8SKeerthy #define K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN BIT(11) 36ffcb2fc8SKeerthy 37ffcb2fc8SKeerthy #define K3_VTM_CORRECTION_TEMP_CNT 3 38ffcb2fc8SKeerthy 39ffcb2fc8SKeerthy #define MINUS40CREF 5 40ffcb2fc8SKeerthy #define PLUS30CREF 253 41ffcb2fc8SKeerthy #define PLUS125CREF 730 42ffcb2fc8SKeerthy #define PLUS150CREF 940 43ffcb2fc8SKeerthy 44ffcb2fc8SKeerthy #define TABLE_SIZE 1024 45ffcb2fc8SKeerthy #define MAX_TEMP 123000 46ffcb2fc8SKeerthy #define COOL_DOWN_TEMP 105000 47ffcb2fc8SKeerthy 48ffcb2fc8SKeerthy #define FACTORS_REDUCTION 13 49ffcb2fc8SKeerthy static int *derived_table; 50ffcb2fc8SKeerthy 51ffcb2fc8SKeerthy static int compute_value(int index, const s64 *factors, int nr_factors, 52ffcb2fc8SKeerthy int reduction) 53ffcb2fc8SKeerthy { 54ffcb2fc8SKeerthy s64 value = 0; 55ffcb2fc8SKeerthy int i; 56ffcb2fc8SKeerthy 57ffcb2fc8SKeerthy for (i = 0; i < nr_factors; i++) 58ffcb2fc8SKeerthy value += factors[i] * int_pow(index, i); 59ffcb2fc8SKeerthy 60ffcb2fc8SKeerthy return (int)div64_s64(value, int_pow(10, reduction)); 61ffcb2fc8SKeerthy } 62ffcb2fc8SKeerthy 63ffcb2fc8SKeerthy static void init_table(int factors_size, int *table, const s64 *factors) 64ffcb2fc8SKeerthy { 65ffcb2fc8SKeerthy int i; 66ffcb2fc8SKeerthy 67ffcb2fc8SKeerthy for (i = 0; i < TABLE_SIZE; i++) 68ffcb2fc8SKeerthy table[i] = compute_value(i, factors, factors_size, 69ffcb2fc8SKeerthy FACTORS_REDUCTION); 70ffcb2fc8SKeerthy } 71ffcb2fc8SKeerthy 72ffcb2fc8SKeerthy /** 73ffcb2fc8SKeerthy * struct err_values - structure containing error/reference values 74ffcb2fc8SKeerthy * @refs: reference error values for -40C, 30C, 125C & 150C 75ffcb2fc8SKeerthy * @errs: Actual error values for -40C, 30C, 125C & 150C read from the efuse 76ffcb2fc8SKeerthy */ 77ffcb2fc8SKeerthy struct err_values { 78ffcb2fc8SKeerthy int refs[4]; 79ffcb2fc8SKeerthy int errs[4]; 80ffcb2fc8SKeerthy }; 81ffcb2fc8SKeerthy 82ffcb2fc8SKeerthy static void create_table_segments(struct err_values *err_vals, int seg, 83ffcb2fc8SKeerthy int *ref_table) 84ffcb2fc8SKeerthy { 85ffcb2fc8SKeerthy int m = 0, c, num, den, i, err, idx1, idx2, err1, err2, ref1, ref2; 86ffcb2fc8SKeerthy 87ffcb2fc8SKeerthy if (seg == 0) 88ffcb2fc8SKeerthy idx1 = 0; 89ffcb2fc8SKeerthy else 90ffcb2fc8SKeerthy idx1 = err_vals->refs[seg]; 91ffcb2fc8SKeerthy 92ffcb2fc8SKeerthy idx2 = err_vals->refs[seg + 1]; 93ffcb2fc8SKeerthy err1 = err_vals->errs[seg]; 94ffcb2fc8SKeerthy err2 = err_vals->errs[seg + 1]; 95ffcb2fc8SKeerthy ref1 = err_vals->refs[seg]; 96ffcb2fc8SKeerthy ref2 = err_vals->refs[seg + 1]; 97ffcb2fc8SKeerthy 98ffcb2fc8SKeerthy /* 99ffcb2fc8SKeerthy * Calculate the slope with adc values read from the register 100ffcb2fc8SKeerthy * as the y-axis param and err in adc value as x-axis param 101ffcb2fc8SKeerthy */ 102ffcb2fc8SKeerthy num = ref2 - ref1; 103ffcb2fc8SKeerthy den = err2 - err1; 104ffcb2fc8SKeerthy if (den) 105ffcb2fc8SKeerthy m = num / den; 106ffcb2fc8SKeerthy c = ref2 - m * err2; 107ffcb2fc8SKeerthy 108ffcb2fc8SKeerthy /* 109ffcb2fc8SKeerthy * Take care of divide by zero error if error values are same 110ffcb2fc8SKeerthy * Or when the slope is 0 111ffcb2fc8SKeerthy */ 112ffcb2fc8SKeerthy if (den != 0 && m != 0) { 113ffcb2fc8SKeerthy for (i = idx1; i <= idx2; i++) { 114ffcb2fc8SKeerthy err = (i - c) / m; 115ffcb2fc8SKeerthy if (((i + err) < 0) || ((i + err) >= TABLE_SIZE)) 116ffcb2fc8SKeerthy continue; 117ffcb2fc8SKeerthy derived_table[i] = ref_table[i + err]; 118ffcb2fc8SKeerthy } 119ffcb2fc8SKeerthy } else { /* Constant error take care of divide by zero */ 120ffcb2fc8SKeerthy for (i = idx1; i <= idx2; i++) { 121ffcb2fc8SKeerthy if (((i + err1) < 0) || ((i + err1) >= TABLE_SIZE)) 122ffcb2fc8SKeerthy continue; 123ffcb2fc8SKeerthy derived_table[i] = ref_table[i + err1]; 124ffcb2fc8SKeerthy } 125ffcb2fc8SKeerthy } 126ffcb2fc8SKeerthy } 127ffcb2fc8SKeerthy 128ffcb2fc8SKeerthy static int prep_lookup_table(struct err_values *err_vals, int *ref_table) 129ffcb2fc8SKeerthy { 130ffcb2fc8SKeerthy int inc, i, seg; 131ffcb2fc8SKeerthy 132ffcb2fc8SKeerthy /* 133ffcb2fc8SKeerthy * Fill up the lookup table under 3 segments 134ffcb2fc8SKeerthy * region -40C to +30C 135ffcb2fc8SKeerthy * region +30C to +125C 136ffcb2fc8SKeerthy * region +125C to +150C 137ffcb2fc8SKeerthy */ 138ffcb2fc8SKeerthy for (seg = 0; seg < 3; seg++) 139ffcb2fc8SKeerthy create_table_segments(err_vals, seg, ref_table); 140ffcb2fc8SKeerthy 141ffcb2fc8SKeerthy /* Get to the first valid temperature */ 142ffcb2fc8SKeerthy i = 0; 143ffcb2fc8SKeerthy while (!derived_table[i]) 144ffcb2fc8SKeerthy i++; 145ffcb2fc8SKeerthy 146ffcb2fc8SKeerthy /* 147ffcb2fc8SKeerthy * Get to the last zero index and back fill the temperature for 148ffcb2fc8SKeerthy * sake of continuity 149ffcb2fc8SKeerthy */ 150ffcb2fc8SKeerthy if (i) { 151ffcb2fc8SKeerthy /* 300 milli celsius steps */ 152ffcb2fc8SKeerthy while (i--) 153ffcb2fc8SKeerthy derived_table[i] = derived_table[i + 1] - 300; 154ffcb2fc8SKeerthy } 155ffcb2fc8SKeerthy 156ffcb2fc8SKeerthy /* 157ffcb2fc8SKeerthy * Fill the last trailing 0s which are unfilled with increments of 158ffcb2fc8SKeerthy * 100 milli celsius till 1023 code 159ffcb2fc8SKeerthy */ 160ffcb2fc8SKeerthy i = TABLE_SIZE - 1; 161ffcb2fc8SKeerthy while (!derived_table[i]) 162ffcb2fc8SKeerthy i--; 163ffcb2fc8SKeerthy 164ffcb2fc8SKeerthy i++; 165ffcb2fc8SKeerthy inc = 1; 166ffcb2fc8SKeerthy while (i < TABLE_SIZE) { 167ffcb2fc8SKeerthy derived_table[i] = derived_table[i - 1] + inc * 100; 168ffcb2fc8SKeerthy i++; 169ffcb2fc8SKeerthy } 170ffcb2fc8SKeerthy 171ffcb2fc8SKeerthy return 0; 172ffcb2fc8SKeerthy } 173ffcb2fc8SKeerthy 174ffcb2fc8SKeerthy struct k3_thermal_data; 175ffcb2fc8SKeerthy 176ffcb2fc8SKeerthy struct k3_j72xx_bandgap { 177ffcb2fc8SKeerthy struct device *dev; 178ffcb2fc8SKeerthy void __iomem *base; 179ffcb2fc8SKeerthy void __iomem *cfg2_base; 180ffcb2fc8SKeerthy struct k3_thermal_data *ts_data[K3_VTM_MAX_NUM_TS]; 181ffcb2fc8SKeerthy }; 182ffcb2fc8SKeerthy 183ffcb2fc8SKeerthy /* common data structures */ 184ffcb2fc8SKeerthy struct k3_thermal_data { 185ffcb2fc8SKeerthy struct k3_j72xx_bandgap *bgp; 186ffcb2fc8SKeerthy u32 ctrl_offset; 187ffcb2fc8SKeerthy u32 stat_offset; 188ffcb2fc8SKeerthy }; 189ffcb2fc8SKeerthy 190ffcb2fc8SKeerthy static int two_cmp(int tmp, int mask) 191ffcb2fc8SKeerthy { 192ffcb2fc8SKeerthy tmp = ~(tmp); 193ffcb2fc8SKeerthy tmp &= mask; 194ffcb2fc8SKeerthy tmp += 1; 195ffcb2fc8SKeerthy 196ffcb2fc8SKeerthy /* Return negative value */ 197ffcb2fc8SKeerthy return (0 - tmp); 198ffcb2fc8SKeerthy } 199ffcb2fc8SKeerthy 200ffcb2fc8SKeerthy static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1, 201ffcb2fc8SKeerthy unsigned int s2) 202ffcb2fc8SKeerthy { 203ffcb2fc8SKeerthy int d01 = abs(s0 - s1); 204ffcb2fc8SKeerthy int d02 = abs(s0 - s2); 205ffcb2fc8SKeerthy int d12 = abs(s1 - s2); 206ffcb2fc8SKeerthy 207ffcb2fc8SKeerthy if (d01 <= d02 && d01 <= d12) 208ffcb2fc8SKeerthy return (s0 + s1) / 2; 209ffcb2fc8SKeerthy 210ffcb2fc8SKeerthy if (d02 <= d01 && d02 <= d12) 211ffcb2fc8SKeerthy return (s0 + s2) / 2; 212ffcb2fc8SKeerthy 213ffcb2fc8SKeerthy return (s1 + s2) / 2; 214ffcb2fc8SKeerthy } 215ffcb2fc8SKeerthy 216ffcb2fc8SKeerthy static inline int k3_bgp_read_temp(struct k3_thermal_data *devdata, 217ffcb2fc8SKeerthy int *temp) 218ffcb2fc8SKeerthy { 219ffcb2fc8SKeerthy struct k3_j72xx_bandgap *bgp; 220ffcb2fc8SKeerthy unsigned int dtemp, s0, s1, s2; 221ffcb2fc8SKeerthy 222ffcb2fc8SKeerthy bgp = devdata->bgp; 223ffcb2fc8SKeerthy /* 224ffcb2fc8SKeerthy * Errata is applicable for am654 pg 1.0 silicon/J7ES. There 225ffcb2fc8SKeerthy * is a variation of the order for certain degree centigrade on AM654. 226ffcb2fc8SKeerthy * Work around that by getting the average of two closest 227ffcb2fc8SKeerthy * readings out of three readings everytime we want to 228ffcb2fc8SKeerthy * report temperatures. 229ffcb2fc8SKeerthy * 230ffcb2fc8SKeerthy * Errata workaround. 231ffcb2fc8SKeerthy */ 232ffcb2fc8SKeerthy s0 = readl(bgp->base + devdata->stat_offset) & 233ffcb2fc8SKeerthy K3_VTM_TS_STAT_DTEMP_MASK; 234ffcb2fc8SKeerthy s1 = readl(bgp->base + devdata->stat_offset) & 235ffcb2fc8SKeerthy K3_VTM_TS_STAT_DTEMP_MASK; 236ffcb2fc8SKeerthy s2 = readl(bgp->base + devdata->stat_offset) & 237ffcb2fc8SKeerthy K3_VTM_TS_STAT_DTEMP_MASK; 238ffcb2fc8SKeerthy dtemp = vtm_get_best_value(s0, s1, s2); 239ffcb2fc8SKeerthy 240ffcb2fc8SKeerthy if (dtemp < 0 || dtemp >= TABLE_SIZE) 241ffcb2fc8SKeerthy return -EINVAL; 242ffcb2fc8SKeerthy 243ffcb2fc8SKeerthy *temp = derived_table[dtemp]; 244ffcb2fc8SKeerthy 245ffcb2fc8SKeerthy return 0; 246ffcb2fc8SKeerthy } 247ffcb2fc8SKeerthy 248ffcb2fc8SKeerthy /* Get temperature callback function for thermal zone */ 249b86105edSDaniel Lezcano static int k3_thermal_get_temp(struct thermal_zone_device *tz, int *temp) 250ffcb2fc8SKeerthy { 25146cab93aSBryan Brattlof return k3_bgp_read_temp(tz->devdata, temp); 252ffcb2fc8SKeerthy } 253ffcb2fc8SKeerthy 254b86105edSDaniel Lezcano static const struct thermal_zone_device_ops k3_of_thermal_ops = { 255ffcb2fc8SKeerthy .get_temp = k3_thermal_get_temp, 256ffcb2fc8SKeerthy }; 257ffcb2fc8SKeerthy 258ffcb2fc8SKeerthy static int k3_j72xx_bandgap_temp_to_adc_code(int temp) 259ffcb2fc8SKeerthy { 260ffcb2fc8SKeerthy int low = 0, high = TABLE_SIZE - 1, mid; 261ffcb2fc8SKeerthy 262ffcb2fc8SKeerthy if (temp > 160000 || temp < -50000) 263ffcb2fc8SKeerthy return -EINVAL; 264ffcb2fc8SKeerthy 265ffcb2fc8SKeerthy /* Binary search to find the adc code */ 266ffcb2fc8SKeerthy while (low < (high - 1)) { 267ffcb2fc8SKeerthy mid = (low + high) / 2; 268ffcb2fc8SKeerthy if (temp <= derived_table[mid]) 269ffcb2fc8SKeerthy high = mid; 270ffcb2fc8SKeerthy else 271ffcb2fc8SKeerthy low = mid; 272ffcb2fc8SKeerthy } 273ffcb2fc8SKeerthy 274ffcb2fc8SKeerthy return mid; 275ffcb2fc8SKeerthy } 276ffcb2fc8SKeerthy 277ffcb2fc8SKeerthy static void get_efuse_values(int id, struct k3_thermal_data *data, int *err, 278156f0e2fSBryan Brattlof void __iomem *fuse_base) 279ffcb2fc8SKeerthy { 280ffcb2fc8SKeerthy int i, tmp, pow; 281ffcb2fc8SKeerthy int ct_offsets[5][K3_VTM_CORRECTION_TEMP_CNT] = { 282ffcb2fc8SKeerthy { 0x0, 0x8, 0x4 }, 283ffcb2fc8SKeerthy { 0x0, 0x8, 0x4 }, 284ffcb2fc8SKeerthy { 0x0, -1, 0x4 }, 285ffcb2fc8SKeerthy { 0x0, 0xC, -1 }, 286ffcb2fc8SKeerthy { 0x0, 0xc, 0x8 } 287ffcb2fc8SKeerthy }; 288ffcb2fc8SKeerthy int ct_bm[5][K3_VTM_CORRECTION_TEMP_CNT] = { 289ffcb2fc8SKeerthy { 0x3f, 0x1fe000, 0x1ff }, 290ffcb2fc8SKeerthy { 0xfc0, 0x1fe000, 0x3fe00 }, 291ffcb2fc8SKeerthy { 0x3f000, 0x7f800000, 0x7fc0000 }, 292ffcb2fc8SKeerthy { 0xfc0000, 0x1fe0, 0x1f800000 }, 293ffcb2fc8SKeerthy { 0x3f000000, 0x1fe000, 0x1ff0 } 294ffcb2fc8SKeerthy }; 295ffcb2fc8SKeerthy 296ffcb2fc8SKeerthy for (i = 0; i < 3; i++) { 297ffcb2fc8SKeerthy /* Extract the offset value using bit-mask */ 298ffcb2fc8SKeerthy if (ct_offsets[id][i] == -1 && i == 1) { 299ffcb2fc8SKeerthy /* 25C offset Case of Sensor 2 split between 2 regs */ 300156f0e2fSBryan Brattlof tmp = (readl(fuse_base + 0x8) & 0xE0000000) >> (29); 301156f0e2fSBryan Brattlof tmp |= ((readl(fuse_base + 0xC) & 0x1F) << 3); 302ffcb2fc8SKeerthy pow = tmp & 0x80; 303ffcb2fc8SKeerthy } else if (ct_offsets[id][i] == -1 && i == 2) { 304ffcb2fc8SKeerthy /* 125C Case of Sensor 3 split between 2 regs */ 305156f0e2fSBryan Brattlof tmp = (readl(fuse_base + 0x4) & 0xF8000000) >> (27); 306156f0e2fSBryan Brattlof tmp |= ((readl(fuse_base + 0x8) & 0xF) << 5); 307ffcb2fc8SKeerthy pow = tmp & 0x100; 308ffcb2fc8SKeerthy } else { 309156f0e2fSBryan Brattlof tmp = readl(fuse_base + ct_offsets[id][i]); 310ffcb2fc8SKeerthy tmp &= ct_bm[id][i]; 311ffcb2fc8SKeerthy tmp = tmp >> __ffs(ct_bm[id][i]); 312ffcb2fc8SKeerthy 313ffcb2fc8SKeerthy /* Obtain the sign bit pow*/ 314ffcb2fc8SKeerthy pow = ct_bm[id][i] >> __ffs(ct_bm[id][i]); 315ffcb2fc8SKeerthy pow += 1; 316ffcb2fc8SKeerthy pow /= 2; 317ffcb2fc8SKeerthy } 318ffcb2fc8SKeerthy 319ffcb2fc8SKeerthy /* Check for negative value */ 320ffcb2fc8SKeerthy if (tmp & pow) { 321ffcb2fc8SKeerthy /* 2's complement value */ 322ffcb2fc8SKeerthy tmp = two_cmp(tmp, ct_bm[id][i] >> __ffs(ct_bm[id][i])); 323ffcb2fc8SKeerthy } 324ffcb2fc8SKeerthy err[i] = tmp; 325ffcb2fc8SKeerthy } 326ffcb2fc8SKeerthy 327ffcb2fc8SKeerthy /* Err value for 150C is set to 0 */ 328ffcb2fc8SKeerthy err[i] = 0; 329ffcb2fc8SKeerthy } 330ffcb2fc8SKeerthy 331ffcb2fc8SKeerthy static void print_look_up_table(struct device *dev, int *ref_table) 332ffcb2fc8SKeerthy { 333ffcb2fc8SKeerthy int i; 334ffcb2fc8SKeerthy 335ffcb2fc8SKeerthy dev_dbg(dev, "The contents of derived array\n"); 336ffcb2fc8SKeerthy dev_dbg(dev, "Code Temperature\n"); 337ffcb2fc8SKeerthy for (i = 0; i < TABLE_SIZE; i++) 338ffcb2fc8SKeerthy dev_dbg(dev, "%d %d %d\n", i, derived_table[i], ref_table[i]); 339ffcb2fc8SKeerthy } 340ffcb2fc8SKeerthy 341ffcb2fc8SKeerthy struct k3_j72xx_bandgap_data { 342311f328fSBryan Brattlof const bool has_errata_i2128; 343ffcb2fc8SKeerthy }; 344ffcb2fc8SKeerthy 345ffcb2fc8SKeerthy static int k3_j72xx_bandgap_probe(struct platform_device *pdev) 346ffcb2fc8SKeerthy { 347ffcb2fc8SKeerthy int ret = 0, cnt, val, id; 348ffcb2fc8SKeerthy int high_max, low_temp; 349ffcb2fc8SKeerthy struct resource *res; 350ffcb2fc8SKeerthy struct device *dev = &pdev->dev; 351ffcb2fc8SKeerthy struct k3_j72xx_bandgap *bgp; 352ffcb2fc8SKeerthy struct k3_thermal_data *data; 353311f328fSBryan Brattlof bool workaround_needed = false; 354ffcb2fc8SKeerthy const struct k3_j72xx_bandgap_data *driver_data; 355ffcb2fc8SKeerthy struct thermal_zone_device *ti_thermal; 356ffcb2fc8SKeerthy int *ref_table; 357ffcb2fc8SKeerthy struct err_values err_vals; 358156f0e2fSBryan Brattlof void __iomem *fuse_base; 359ffcb2fc8SKeerthy 360ffcb2fc8SKeerthy const s64 golden_factors[] = { 361ffcb2fc8SKeerthy -490019999999999936, 362ffcb2fc8SKeerthy 3251200000000000, 363ffcb2fc8SKeerthy -1705800000000, 364ffcb2fc8SKeerthy 603730000, 365ffcb2fc8SKeerthy -92627, 366ffcb2fc8SKeerthy }; 367ffcb2fc8SKeerthy 368ffcb2fc8SKeerthy const s64 pvt_wa_factors[] = { 369ffcb2fc8SKeerthy -415230000000000000, 370ffcb2fc8SKeerthy 3126600000000000, 371ffcb2fc8SKeerthy -1157800000000, 372ffcb2fc8SKeerthy }; 373ffcb2fc8SKeerthy 374ffcb2fc8SKeerthy bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL); 375ffcb2fc8SKeerthy if (!bgp) 376ffcb2fc8SKeerthy return -ENOMEM; 377ffcb2fc8SKeerthy 378ffcb2fc8SKeerthy bgp->dev = dev; 379ffcb2fc8SKeerthy res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 380ffcb2fc8SKeerthy bgp->base = devm_ioremap_resource(dev, res); 381ffcb2fc8SKeerthy if (IS_ERR(bgp->base)) 382ffcb2fc8SKeerthy return PTR_ERR(bgp->base); 383ffcb2fc8SKeerthy 384ffcb2fc8SKeerthy res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 385ffcb2fc8SKeerthy bgp->cfg2_base = devm_ioremap_resource(dev, res); 386ffcb2fc8SKeerthy if (IS_ERR(bgp->cfg2_base)) 387ffcb2fc8SKeerthy return PTR_ERR(bgp->cfg2_base); 388ffcb2fc8SKeerthy 389*366444ebSBryan Brattlof driver_data = of_device_get_match_data(dev); 390*366444ebSBryan Brattlof if (driver_data) 391*366444ebSBryan Brattlof workaround_needed = driver_data->has_errata_i2128; 392*366444ebSBryan Brattlof 393*366444ebSBryan Brattlof /* 394*366444ebSBryan Brattlof * Some of TI's J721E SoCs require a software trimming procedure 395*366444ebSBryan Brattlof * for the temperature monitors to function properly. To determine 396*366444ebSBryan Brattlof * if this particular SoC is NOT affected, both bits in the 397*366444ebSBryan Brattlof * WKUP_SPARE_FUSE0[31:30] will be set (0xC0000000) indicating 398*366444ebSBryan Brattlof * when software trimming should NOT be applied. 399*366444ebSBryan Brattlof * 400*366444ebSBryan Brattlof * https://www.ti.com/lit/er/sprz455c/sprz455c.pdf 401*366444ebSBryan Brattlof */ 402*366444ebSBryan Brattlof if (workaround_needed) { 403ffcb2fc8SKeerthy res = platform_get_resource(pdev, IORESOURCE_MEM, 2); 404156f0e2fSBryan Brattlof fuse_base = devm_ioremap_resource(dev, res); 405156f0e2fSBryan Brattlof if (IS_ERR(fuse_base)) 406156f0e2fSBryan Brattlof return PTR_ERR(fuse_base); 407ffcb2fc8SKeerthy 408*366444ebSBryan Brattlof if ((readl(fuse_base) & 0xc0000000) == 0xc0000000) 409*366444ebSBryan Brattlof workaround_needed = false; 410*366444ebSBryan Brattlof } 411*366444ebSBryan Brattlof 412*366444ebSBryan Brattlof dev_dbg(bgp->dev, "Work around %sneeded\n", 413*366444ebSBryan Brattlof workaround_needed ? "" : "not "); 414ffcb2fc8SKeerthy 415ffcb2fc8SKeerthy pm_runtime_enable(dev); 416ffcb2fc8SKeerthy ret = pm_runtime_get_sync(dev); 417ffcb2fc8SKeerthy if (ret < 0) { 418ffcb2fc8SKeerthy pm_runtime_put_noidle(dev); 419ffcb2fc8SKeerthy pm_runtime_disable(dev); 420ffcb2fc8SKeerthy return ret; 421ffcb2fc8SKeerthy } 422ffcb2fc8SKeerthy 423ffcb2fc8SKeerthy /* Get the sensor count in the VTM */ 424ffcb2fc8SKeerthy val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET); 425ffcb2fc8SKeerthy cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK; 426ffcb2fc8SKeerthy cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK); 427ffcb2fc8SKeerthy 428ffcb2fc8SKeerthy data = devm_kcalloc(bgp->dev, cnt, sizeof(*data), GFP_KERNEL); 429ffcb2fc8SKeerthy if (!data) { 430ffcb2fc8SKeerthy ret = -ENOMEM; 431ffcb2fc8SKeerthy goto err_alloc; 432ffcb2fc8SKeerthy } 433ffcb2fc8SKeerthy 434ffcb2fc8SKeerthy ref_table = kzalloc(sizeof(*ref_table) * TABLE_SIZE, GFP_KERNEL); 435ffcb2fc8SKeerthy if (!ref_table) { 436ffcb2fc8SKeerthy ret = -ENOMEM; 437ffcb2fc8SKeerthy goto err_alloc; 438ffcb2fc8SKeerthy } 439ffcb2fc8SKeerthy 440ffcb2fc8SKeerthy derived_table = devm_kzalloc(bgp->dev, sizeof(*derived_table) * TABLE_SIZE, 441ffcb2fc8SKeerthy GFP_KERNEL); 442ffcb2fc8SKeerthy if (!derived_table) { 443ffcb2fc8SKeerthy ret = -ENOMEM; 44499a049aaSBryan Brattlof goto err_free_ref_table; 445ffcb2fc8SKeerthy } 446ffcb2fc8SKeerthy 447ffcb2fc8SKeerthy if (!workaround_needed) 448ffcb2fc8SKeerthy init_table(5, ref_table, golden_factors); 449ffcb2fc8SKeerthy else 450ffcb2fc8SKeerthy init_table(3, ref_table, pvt_wa_factors); 451ffcb2fc8SKeerthy 452ffcb2fc8SKeerthy /* Register the thermal sensors */ 453ffcb2fc8SKeerthy for (id = 0; id < cnt; id++) { 454ffcb2fc8SKeerthy data[id].bgp = bgp; 455ffcb2fc8SKeerthy data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20; 456ffcb2fc8SKeerthy data[id].stat_offset = data[id].ctrl_offset + 457ffcb2fc8SKeerthy K3_VTM_TMPSENS_STAT_OFFSET; 458ffcb2fc8SKeerthy 459ffcb2fc8SKeerthy if (workaround_needed) { 460ffcb2fc8SKeerthy /* ref adc values for -40C, 30C & 125C respectively */ 461ffcb2fc8SKeerthy err_vals.refs[0] = MINUS40CREF; 462ffcb2fc8SKeerthy err_vals.refs[1] = PLUS30CREF; 463ffcb2fc8SKeerthy err_vals.refs[2] = PLUS125CREF; 464ffcb2fc8SKeerthy err_vals.refs[3] = PLUS150CREF; 465156f0e2fSBryan Brattlof get_efuse_values(id, &data[id], err_vals.errs, fuse_base); 466ffcb2fc8SKeerthy } 467ffcb2fc8SKeerthy 468ffcb2fc8SKeerthy if (id == 0 && workaround_needed) 469ffcb2fc8SKeerthy prep_lookup_table(&err_vals, ref_table); 470ffcb2fc8SKeerthy else if (id == 0 && !workaround_needed) 471ffcb2fc8SKeerthy memcpy(derived_table, ref_table, TABLE_SIZE * 4); 472ffcb2fc8SKeerthy 473ffcb2fc8SKeerthy val = readl(data[id].bgp->cfg2_base + data[id].ctrl_offset); 474ffcb2fc8SKeerthy val |= (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN | 475ffcb2fc8SKeerthy K3_VTM_TMPSENS_CTRL_SOC | 476ffcb2fc8SKeerthy K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4)); 477ffcb2fc8SKeerthy writel(val, data[id].bgp->cfg2_base + data[id].ctrl_offset); 478ffcb2fc8SKeerthy 479ffcb2fc8SKeerthy bgp->ts_data[id] = &data[id]; 480b86105edSDaniel Lezcano ti_thermal = devm_thermal_of_zone_register(bgp->dev, id, &data[id], 481ffcb2fc8SKeerthy &k3_of_thermal_ops); 482ffcb2fc8SKeerthy if (IS_ERR(ti_thermal)) { 483ffcb2fc8SKeerthy dev_err(bgp->dev, "thermal zone device is NULL\n"); 484ffcb2fc8SKeerthy ret = PTR_ERR(ti_thermal); 48599a049aaSBryan Brattlof goto err_free_ref_table; 486ffcb2fc8SKeerthy } 487ffcb2fc8SKeerthy } 488ffcb2fc8SKeerthy 489ffcb2fc8SKeerthy /* 490ffcb2fc8SKeerthy * Program TSHUT thresholds 491ffcb2fc8SKeerthy * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2 492ffcb2fc8SKeerthy * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN bit 493ffcb2fc8SKeerthy * This is already taken care as per of init 494ffcb2fc8SKeerthy * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN bit 495ffcb2fc8SKeerthy */ 496ffcb2fc8SKeerthy high_max = k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP); 497ffcb2fc8SKeerthy low_temp = k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP); 498ffcb2fc8SKeerthy 499ffcb2fc8SKeerthy writel((low_temp << 16) | high_max, data[0].bgp->cfg2_base + 500ffcb2fc8SKeerthy K3_VTM_MISC_CTRL2_OFFSET); 501ffcb2fc8SKeerthy mdelay(100); 502ffcb2fc8SKeerthy writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, data[0].bgp->cfg2_base + 503ffcb2fc8SKeerthy K3_VTM_MISC_CTRL_OFFSET); 504ffcb2fc8SKeerthy 505ffcb2fc8SKeerthy platform_set_drvdata(pdev, bgp); 506ffcb2fc8SKeerthy 507ffcb2fc8SKeerthy print_look_up_table(dev, ref_table); 508ffcb2fc8SKeerthy /* 509ffcb2fc8SKeerthy * Now that the derived_table has the appropriate look up values 510ffcb2fc8SKeerthy * Free up the ref_table 511ffcb2fc8SKeerthy */ 512ffcb2fc8SKeerthy kfree(ref_table); 513ffcb2fc8SKeerthy 514ffcb2fc8SKeerthy return 0; 515ffcb2fc8SKeerthy 51699a049aaSBryan Brattlof err_free_ref_table: 51799a049aaSBryan Brattlof kfree(ref_table); 51899a049aaSBryan Brattlof 519ffcb2fc8SKeerthy err_alloc: 520ffcb2fc8SKeerthy pm_runtime_put_sync(&pdev->dev); 521ffcb2fc8SKeerthy pm_runtime_disable(&pdev->dev); 522ffcb2fc8SKeerthy 523ffcb2fc8SKeerthy return ret; 524ffcb2fc8SKeerthy } 525ffcb2fc8SKeerthy 526ffcb2fc8SKeerthy static int k3_j72xx_bandgap_remove(struct platform_device *pdev) 527ffcb2fc8SKeerthy { 528ffcb2fc8SKeerthy pm_runtime_put_sync(&pdev->dev); 529ffcb2fc8SKeerthy pm_runtime_disable(&pdev->dev); 530ffcb2fc8SKeerthy 531ffcb2fc8SKeerthy return 0; 532ffcb2fc8SKeerthy } 533ffcb2fc8SKeerthy 5344aaec53bSJin Xiaoyun static const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j721e_data = { 535311f328fSBryan Brattlof .has_errata_i2128 = true, 536ffcb2fc8SKeerthy }; 537ffcb2fc8SKeerthy 5384aaec53bSJin Xiaoyun static const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j7200_data = { 539311f328fSBryan Brattlof .has_errata_i2128 = false, 540ffcb2fc8SKeerthy }; 541ffcb2fc8SKeerthy 542ffcb2fc8SKeerthy static const struct of_device_id of_k3_j72xx_bandgap_match[] = { 543ffcb2fc8SKeerthy { 544ffcb2fc8SKeerthy .compatible = "ti,j721e-vtm", 545ffcb2fc8SKeerthy .data = &k3_j72xx_bandgap_j721e_data, 546ffcb2fc8SKeerthy }, 547ffcb2fc8SKeerthy { 548ffcb2fc8SKeerthy .compatible = "ti,j7200-vtm", 549ffcb2fc8SKeerthy .data = &k3_j72xx_bandgap_j7200_data, 550ffcb2fc8SKeerthy }, 551ffcb2fc8SKeerthy { /* sentinel */ }, 552ffcb2fc8SKeerthy }; 553ffcb2fc8SKeerthy MODULE_DEVICE_TABLE(of, of_k3_j72xx_bandgap_match); 554ffcb2fc8SKeerthy 555ffcb2fc8SKeerthy static struct platform_driver k3_j72xx_bandgap_sensor_driver = { 556ffcb2fc8SKeerthy .probe = k3_j72xx_bandgap_probe, 557ffcb2fc8SKeerthy .remove = k3_j72xx_bandgap_remove, 558ffcb2fc8SKeerthy .driver = { 559ffcb2fc8SKeerthy .name = "k3-j72xx-soc-thermal", 560ffcb2fc8SKeerthy .of_match_table = of_k3_j72xx_bandgap_match, 561ffcb2fc8SKeerthy }, 562ffcb2fc8SKeerthy }; 563ffcb2fc8SKeerthy 564ffcb2fc8SKeerthy module_platform_driver(k3_j72xx_bandgap_sensor_driver); 565ffcb2fc8SKeerthy 566ffcb2fc8SKeerthy MODULE_DESCRIPTION("K3 bandgap temperature sensor driver"); 567ffcb2fc8SKeerthy MODULE_LICENSE("GPL"); 568ffcb2fc8SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>"); 569