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