Merge tag 'for-6.9/dm-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/device...
[linux-2.6-microblaze.git] / drivers / thermal / qoriq_thermal.c
index e587563..404f01c 100644 (file)
@@ -31,7 +31,6 @@
 #define TMR_DISABLE    0x0
 #define TMR_ME         0x80000000
 #define TMR_ALPF       0x0c000000
-#define TMR_MSITE_ALL  GENMASK(15, 0)
 
 #define REGS_TMTMIR    0x008   /* Temperature measurement interval Register */
 #define TMTMIR_DEFAULT 0x0000000f
                                            * Site Register
                                            */
 #define TRITSR_V       BIT(31)
+#define TRITSR_TP5     BIT(9)
 #define REGS_V2_TMSAR(n)       (0x304 + 16 * (n))      /* TMU monitoring
                                                * site adjustment register
                                                */
 #define REGS_TTRnCR(n) (0xf10 + 4 * (n)) /* Temperature Range n
                                           * Control Register
                                           */
+#define NUM_TTRCR_V1   4
+#define NUM_TTRCR_MAX  16
+
 #define REGS_IPBRR(n)          (0xbf8 + 4 * (n)) /* IP Block Revision
                                                   * Register n
                                                   */
@@ -71,6 +74,7 @@ struct qoriq_sensor {
 
 struct qoriq_tmu_data {
        int ver;
+       u32 ttrcr[NUM_TTRCR_MAX];
        struct regmap *regmap;
        struct clk *clk;
        struct qoriq_sensor     sensor[SITES_MAX];
@@ -105,6 +109,11 @@ static int tmu_get_temp(struct thermal_zone_device *tz, int *temp)
         * within sensor range. TEMP is an 9 bit value representing
         * temperature in KelVin.
         */
+
+       regmap_read(qdata->regmap, REGS_TMR, &val);
+       if (!(val & TMR_ME))
+               return -EAGAIN;
+
        if (regmap_read_poll_timeout(qdata->regmap,
                                     REGS_TRITSR(qsensor->id),
                                     val,
@@ -113,10 +122,15 @@ static int tmu_get_temp(struct thermal_zone_device *tz, int *temp)
                                     10 * USEC_PER_MSEC))
                return -ENODATA;
 
-       if (qdata->ver == TMU_VER1)
+       if (qdata->ver == TMU_VER1) {
                *temp = (val & GENMASK(7, 0)) * MILLIDEGREE_PER_DEGREE;
-       else
-               *temp = kelvin_to_millicelsius(val & GENMASK(8, 0));
+       } else {
+               if (val & TRITSR_TP5)
+                       *temp = milli_kelvin_to_millicelsius((val & GENMASK(8, 0)) *
+                                                            MILLIDEGREE_PER_DEGREE + 500);
+               else
+                       *temp = kelvin_to_millicelsius(val & GENMASK(8, 0));
+       }
 
        return 0;
 }
@@ -128,15 +142,7 @@ static const struct thermal_zone_device_ops tmu_tz_ops = {
 static int qoriq_tmu_register_tmu_zone(struct device *dev,
                                       struct qoriq_tmu_data *qdata)
 {
-       int id;
-
-       if (qdata->ver == TMU_VER1) {
-               regmap_write(qdata->regmap, REGS_TMR,
-                            TMR_MSITE_ALL | TMR_ME | TMR_ALPF);
-       } else {
-               regmap_write(qdata->regmap, REGS_V2_TMSR, TMR_MSITE_ALL);
-               regmap_write(qdata->regmap, REGS_TMR, TMR_ME | TMR_ALPF_V2);
-       }
+       int id, sites = 0;
 
        for (id = 0; id < SITES_MAX; id++) {
                struct thermal_zone_device *tzd;
@@ -153,14 +159,24 @@ static int qoriq_tmu_register_tmu_zone(struct device *dev,
                        if (ret == -ENODEV)
                                continue;
 
-                       regmap_write(qdata->regmap, REGS_TMR, TMR_DISABLE);
                        return ret;
                }
 
-               if (devm_thermal_add_hwmon_sysfs(dev, tzd))
-                       dev_warn(dev,
-                                "Failed to add hwmon sysfs attributes\n");
+               if (qdata->ver == TMU_VER1)
+                       sites |= 0x1 << (15 - id);
+               else
+                       sites |= 0x1 << id;
 
+               devm_thermal_add_hwmon_sysfs(dev, tzd);
+       }
+
+       if (sites) {
+               if (qdata->ver == TMU_VER1) {
+                       regmap_write(qdata->regmap, REGS_TMR, TMR_ME | TMR_ALPF | sites);
+               } else {
+                       regmap_write(qdata->regmap, REGS_V2_TMSR, sites);
+                       regmap_write(qdata->regmap, REGS_TMR, TMR_ME | TMR_ALPF_V2);
+               }
        }
 
        return 0;
@@ -170,17 +186,17 @@ static int qoriq_tmu_calibration(struct device *dev,
                                 struct qoriq_tmu_data *data)
 {
        int i, val, len;
-       u32 range[4];
        const u32 *calibration;
        struct device_node *np = dev->of_node;
 
        len = of_property_count_u32_elems(np, "fsl,tmu-range");
-       if (len < 0 || len > 4) {
+       if (len < 0 || (data->ver == TMU_VER1 && len > NUM_TTRCR_V1) ||
+           (data->ver > TMU_VER1 && len > NUM_TTRCR_MAX)) {
                dev_err(dev, "invalid range data.\n");
                return len;
        }
 
-       val = of_property_read_u32_array(np, "fsl,tmu-range", range, len);
+       val = of_property_read_u32_array(np, "fsl,tmu-range", data->ttrcr, len);
        if (val != 0) {
                dev_err(dev, "failed to read range data.\n");
                return val;
@@ -188,7 +204,7 @@ static int qoriq_tmu_calibration(struct device *dev,
 
        /* Init temperature range registers */
        for (i = 0; i < len; i++)
-               regmap_write(data->regmap, REGS_TTRnCR(i), range[i]);
+               regmap_write(data->regmap, REGS_TTRnCR(i), data->ttrcr[i]);
 
        calibration = of_get_property(np, "fsl,tmu-calibration", &len);
        if (calibration == NULL || len % 8) {
@@ -208,8 +224,6 @@ static int qoriq_tmu_calibration(struct device *dev,
 
 static void qoriq_tmu_init_device(struct qoriq_tmu_data *data)
 {
-       int i;
-
        /* Disable interrupt, using polling instead */
        regmap_write(data->regmap, REGS_TIER, TIER_DISABLE);
 
@@ -220,8 +234,6 @@ static void qoriq_tmu_init_device(struct qoriq_tmu_data *data)
        } else {
                regmap_write(data->regmap, REGS_V2_TMTMIR, TMTMIR_DEFAULT);
                regmap_write(data->regmap, REGS_V2_TEUMR(0), TEUMR0_V2);
-               for (i = 0; i < SITES_MAX; i++)
-                       regmap_write(data->regmap, REGS_V2_TMSAR(i), TMSARA_V2);
        }
 
        /* Disable monitoring */
@@ -230,7 +242,7 @@ static void qoriq_tmu_init_device(struct qoriq_tmu_data *data)
 
 static const struct regmap_range qoriq_yes_ranges[] = {
        regmap_reg_range(REGS_TMR, REGS_TSCFGR),
-       regmap_reg_range(REGS_TTRnCR(0), REGS_TTRnCR(3)),
+       regmap_reg_range(REGS_TTRnCR(0), REGS_TTRnCR(15)),
        regmap_reg_range(REGS_V2_TEUMR(0), REGS_V2_TEUMR(2)),
        regmap_reg_range(REGS_V2_TMSAR(0), REGS_V2_TMSAR(15)),
        regmap_reg_range(REGS_IPBRR(0), REGS_IPBRR(1)),