Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / thermal / broadcom / sr-thermal.c
blob9a29dfd4c7fe5f546d003800c33bf700f694544f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2018 Broadcom
4 */
6 #include <linux/module.h>
7 #include <linux/of_address.h>
8 #include <linux/platform_device.h>
9 #include <linux/thermal.h>
12 * In stingray thermal IO memory,
13 * Total Number of available TMONs MASK is at offset 0
14 * temperature registers BASE is at 4 byte offset.
15 * Each TMON temperature register size is 4.
17 #define SR_TMON_TEMP_BASE(id) ((id) * 0x4)
19 #define SR_TMON_MAX_LIST 6
21 struct sr_tmon {
22 unsigned int crit_temp;
23 unsigned int tmon_id;
24 struct sr_thermal *priv;
27 struct sr_thermal {
28 void __iomem *regs;
29 unsigned int max_crit_temp;
30 struct sr_tmon tmon[SR_TMON_MAX_LIST];
33 static int sr_get_temp(struct thermal_zone_device *tz, int *temp)
35 struct sr_tmon *tmon = thermal_zone_device_priv(tz);
36 struct sr_thermal *sr_thermal = tmon->priv;
38 *temp = readl(sr_thermal->regs + SR_TMON_TEMP_BASE(tmon->tmon_id));
40 return 0;
43 static const struct thermal_zone_device_ops sr_tz_ops = {
44 .get_temp = sr_get_temp,
47 static int sr_thermal_probe(struct platform_device *pdev)
49 struct device *dev = &pdev->dev;
50 struct thermal_zone_device *tz;
51 struct sr_thermal *sr_thermal;
52 struct sr_tmon *tmon;
53 struct resource *res;
54 u32 sr_tmon_list = 0;
55 unsigned int i;
56 int ret;
58 sr_thermal = devm_kzalloc(dev, sizeof(*sr_thermal), GFP_KERNEL);
59 if (!sr_thermal)
60 return -ENOMEM;
62 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
63 if (!res)
64 return -ENOENT;
66 sr_thermal->regs = (void __iomem *)devm_memremap(&pdev->dev, res->start,
67 resource_size(res),
68 MEMREMAP_WB);
69 if (IS_ERR(sr_thermal->regs)) {
70 dev_err(dev, "failed to get io address\n");
71 return PTR_ERR(sr_thermal->regs);
74 ret = device_property_read_u32(dev, "brcm,tmon-mask", &sr_tmon_list);
75 if (ret)
76 return ret;
78 tmon = sr_thermal->tmon;
79 for (i = 0; i < SR_TMON_MAX_LIST; i++, tmon++) {
80 if (!(sr_tmon_list & BIT(i)))
81 continue;
83 /* Flush temperature registers */
84 writel(0, sr_thermal->regs + SR_TMON_TEMP_BASE(i));
85 tmon->tmon_id = i;
86 tmon->priv = sr_thermal;
87 tz = devm_thermal_of_zone_register(dev, i, tmon,
88 &sr_tz_ops);
89 if (IS_ERR(tz))
90 return PTR_ERR(tz);
92 dev_dbg(dev, "thermal sensor %d registered\n", i);
95 return 0;
98 static const struct of_device_id sr_thermal_of_match[] = {
99 { .compatible = "brcm,sr-thermal", },
102 MODULE_DEVICE_TABLE(of, sr_thermal_of_match);
104 static struct platform_driver sr_thermal_driver = {
105 .probe = sr_thermal_probe,
106 .driver = {
107 .name = "sr-thermal",
108 .of_match_table = sr_thermal_of_match,
111 module_platform_driver(sr_thermal_driver);
113 MODULE_AUTHOR("Pramod Kumar <pramod.kumar@broadcom.com>");
114 MODULE_DESCRIPTION("Stingray thermal driver");
115 MODULE_LICENSE("GPL v2");