1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Broadcom AVS RO thermal sensor driver 4 * 5 * based on brcmstb_thermal 6 * 7 * Copyright (C) 2020 Stefan Wahren 8 */ 9 10 #include <linux/bitops.h> 11 #include <linux/clk.h> 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/io.h> 15 #include <linux/kernel.h> 16 #include <linux/mfd/syscon.h> 17 #include <linux/module.h> 18 #include <linux/platform_device.h> 19 #include <linux/of_device.h> 20 #include <linux/regmap.h> 21 #include <linux/thermal.h> 22 23 #include "../thermal_hwmon.h" 24 25 #define AVS_RO_TEMP_STATUS 0x200 26 #define AVS_RO_TEMP_STATUS_VALID_MSK (BIT(16) | BIT(10)) 27 #define AVS_RO_TEMP_STATUS_DATA_MSK GENMASK(9, 0) 28 29 struct bcm2711_thermal_priv { 30 struct regmap *regmap; 31 struct thermal_zone_device *thermal; 32 }; 33 34 static int bcm2711_get_temp(struct thermal_zone_device *tz, int *temp) 35 { 36 struct bcm2711_thermal_priv *priv = thermal_zone_device_priv(tz); 37 int slope = thermal_zone_get_slope(tz); 38 int offset = thermal_zone_get_offset(tz); 39 u32 val; 40 int ret; 41 42 ret = regmap_read(priv->regmap, AVS_RO_TEMP_STATUS, &val); 43 if (ret) 44 return ret; 45 46 if (!(val & AVS_RO_TEMP_STATUS_VALID_MSK)) 47 return -EIO; 48 49 val &= AVS_RO_TEMP_STATUS_DATA_MSK; 50 51 /* Convert a HW code to a temperature reading (millidegree celsius) */ 52 *temp = slope * val + offset; 53 54 return 0; 55 } 56 57 static const struct thermal_zone_device_ops bcm2711_thermal_of_ops = { 58 .get_temp = bcm2711_get_temp, 59 }; 60 61 static const struct of_device_id bcm2711_thermal_id_table[] = { 62 { .compatible = "brcm,bcm2711-thermal" }, 63 {}, 64 }; 65 MODULE_DEVICE_TABLE(of, bcm2711_thermal_id_table); 66 67 static int bcm2711_thermal_probe(struct platform_device *pdev) 68 { 69 struct thermal_zone_device *thermal; 70 struct bcm2711_thermal_priv *priv; 71 struct device *dev = &pdev->dev; 72 struct device_node *parent; 73 struct regmap *regmap; 74 int ret; 75 76 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 77 if (!priv) 78 return -ENOMEM; 79 80 /* get regmap from syscon node */ 81 parent = of_get_parent(dev->of_node); /* parent should be syscon node */ 82 regmap = syscon_node_to_regmap(parent); 83 of_node_put(parent); 84 if (IS_ERR(regmap)) { 85 ret = PTR_ERR(regmap); 86 dev_err(dev, "failed to get regmap: %d\n", ret); 87 return ret; 88 } 89 priv->regmap = regmap; 90 91 thermal = devm_thermal_of_zone_register(dev, 0, priv, 92 &bcm2711_thermal_of_ops); 93 if (IS_ERR(thermal)) { 94 ret = PTR_ERR(thermal); 95 dev_err(dev, "could not register sensor: %d\n", ret); 96 return ret; 97 } 98 99 priv->thermal = thermal; 100 101 return thermal_add_hwmon_sysfs(thermal); 102 } 103 104 static struct platform_driver bcm2711_thermal_driver = { 105 .probe = bcm2711_thermal_probe, 106 .driver = { 107 .name = "bcm2711_thermal", 108 .of_match_table = bcm2711_thermal_id_table, 109 }, 110 }; 111 module_platform_driver(bcm2711_thermal_driver); 112 113 MODULE_LICENSE("GPL"); 114 MODULE_AUTHOR("Stefan Wahren"); 115 MODULE_DESCRIPTION("Broadcom AVS RO thermal sensor driver"); 116