This source file includes following definitions.
- avs_tmon_code_to_temp
- avs_tmon_temp_to_code
- brcmstb_get_temp
- avs_tmon_trip_enable
- avs_tmon_get_trip_temp
- avs_tmon_set_trip_temp
- avs_tmon_get_intr_temp
- brcmstb_tmon_irq_thread
- brcmstb_set_trips
- brcmstb_thermal_probe
1
2
3
4
5
6
7
8 #define DRV_NAME "brcmstb_thermal"
9
10 #define pr_fmt(fmt) DRV_NAME ": " fmt
11
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/io.h>
16 #include <linux/irqreturn.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/of_device.h>
22 #include <linux/thermal.h>
23
24 #define AVS_TMON_STATUS 0x00
25 #define AVS_TMON_STATUS_valid_msk BIT(11)
26 #define AVS_TMON_STATUS_data_msk GENMASK(10, 1)
27 #define AVS_TMON_STATUS_data_shift 1
28
29 #define AVS_TMON_EN_OVERTEMP_RESET 0x04
30 #define AVS_TMON_EN_OVERTEMP_RESET_msk BIT(0)
31
32 #define AVS_TMON_RESET_THRESH 0x08
33 #define AVS_TMON_RESET_THRESH_msk GENMASK(10, 1)
34 #define AVS_TMON_RESET_THRESH_shift 1
35
36 #define AVS_TMON_INT_IDLE_TIME 0x10
37
38 #define AVS_TMON_EN_TEMP_INT_SRCS 0x14
39 #define AVS_TMON_EN_TEMP_INT_SRCS_high BIT(1)
40 #define AVS_TMON_EN_TEMP_INT_SRCS_low BIT(0)
41
42 #define AVS_TMON_INT_THRESH 0x18
43 #define AVS_TMON_INT_THRESH_high_msk GENMASK(26, 17)
44 #define AVS_TMON_INT_THRESH_high_shift 17
45 #define AVS_TMON_INT_THRESH_low_msk GENMASK(10, 1)
46 #define AVS_TMON_INT_THRESH_low_shift 1
47
48 #define AVS_TMON_TEMP_INT_CODE 0x1c
49 #define AVS_TMON_TP_TEST_ENABLE 0x20
50
51
52 #define AVS_TMON_TEMP_SLOPE 487
53 #define AVS_TMON_TEMP_OFFSET 410040
54
55
56 #define AVS_TMON_TEMP_MAX 0x3ff
57 #define AVS_TMON_TEMP_MIN -88161
58 #define AVS_TMON_TEMP_MASK AVS_TMON_TEMP_MAX
59
60 enum avs_tmon_trip_type {
61 TMON_TRIP_TYPE_LOW = 0,
62 TMON_TRIP_TYPE_HIGH,
63 TMON_TRIP_TYPE_RESET,
64 TMON_TRIP_TYPE_MAX,
65 };
66
67 struct avs_tmon_trip {
68
69 u32 enable_offs;
70 u32 enable_mask;
71
72
73 u32 reg_offs;
74 u32 reg_msk;
75 int reg_shift;
76 };
77
78 static struct avs_tmon_trip avs_tmon_trips[] = {
79
80 [TMON_TRIP_TYPE_LOW] = {
81 .enable_offs = AVS_TMON_EN_TEMP_INT_SRCS,
82 .enable_mask = AVS_TMON_EN_TEMP_INT_SRCS_low,
83 .reg_offs = AVS_TMON_INT_THRESH,
84 .reg_msk = AVS_TMON_INT_THRESH_low_msk,
85 .reg_shift = AVS_TMON_INT_THRESH_low_shift,
86 },
87
88 [TMON_TRIP_TYPE_HIGH] = {
89 .enable_offs = AVS_TMON_EN_TEMP_INT_SRCS,
90 .enable_mask = AVS_TMON_EN_TEMP_INT_SRCS_high,
91 .reg_offs = AVS_TMON_INT_THRESH,
92 .reg_msk = AVS_TMON_INT_THRESH_high_msk,
93 .reg_shift = AVS_TMON_INT_THRESH_high_shift,
94 },
95
96 [TMON_TRIP_TYPE_RESET] = {
97 .enable_offs = AVS_TMON_EN_OVERTEMP_RESET,
98 .enable_mask = AVS_TMON_EN_OVERTEMP_RESET_msk,
99 .reg_offs = AVS_TMON_RESET_THRESH,
100 .reg_msk = AVS_TMON_RESET_THRESH_msk,
101 .reg_shift = AVS_TMON_RESET_THRESH_shift,
102 },
103 };
104
105 struct brcmstb_thermal_priv {
106 void __iomem *tmon_base;
107 struct device *dev;
108 struct thermal_zone_device *thermal;
109 };
110
111
112 static inline int avs_tmon_code_to_temp(struct thermal_zone_device *tz,
113 u32 code)
114 {
115 return (AVS_TMON_TEMP_OFFSET -
116 (int)((code & AVS_TMON_TEMP_MAX) * AVS_TMON_TEMP_SLOPE));
117 }
118
119
120
121
122
123
124
125 static inline u32 avs_tmon_temp_to_code(struct thermal_zone_device *tz,
126 int temp, bool low)
127 {
128 if (temp < AVS_TMON_TEMP_MIN)
129 return AVS_TMON_TEMP_MAX;
130
131 if (temp >= AVS_TMON_TEMP_OFFSET)
132 return 0;
133
134 if (low)
135 return (u32)(DIV_ROUND_UP(AVS_TMON_TEMP_OFFSET - temp,
136 AVS_TMON_TEMP_SLOPE));
137 else
138 return (u32)((AVS_TMON_TEMP_OFFSET - temp) /
139 AVS_TMON_TEMP_SLOPE);
140 }
141
142 static int brcmstb_get_temp(void *data, int *temp)
143 {
144 struct brcmstb_thermal_priv *priv = data;
145 u32 val;
146 long t;
147
148 val = __raw_readl(priv->tmon_base + AVS_TMON_STATUS);
149
150 if (!(val & AVS_TMON_STATUS_valid_msk)) {
151 dev_err(priv->dev, "reading not valid\n");
152 return -EIO;
153 }
154
155 val = (val & AVS_TMON_STATUS_data_msk) >> AVS_TMON_STATUS_data_shift;
156
157 t = avs_tmon_code_to_temp(priv->thermal, val);
158 if (t < 0)
159 *temp = 0;
160 else
161 *temp = t;
162
163 return 0;
164 }
165
166 static void avs_tmon_trip_enable(struct brcmstb_thermal_priv *priv,
167 enum avs_tmon_trip_type type, int en)
168 {
169 struct avs_tmon_trip *trip = &avs_tmon_trips[type];
170 u32 val = __raw_readl(priv->tmon_base + trip->enable_offs);
171
172 dev_dbg(priv->dev, "%sable trip, type %d\n", en ? "en" : "dis", type);
173
174 if (en)
175 val |= trip->enable_mask;
176 else
177 val &= ~trip->enable_mask;
178
179 __raw_writel(val, priv->tmon_base + trip->enable_offs);
180 }
181
182 static int avs_tmon_get_trip_temp(struct brcmstb_thermal_priv *priv,
183 enum avs_tmon_trip_type type)
184 {
185 struct avs_tmon_trip *trip = &avs_tmon_trips[type];
186 u32 val = __raw_readl(priv->tmon_base + trip->reg_offs);
187
188 val &= trip->reg_msk;
189 val >>= trip->reg_shift;
190
191 return avs_tmon_code_to_temp(priv->thermal, val);
192 }
193
194 static void avs_tmon_set_trip_temp(struct brcmstb_thermal_priv *priv,
195 enum avs_tmon_trip_type type,
196 int temp)
197 {
198 struct avs_tmon_trip *trip = &avs_tmon_trips[type];
199 u32 val, orig;
200
201 dev_dbg(priv->dev, "set temp %d to %d\n", type, temp);
202
203
204 val = avs_tmon_temp_to_code(priv->thermal, temp,
205 type == TMON_TRIP_TYPE_LOW);
206
207 val <<= trip->reg_shift;
208 val &= trip->reg_msk;
209
210 orig = __raw_readl(priv->tmon_base + trip->reg_offs);
211 orig &= ~trip->reg_msk;
212 orig |= val;
213 __raw_writel(orig, priv->tmon_base + trip->reg_offs);
214 }
215
216 static int avs_tmon_get_intr_temp(struct brcmstb_thermal_priv *priv)
217 {
218 u32 val;
219
220 val = __raw_readl(priv->tmon_base + AVS_TMON_TEMP_INT_CODE);
221 return avs_tmon_code_to_temp(priv->thermal, val);
222 }
223
224 static irqreturn_t brcmstb_tmon_irq_thread(int irq, void *data)
225 {
226 struct brcmstb_thermal_priv *priv = data;
227 int low, high, intr;
228
229 low = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_LOW);
230 high = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_HIGH);
231 intr = avs_tmon_get_intr_temp(priv);
232
233 dev_dbg(priv->dev, "low/intr/high: %d/%d/%d\n",
234 low, intr, high);
235
236
237 if (intr >= high)
238 avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
239
240 if (intr <= low)
241 avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
242
243
244
245
246
247 thermal_zone_device_update(priv->thermal, intr);
248
249 return IRQ_HANDLED;
250 }
251
252 static int brcmstb_set_trips(void *data, int low, int high)
253 {
254 struct brcmstb_thermal_priv *priv = data;
255
256 dev_dbg(priv->dev, "set trips %d <--> %d\n", low, high);
257
258
259
260
261
262 if (low <= -INT_MAX) {
263 avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
264 } else {
265 avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_LOW, low);
266 avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 1);
267 }
268
269
270 if (high == INT_MAX) {
271 avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
272 } else {
273 avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_HIGH, high);
274 avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 1);
275 }
276
277 return 0;
278 }
279
280 static const struct thermal_zone_of_device_ops of_ops = {
281 .get_temp = brcmstb_get_temp,
282 .set_trips = brcmstb_set_trips,
283 };
284
285 static const struct of_device_id brcmstb_thermal_id_table[] = {
286 { .compatible = "brcm,avs-tmon" },
287 {},
288 };
289 MODULE_DEVICE_TABLE(of, brcmstb_thermal_id_table);
290
291 static int brcmstb_thermal_probe(struct platform_device *pdev)
292 {
293 struct thermal_zone_device *thermal;
294 struct brcmstb_thermal_priv *priv;
295 struct resource *res;
296 int irq, ret;
297
298 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
299 if (!priv)
300 return -ENOMEM;
301
302 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
303 priv->tmon_base = devm_ioremap_resource(&pdev->dev, res);
304 if (IS_ERR(priv->tmon_base))
305 return PTR_ERR(priv->tmon_base);
306
307 priv->dev = &pdev->dev;
308 platform_set_drvdata(pdev, priv);
309
310 thermal = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, priv,
311 &of_ops);
312 if (IS_ERR(thermal)) {
313 ret = PTR_ERR(thermal);
314 dev_err(&pdev->dev, "could not register sensor: %d\n", ret);
315 return ret;
316 }
317
318 priv->thermal = thermal;
319
320 irq = platform_get_irq(pdev, 0);
321 if (irq < 0) {
322 dev_err(&pdev->dev, "could not get IRQ\n");
323 return irq;
324 }
325 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
326 brcmstb_tmon_irq_thread, IRQF_ONESHOT,
327 DRV_NAME, priv);
328 if (ret < 0) {
329 dev_err(&pdev->dev, "could not request IRQ: %d\n", ret);
330 return ret;
331 }
332
333 dev_info(&pdev->dev, "registered AVS TMON of-sensor driver\n");
334
335 return 0;
336 }
337
338 static struct platform_driver brcmstb_thermal_driver = {
339 .probe = brcmstb_thermal_probe,
340 .driver = {
341 .name = DRV_NAME,
342 .of_match_table = brcmstb_thermal_id_table,
343 },
344 };
345 module_platform_driver(brcmstb_thermal_driver);
346
347 MODULE_LICENSE("GPL v2");
348 MODULE_AUTHOR("Brian Norris");
349 MODULE_DESCRIPTION("Broadcom STB AVS TMON thermal driver");