Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Marvell Armada 370/XP thermal sensor driver |
| 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> |
| 26 | |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 27 | #define THERMAL_VALID_MASK 0x1 |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 28 | |
| 29 | /* Thermal Manager Control and Status Register */ |
| 30 | #define PMU_TDC0_SW_RST_MASK (0x1 << 1) |
| 31 | #define PMU_TM_DISABLE_OFFS 0 |
| 32 | #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS) |
| 33 | #define PMU_TDC0_REF_CAL_CNT_OFFS 11 |
| 34 | #define PMU_TDC0_REF_CAL_CNT_MASK (0x1ff << PMU_TDC0_REF_CAL_CNT_OFFS) |
| 35 | #define PMU_TDC0_OTF_CAL_MASK (0x1 << 30) |
| 36 | #define PMU_TDC0_START_CAL_MASK (0x1 << 25) |
| 37 | |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 38 | #define A375_UNIT_CONTROL_SHIFT 27 |
| 39 | #define A375_UNIT_CONTROL_MASK 0x7 |
| 40 | #define A375_READOUT_INVERT BIT(15) |
| 41 | #define A375_HW_RESETn BIT(8) |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 42 | #define A380_HW_RESET BIT(8) |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 43 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 44 | struct armada_thermal_data; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 45 | |
| 46 | /* Marvell EBU Thermal Sensor Dev Structure */ |
| 47 | struct armada_thermal_priv { |
| 48 | void __iomem *sensor; |
| 49 | void __iomem *control; |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 50 | struct armada_thermal_data *data; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 53 | struct armada_thermal_data { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 54 | /* Initialize the sensor */ |
Ezequiel Garcia | 04bf3d7 | 2014-05-06 13:59:48 -0300 | [diff] [blame] | 55 | void (*init_sensor)(struct platform_device *pdev, |
| 56 | struct armada_thermal_priv *); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 57 | |
| 58 | /* Test for a valid sensor value (optional) */ |
| 59 | bool (*is_valid)(struct armada_thermal_priv *); |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 60 | |
| 61 | /* Formula coeficients: temp = (b + m * reg) / div */ |
| 62 | unsigned long coef_b; |
| 63 | unsigned long coef_m; |
| 64 | unsigned long coef_div; |
Ezequiel Garcia | fd2c94d | 2014-05-06 13:59:49 -0300 | [diff] [blame] | 65 | bool inverted; |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 66 | |
| 67 | /* Register shift and mask to access the sensor temperature */ |
| 68 | unsigned int temp_shift; |
| 69 | unsigned int temp_mask; |
| 70 | unsigned int is_valid_shift; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Ezequiel Garcia | 04bf3d7 | 2014-05-06 13:59:48 -0300 | [diff] [blame] | 73 | static void armadaxp_init_sensor(struct platform_device *pdev, |
| 74 | struct armada_thermal_priv *priv) |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 75 | { |
| 76 | unsigned long reg; |
| 77 | |
| 78 | reg = readl_relaxed(priv->control); |
| 79 | reg |= PMU_TDC0_OTF_CAL_MASK; |
| 80 | writel(reg, priv->control); |
| 81 | |
| 82 | /* Reference calibration value */ |
| 83 | reg &= ~PMU_TDC0_REF_CAL_CNT_MASK; |
| 84 | reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS); |
| 85 | writel(reg, priv->control); |
| 86 | |
| 87 | /* Reset the sensor */ |
| 88 | reg = readl_relaxed(priv->control); |
| 89 | writel((reg | PMU_TDC0_SW_RST_MASK), priv->control); |
| 90 | |
| 91 | writel(reg, priv->control); |
| 92 | |
| 93 | /* Enable the sensor */ |
| 94 | reg = readl_relaxed(priv->sensor); |
| 95 | reg &= ~PMU_TM_DISABLE_MASK; |
| 96 | writel(reg, priv->sensor); |
| 97 | } |
| 98 | |
Ezequiel Garcia | 04bf3d7 | 2014-05-06 13:59:48 -0300 | [diff] [blame] | 99 | static void armada370_init_sensor(struct platform_device *pdev, |
| 100 | struct armada_thermal_priv *priv) |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 101 | { |
| 102 | unsigned long reg; |
| 103 | |
| 104 | reg = readl_relaxed(priv->control); |
| 105 | reg |= PMU_TDC0_OTF_CAL_MASK; |
| 106 | writel(reg, priv->control); |
| 107 | |
| 108 | /* Reference calibration value */ |
| 109 | reg &= ~PMU_TDC0_REF_CAL_CNT_MASK; |
| 110 | reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS); |
| 111 | writel(reg, priv->control); |
| 112 | |
| 113 | reg &= ~PMU_TDC0_START_CAL_MASK; |
| 114 | writel(reg, priv->control); |
| 115 | |
| 116 | mdelay(10); |
| 117 | } |
| 118 | |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 119 | static void armada375_init_sensor(struct platform_device *pdev, |
| 120 | struct armada_thermal_priv *priv) |
| 121 | { |
| 122 | unsigned long reg; |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 123 | |
| 124 | reg = readl(priv->control + 4); |
| 125 | reg &= ~(A375_UNIT_CONTROL_MASK << A375_UNIT_CONTROL_SHIFT); |
| 126 | reg &= ~A375_READOUT_INVERT; |
| 127 | reg &= ~A375_HW_RESETn; |
| 128 | |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 129 | writel(reg, priv->control + 4); |
| 130 | mdelay(20); |
| 131 | |
| 132 | reg |= A375_HW_RESETn; |
| 133 | writel(reg, priv->control + 4); |
| 134 | mdelay(50); |
| 135 | } |
| 136 | |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 137 | static void armada380_init_sensor(struct platform_device *pdev, |
| 138 | struct armada_thermal_priv *priv) |
| 139 | { |
| 140 | unsigned long reg = readl_relaxed(priv->control); |
| 141 | |
| 142 | /* Reset hardware once */ |
| 143 | if (!(reg & A380_HW_RESET)) { |
| 144 | reg |= A380_HW_RESET; |
| 145 | writel(reg, priv->control); |
| 146 | mdelay(10); |
| 147 | } |
| 148 | } |
| 149 | |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 150 | static bool armada_is_valid(struct armada_thermal_priv *priv) |
| 151 | { |
| 152 | unsigned long reg = readl_relaxed(priv->sensor); |
| 153 | |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 154 | return (reg >> priv->data->is_valid_shift) & THERMAL_VALID_MASK; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static int armada_get_temp(struct thermal_zone_device *thermal, |
| 158 | unsigned long *temp) |
| 159 | { |
| 160 | struct armada_thermal_priv *priv = thermal->devdata; |
| 161 | unsigned long reg; |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 162 | unsigned long m, b, div; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 163 | |
| 164 | /* Valid check */ |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 165 | if (priv->data->is_valid && !priv->data->is_valid(priv)) { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 166 | dev_err(&thermal->device, |
| 167 | "Temperature sensor reading not valid\n"); |
| 168 | return -EIO; |
| 169 | } |
| 170 | |
| 171 | reg = readl_relaxed(priv->sensor); |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 172 | reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask; |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 173 | |
| 174 | /* Get formula coeficients */ |
| 175 | b = priv->data->coef_b; |
| 176 | m = priv->data->coef_m; |
| 177 | div = priv->data->coef_div; |
| 178 | |
Ezequiel Garcia | fd2c94d | 2014-05-06 13:59:49 -0300 | [diff] [blame] | 179 | if (priv->data->inverted) |
| 180 | *temp = ((m * reg) - b) / div; |
| 181 | else |
| 182 | *temp = (b - (m * reg)) / div; |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static struct thermal_zone_device_ops ops = { |
| 187 | .get_temp = armada_get_temp, |
| 188 | }; |
| 189 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 190 | static const struct armada_thermal_data armadaxp_data = { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 191 | .init_sensor = armadaxp_init_sensor, |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 192 | .temp_shift = 10, |
| 193 | .temp_mask = 0x1ff, |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 194 | .coef_b = 3153000000UL, |
| 195 | .coef_m = 10000000UL, |
| 196 | .coef_div = 13825, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 199 | static const struct armada_thermal_data armada370_data = { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 200 | .is_valid = armada_is_valid, |
| 201 | .init_sensor = armada370_init_sensor, |
Ezequiel Garcia | 1fcacca | 2014-05-06 13:59:47 -0300 | [diff] [blame] | 202 | .is_valid_shift = 9, |
| 203 | .temp_shift = 10, |
| 204 | .temp_mask = 0x1ff, |
Ezequiel Garcia | 9484bc6 | 2014-05-06 13:59:46 -0300 | [diff] [blame] | 205 | .coef_b = 3153000000UL, |
| 206 | .coef_m = 10000000UL, |
| 207 | .coef_div = 13825, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 210 | static const struct armada_thermal_data armada375_data = { |
| 211 | .is_valid = armada_is_valid, |
| 212 | .init_sensor = armada375_init_sensor, |
| 213 | .is_valid_shift = 10, |
| 214 | .temp_shift = 0, |
| 215 | .temp_mask = 0x1ff, |
| 216 | .coef_b = 3171900000UL, |
| 217 | .coef_m = 10000000UL, |
| 218 | .coef_div = 13616, |
| 219 | }; |
| 220 | |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 221 | static const struct armada_thermal_data armada380_data = { |
| 222 | .is_valid = armada_is_valid, |
| 223 | .init_sensor = armada380_init_sensor, |
| 224 | .is_valid_shift = 10, |
| 225 | .temp_shift = 0, |
| 226 | .temp_mask = 0x3ff, |
Nadav Haklai | efa8685 | 2015-04-15 19:08:08 +0200 | [diff] [blame] | 227 | .coef_b = 2931108200UL, |
| 228 | .coef_m = 5000000UL, |
| 229 | .coef_div = 10502, |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 230 | .inverted = true, |
| 231 | }; |
| 232 | |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 233 | static const struct of_device_id armada_thermal_id_table[] = { |
| 234 | { |
| 235 | .compatible = "marvell,armadaxp-thermal", |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 236 | .data = &armadaxp_data, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 237 | }, |
| 238 | { |
| 239 | .compatible = "marvell,armada370-thermal", |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 240 | .data = &armada370_data, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 241 | }, |
| 242 | { |
Ezequiel Garcia | e2d5f05 | 2014-05-06 13:59:50 -0300 | [diff] [blame] | 243 | .compatible = "marvell,armada375-thermal", |
| 244 | .data = &armada375_data, |
| 245 | }, |
| 246 | { |
Ezequiel Garcia | e6e0a68 | 2014-05-06 13:59:51 -0300 | [diff] [blame] | 247 | .compatible = "marvell,armada380-thermal", |
| 248 | .data = &armada380_data, |
| 249 | }, |
| 250 | { |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 251 | /* sentinel */ |
| 252 | }, |
| 253 | }; |
| 254 | MODULE_DEVICE_TABLE(of, armada_thermal_id_table); |
| 255 | |
| 256 | static int armada_thermal_probe(struct platform_device *pdev) |
| 257 | { |
| 258 | struct thermal_zone_device *thermal; |
| 259 | const struct of_device_id *match; |
| 260 | struct armada_thermal_priv *priv; |
| 261 | struct resource *res; |
| 262 | |
| 263 | match = of_match_device(armada_thermal_id_table, &pdev->dev); |
| 264 | if (!match) |
| 265 | return -ENODEV; |
| 266 | |
| 267 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 268 | if (!priv) |
| 269 | return -ENOMEM; |
| 270 | |
| 271 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 272 | priv->sensor = devm_ioremap_resource(&pdev->dev, res); |
| 273 | if (IS_ERR(priv->sensor)) |
| 274 | return PTR_ERR(priv->sensor); |
| 275 | |
| 276 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 277 | priv->control = devm_ioremap_resource(&pdev->dev, res); |
| 278 | if (IS_ERR(priv->control)) |
| 279 | return PTR_ERR(priv->control); |
| 280 | |
Ezequiel Garcia | 66fdb7b | 2014-05-06 13:59:45 -0300 | [diff] [blame] | 281 | priv->data = (struct armada_thermal_data *)match->data; |
Ezequiel Garcia | 04bf3d7 | 2014-05-06 13:59:48 -0300 | [diff] [blame] | 282 | priv->data->init_sensor(pdev, priv); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 283 | |
| 284 | thermal = thermal_zone_device_register("armada_thermal", 0, 0, |
| 285 | priv, &ops, NULL, 0, 0); |
| 286 | if (IS_ERR(thermal)) { |
| 287 | dev_err(&pdev->dev, |
| 288 | "Failed to register thermal zone device\n"); |
| 289 | return PTR_ERR(thermal); |
| 290 | } |
| 291 | |
| 292 | platform_set_drvdata(pdev, thermal); |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int armada_thermal_exit(struct platform_device *pdev) |
| 298 | { |
| 299 | struct thermal_zone_device *armada_thermal = |
| 300 | platform_get_drvdata(pdev); |
| 301 | |
| 302 | thermal_zone_device_unregister(armada_thermal); |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static struct platform_driver armada_thermal_driver = { |
| 308 | .probe = armada_thermal_probe, |
| 309 | .remove = armada_thermal_exit, |
| 310 | .driver = { |
| 311 | .name = "armada_thermal", |
Sachin Kamat | 1d089e0 | 2013-05-16 10:28:08 +0000 | [diff] [blame] | 312 | .of_match_table = armada_thermal_id_table, |
Ezequiel Garcia | fa0d654 | 2013-04-02 01:37:41 +0000 | [diff] [blame] | 313 | }, |
| 314 | }; |
| 315 | |
| 316 | module_platform_driver(armada_thermal_driver); |
| 317 | |
| 318 | MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>"); |
| 319 | MODULE_DESCRIPTION("Armada 370/XP thermal driver"); |
| 320 | MODULE_LICENSE("GPL v2"); |