Skip to content

Commit bd0b451

Browse files
Andre-ARMdlezcano
authored andcommitted
thermal/drivers/sun8i: Add SRAM register access code
The Allwinner H616 SoC needs to clear a bit in one register in the SRAM controller, to report reasonable temperature values. On reset, bit 16 in register 0x3000000 is set, which leads to the driver reporting temperatures around 200C. Clearing this bit brings the values down to the expected range. The BSP code does a one-time write in U-Boot, with a comment just mentioning the effect on the THS, but offering no further explanation. To not rely on firmware to set things up for us, add code that queries the SRAM controller device via a DT phandle link, then clear just this single bit. Signed-off-by: Andre Przywara <[email protected]> Acked-by: Vasily Khoruzhick <[email protected]> Signed-off-by: Daniel Lezcano <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 63e39fc commit bd0b451

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

drivers/thermal/sun8i_thermal.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/module.h>
1616
#include <linux/nvmem-consumer.h>
1717
#include <linux/of.h>
18+
#include <linux/of_platform.h>
1819
#include <linux/platform_device.h>
1920
#include <linux/regmap.h>
2021
#include <linux/reset.h>
@@ -66,6 +67,7 @@ struct tsensor {
6667
struct ths_thermal_chip {
6768
bool has_mod_clk;
6869
bool has_bus_clk_reset;
70+
bool needs_sram;
6971
int sensor_num;
7072
int offset;
7173
int scale;
@@ -83,12 +85,16 @@ struct ths_device {
8385
const struct ths_thermal_chip *chip;
8486
struct device *dev;
8587
struct regmap *regmap;
88+
struct regmap_field *sram_regmap_field;
8689
struct reset_control *reset;
8790
struct clk *bus_clk;
8891
struct clk *mod_clk;
8992
struct tsensor sensor[MAX_SENSOR_NUM];
9093
};
9194

95+
/* The H616 needs to have a bit 16 in the SRAM control register cleared. */
96+
static const struct reg_field sun8i_ths_sram_reg_field = REG_FIELD(0x0, 16, 16);
97+
9298
/* Temp Unit: millidegree Celsius */
9399
static int sun8i_ths_calc_temp(struct ths_device *tmdev,
94100
int id, int reg)
@@ -337,6 +343,34 @@ static void sun8i_ths_reset_control_assert(void *data)
337343
reset_control_assert(data);
338344
}
339345

346+
static struct regmap *sun8i_ths_get_sram_regmap(struct device_node *node)
347+
{
348+
struct device_node *sram_node;
349+
struct platform_device *sram_pdev;
350+
struct regmap *regmap = NULL;
351+
352+
sram_node = of_parse_phandle(node, "allwinner,sram", 0);
353+
if (!sram_node)
354+
return ERR_PTR(-ENODEV);
355+
356+
sram_pdev = of_find_device_by_node(sram_node);
357+
if (!sram_pdev) {
358+
/* platform device might not be probed yet */
359+
regmap = ERR_PTR(-EPROBE_DEFER);
360+
goto out_put_node;
361+
}
362+
363+
/* If no regmap is found then the other device driver is at fault */
364+
regmap = dev_get_regmap(&sram_pdev->dev, NULL);
365+
if (!regmap)
366+
regmap = ERR_PTR(-EINVAL);
367+
368+
platform_device_put(sram_pdev);
369+
out_put_node:
370+
of_node_put(sram_node);
371+
return regmap;
372+
}
373+
340374
static int sun8i_ths_resource_init(struct ths_device *tmdev)
341375
{
342376
struct device *dev = tmdev->dev;
@@ -381,6 +415,19 @@ static int sun8i_ths_resource_init(struct ths_device *tmdev)
381415
if (ret)
382416
return ret;
383417

418+
if (tmdev->chip->needs_sram) {
419+
struct regmap *regmap;
420+
421+
regmap = sun8i_ths_get_sram_regmap(dev->of_node);
422+
if (IS_ERR(regmap))
423+
return PTR_ERR(regmap);
424+
tmdev->sram_regmap_field = devm_regmap_field_alloc(dev,
425+
regmap,
426+
sun8i_ths_sram_reg_field);
427+
if (IS_ERR(tmdev->sram_regmap_field))
428+
return PTR_ERR(tmdev->sram_regmap_field);
429+
}
430+
384431
ret = sun8i_ths_calibrate(tmdev);
385432
if (ret)
386433
return ret;
@@ -427,6 +474,10 @@ static int sun50i_h6_thermal_init(struct ths_device *tmdev)
427474
{
428475
int val;
429476

477+
/* The H616 needs to have a bit in the SRAM control register cleared. */
478+
if (tmdev->sram_regmap_field)
479+
regmap_field_write(tmdev->sram_regmap_field, 0);
480+
430481
/*
431482
* The manual recommends an overall sample frequency of 50 KHz (20us,
432483
* 480 cycles at 24 MHz), which provides plenty of time for both the

0 commit comments

Comments
 (0)