xref: /linux/drivers/thermal/broadcom/sr-thermal.c (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018 Broadcom
4  */
5 
6 #include <linux/module.h>
7 #include <linux/of_address.h>
8 #include <linux/platform_device.h>
9 #include <linux/thermal.h>
10 
11 /*
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.
16  */
17 #define SR_TMON_TEMP_BASE(id)   ((id) * 0x4)
18 
19 #define SR_TMON_MAX_LIST        6
20 
21 struct sr_tmon {
22 	struct thermal_zone_device *tz;
23 	unsigned int crit_temp;
24 	unsigned int tmon_id;
25 	struct sr_thermal *priv;
26 };
27 
28 struct sr_thermal {
29 	void __iomem *regs;
30 	unsigned int max_crit_temp;
31 	struct sr_tmon tmon[SR_TMON_MAX_LIST];
32 };
33 
34 static int sr_get_temp(void *data, int *temp)
35 {
36 	struct sr_tmon *tmon = data;
37 	struct sr_thermal *sr_thermal = tmon->priv;
38 
39 	*temp = readl(sr_thermal->regs + SR_TMON_TEMP_BASE(tmon->tmon_id));
40 
41 	return 0;
42 }
43 
44 static const struct thermal_zone_of_device_ops sr_tz_ops = {
45 	.get_temp = sr_get_temp,
46 };
47 
48 static int sr_thermal_probe(struct platform_device *pdev)
49 {
50 	struct device *dev = &pdev->dev;
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;
57 
58 	sr_thermal = devm_kzalloc(dev, sizeof(*sr_thermal), GFP_KERNEL);
59 	if (!sr_thermal)
60 		return -ENOMEM;
61 
62 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
63 	sr_thermal->regs = (void __iomem *)devm_memremap(&pdev->dev, res->start,
64 							 resource_size(res),
65 							 MEMREMAP_WB);
66 	if (IS_ERR(sr_thermal->regs)) {
67 		dev_err(dev, "failed to get io address\n");
68 		return PTR_ERR(sr_thermal->regs);
69 	}
70 
71 	ret = device_property_read_u32(dev, "brcm,tmon-mask", &sr_tmon_list);
72 	if (ret)
73 		return ret;
74 
75 	tmon = sr_thermal->tmon;
76 	for (i = 0; i < SR_TMON_MAX_LIST; i++, tmon++) {
77 		if (!(sr_tmon_list & BIT(i)))
78 			continue;
79 
80 		/* Flush temperature registers */
81 		writel(0, sr_thermal->regs + SR_TMON_TEMP_BASE(i));
82 		tmon->tmon_id = i;
83 		tmon->priv = sr_thermal;
84 		tmon->tz = devm_thermal_zone_of_sensor_register(dev, i, tmon,
85 								&sr_tz_ops);
86 		if (IS_ERR(tmon->tz))
87 			return PTR_ERR(tmon->tz);
88 
89 		dev_dbg(dev, "thermal sensor %d registered\n", i);
90 	}
91 	platform_set_drvdata(pdev, sr_thermal);
92 
93 	return 0;
94 }
95 
96 static const struct of_device_id sr_thermal_of_match[] = {
97 	{ .compatible = "brcm,sr-thermal", },
98 	{},
99 };
100 MODULE_DEVICE_TABLE(of, sr_thermal_of_match);
101 
102 static struct platform_driver sr_thermal_driver = {
103 	.probe		= sr_thermal_probe,
104 	.driver = {
105 		.name = "sr-thermal",
106 		.of_match_table = sr_thermal_of_match,
107 	},
108 };
109 module_platform_driver(sr_thermal_driver);
110 
111 MODULE_AUTHOR("Pramod Kumar <pramod.kumar@broadcom.com>");
112 MODULE_DESCRIPTION("Stingray thermal driver");
113 MODULE_LICENSE("GPL v2");
114