Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 1 | /* |
Miquel Raynal | a9d58a1 | 2017-12-22 17:14:10 +0100 | [diff] [blame] | 2 | * Marvell EBU Armada SoCs thermal sensor driver |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2013 Marvell |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/of_device.h> |
| 25 | #include <linux/thermal.h> |
Miquel Raynal | 6416368 | 2017-12-22 17:14:12 +0100 | [diff] [blame^] | 26 | #include <linux/iopoll.h> |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 27 | |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 28 | /* Thermal Manager Control and Status Register */ |
| 29 | #define PMU_TDC0_SW_RST_MASK (0x1 << 1) |
| 30 | #define PMU_TM_DISABLE_OFFS 0 |
| 31 | #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS) |
| 32 | #define PMU_TDC0_REF_CAL_CNT_OFFS 11 |
| 33 | #define PMU_TDC0_REF_CAL_CNT_MASK (0x1ff << PMU_TDC0_REF_CAL_CNT_OFFS) |
| 34 | #define PMU_TDC0_OTF_CAL_MASK (0x1 << 30) |
| 35 | #define PMU_TDC0_START_CAL_MASK (0x1 << 25) |
| 36 | |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 37 | #define A375_UNIT_CONTROL_SHIFT 27 |
| 38 | #define A375_UNIT_CONTROL_MASK 0x7 |
| 39 | #define A375_READOUT_INVERT BIT(15) |
| 40 | #define A375_HW_RESETn BIT(8) |
| 41 | |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 42 | /* Legacy bindings */ |
| 43 | #define LEGACY_CONTROL_MEM_LEN 0x4 |
| 44 | |
| 45 | /* Current bindings with the 2 control registers under the same memory area */ |
| 46 | #define LEGACY_CONTROL1_OFFSET 0x0 |
| 47 | #define CONTROL0_OFFSET 0x0 |
| 48 | #define CONTROL1_OFFSET 0x4 |
| 49 | |
Miquel Raynal | 8c0b888 | 2017-12-22 17:14:11 +0100 | [diff] [blame] | 50 | /* Errata fields */ |
| 51 | #define CONTROL0_TSEN_TC_TRIM_MASK 0x7 |
| 52 | #define CONTROL0_TSEN_TC_TRIM_VAL 0x3 |
| 53 | |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 54 | /* TSEN refers to the temperature sensors within the AP */ |
| 55 | #define CONTROL0_TSEN_START BIT(0) |
| 56 | #define CONTROL0_TSEN_RESET BIT(1) |
| 57 | #define CONTROL0_TSEN_ENABLE BIT(2) |
| 58 | |
Baruch Siach | ccf8f52 | 2017-12-22 17:14:09 +0100 | [diff] [blame] | 59 | /* EXT_TSEN refers to the external temperature sensors, out of the AP */ |
| 60 | #define CONTROL1_EXT_TSEN_SW_RESET BIT(7) |
| 61 | #define CONTROL1_EXT_TSEN_HW_RESETn BIT(8) |
| 62 | |
Miquel Raynal | 6416368 | 2017-12-22 17:14:12 +0100 | [diff] [blame^] | 63 | #define STATUS_POLL_PERIOD_US 1000 |
| 64 | #define STATUS_POLL_TIMEOUT_US 100000 |
| 65 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 66 | struct armada_thermal_data; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 67 | |
| 68 | /* Marvell EBU Thermal Sensor Dev Structure */ |
| 69 | struct armada_thermal_priv { |
Miquel Raynal | 8371b8a | 2017-12-22 17:14:07 +0100 | [diff] [blame] | 70 | void __iomem *status; |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 71 | void __iomem *control0; |
| 72 | void __iomem *control1; |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 73 | struct armada_thermal_data *data; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 76 | struct armada_thermal_data { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 77 | /* Initialize the sensor */ |
Ezequiel Garcia | 04bf3d7 | 2014-05-06 13:59:48 -0300 | [diff] [blame] | 78 | void (*init_sensor)(struct platform_device *pdev, |
| 79 | struct armada_thermal_priv *); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 80 | |
| 81 | /* Test for a valid sensor value (optional) */ |
| 82 | bool (*is_valid)(struct armada_thermal_priv *); |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 83 | |
Baruch Siach | 0cf3a1a | 2017-09-14 18:06:57 +0300 | [diff] [blame] | 84 | /* Formula coeficients: temp = (b - m * reg) / div */ |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 85 | s64 coef_b; |
| 86 | s64 coef_m; |
| 87 | u32 coef_div; |
Ezequiel Garcia | fd2c94d | 2014-05-06 13:59:49 -0300 | [diff] [blame] | 88 | bool inverted; |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 89 | bool signed_sample; |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 90 | |
| 91 | /* Register shift and mask to access the sensor temperature */ |
| 92 | unsigned int temp_shift; |
| 93 | unsigned int temp_mask; |
Miquel Raynal | 27d92f2 | 2017-12-22 17:14:05 +0100 | [diff] [blame] | 94 | u32 is_valid_bit; |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 95 | bool needs_control0; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Ezequiel Garcia | 04bf3d7 | 2014-05-06 13:59:48 -0300 | [diff] [blame] | 98 | static void armadaxp_init_sensor(struct platform_device *pdev, |
| 99 | struct armada_thermal_priv *priv) |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 100 | { |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 101 | u32 reg; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 102 | |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 103 | reg = readl_relaxed(priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 104 | reg |= PMU_TDC0_OTF_CAL_MASK; |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 105 | writel(reg, priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 106 | |
| 107 | /* Reference calibration value */ |
| 108 | reg &= ~PMU_TDC0_REF_CAL_CNT_MASK; |
| 109 | reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS); |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 110 | writel(reg, priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 111 | |
| 112 | /* Reset the sensor */ |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 113 | reg = readl_relaxed(priv->control1); |
| 114 | writel((reg | PMU_TDC0_SW_RST_MASK), priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 115 | |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 116 | writel(reg, priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 117 | |
| 118 | /* Enable the sensor */ |
Miquel Raynal | 8371b8a | 2017-12-22 17:14:07 +0100 | [diff] [blame] | 119 | reg = readl_relaxed(priv->status); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 120 | reg &= ~PMU_TM_DISABLE_MASK; |
Miquel Raynal | 8371b8a | 2017-12-22 17:14:07 +0100 | [diff] [blame] | 121 | writel(reg, priv->status); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Ezequiel Garcia | 04bf3d7 | 2014-05-06 13:59:48 -0300 | [diff] [blame] | 124 | static void armada370_init_sensor(struct platform_device *pdev, |
| 125 | struct armada_thermal_priv *priv) |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 126 | { |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 127 | u32 reg; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 128 | |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 129 | reg = readl_relaxed(priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 130 | reg |= PMU_TDC0_OTF_CAL_MASK; |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 131 | writel(reg, priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 132 | |
| 133 | /* Reference calibration value */ |
| 134 | reg &= ~PMU_TDC0_REF_CAL_CNT_MASK; |
| 135 | reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS); |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 136 | writel(reg, priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 137 | |
| 138 | reg &= ~PMU_TDC0_START_CAL_MASK; |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 139 | writel(reg, priv->control1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 140 | |
Baruch Siach | 7f3be01 | 2017-12-22 17:14:04 +0100 | [diff] [blame] | 141 | msleep(10); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 144 | static void armada375_init_sensor(struct platform_device *pdev, |
| 145 | struct armada_thermal_priv *priv) |
| 146 | { |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 147 | u32 reg; |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 148 | |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 149 | reg = readl(priv->control1); |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 150 | reg &= ~(A375_UNIT_CONTROL_MASK << A375_UNIT_CONTROL_SHIFT); |
| 151 | reg &= ~A375_READOUT_INVERT; |
| 152 | reg &= ~A375_HW_RESETn; |
| 153 | |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 154 | writel(reg, priv->control1); |
Baruch Siach | 7f3be01 | 2017-12-22 17:14:04 +0100 | [diff] [blame] | 155 | msleep(20); |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 156 | |
| 157 | reg |= A375_HW_RESETn; |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 158 | writel(reg, priv->control1); |
Baruch Siach | 7f3be01 | 2017-12-22 17:14:04 +0100 | [diff] [blame] | 159 | msleep(50); |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 160 | } |
| 161 | |
Miquel Raynal | 6416368 | 2017-12-22 17:14:12 +0100 | [diff] [blame^] | 162 | static void armada_wait_sensor_validity(struct armada_thermal_priv *priv) |
| 163 | { |
| 164 | u32 reg; |
| 165 | |
| 166 | readl_relaxed_poll_timeout(priv->status, reg, |
| 167 | reg & priv->data->is_valid_bit, |
| 168 | STATUS_POLL_PERIOD_US, |
| 169 | STATUS_POLL_TIMEOUT_US); |
| 170 | } |
| 171 | |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 172 | static void armada380_init_sensor(struct platform_device *pdev, |
| 173 | struct armada_thermal_priv *priv) |
| 174 | { |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 175 | u32 reg = readl_relaxed(priv->control1); |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 176 | |
Baruch Siach | ccf8f52 | 2017-12-22 17:14:09 +0100 | [diff] [blame] | 177 | /* Disable the HW/SW reset */ |
| 178 | reg |= CONTROL1_EXT_TSEN_HW_RESETn; |
| 179 | reg &= ~CONTROL1_EXT_TSEN_SW_RESET; |
| 180 | writel(reg, priv->control1); |
Miquel Raynal | 8c0b888 | 2017-12-22 17:14:11 +0100 | [diff] [blame] | 181 | |
| 182 | /* Set Tsen Tc Trim to correct default value (errata #132698) */ |
| 183 | if (priv->control0) { |
| 184 | reg = readl_relaxed(priv->control0); |
| 185 | reg &= ~CONTROL0_TSEN_TC_TRIM_MASK; |
| 186 | reg |= CONTROL0_TSEN_TC_TRIM_VAL; |
| 187 | writel(reg, priv->control0); |
Miquel Raynal | 8c0b888 | 2017-12-22 17:14:11 +0100 | [diff] [blame] | 188 | } |
Miquel Raynal | 6416368 | 2017-12-22 17:14:12 +0100 | [diff] [blame^] | 189 | |
| 190 | /* Wait the sensors to be valid or the core will warn the user */ |
| 191 | armada_wait_sensor_validity(priv); |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 192 | } |
| 193 | |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 194 | static void armada_ap806_init_sensor(struct platform_device *pdev, |
| 195 | struct armada_thermal_priv *priv) |
| 196 | { |
| 197 | u32 reg; |
| 198 | |
| 199 | reg = readl_relaxed(priv->control0); |
| 200 | reg &= ~CONTROL0_TSEN_RESET; |
| 201 | reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_ENABLE; |
| 202 | writel(reg, priv->control0); |
Miquel Raynal | 6416368 | 2017-12-22 17:14:12 +0100 | [diff] [blame^] | 203 | |
| 204 | /* Wait the sensors to be valid or the core will warn the user */ |
| 205 | armada_wait_sensor_validity(priv); |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 208 | static bool armada_is_valid(struct armada_thermal_priv *priv) |
| 209 | { |
Miquel Raynal | 8371b8a | 2017-12-22 17:14:07 +0100 | [diff] [blame] | 210 | u32 reg = readl_relaxed(priv->status); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 211 | |
Miquel Raynal | 27d92f2 | 2017-12-22 17:14:05 +0100 | [diff] [blame] | 212 | return reg & priv->data->is_valid_bit; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static int armada_get_temp(struct thermal_zone_device *thermal, |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 216 | int *temp) |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 217 | { |
| 218 | struct armada_thermal_priv *priv = thermal->devdata; |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 219 | u32 reg, div; |
| 220 | s64 sample, b, m; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 221 | |
| 222 | /* Valid check */ |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 223 | if (priv->data->is_valid && !priv->data->is_valid(priv)) { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 224 | dev_err(&thermal->device, |
| 225 | "Temperature sensor reading not valid\n"); |
| 226 | return -EIO; |
| 227 | } |
| 228 | |
Miquel Raynal | 8371b8a | 2017-12-22 17:14:07 +0100 | [diff] [blame] | 229 | reg = readl_relaxed(priv->status); |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 230 | reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask; |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 231 | if (priv->data->signed_sample) |
| 232 | /* The most significant bit is the sign bit */ |
| 233 | sample = sign_extend32(reg, fls(priv->data->temp_mask) - 1); |
| 234 | else |
| 235 | sample = reg; |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 236 | |
| 237 | /* Get formula coeficients */ |
| 238 | b = priv->data->coef_b; |
| 239 | m = priv->data->coef_m; |
| 240 | div = priv->data->coef_div; |
| 241 | |
Ezequiel Garcia | fd2c94d | 2014-05-06 13:59:49 -0300 | [diff] [blame] | 242 | if (priv->data->inverted) |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 243 | *temp = div_s64((m * sample) - b, div); |
Ezequiel Garcia | fd2c94d | 2014-05-06 13:59:49 -0300 | [diff] [blame] | 244 | else |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 245 | *temp = div_s64(b - (m * sample), div); |
| 246 | |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static struct thermal_zone_device_ops ops = { |
| 251 | .get_temp = armada_get_temp, |
| 252 | }; |
| 253 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 254 | static const struct armada_thermal_data armadaxp_data = { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 255 | .init_sensor = armadaxp_init_sensor, |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 256 | .temp_shift = 10, |
| 257 | .temp_mask = 0x1ff, |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 258 | .coef_b = 3153000000ULL, |
| 259 | .coef_m = 10000000ULL, |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 260 | .coef_div = 13825, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 261 | }; |
| 262 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 263 | static const struct armada_thermal_data armada370_data = { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 264 | .is_valid = armada_is_valid, |
| 265 | .init_sensor = armada370_init_sensor, |
Miquel Raynal | 27d92f2 | 2017-12-22 17:14:05 +0100 | [diff] [blame] | 266 | .is_valid_bit = BIT(9), |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 267 | .temp_shift = 10, |
| 268 | .temp_mask = 0x1ff, |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 269 | .coef_b = 3153000000ULL, |
| 270 | .coef_m = 10000000ULL, |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 271 | .coef_div = 13825, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 274 | static const struct armada_thermal_data armada375_data = { |
| 275 | .is_valid = armada_is_valid, |
| 276 | .init_sensor = armada375_init_sensor, |
Miquel Raynal | 27d92f2 | 2017-12-22 17:14:05 +0100 | [diff] [blame] | 277 | .is_valid_bit = BIT(10), |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 278 | .temp_shift = 0, |
| 279 | .temp_mask = 0x1ff, |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 280 | .coef_b = 3171900000ULL, |
| 281 | .coef_m = 10000000ULL, |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 282 | .coef_div = 13616, |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 283 | .needs_control0 = true, |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 284 | }; |
| 285 | |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 286 | static const struct armada_thermal_data armada380_data = { |
| 287 | .is_valid = armada_is_valid, |
| 288 | .init_sensor = armada380_init_sensor, |
Miquel Raynal | 27d92f2 | 2017-12-22 17:14:05 +0100 | [diff] [blame] | 289 | .is_valid_bit = BIT(10), |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 290 | .temp_shift = 0, |
| 291 | .temp_mask = 0x3ff, |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 292 | .coef_b = 1172499100ULL, |
| 293 | .coef_m = 2000096ULL, |
Nadav Haklai | b56100d | 2015-08-06 18:03:49 +0200 | [diff] [blame] | 294 | .coef_div = 4201, |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 295 | .inverted = true, |
| 296 | }; |
| 297 | |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 298 | static const struct armada_thermal_data armada_ap806_data = { |
| 299 | .is_valid = armada_is_valid, |
| 300 | .init_sensor = armada_ap806_init_sensor, |
| 301 | .is_valid_bit = BIT(16), |
| 302 | .temp_shift = 0, |
| 303 | .temp_mask = 0x3ff, |
| 304 | .coef_b = -150000LL, |
| 305 | .coef_m = 423ULL, |
| 306 | .coef_div = 1, |
| 307 | .inverted = true, |
| 308 | .signed_sample = true, |
| 309 | .needs_control0 = true, |
| 310 | }; |
| 311 | |
Baruch Siach | ccf8f52 | 2017-12-22 17:14:09 +0100 | [diff] [blame] | 312 | static const struct armada_thermal_data armada_cp110_data = { |
| 313 | .is_valid = armada_is_valid, |
| 314 | .init_sensor = armada380_init_sensor, |
| 315 | .is_valid_bit = BIT(10), |
| 316 | .temp_shift = 0, |
| 317 | .temp_mask = 0x3ff, |
| 318 | .coef_b = 1172499100ULL, |
| 319 | .coef_m = 2000096ULL, |
| 320 | .coef_div = 4201, |
| 321 | .inverted = true, |
| 322 | .needs_control0 = true, |
| 323 | }; |
| 324 | |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 325 | static const struct of_device_id armada_thermal_id_table[] = { |
| 326 | { |
| 327 | .compatible = "marvell,armadaxp-thermal", |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 328 | .data = &armadaxp_data, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 329 | }, |
| 330 | { |
| 331 | .compatible = "marvell,armada370-thermal", |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 332 | .data = &armada370_data, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 333 | }, |
| 334 | { |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 335 | .compatible = "marvell,armada375-thermal", |
| 336 | .data = &armada375_data, |
| 337 | }, |
| 338 | { |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 339 | .compatible = "marvell,armada380-thermal", |
| 340 | .data = &armada380_data, |
| 341 | }, |
| 342 | { |
Baruch Siach | 2ff1279 | 2017-12-22 17:14:08 +0100 | [diff] [blame] | 343 | .compatible = "marvell,armada-ap806-thermal", |
| 344 | .data = &armada_ap806_data, |
| 345 | }, |
| 346 | { |
Baruch Siach | ccf8f52 | 2017-12-22 17:14:09 +0100 | [diff] [blame] | 347 | .compatible = "marvell,armada-cp110-thermal", |
| 348 | .data = &armada_cp110_data, |
| 349 | }, |
| 350 | { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 351 | /* sentinel */ |
| 352 | }, |
| 353 | }; |
| 354 | MODULE_DEVICE_TABLE(of, armada_thermal_id_table); |
| 355 | |
| 356 | static int armada_thermal_probe(struct platform_device *pdev) |
| 357 | { |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 358 | void __iomem *control = NULL; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 359 | struct thermal_zone_device *thermal; |
| 360 | const struct of_device_id *match; |
| 361 | struct armada_thermal_priv *priv; |
| 362 | struct resource *res; |
| 363 | |
| 364 | match = of_match_device(armada_thermal_id_table, &pdev->dev); |
| 365 | if (!match) |
| 366 | return -ENODEV; |
| 367 | |
| 368 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 369 | if (!priv) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Miquel Raynal | 8371b8a | 2017-12-22 17:14:07 +0100 | [diff] [blame] | 373 | priv->status = devm_ioremap_resource(&pdev->dev, res); |
| 374 | if (IS_ERR(priv->status)) |
| 375 | return PTR_ERR(priv->status); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 376 | |
| 377 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 378 | control = devm_ioremap_resource(&pdev->dev, res); |
| 379 | if (IS_ERR(control)) |
| 380 | return PTR_ERR(control); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 381 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 382 | priv->data = (struct armada_thermal_data *)match->data; |
Miquel Raynal | 2f28e4c | 2017-12-22 17:14:06 +0100 | [diff] [blame] | 383 | |
| 384 | /* |
| 385 | * Legacy DT bindings only described "control1" register (also referred |
| 386 | * as "control MSB" on old documentation). New bindings cover |
| 387 | * "control0/control LSB" and "control1/control MSB" registers within |
| 388 | * the same resource, which is then of size 8 instead of 4. |
| 389 | */ |
| 390 | if (resource_size(res) == LEGACY_CONTROL_MEM_LEN) { |
| 391 | /* ->control0 unavailable in this configuration */ |
| 392 | if (priv->data->needs_control0) { |
| 393 | dev_err(&pdev->dev, "No access to control0 register\n"); |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | |
| 397 | priv->control1 = control + LEGACY_CONTROL1_OFFSET; |
| 398 | } else { |
| 399 | priv->control0 = control + CONTROL0_OFFSET; |
| 400 | priv->control1 = control + CONTROL1_OFFSET; |
| 401 | } |
| 402 | |
Ezequiel Garcia | 04bf3d7 | 2014-05-06 13:59:48 -0300 | [diff] [blame] | 403 | priv->data->init_sensor(pdev, priv); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 404 | |
| 405 | thermal = thermal_zone_device_register("armada_thermal", 0, 0, |
| 406 | priv, &ops, NULL, 0, 0); |
| 407 | if (IS_ERR(thermal)) { |
| 408 | dev_err(&pdev->dev, |
| 409 | "Failed to register thermal zone device\n"); |
| 410 | return PTR_ERR(thermal); |
| 411 | } |
| 412 | |
| 413 | platform_set_drvdata(pdev, thermal); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int armada_thermal_exit(struct platform_device *pdev) |
| 419 | { |
| 420 | struct thermal_zone_device *armada_thermal = |
| 421 | platform_get_drvdata(pdev); |
| 422 | |
| 423 | thermal_zone_device_unregister(armada_thermal); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static struct platform_driver armada_thermal_driver = { |
| 429 | .probe = armada_thermal_probe, |
| 430 | .remove = armada_thermal_exit, |
| 431 | .driver = { |
| 432 | .name = "armada_thermal", |
Sachin Kamat | 1d089e0 | 2013-05-16 10:28:08 +0000 | [diff] [blame] | 433 | .of_match_table = armada_thermal_id_table, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 434 | }, |
| 435 | }; |
| 436 | |
| 437 | module_platform_driver(armada_thermal_driver); |
| 438 | |
| 439 | MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>"); |
Miquel Raynal | a9d58a1 | 2017-12-22 17:14:10 +0100 | [diff] [blame] | 440 | MODULE_DESCRIPTION("Marvell EBU Armada SoCs thermal driver"); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 441 | MODULE_LICENSE("GPL v2"); |