1 From 325fadf27b21f7d79843c3cc282b7f3e6620ad3d Mon Sep 17 00:00:00 2001
2 From: Balsam CHIHI <bchihi@baylibre.com>
3 Date: Thu, 9 Feb 2023 11:56:26 +0100
4 Subject: [PATCH 06/42] thermal/drivers/mediatek: Add the Low Voltage Thermal
7 The Low Voltage Thermal Sensor (LVTS) is a multiple sensors, multi
8 controllers contained in a thermal domain.
10 A thermal domains can be the MCU or the AP.
12 Each thermal domains contain up to seven controllers, each thermal
13 controller handle up to four thermal sensors.
15 The LVTS has two Finite State Machines (FSM), one to handle the
16 functionin temperatures range like hot or cold temperature and another
17 one to handle monitoring trip point. The FSM notifies via interrupts
18 when a trip point is crossed.
20 The interrupt is managed at the thermal controller level, so when an
21 interrupt occurs, the driver has to find out which sensor triggered
24 The sampling of the thermal can be filtered or immediate. For the
25 former, the LVTS measures several points and applies a low pass
28 Signed-off-by: Balsam CHIHI <bchihi@baylibre.com>
29 Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
31 On MT8195 Tomato Chromebook:
33 Tested-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
34 Link: https://lore.kernel.org/r/20230209105628.50294-5-bchihi@baylibre.com
35 Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
36 Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
38 drivers/thermal/mediatek/Kconfig | 16 +
39 drivers/thermal/mediatek/Makefile | 1 +
40 drivers/thermal/mediatek/lvts_thermal.c | 1224 +++++++++++++++++++++++
41 3 files changed, 1241 insertions(+)
42 create mode 100644 drivers/thermal/mediatek/lvts_thermal.c
44 --- a/drivers/thermal/mediatek/Kconfig
45 +++ b/drivers/thermal/mediatek/Kconfig
46 @@ -18,4 +18,20 @@ config MTK_SOC_THERMAL
47 This driver configures thermal controllers to collect
48 temperature via AUXADC interface.
50 +config MTK_LVTS_THERMAL
51 + tristate "LVTS Thermal Driver for MediaTek SoCs"
52 + depends on HAS_IOMEM
54 + Enable this option if you want to get SoC temperature
55 + information for supported MediaTek platforms.
56 + This driver configures LVTS (Low Voltage Thermal Sensor)
57 + thermal controllers to collect temperatures via ASIF
58 + (Analog Serial Interface).
60 +config MTK_LVTS_THERMAL_DEBUGFS
61 + bool "LVTS thermal debugfs"
62 + depends on MTK_LVTS_THERMAL && DEBUG_FS
64 + Enable this option to debug the internals of the device driver.
67 --- a/drivers/thermal/mediatek/Makefile
68 +++ b/drivers/thermal/mediatek/Makefile
70 obj-$(CONFIG_MTK_SOC_THERMAL) += auxadc_thermal.o
71 +obj-$(CONFIG_MTK_LVTS_THERMAL) += lvts_thermal.o
73 +++ b/drivers/thermal/mediatek/lvts_thermal.c
75 +// SPDX-License-Identifier: GPL-2.0-only
77 + * Copyright (c) 2023 MediaTek Inc.
78 + * Author: Balsam CHIHI <bchihi@baylibre.com>
81 +#include <linux/clk.h>
82 +#include <linux/clk-provider.h>
83 +#include <linux/delay.h>
84 +#include <linux/debugfs.h>
85 +#include <linux/init.h>
86 +#include <linux/interrupt.h>
87 +#include <linux/iopoll.h>
88 +#include <linux/kernel.h>
89 +#include <linux/nvmem-consumer.h>
90 +#include <linux/of_device.h>
91 +#include <linux/platform_device.h>
92 +#include <linux/reset.h>
93 +#include <linux/thermal.h>
94 +#include <dt-bindings/thermal/mediatek,lvts-thermal.h>
96 +#define LVTS_MONCTL0(__base) (__base + 0x0000)
97 +#define LVTS_MONCTL1(__base) (__base + 0x0004)
98 +#define LVTS_MONCTL2(__base) (__base + 0x0008)
99 +#define LVTS_MONINT(__base) (__base + 0x000C)
100 +#define LVTS_MONINTSTS(__base) (__base + 0x0010)
101 +#define LVTS_MONIDET0(__base) (__base + 0x0014)
102 +#define LVTS_MONIDET1(__base) (__base + 0x0018)
103 +#define LVTS_MONIDET2(__base) (__base + 0x001C)
104 +#define LVTS_MONIDET3(__base) (__base + 0x0020)
105 +#define LVTS_H2NTHRE(__base) (__base + 0x0024)
106 +#define LVTS_HTHRE(__base) (__base + 0x0028)
107 +#define LVTS_OFFSETH(__base) (__base + 0x0030)
108 +#define LVTS_OFFSETL(__base) (__base + 0x0034)
109 +#define LVTS_MSRCTL0(__base) (__base + 0x0038)
110 +#define LVTS_MSRCTL1(__base) (__base + 0x003C)
111 +#define LVTS_TSSEL(__base) (__base + 0x0040)
112 +#define LVTS_CALSCALE(__base) (__base + 0x0048)
113 +#define LVTS_ID(__base) (__base + 0x004C)
114 +#define LVTS_CONFIG(__base) (__base + 0x0050)
115 +#define LVTS_EDATA00(__base) (__base + 0x0054)
116 +#define LVTS_EDATA01(__base) (__base + 0x0058)
117 +#define LVTS_EDATA02(__base) (__base + 0x005C)
118 +#define LVTS_EDATA03(__base) (__base + 0x0060)
119 +#define LVTS_MSR0(__base) (__base + 0x0090)
120 +#define LVTS_MSR1(__base) (__base + 0x0094)
121 +#define LVTS_MSR2(__base) (__base + 0x0098)
122 +#define LVTS_MSR3(__base) (__base + 0x009C)
123 +#define LVTS_IMMD0(__base) (__base + 0x00A0)
124 +#define LVTS_IMMD1(__base) (__base + 0x00A4)
125 +#define LVTS_IMMD2(__base) (__base + 0x00A8)
126 +#define LVTS_IMMD3(__base) (__base + 0x00AC)
127 +#define LVTS_PROTCTL(__base) (__base + 0x00C0)
128 +#define LVTS_PROTTA(__base) (__base + 0x00C4)
129 +#define LVTS_PROTTB(__base) (__base + 0x00C8)
130 +#define LVTS_PROTTC(__base) (__base + 0x00CC)
131 +#define LVTS_CLKEN(__base) (__base + 0x00E4)
133 +#define LVTS_PERIOD_UNIT ((118 * 1000) / (256 * 38))
134 +#define LVTS_GROUP_INTERVAL 1
135 +#define LVTS_FILTER_INTERVAL 1
136 +#define LVTS_SENSOR_INTERVAL 1
137 +#define LVTS_HW_FILTER 0x2
138 +#define LVTS_TSSEL_CONF 0x13121110
139 +#define LVTS_CALSCALE_CONF 0x300
140 +#define LVTS_MONINT_CONF 0x9FBF7BDE
142 +#define LVTS_INT_SENSOR0 0x0009001F
143 +#define LVTS_INT_SENSOR1 0X000881F0
144 +#define LVTS_INT_SENSOR2 0x00247C00
145 +#define LVTS_INT_SENSOR3 0x1FC00000
147 +#define LVTS_SENSOR_MAX 4
148 +#define LVTS_GOLDEN_TEMP_MAX 62
149 +#define LVTS_GOLDEN_TEMP_DEFAULT 50
150 +#define LVTS_COEFF_A -250460
151 +#define LVTS_COEFF_B 250460
153 +#define LVTS_MSR_IMMEDIATE_MODE 0
154 +#define LVTS_MSR_FILTERED_MODE 1
156 +#define LVTS_HW_SHUTDOWN_MT8195 105000
158 +static int golden_temp = LVTS_GOLDEN_TEMP_DEFAULT;
159 +static int coeff_b = LVTS_COEFF_B;
161 +struct lvts_sensor_data {
165 +struct lvts_ctrl_data {
166 + struct lvts_sensor_data lvts_sensor[LVTS_SENSOR_MAX];
167 + int cal_offset[LVTS_SENSOR_MAX];
169 + int num_lvts_sensor;
175 + const struct lvts_ctrl_data *lvts_ctrl;
179 +struct lvts_sensor {
180 + struct thermal_zone_device *tz;
182 + void __iomem *base;
188 + struct lvts_sensor sensors[LVTS_SENSOR_MAX];
189 + u32 calibration[LVTS_SENSOR_MAX];
190 + u32 hw_tshut_raw_temp;
191 + int num_lvts_sensor;
193 + void __iomem *base;
196 +struct lvts_domain {
197 + struct lvts_ctrl *lvts_ctrl;
198 + struct reset_control *reset;
201 + void __iomem *base;
204 +#ifdef CONFIG_DEBUG_FS
205 + struct dentry *dom_dentry;
209 +#ifdef CONFIG_MTK_LVTS_THERMAL_DEBUGFS
211 +#define LVTS_DEBUG_FS_REGS(__reg) \
213 + .name = __stringify(__reg), \
214 + .offset = __reg(0), \
217 +static const struct debugfs_reg32 lvts_regs[] = {
218 + LVTS_DEBUG_FS_REGS(LVTS_MONCTL0),
219 + LVTS_DEBUG_FS_REGS(LVTS_MONCTL1),
220 + LVTS_DEBUG_FS_REGS(LVTS_MONCTL2),
221 + LVTS_DEBUG_FS_REGS(LVTS_MONINT),
222 + LVTS_DEBUG_FS_REGS(LVTS_MONINTSTS),
223 + LVTS_DEBUG_FS_REGS(LVTS_MONIDET0),
224 + LVTS_DEBUG_FS_REGS(LVTS_MONIDET1),
225 + LVTS_DEBUG_FS_REGS(LVTS_MONIDET2),
226 + LVTS_DEBUG_FS_REGS(LVTS_MONIDET3),
227 + LVTS_DEBUG_FS_REGS(LVTS_H2NTHRE),
228 + LVTS_DEBUG_FS_REGS(LVTS_HTHRE),
229 + LVTS_DEBUG_FS_REGS(LVTS_OFFSETH),
230 + LVTS_DEBUG_FS_REGS(LVTS_OFFSETL),
231 + LVTS_DEBUG_FS_REGS(LVTS_MSRCTL0),
232 + LVTS_DEBUG_FS_REGS(LVTS_MSRCTL1),
233 + LVTS_DEBUG_FS_REGS(LVTS_TSSEL),
234 + LVTS_DEBUG_FS_REGS(LVTS_CALSCALE),
235 + LVTS_DEBUG_FS_REGS(LVTS_ID),
236 + LVTS_DEBUG_FS_REGS(LVTS_CONFIG),
237 + LVTS_DEBUG_FS_REGS(LVTS_EDATA00),
238 + LVTS_DEBUG_FS_REGS(LVTS_EDATA01),
239 + LVTS_DEBUG_FS_REGS(LVTS_EDATA02),
240 + LVTS_DEBUG_FS_REGS(LVTS_EDATA03),
241 + LVTS_DEBUG_FS_REGS(LVTS_MSR0),
242 + LVTS_DEBUG_FS_REGS(LVTS_MSR1),
243 + LVTS_DEBUG_FS_REGS(LVTS_MSR2),
244 + LVTS_DEBUG_FS_REGS(LVTS_MSR3),
245 + LVTS_DEBUG_FS_REGS(LVTS_IMMD0),
246 + LVTS_DEBUG_FS_REGS(LVTS_IMMD1),
247 + LVTS_DEBUG_FS_REGS(LVTS_IMMD2),
248 + LVTS_DEBUG_FS_REGS(LVTS_IMMD3),
249 + LVTS_DEBUG_FS_REGS(LVTS_PROTCTL),
250 + LVTS_DEBUG_FS_REGS(LVTS_PROTTA),
251 + LVTS_DEBUG_FS_REGS(LVTS_PROTTB),
252 + LVTS_DEBUG_FS_REGS(LVTS_PROTTC),
253 + LVTS_DEBUG_FS_REGS(LVTS_CLKEN),
256 +static int lvts_debugfs_init(struct device *dev, struct lvts_domain *lvts_td)
258 + struct debugfs_regset32 *regset;
259 + struct lvts_ctrl *lvts_ctrl;
260 + struct dentry *dentry;
264 + lvts_td->dom_dentry = debugfs_create_dir(dev_name(dev), NULL);
265 + if (!lvts_td->dom_dentry)
268 + for (i = 0; i < lvts_td->num_lvts_ctrl; i++) {
270 + lvts_ctrl = &lvts_td->lvts_ctrl[i];
272 + sprintf(name, "controller%d", i);
273 + dentry = debugfs_create_dir(name, lvts_td->dom_dentry);
277 + regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
281 + regset->base = lvts_ctrl->base;
282 + regset->regs = lvts_regs;
283 + regset->nregs = ARRAY_SIZE(lvts_regs);
285 + debugfs_create_regset32("registers", 0400, dentry, regset);
291 +static void lvts_debugfs_exit(struct lvts_domain *lvts_td)
293 + debugfs_remove_recursive(lvts_td->dom_dentry);
298 +static inline int lvts_debugfs_init(struct device *dev,
299 + struct lvts_domain *lvts_td)
304 +static void lvts_debugfs_exit(struct lvts_domain *lvts_td) { }
308 +static int lvts_raw_to_temp(u32 raw_temp)
312 + temperature = ((s64)(raw_temp & 0xFFFF) * LVTS_COEFF_A) >> 14;
313 + temperature += coeff_b;
315 + return temperature;
318 +static u32 lvts_temp_to_raw(int temperature)
320 + u32 raw_temp = ((s64)(coeff_b - temperature)) << 14;
322 + raw_temp = div_s64(raw_temp, -LVTS_COEFF_A);
327 +static int lvts_get_temp(struct thermal_zone_device *tz, int *temp)
329 + struct lvts_sensor *lvts_sensor = tz->devdata;
330 + void __iomem *msr = lvts_sensor->msr;
334 + * Measurement registers:
336 + * LVTS_MSR[0-3] / LVTS_IMMD[0-3]
341 + * 16 : Valid temperature
342 + * 15-0 : Raw temperature
344 + value = readl(msr);
347 + * As the thermal zone temperature will read before the
348 + * hardware sensor is fully initialized, we have to check the
349 + * validity of the temperature returned when reading the
350 + * measurement register. The thermal controller will set the
351 + * valid bit temperature only when it is totally initialized.
353 + * Otherwise, we may end up with garbage values out of the
354 + * functionning temperature and directly jump to a system
357 + if (!(value & BIT(16)))
360 + *temp = lvts_raw_to_temp(value & 0xFFFF);
365 +static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
367 + struct lvts_sensor *lvts_sensor = tz->devdata;
368 + void __iomem *base = lvts_sensor->base;
369 + u32 raw_low = lvts_temp_to_raw(low);
370 + u32 raw_high = lvts_temp_to_raw(high);
373 + * Hot to normal temperature threshold
379 + * 14-0 : Raw temperature for threshold
381 + if (low != -INT_MAX) {
382 + dev_dbg(&tz->device, "Setting low limit temperature interrupt: %d\n", low);
383 + writel(raw_low, LVTS_H2NTHRE(base));
387 + * Hot temperature threshold
393 + * 14-0 : Raw temperature for threshold
395 + dev_dbg(&tz->device, "Setting high limit temperature interrupt: %d\n", high);
396 + writel(raw_high, LVTS_HTHRE(base));
401 +static irqreturn_t lvts_ctrl_irq_handler(struct lvts_ctrl *lvts_ctrl)
403 + irqreturn_t iret = IRQ_NONE;
414 + * Interrupt monitoring status
420 + * 31 : Interrupt for stage 3
421 + * 30 : Interrupt for stage 2
422 + * 29 : Interrupt for state 1
423 + * 28 : Interrupt using filter on sensor 3
425 + * 27 : Interrupt using immediate on sensor 3
426 + * 26 : Interrupt normal to hot on sensor 3
427 + * 25 : Interrupt high offset on sensor 3
428 + * 24 : Interrupt low offset on sensor 3
430 + * 23 : Interrupt hot threshold on sensor 3
431 + * 22 : Interrupt cold threshold on sensor 3
432 + * 21 : Interrupt using filter on sensor 2
433 + * 20 : Interrupt using filter on sensor 1
435 + * 19 : Interrupt using filter on sensor 0
436 + * 18 : Interrupt using immediate on sensor 2
437 + * 17 : Interrupt using immediate on sensor 1
438 + * 16 : Interrupt using immediate on sensor 0
440 + * 15 : Interrupt device access timeout interrupt
441 + * 14 : Interrupt normal to hot on sensor 2
442 + * 13 : Interrupt high offset interrupt on sensor 2
443 + * 12 : Interrupt low offset interrupt on sensor 2
445 + * 11 : Interrupt hot threshold on sensor 2
446 + * 10 : Interrupt cold threshold on sensor 2
447 + * 9 : Interrupt normal to hot on sensor 1
448 + * 8 : Interrupt high offset interrupt on sensor 1
450 + * 7 : Interrupt low offset interrupt on sensor 1
451 + * 6 : Interrupt hot threshold on sensor 1
452 + * 5 : Interrupt cold threshold on sensor 1
453 + * 4 : Interrupt normal to hot on sensor 0
455 + * 3 : Interrupt high offset interrupt on sensor 0
456 + * 2 : Interrupt low offset interrupt on sensor 0
457 + * 1 : Interrupt hot threshold on sensor 0
458 + * 0 : Interrupt cold threshold on sensor 0
460 + * We are interested in the sensor(s) responsible of the
461 + * interrupt event. We update the thermal framework with the
462 + * thermal zone associated with the sensor. The framework will
463 + * take care of the rest whatever the kind of interrupt, we
464 + * are only interested in which sensor raised the interrupt.
466 + * sensor 3 interrupt: 0001 1111 1100 0000 0000 0000 0000 0000
468 + * sensor 2 interrupt: 0000 0000 0010 0100 0111 1100 0000 0000
470 + * sensor 1 interrupt: 0000 0000 0001 0001 0000 0011 1110 0000
472 + * sensor 0 interrupt: 0000 0000 0000 1001 0000 0000 0001 1111
475 + value = readl(LVTS_MONINTSTS(lvts_ctrl->base));
478 + * Let's figure out which sensors raised the interrupt
480 + * NOTE: the masks array must be ordered with the index
481 + * corresponding to the sensor id eg. index=0, mask for
484 + for (i = 0; i < ARRAY_SIZE(masks); i++) {
486 + if (!(value & masks[i]))
489 + thermal_zone_device_update(lvts_ctrl->sensors[i].tz,
490 + THERMAL_TRIP_VIOLATED);
491 + iret = IRQ_HANDLED;
495 + * Write back to clear the interrupt status (W1C)
497 + writel(value, LVTS_MONINTSTS(lvts_ctrl->base));
503 + * Temperature interrupt handler. Even if the driver supports more
504 + * interrupt modes, we use the interrupt when the temperature crosses
505 + * the hot threshold the way up and the way down (modulo the
508 + * Each thermal domain has a couple of interrupts, one for hardware
509 + * reset and another one for all the thermal events happening on the
510 + * different sensors.
512 + * The interrupt is configured for thermal events when crossing the
513 + * hot temperature limit. At each interrupt, we check in every
514 + * controller if there is an interrupt pending.
516 +static irqreturn_t lvts_irq_handler(int irq, void *data)
518 + struct lvts_domain *lvts_td = data;
519 + irqreturn_t aux, iret = IRQ_NONE;
522 + for (i = 0; i < lvts_td->num_lvts_ctrl; i++) {
524 + aux = lvts_ctrl_irq_handler(lvts_td->lvts_ctrl);
525 + if (aux != IRQ_HANDLED)
528 + iret = IRQ_HANDLED;
534 +static struct thermal_zone_device_ops lvts_ops = {
535 + .get_temp = lvts_get_temp,
536 + .set_trips = lvts_set_trips,
539 +static int lvts_sensor_init(struct device *dev, struct lvts_ctrl *lvts_ctrl,
540 + const struct lvts_ctrl_data *lvts_ctrl_data)
542 + struct lvts_sensor *lvts_sensor = lvts_ctrl->sensors;
543 + void __iomem *msr_regs[] = {
544 + LVTS_MSR0(lvts_ctrl->base),
545 + LVTS_MSR1(lvts_ctrl->base),
546 + LVTS_MSR2(lvts_ctrl->base),
547 + LVTS_MSR3(lvts_ctrl->base)
550 + void __iomem *imm_regs[] = {
551 + LVTS_IMMD0(lvts_ctrl->base),
552 + LVTS_IMMD1(lvts_ctrl->base),
553 + LVTS_IMMD2(lvts_ctrl->base),
554 + LVTS_IMMD3(lvts_ctrl->base)
559 + for (i = 0; i < lvts_ctrl_data->num_lvts_sensor; i++) {
561 + int dt_id = lvts_ctrl_data->lvts_sensor[i].dt_id;
564 + * At this point, we don't know which id matches which
565 + * sensor. Let's set arbitrally the id from the index.
567 + lvts_sensor[i].id = i;
570 + * The thermal zone registration will set the trip
571 + * point interrupt in the thermal controller
572 + * register. But this one will be reset in the
573 + * initialization after. So we need to post pone the
574 + * thermal zone creation after the controller is
575 + * setup. For this reason, we store the device tree
576 + * node id from the data in the sensor structure
578 + lvts_sensor[i].dt_id = dt_id;
581 + * We assign the base address of the thermal
582 + * controller as a back pointer. So it will be
583 + * accessible from the different thermal framework ops
584 + * as we pass the lvts_sensor pointer as thermal zone
587 + lvts_sensor[i].base = lvts_ctrl->base;
590 + * Each sensor has its own register address to read from.
592 + lvts_sensor[i].msr = lvts_ctrl_data->mode == LVTS_MSR_IMMEDIATE_MODE ?
593 + imm_regs[i] : msr_regs[i];
596 + lvts_ctrl->num_lvts_sensor = lvts_ctrl_data->num_lvts_sensor;
602 + * The efuse blob values follows the sensor enumeration per thermal
603 + * controller. The decoding of the stream is as follow:
605 + * <--?-> <----big0 ???---> <-sensor0-> <-0->
606 + * ------------------------------------------
607 + * index in the stream: : | 0x0 | 0x1 | 0x2 | 0x3 | 0x4 | 0x5 | 0x6 |
608 + * ------------------------------------------
610 + * <--sensor1--><-0-> <----big1 ???---> <-sen
611 + * ------------------------------------------
612 + * | 0x7 | 0x8 | 0x9 | 0xA | 0xB | OxC | OxD |
613 + * ------------------------------------------
615 + * sor0-> <-0-> <-sensor1-> <-0-> ..........
616 + * ------------------------------------------
617 + * | 0x7 | 0x8 | 0x9 | 0xA | 0xB | OxC | OxD |
618 + * ------------------------------------------
622 + * The data description gives the offset of the calibration data in
623 + * this bytes stream for each sensor.
625 + * Each thermal controller can handle up to 4 sensors max, we don't
626 + * care if there are less as the array of calibration is sized to 4
627 + * anyway. The unused sensor slot will be zeroed.
629 +static int lvts_calibration_init(struct device *dev, struct lvts_ctrl *lvts_ctrl,
630 + const struct lvts_ctrl_data *lvts_ctrl_data,
631 + u8 *efuse_calibration)
635 + for (i = 0; i < lvts_ctrl_data->num_lvts_sensor; i++)
636 + memcpy(&lvts_ctrl->calibration[i],
637 + efuse_calibration + lvts_ctrl_data->cal_offset[i], 2);
643 + * The efuse bytes stream can be split into different chunk of
644 + * nvmems. This function reads and concatenate those into a single
645 + * buffer so it can be read sequentially when initializing the
646 + * calibration data.
648 +static int lvts_calibration_read(struct device *dev, struct lvts_domain *lvts_td,
649 + const struct lvts_data *lvts_data)
651 + struct device_node *np = dev_of_node(dev);
652 + struct nvmem_cell *cell;
653 + struct property *prop;
654 + const char *cell_name;
656 + of_property_for_each_string(np, "nvmem-cell-names", prop, cell_name) {
660 + cell = of_nvmem_cell_get(np, cell_name);
661 + if (IS_ERR(cell)) {
662 + dev_err(dev, "Failed to get cell '%s'\n", cell_name);
663 + return PTR_ERR(cell);
666 + efuse = nvmem_cell_read(cell, &len);
668 + nvmem_cell_put(cell);
670 + if (IS_ERR(efuse)) {
671 + dev_err(dev, "Failed to read cell '%s'\n", cell_name);
672 + return PTR_ERR(efuse);
675 + lvts_td->calib = devm_krealloc(dev, lvts_td->calib,
676 + lvts_td->calib_len + len, GFP_KERNEL);
677 + if (!lvts_td->calib)
680 + memcpy(lvts_td->calib + lvts_td->calib_len, efuse, len);
682 + lvts_td->calib_len += len;
690 +static int lvts_golden_temp_init(struct device *dev, u32 *value)
694 + gt = (*value) >> 24;
696 + if (gt && gt < LVTS_GOLDEN_TEMP_MAX)
699 + coeff_b = golden_temp * 500 + LVTS_COEFF_B;
704 +static int lvts_ctrl_init(struct device *dev, struct lvts_domain *lvts_td,
705 + const struct lvts_data *lvts_data)
707 + size_t size = sizeof(*lvts_td->lvts_ctrl) * lvts_data->num_lvts_ctrl;
708 + struct lvts_ctrl *lvts_ctrl;
712 + * Create the calibration bytes stream from efuse data
714 + ret = lvts_calibration_read(dev, lvts_td, lvts_data);
719 + * The golden temp information is contained in the first chunk
722 + ret = lvts_golden_temp_init(dev, (u32 *)lvts_td->calib);
726 + lvts_ctrl = devm_kzalloc(dev, size, GFP_KERNEL);
730 + for (i = 0; i < lvts_data->num_lvts_ctrl; i++) {
732 + lvts_ctrl[i].base = lvts_td->base + lvts_data->lvts_ctrl[i].offset;
734 + ret = lvts_sensor_init(dev, &lvts_ctrl[i],
735 + &lvts_data->lvts_ctrl[i]);
739 + ret = lvts_calibration_init(dev, &lvts_ctrl[i],
740 + &lvts_data->lvts_ctrl[i],
746 + * The mode the ctrl will use to read the temperature
747 + * (filtered or immediate)
749 + lvts_ctrl[i].mode = lvts_data->lvts_ctrl[i].mode;
752 + * The temperature to raw temperature must be done
753 + * after initializing the calibration.
755 + lvts_ctrl[i].hw_tshut_raw_temp =
756 + lvts_temp_to_raw(lvts_data->lvts_ctrl[i].hw_tshut_temp);
760 + * We no longer need the efuse bytes stream, let's free it
762 + devm_kfree(dev, lvts_td->calib);
764 + lvts_td->lvts_ctrl = lvts_ctrl;
765 + lvts_td->num_lvts_ctrl = lvts_data->num_lvts_ctrl;
771 + * At this point the configuration register is the only place in the
772 + * driver where we write multiple values. Per hardware constraint,
773 + * each write in the configuration register must be separated by a
776 +static void lvts_write_config(struct lvts_ctrl *lvts_ctrl, u32 *cmds, int nr_cmds)
781 + * Configuration register
783 + for (i = 0; i < nr_cmds; i++) {
784 + writel(cmds[i], LVTS_CONFIG(lvts_ctrl->base));
785 + usleep_range(2, 4);
789 +static int lvts_irq_init(struct lvts_ctrl *lvts_ctrl)
792 + * LVTS_PROTCTL : Thermal Protection Sensor Selection
796 + * 19-18 : Sensor to base the protection on
797 + * 17-16 : Strategy:
798 + * 00 : Average of 4 sensors
799 + * 01 : Max of 4 sensors
800 + * 10 : Selected sensor with bits 19-18
803 + writel(BIT(16), LVTS_PROTCTL(lvts_ctrl->base));
806 + * LVTS_PROTTA : Stage 1 temperature threshold
807 + * LVTS_PROTTB : Stage 2 temperature threshold
808 + * LVTS_PROTTC : Stage 3 temperature threshold
812 + * 14-0: Raw temperature threshold
814 + * writel(0x0, LVTS_PROTTA(lvts_ctrl->base));
815 + * writel(0x0, LVTS_PROTTB(lvts_ctrl->base));
817 + writel(lvts_ctrl->hw_tshut_raw_temp, LVTS_PROTTC(lvts_ctrl->base));
820 + * LVTS_MONINT : Interrupt configuration register
822 + * The LVTS_MONINT register layout is the same as the LVTS_MONINTSTS
823 + * register, except we set the bits to enable the interrupt.
825 + writel(LVTS_MONINT_CONF, LVTS_MONINT(lvts_ctrl->base));
830 +static int lvts_domain_reset(struct device *dev, struct reset_control *reset)
834 + ret = reset_control_assert(reset);
838 + return reset_control_deassert(reset);
842 + * Enable or disable the clocks of a specified thermal controller
844 +static int lvts_ctrl_set_enable(struct lvts_ctrl *lvts_ctrl, int enable)
847 + * LVTS_CLKEN : Internal LVTS clock
851 + * 0 : enable / disable clock
853 + writel(enable, LVTS_CLKEN(lvts_ctrl->base));
858 +static int lvts_ctrl_connect(struct device *dev, struct lvts_ctrl *lvts_ctrl)
860 + u32 id, cmds[] = { 0xC103FFFF, 0xC502FF55 };
862 + lvts_write_config(lvts_ctrl, cmds, ARRAY_SIZE(cmds));
865 + * LVTS_ID : Get ID and status of the thermal controller
869 + * 0-5 : thermal controller id
870 + * 7 : thermal controller connection is valid
872 + id = readl(LVTS_ID(lvts_ctrl->base));
873 + if (!(id & BIT(7)))
879 +static int lvts_ctrl_initialize(struct device *dev, struct lvts_ctrl *lvts_ctrl)
882 + * Write device mask: 0xC1030000
885 + 0xC1030E01, 0xC1030CFC, 0xC1030A8C, 0xC103098D, 0xC10308F1,
886 + 0xC10307A6, 0xC10306B8, 0xC1030500, 0xC1030420, 0xC1030300,
887 + 0xC1030030, 0xC10300F6, 0xC1030050, 0xC1030060, 0xC10300AC,
888 + 0xC10300FC, 0xC103009D, 0xC10300F1, 0xC10300E1
891 + lvts_write_config(lvts_ctrl, cmds, ARRAY_SIZE(cmds));
896 +static int lvts_ctrl_calibrate(struct device *dev, struct lvts_ctrl *lvts_ctrl)
899 + void __iomem *lvts_edata[] = {
900 + LVTS_EDATA00(lvts_ctrl->base),
901 + LVTS_EDATA01(lvts_ctrl->base),
902 + LVTS_EDATA02(lvts_ctrl->base),
903 + LVTS_EDATA03(lvts_ctrl->base)
907 + * LVTS_EDATA0X : Efuse calibration reference value for sensor X
911 + * 20-0 : Efuse value for normalization data
913 + for (i = 0; i < LVTS_SENSOR_MAX; i++)
914 + writel(lvts_ctrl->calibration[i], lvts_edata[i]);
919 +static int lvts_ctrl_configure(struct device *dev, struct lvts_ctrl *lvts_ctrl)
924 + * LVTS_TSSEL : Sensing point index numbering
928 + * 31-24: ADC Sense 3
929 + * 23-16: ADC Sense 2
930 + * 15-8 : ADC Sense 1
931 + * 7-0 : ADC Sense 0
933 + value = LVTS_TSSEL_CONF;
934 + writel(value, LVTS_TSSEL(lvts_ctrl->base));
937 + * LVTS_CALSCALE : ADC voltage round
940 + value = LVTS_CALSCALE_CONF;
943 + * LVTS_MSRCTL0 : Sensor filtering strategy
948 + * 001 : Avg 2 samples
949 + * 010 : 4 samples, drop min and max, avg 2 samples
950 + * 011 : 6 samples, drop min and max, avg 4 samples
951 + * 100 : 10 samples, drop min and max, avg 8 samples
952 + * 101 : 18 samples, drop min and max, avg 16 samples
956 + * 0-2 : Sensor0 filter
957 + * 3-5 : Sensor1 filter
958 + * 6-8 : Sensor2 filter
959 + * 9-11 : Sensor3 filter
961 + value = LVTS_HW_FILTER << 9 | LVTS_HW_FILTER << 6 |
962 + LVTS_HW_FILTER << 3 | LVTS_HW_FILTER;
963 + writel(value, LVTS_MSRCTL0(lvts_ctrl->base));
966 + * LVTS_MSRCTL1 : Measurement control
970 + * 9: Ignore MSRCTL0 config and do immediate measurement on sensor3
971 + * 6: Ignore MSRCTL0 config and do immediate measurement on sensor2
972 + * 5: Ignore MSRCTL0 config and do immediate measurement on sensor1
973 + * 4: Ignore MSRCTL0 config and do immediate measurement on sensor0
975 + * That configuration will ignore the filtering and the delays
976 + * introduced below in MONCTL1 and MONCTL2
978 + if (lvts_ctrl->mode == LVTS_MSR_IMMEDIATE_MODE) {
979 + value = BIT(9) | BIT(6) | BIT(5) | BIT(4);
980 + writel(value, LVTS_MSRCTL1(lvts_ctrl->base));
984 + * LVTS_MONCTL1 : Period unit and group interval configuration
986 + * The clock source of LVTS thermal controller is 26MHz.
988 + * The period unit is a time base for all the interval delays
989 + * specified in the registers. By default we use 12. The time
990 + * conversion is done by multiplying by 256 and 1/26.10^6
992 + * An interval delay multiplied by the period unit gives the
993 + * duration in seconds.
995 + * - Filter interval delay is a delay between two samples of
998 + * - Sensor interval delay is a delay between two samples of
999 + * different sensors.
1001 + * - Group interval delay is a delay between different rounds.
1004 + * If Period unit = C, filter delay = 1, sensor delay = 2, group delay = 1,
1005 + * and two sensors, TS1 and TS2, are in a LVTS thermal controller
1007 + * Period unit time = C * 1/26M * 256 = 12 * 38.46ns * 256 = 118.149us
1008 + * Filter interval delay = 1 * Period unit = 118.149us
1009 + * Sensor interval delay = 2 * Period unit = 236.298us
1010 + * Group interval delay = 1 * Period unit = 118.149us
1012 + * TS1 TS1 ... TS1 TS2 TS2 ... TS2 TS1...
1013 + * <--> Filter interval delay
1014 + * <--> Sensor interval delay
1015 + * <--> Group interval delay
1017 + * 29 - 20 : Group interval
1018 + * 16 - 13 : Send a single interrupt when crossing the hot threshold (1)
1019 + * or an interrupt everytime the hot threshold is crossed (0)
1020 + * 9 - 0 : Period unit
1023 + value = LVTS_GROUP_INTERVAL << 20 | LVTS_PERIOD_UNIT;
1024 + writel(value, LVTS_MONCTL1(lvts_ctrl->base));
1027 + * LVTS_MONCTL2 : Filtering and sensor interval
1031 + * 25-16 : Interval unit in PERIOD_UNIT between sample on
1032 + * the same sensor, filter interval
1033 + * 9-0 : Interval unit in PERIOD_UNIT between each sensor
1036 + value = LVTS_FILTER_INTERVAL << 16 | LVTS_SENSOR_INTERVAL;
1037 + writel(value, LVTS_MONCTL2(lvts_ctrl->base));
1039 + return lvts_irq_init(lvts_ctrl);
1042 +static int lvts_ctrl_start(struct device *dev, struct lvts_ctrl *lvts_ctrl)
1044 + struct lvts_sensor *lvts_sensors = lvts_ctrl->sensors;
1045 + struct thermal_zone_device *tz;
1046 + u32 sensor_map = 0;
1049 + for (i = 0; i < lvts_ctrl->num_lvts_sensor; i++) {
1051 + int dt_id = lvts_sensors[i].dt_id;
1053 + tz = devm_thermal_of_zone_register(dev, dt_id, &lvts_sensors[i],
1057 + * This thermal zone is not described in the
1058 + * device tree. It is not an error from the
1059 + * thermal OF code POV, we just continue.
1061 + if (PTR_ERR(tz) == -ENODEV)
1064 + return PTR_ERR(tz);
1068 + * The thermal zone pointer will be needed in the
1069 + * interrupt handler, we store it in the sensor
1070 + * structure. The thermal domain structure will be
1071 + * passed to the interrupt handler private data as the
1072 + * interrupt is shared for all the controller
1073 + * belonging to the thermal domain.
1075 + lvts_sensors[i].tz = tz;
1078 + * This sensor was correctly associated with a thermal
1079 + * zone, let's set the corresponding bit in the sensor
1080 + * map, so we can enable the temperature monitoring in
1081 + * the hardware thermal controller.
1083 + sensor_map |= BIT(i);
1088 + * 9: Single point access flow
1089 + * 0-3: Enable sensing point 0-3
1091 + * The initialization of the thermal zones give us
1092 + * which sensor point to enable. If any thermal zone
1093 + * was not described in the device tree, it won't be
1094 + * enabled here in the sensor map.
1096 + writel(sensor_map | BIT(9), LVTS_MONCTL0(lvts_ctrl->base));
1101 +static int lvts_domain_init(struct device *dev, struct lvts_domain *lvts_td,
1102 + const struct lvts_data *lvts_data)
1104 + struct lvts_ctrl *lvts_ctrl;
1107 + ret = lvts_ctrl_init(dev, lvts_td, lvts_data);
1111 + ret = lvts_domain_reset(dev, lvts_td->reset);
1113 + dev_dbg(dev, "Failed to reset domain");
1117 + for (i = 0; i < lvts_td->num_lvts_ctrl; i++) {
1119 + lvts_ctrl = &lvts_td->lvts_ctrl[i];
1122 + * Initialization steps:
1124 + * - Enable the clock
1125 + * - Connect to the LVTS
1126 + * - Initialize the LVTS
1127 + * - Prepare the calibration data
1128 + * - Select monitored sensors
1129 + * [ Configure sampling ]
1130 + * [ Configure the interrupt ]
1131 + * - Start measurement
1133 + ret = lvts_ctrl_set_enable(lvts_ctrl, true);
1135 + dev_dbg(dev, "Failed to enable LVTS clock");
1139 + ret = lvts_ctrl_connect(dev, lvts_ctrl);
1141 + dev_dbg(dev, "Failed to connect to LVTS controller");
1145 + ret = lvts_ctrl_initialize(dev, lvts_ctrl);
1147 + dev_dbg(dev, "Failed to initialize controller");
1151 + ret = lvts_ctrl_calibrate(dev, lvts_ctrl);
1153 + dev_dbg(dev, "Failed to calibrate controller");
1157 + ret = lvts_ctrl_configure(dev, lvts_ctrl);
1159 + dev_dbg(dev, "Failed to configure controller");
1163 + ret = lvts_ctrl_start(dev, lvts_ctrl);
1165 + dev_dbg(dev, "Failed to start controller");
1170 + return lvts_debugfs_init(dev, lvts_td);
1173 +static int lvts_probe(struct platform_device *pdev)
1175 + const struct lvts_data *lvts_data;
1176 + struct lvts_domain *lvts_td;
1177 + struct device *dev = &pdev->dev;
1178 + struct resource *res;
1181 + lvts_td = devm_kzalloc(dev, sizeof(*lvts_td), GFP_KERNEL);
1185 + lvts_data = of_device_get_match_data(dev);
1187 + lvts_td->clk = devm_clk_get_enabled(dev, NULL);
1188 + if (IS_ERR(lvts_td->clk))
1189 + return dev_err_probe(dev, PTR_ERR(lvts_td->clk), "Failed to retrieve clock\n");
1191 + res = platform_get_mem_or_io(pdev, 0);
1193 + return dev_err_probe(dev, (-ENXIO), "No IO resource\n");
1195 + lvts_td->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1196 + if (IS_ERR(lvts_td->base))
1197 + return dev_err_probe(dev, PTR_ERR(lvts_td->base), "Failed to map io resource\n");
1199 + lvts_td->reset = devm_reset_control_get_by_index(dev, 0);
1200 + if (IS_ERR(lvts_td->reset))
1201 + return dev_err_probe(dev, PTR_ERR(lvts_td->reset), "Failed to get reset control\n");
1203 + irq = platform_get_irq(pdev, 0);
1205 + return dev_err_probe(dev, irq, "No irq resource\n");
1207 + ret = lvts_domain_init(dev, lvts_td, lvts_data);
1209 + return dev_err_probe(dev, ret, "Failed to initialize the lvts domain\n");
1212 + * At this point the LVTS is initialized and enabled. We can
1213 + * safely enable the interrupt.
1215 + ret = devm_request_threaded_irq(dev, irq, NULL, lvts_irq_handler,
1216 + IRQF_ONESHOT, dev_name(dev), lvts_td);
1218 + return dev_err_probe(dev, ret, "Failed to request interrupt\n");
1220 + platform_set_drvdata(pdev, lvts_td);
1225 +static int lvts_remove(struct platform_device *pdev)
1227 + struct lvts_domain *lvts_td;
1230 + lvts_td = platform_get_drvdata(pdev);
1232 + for (i = 0; i < lvts_td->num_lvts_ctrl; i++)
1233 + lvts_ctrl_set_enable(&lvts_td->lvts_ctrl[i], false);
1235 + lvts_debugfs_exit(lvts_td);
1240 +static const struct lvts_ctrl_data mt8195_lvts_data_ctrl[] = {
1242 + .cal_offset = { 0x04, 0x07 },
1244 + { .dt_id = MT8195_MCU_BIG_CPU0 },
1245 + { .dt_id = MT8195_MCU_BIG_CPU1 }
1247 + .num_lvts_sensor = 2,
1249 + .hw_tshut_temp = LVTS_HW_SHUTDOWN_MT8195,
1252 + .cal_offset = { 0x0d, 0x10 },
1254 + { .dt_id = MT8195_MCU_BIG_CPU2 },
1255 + { .dt_id = MT8195_MCU_BIG_CPU3 }
1257 + .num_lvts_sensor = 2,
1259 + .hw_tshut_temp = LVTS_HW_SHUTDOWN_MT8195,
1262 + .cal_offset = { 0x16, 0x19, 0x1c, 0x1f },
1264 + { .dt_id = MT8195_MCU_LITTLE_CPU0 },
1265 + { .dt_id = MT8195_MCU_LITTLE_CPU1 },
1266 + { .dt_id = MT8195_MCU_LITTLE_CPU2 },
1267 + { .dt_id = MT8195_MCU_LITTLE_CPU3 }
1269 + .num_lvts_sensor = 4,
1271 + .hw_tshut_temp = LVTS_HW_SHUTDOWN_MT8195,
1275 +static const struct lvts_data mt8195_lvts_mcu_data = {
1276 + .lvts_ctrl = mt8195_lvts_data_ctrl,
1277 + .num_lvts_ctrl = ARRAY_SIZE(mt8195_lvts_data_ctrl),
1280 +static const struct of_device_id lvts_of_match[] = {
1281 + { .compatible = "mediatek,mt8195-lvts-mcu", .data = &mt8195_lvts_mcu_data },
1284 +MODULE_DEVICE_TABLE(of, lvts_of_match);
1286 +static struct platform_driver lvts_driver = {
1287 + .probe = lvts_probe,
1288 + .remove = lvts_remove,
1290 + .name = "mtk-lvts-thermal",
1291 + .of_match_table = lvts_of_match,
1294 +module_platform_driver(lvts_driver);
1296 +MODULE_AUTHOR("Balsam CHIHI <bchihi@baylibre.com>");
1297 +MODULE_DESCRIPTION("MediaTek LVTS Thermal Driver");
1298 +MODULE_LICENSE("GPL");