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