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 void __iomem *fuse_base; 181ffcb2fc8SKeerthy struct k3_thermal_data *ts_data[K3_VTM_MAX_NUM_TS]; 182ffcb2fc8SKeerthy }; 183ffcb2fc8SKeerthy 184ffcb2fc8SKeerthy /* common data structures */ 185ffcb2fc8SKeerthy struct k3_thermal_data { 186ffcb2fc8SKeerthy struct k3_j72xx_bandgap *bgp; 187ffcb2fc8SKeerthy u32 ctrl_offset; 188ffcb2fc8SKeerthy u32 stat_offset; 189ffcb2fc8SKeerthy }; 190ffcb2fc8SKeerthy 191ffcb2fc8SKeerthy static int two_cmp(int tmp, int mask) 192ffcb2fc8SKeerthy { 193ffcb2fc8SKeerthy tmp = ~(tmp); 194ffcb2fc8SKeerthy tmp &= mask; 195ffcb2fc8SKeerthy tmp += 1; 196ffcb2fc8SKeerthy 197ffcb2fc8SKeerthy /* Return negative value */ 198ffcb2fc8SKeerthy return (0 - tmp); 199ffcb2fc8SKeerthy } 200ffcb2fc8SKeerthy 201ffcb2fc8SKeerthy static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1, 202ffcb2fc8SKeerthy unsigned int s2) 203ffcb2fc8SKeerthy { 204ffcb2fc8SKeerthy int d01 = abs(s0 - s1); 205ffcb2fc8SKeerthy int d02 = abs(s0 - s2); 206ffcb2fc8SKeerthy int d12 = abs(s1 - s2); 207ffcb2fc8SKeerthy 208ffcb2fc8SKeerthy if (d01 <= d02 && d01 <= d12) 209ffcb2fc8SKeerthy return (s0 + s1) / 2; 210ffcb2fc8SKeerthy 211ffcb2fc8SKeerthy if (d02 <= d01 && d02 <= d12) 212ffcb2fc8SKeerthy return (s0 + s2) / 2; 213ffcb2fc8SKeerthy 214ffcb2fc8SKeerthy return (s1 + s2) / 2; 215ffcb2fc8SKeerthy } 216ffcb2fc8SKeerthy 217ffcb2fc8SKeerthy static inline int k3_bgp_read_temp(struct k3_thermal_data *devdata, 218ffcb2fc8SKeerthy int *temp) 219ffcb2fc8SKeerthy { 220ffcb2fc8SKeerthy struct k3_j72xx_bandgap *bgp; 221ffcb2fc8SKeerthy unsigned int dtemp, s0, s1, s2; 222ffcb2fc8SKeerthy 223ffcb2fc8SKeerthy bgp = devdata->bgp; 224ffcb2fc8SKeerthy /* 225ffcb2fc8SKeerthy * Errata is applicable for am654 pg 1.0 silicon/J7ES. There 226ffcb2fc8SKeerthy * is a variation of the order for certain degree centigrade on AM654. 227ffcb2fc8SKeerthy * Work around that by getting the average of two closest 228ffcb2fc8SKeerthy * readings out of three readings everytime we want to 229ffcb2fc8SKeerthy * report temperatures. 230ffcb2fc8SKeerthy * 231ffcb2fc8SKeerthy * Errata workaround. 232ffcb2fc8SKeerthy */ 233ffcb2fc8SKeerthy s0 = readl(bgp->base + devdata->stat_offset) & 234ffcb2fc8SKeerthy K3_VTM_TS_STAT_DTEMP_MASK; 235ffcb2fc8SKeerthy s1 = readl(bgp->base + devdata->stat_offset) & 236ffcb2fc8SKeerthy K3_VTM_TS_STAT_DTEMP_MASK; 237ffcb2fc8SKeerthy s2 = readl(bgp->base + devdata->stat_offset) & 238ffcb2fc8SKeerthy K3_VTM_TS_STAT_DTEMP_MASK; 239ffcb2fc8SKeerthy dtemp = vtm_get_best_value(s0, s1, s2); 240ffcb2fc8SKeerthy 241ffcb2fc8SKeerthy if (dtemp < 0 || dtemp >= TABLE_SIZE) 242ffcb2fc8SKeerthy return -EINVAL; 243ffcb2fc8SKeerthy 244ffcb2fc8SKeerthy *temp = derived_table[dtemp]; 245ffcb2fc8SKeerthy 246ffcb2fc8SKeerthy return 0; 247ffcb2fc8SKeerthy } 248ffcb2fc8SKeerthy 249ffcb2fc8SKeerthy /* Get temperature callback function for thermal zone */ 250ffcb2fc8SKeerthy static int k3_thermal_get_temp(void *devdata, int *temp) 251ffcb2fc8SKeerthy { 252ffcb2fc8SKeerthy struct k3_thermal_data *data = devdata; 253ffcb2fc8SKeerthy int ret = 0; 254ffcb2fc8SKeerthy 255ffcb2fc8SKeerthy ret = k3_bgp_read_temp(data, temp); 256ffcb2fc8SKeerthy if (ret) 257ffcb2fc8SKeerthy return ret; 258ffcb2fc8SKeerthy 259ffcb2fc8SKeerthy return ret; 260ffcb2fc8SKeerthy } 261ffcb2fc8SKeerthy 262ffcb2fc8SKeerthy static const struct thermal_zone_of_device_ops k3_of_thermal_ops = { 263ffcb2fc8SKeerthy .get_temp = k3_thermal_get_temp, 264ffcb2fc8SKeerthy }; 265ffcb2fc8SKeerthy 266ffcb2fc8SKeerthy static int k3_j72xx_bandgap_temp_to_adc_code(int temp) 267ffcb2fc8SKeerthy { 268ffcb2fc8SKeerthy int low = 0, high = TABLE_SIZE - 1, mid; 269ffcb2fc8SKeerthy 270ffcb2fc8SKeerthy if (temp > 160000 || temp < -50000) 271ffcb2fc8SKeerthy return -EINVAL; 272ffcb2fc8SKeerthy 273ffcb2fc8SKeerthy /* Binary search to find the adc code */ 274ffcb2fc8SKeerthy while (low < (high - 1)) { 275ffcb2fc8SKeerthy mid = (low + high) / 2; 276ffcb2fc8SKeerthy if (temp <= derived_table[mid]) 277ffcb2fc8SKeerthy high = mid; 278ffcb2fc8SKeerthy else 279ffcb2fc8SKeerthy low = mid; 280ffcb2fc8SKeerthy } 281ffcb2fc8SKeerthy 282ffcb2fc8SKeerthy return mid; 283ffcb2fc8SKeerthy } 284ffcb2fc8SKeerthy 285ffcb2fc8SKeerthy static void get_efuse_values(int id, struct k3_thermal_data *data, int *err, 286ffcb2fc8SKeerthy struct k3_j72xx_bandgap *bgp) 287ffcb2fc8SKeerthy { 288ffcb2fc8SKeerthy int i, tmp, pow; 289ffcb2fc8SKeerthy int ct_offsets[5][K3_VTM_CORRECTION_TEMP_CNT] = { 290ffcb2fc8SKeerthy { 0x0, 0x8, 0x4 }, 291ffcb2fc8SKeerthy { 0x0, 0x8, 0x4 }, 292ffcb2fc8SKeerthy { 0x0, -1, 0x4 }, 293ffcb2fc8SKeerthy { 0x0, 0xC, -1 }, 294ffcb2fc8SKeerthy { 0x0, 0xc, 0x8 } 295ffcb2fc8SKeerthy }; 296ffcb2fc8SKeerthy int ct_bm[5][K3_VTM_CORRECTION_TEMP_CNT] = { 297ffcb2fc8SKeerthy { 0x3f, 0x1fe000, 0x1ff }, 298ffcb2fc8SKeerthy { 0xfc0, 0x1fe000, 0x3fe00 }, 299ffcb2fc8SKeerthy { 0x3f000, 0x7f800000, 0x7fc0000 }, 300ffcb2fc8SKeerthy { 0xfc0000, 0x1fe0, 0x1f800000 }, 301ffcb2fc8SKeerthy { 0x3f000000, 0x1fe000, 0x1ff0 } 302ffcb2fc8SKeerthy }; 303ffcb2fc8SKeerthy 304ffcb2fc8SKeerthy for (i = 0; i < 3; i++) { 305ffcb2fc8SKeerthy /* Extract the offset value using bit-mask */ 306ffcb2fc8SKeerthy if (ct_offsets[id][i] == -1 && i == 1) { 307ffcb2fc8SKeerthy /* 25C offset Case of Sensor 2 split between 2 regs */ 308ffcb2fc8SKeerthy tmp = (readl(bgp->fuse_base + 0x8) & 0xE0000000) >> (29); 309ffcb2fc8SKeerthy tmp |= ((readl(bgp->fuse_base + 0xC) & 0x1F) << 3); 310ffcb2fc8SKeerthy pow = tmp & 0x80; 311ffcb2fc8SKeerthy } else if (ct_offsets[id][i] == -1 && i == 2) { 312ffcb2fc8SKeerthy /* 125C Case of Sensor 3 split between 2 regs */ 313ffcb2fc8SKeerthy tmp = (readl(bgp->fuse_base + 0x4) & 0xF8000000) >> (27); 314ffcb2fc8SKeerthy tmp |= ((readl(bgp->fuse_base + 0x8) & 0xF) << 5); 315ffcb2fc8SKeerthy pow = tmp & 0x100; 316ffcb2fc8SKeerthy } else { 317ffcb2fc8SKeerthy tmp = readl(bgp->fuse_base + ct_offsets[id][i]); 318ffcb2fc8SKeerthy tmp &= ct_bm[id][i]; 319ffcb2fc8SKeerthy tmp = tmp >> __ffs(ct_bm[id][i]); 320ffcb2fc8SKeerthy 321ffcb2fc8SKeerthy /* Obtain the sign bit pow*/ 322ffcb2fc8SKeerthy pow = ct_bm[id][i] >> __ffs(ct_bm[id][i]); 323ffcb2fc8SKeerthy pow += 1; 324ffcb2fc8SKeerthy pow /= 2; 325ffcb2fc8SKeerthy } 326ffcb2fc8SKeerthy 327ffcb2fc8SKeerthy /* Check for negative value */ 328ffcb2fc8SKeerthy if (tmp & pow) { 329ffcb2fc8SKeerthy /* 2's complement value */ 330ffcb2fc8SKeerthy tmp = two_cmp(tmp, ct_bm[id][i] >> __ffs(ct_bm[id][i])); 331ffcb2fc8SKeerthy } 332ffcb2fc8SKeerthy err[i] = tmp; 333ffcb2fc8SKeerthy } 334ffcb2fc8SKeerthy 335ffcb2fc8SKeerthy /* Err value for 150C is set to 0 */ 336ffcb2fc8SKeerthy err[i] = 0; 337ffcb2fc8SKeerthy } 338ffcb2fc8SKeerthy 339ffcb2fc8SKeerthy static void print_look_up_table(struct device *dev, int *ref_table) 340ffcb2fc8SKeerthy { 341ffcb2fc8SKeerthy int i; 342ffcb2fc8SKeerthy 343ffcb2fc8SKeerthy dev_dbg(dev, "The contents of derived array\n"); 344ffcb2fc8SKeerthy dev_dbg(dev, "Code Temperature\n"); 345ffcb2fc8SKeerthy for (i = 0; i < TABLE_SIZE; i++) 346ffcb2fc8SKeerthy dev_dbg(dev, "%d %d %d\n", i, derived_table[i], ref_table[i]); 347ffcb2fc8SKeerthy } 348ffcb2fc8SKeerthy 349ffcb2fc8SKeerthy struct k3_j72xx_bandgap_data { 350ffcb2fc8SKeerthy unsigned int has_errata_i2128; 351ffcb2fc8SKeerthy }; 352ffcb2fc8SKeerthy 353ffcb2fc8SKeerthy static int k3_j72xx_bandgap_probe(struct platform_device *pdev) 354ffcb2fc8SKeerthy { 355ffcb2fc8SKeerthy int ret = 0, cnt, val, id; 356ffcb2fc8SKeerthy int high_max, low_temp; 357ffcb2fc8SKeerthy struct resource *res; 358ffcb2fc8SKeerthy struct device *dev = &pdev->dev; 359ffcb2fc8SKeerthy struct k3_j72xx_bandgap *bgp; 360ffcb2fc8SKeerthy struct k3_thermal_data *data; 361ffcb2fc8SKeerthy int workaround_needed = 0; 362ffcb2fc8SKeerthy const struct k3_j72xx_bandgap_data *driver_data; 363ffcb2fc8SKeerthy struct thermal_zone_device *ti_thermal; 364ffcb2fc8SKeerthy int *ref_table; 365ffcb2fc8SKeerthy struct err_values err_vals; 366ffcb2fc8SKeerthy 367ffcb2fc8SKeerthy const s64 golden_factors[] = { 368ffcb2fc8SKeerthy -490019999999999936, 369ffcb2fc8SKeerthy 3251200000000000, 370ffcb2fc8SKeerthy -1705800000000, 371ffcb2fc8SKeerthy 603730000, 372ffcb2fc8SKeerthy -92627, 373ffcb2fc8SKeerthy }; 374ffcb2fc8SKeerthy 375ffcb2fc8SKeerthy const s64 pvt_wa_factors[] = { 376ffcb2fc8SKeerthy -415230000000000000, 377ffcb2fc8SKeerthy 3126600000000000, 378ffcb2fc8SKeerthy -1157800000000, 379ffcb2fc8SKeerthy }; 380ffcb2fc8SKeerthy 381ffcb2fc8SKeerthy bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL); 382ffcb2fc8SKeerthy if (!bgp) 383ffcb2fc8SKeerthy return -ENOMEM; 384ffcb2fc8SKeerthy 385ffcb2fc8SKeerthy bgp->dev = dev; 386ffcb2fc8SKeerthy res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 387ffcb2fc8SKeerthy bgp->base = devm_ioremap_resource(dev, res); 388ffcb2fc8SKeerthy if (IS_ERR(bgp->base)) 389ffcb2fc8SKeerthy return PTR_ERR(bgp->base); 390ffcb2fc8SKeerthy 391ffcb2fc8SKeerthy res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 392ffcb2fc8SKeerthy bgp->cfg2_base = devm_ioremap_resource(dev, res); 393ffcb2fc8SKeerthy if (IS_ERR(bgp->cfg2_base)) 394ffcb2fc8SKeerthy return PTR_ERR(bgp->cfg2_base); 395ffcb2fc8SKeerthy 396ffcb2fc8SKeerthy res = platform_get_resource(pdev, IORESOURCE_MEM, 2); 397ffcb2fc8SKeerthy bgp->fuse_base = devm_ioremap_resource(dev, res); 398ffcb2fc8SKeerthy if (IS_ERR(bgp->fuse_base)) 399ffcb2fc8SKeerthy return PTR_ERR(bgp->fuse_base); 400ffcb2fc8SKeerthy 401ffcb2fc8SKeerthy driver_data = of_device_get_match_data(dev); 402ffcb2fc8SKeerthy if (driver_data) 403ffcb2fc8SKeerthy workaround_needed = driver_data->has_errata_i2128; 404ffcb2fc8SKeerthy 405ffcb2fc8SKeerthy pm_runtime_enable(dev); 406ffcb2fc8SKeerthy ret = pm_runtime_get_sync(dev); 407ffcb2fc8SKeerthy if (ret < 0) { 408ffcb2fc8SKeerthy pm_runtime_put_noidle(dev); 409ffcb2fc8SKeerthy pm_runtime_disable(dev); 410ffcb2fc8SKeerthy return ret; 411ffcb2fc8SKeerthy } 412ffcb2fc8SKeerthy 413ffcb2fc8SKeerthy /* Get the sensor count in the VTM */ 414ffcb2fc8SKeerthy val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET); 415ffcb2fc8SKeerthy cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK; 416ffcb2fc8SKeerthy cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK); 417ffcb2fc8SKeerthy 418ffcb2fc8SKeerthy data = devm_kcalloc(bgp->dev, cnt, sizeof(*data), GFP_KERNEL); 419ffcb2fc8SKeerthy if (!data) { 420ffcb2fc8SKeerthy ret = -ENOMEM; 421ffcb2fc8SKeerthy goto err_alloc; 422ffcb2fc8SKeerthy } 423ffcb2fc8SKeerthy 424ffcb2fc8SKeerthy ref_table = kzalloc(sizeof(*ref_table) * TABLE_SIZE, GFP_KERNEL); 425ffcb2fc8SKeerthy if (!ref_table) { 426ffcb2fc8SKeerthy ret = -ENOMEM; 427ffcb2fc8SKeerthy goto err_alloc; 428ffcb2fc8SKeerthy } 429ffcb2fc8SKeerthy 430ffcb2fc8SKeerthy derived_table = devm_kzalloc(bgp->dev, sizeof(*derived_table) * TABLE_SIZE, 431ffcb2fc8SKeerthy GFP_KERNEL); 432ffcb2fc8SKeerthy if (!derived_table) { 433ffcb2fc8SKeerthy ret = -ENOMEM; 43499a049aaSBryan Brattlof goto err_free_ref_table; 435ffcb2fc8SKeerthy } 436ffcb2fc8SKeerthy 437ffcb2fc8SKeerthy /* Workaround not needed if bit30/bit31 is set even for J721e */ 438ffcb2fc8SKeerthy if (workaround_needed && (readl(bgp->fuse_base + 0x0) & 0xc0000000) == 0xc0000000) 439ffcb2fc8SKeerthy workaround_needed = false; 440ffcb2fc8SKeerthy 441ffcb2fc8SKeerthy dev_dbg(bgp->dev, "Work around %sneeded\n", 442ffcb2fc8SKeerthy workaround_needed ? "not " : ""); 443ffcb2fc8SKeerthy 444ffcb2fc8SKeerthy if (!workaround_needed) 445ffcb2fc8SKeerthy init_table(5, ref_table, golden_factors); 446ffcb2fc8SKeerthy else 447ffcb2fc8SKeerthy init_table(3, ref_table, pvt_wa_factors); 448ffcb2fc8SKeerthy 449ffcb2fc8SKeerthy /* Register the thermal sensors */ 450ffcb2fc8SKeerthy for (id = 0; id < cnt; id++) { 451ffcb2fc8SKeerthy data[id].bgp = bgp; 452ffcb2fc8SKeerthy data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20; 453ffcb2fc8SKeerthy data[id].stat_offset = data[id].ctrl_offset + 454ffcb2fc8SKeerthy K3_VTM_TMPSENS_STAT_OFFSET; 455ffcb2fc8SKeerthy 456ffcb2fc8SKeerthy if (workaround_needed) { 457ffcb2fc8SKeerthy /* ref adc values for -40C, 30C & 125C respectively */ 458ffcb2fc8SKeerthy err_vals.refs[0] = MINUS40CREF; 459ffcb2fc8SKeerthy err_vals.refs[1] = PLUS30CREF; 460ffcb2fc8SKeerthy err_vals.refs[2] = PLUS125CREF; 461ffcb2fc8SKeerthy err_vals.refs[3] = PLUS150CREF; 462ffcb2fc8SKeerthy get_efuse_values(id, &data[id], err_vals.errs, bgp); 463ffcb2fc8SKeerthy } 464ffcb2fc8SKeerthy 465ffcb2fc8SKeerthy if (id == 0 && workaround_needed) 466ffcb2fc8SKeerthy prep_lookup_table(&err_vals, ref_table); 467ffcb2fc8SKeerthy else if (id == 0 && !workaround_needed) 468ffcb2fc8SKeerthy memcpy(derived_table, ref_table, TABLE_SIZE * 4); 469ffcb2fc8SKeerthy 470ffcb2fc8SKeerthy val = readl(data[id].bgp->cfg2_base + data[id].ctrl_offset); 471ffcb2fc8SKeerthy val |= (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN | 472ffcb2fc8SKeerthy K3_VTM_TMPSENS_CTRL_SOC | 473ffcb2fc8SKeerthy K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4)); 474ffcb2fc8SKeerthy writel(val, data[id].bgp->cfg2_base + data[id].ctrl_offset); 475ffcb2fc8SKeerthy 476ffcb2fc8SKeerthy bgp->ts_data[id] = &data[id]; 477ffcb2fc8SKeerthy ti_thermal = 478ffcb2fc8SKeerthy devm_thermal_zone_of_sensor_register(bgp->dev, id, 479ffcb2fc8SKeerthy &data[id], 480ffcb2fc8SKeerthy &k3_of_thermal_ops); 481ffcb2fc8SKeerthy if (IS_ERR(ti_thermal)) { 482ffcb2fc8SKeerthy dev_err(bgp->dev, "thermal zone device is NULL\n"); 483ffcb2fc8SKeerthy ret = PTR_ERR(ti_thermal); 48499a049aaSBryan Brattlof goto err_free_ref_table; 485ffcb2fc8SKeerthy } 486ffcb2fc8SKeerthy } 487ffcb2fc8SKeerthy 488ffcb2fc8SKeerthy /* 489ffcb2fc8SKeerthy * Program TSHUT thresholds 490ffcb2fc8SKeerthy * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2 491ffcb2fc8SKeerthy * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN bit 492ffcb2fc8SKeerthy * This is already taken care as per of init 493ffcb2fc8SKeerthy * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN bit 494ffcb2fc8SKeerthy */ 495ffcb2fc8SKeerthy high_max = k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP); 496ffcb2fc8SKeerthy low_temp = k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP); 497ffcb2fc8SKeerthy 498ffcb2fc8SKeerthy writel((low_temp << 16) | high_max, data[0].bgp->cfg2_base + 499ffcb2fc8SKeerthy K3_VTM_MISC_CTRL2_OFFSET); 500ffcb2fc8SKeerthy mdelay(100); 501ffcb2fc8SKeerthy writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, data[0].bgp->cfg2_base + 502ffcb2fc8SKeerthy K3_VTM_MISC_CTRL_OFFSET); 503ffcb2fc8SKeerthy 504ffcb2fc8SKeerthy platform_set_drvdata(pdev, bgp); 505ffcb2fc8SKeerthy 506ffcb2fc8SKeerthy print_look_up_table(dev, ref_table); 507ffcb2fc8SKeerthy /* 508ffcb2fc8SKeerthy * Now that the derived_table has the appropriate look up values 509ffcb2fc8SKeerthy * Free up the ref_table 510ffcb2fc8SKeerthy */ 511ffcb2fc8SKeerthy kfree(ref_table); 512ffcb2fc8SKeerthy 513ffcb2fc8SKeerthy return 0; 514ffcb2fc8SKeerthy 51599a049aaSBryan Brattlof err_free_ref_table: 51699a049aaSBryan Brattlof kfree(ref_table); 51799a049aaSBryan Brattlof 518ffcb2fc8SKeerthy err_alloc: 519ffcb2fc8SKeerthy pm_runtime_put_sync(&pdev->dev); 520ffcb2fc8SKeerthy pm_runtime_disable(&pdev->dev); 521ffcb2fc8SKeerthy 522ffcb2fc8SKeerthy return ret; 523ffcb2fc8SKeerthy } 524ffcb2fc8SKeerthy 525ffcb2fc8SKeerthy static int k3_j72xx_bandgap_remove(struct platform_device *pdev) 526ffcb2fc8SKeerthy { 527ffcb2fc8SKeerthy pm_runtime_put_sync(&pdev->dev); 528ffcb2fc8SKeerthy pm_runtime_disable(&pdev->dev); 529ffcb2fc8SKeerthy 530ffcb2fc8SKeerthy return 0; 531ffcb2fc8SKeerthy } 532ffcb2fc8SKeerthy 533*4aaec53bSJin Xiaoyun static const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j721e_data = { 534ffcb2fc8SKeerthy .has_errata_i2128 = 1, 535ffcb2fc8SKeerthy }; 536ffcb2fc8SKeerthy 537*4aaec53bSJin Xiaoyun static const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j7200_data = { 538ffcb2fc8SKeerthy .has_errata_i2128 = 0, 539ffcb2fc8SKeerthy }; 540ffcb2fc8SKeerthy 541ffcb2fc8SKeerthy static const struct of_device_id of_k3_j72xx_bandgap_match[] = { 542ffcb2fc8SKeerthy { 543ffcb2fc8SKeerthy .compatible = "ti,j721e-vtm", 544ffcb2fc8SKeerthy .data = &k3_j72xx_bandgap_j721e_data, 545ffcb2fc8SKeerthy }, 546ffcb2fc8SKeerthy { 547ffcb2fc8SKeerthy .compatible = "ti,j7200-vtm", 548ffcb2fc8SKeerthy .data = &k3_j72xx_bandgap_j7200_data, 549ffcb2fc8SKeerthy }, 550ffcb2fc8SKeerthy { /* sentinel */ }, 551ffcb2fc8SKeerthy }; 552ffcb2fc8SKeerthy MODULE_DEVICE_TABLE(of, of_k3_j72xx_bandgap_match); 553ffcb2fc8SKeerthy 554ffcb2fc8SKeerthy static struct platform_driver k3_j72xx_bandgap_sensor_driver = { 555ffcb2fc8SKeerthy .probe = k3_j72xx_bandgap_probe, 556ffcb2fc8SKeerthy .remove = k3_j72xx_bandgap_remove, 557ffcb2fc8SKeerthy .driver = { 558ffcb2fc8SKeerthy .name = "k3-j72xx-soc-thermal", 559ffcb2fc8SKeerthy .of_match_table = of_k3_j72xx_bandgap_match, 560ffcb2fc8SKeerthy }, 561ffcb2fc8SKeerthy }; 562ffcb2fc8SKeerthy 563ffcb2fc8SKeerthy module_platform_driver(k3_j72xx_bandgap_sensor_driver); 564ffcb2fc8SKeerthy 565ffcb2fc8SKeerthy MODULE_DESCRIPTION("K3 bandgap temperature sensor driver"); 566ffcb2fc8SKeerthy MODULE_LICENSE("GPL"); 567ffcb2fc8SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>"); 568