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