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