1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Dove thermal sensor driver
4  *
5  * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
6  */
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/kernel.h>
11 #include <linux/of.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/thermal.h>
15 
16 #define DOVE_THERMAL_TEMP_OFFSET	1
17 #define DOVE_THERMAL_TEMP_MASK		0x1FF
18 
19 /* Dove Thermal Manager Control and Status Register */
20 #define PMU_TM_DISABLE_OFFS		0
21 #define PMU_TM_DISABLE_MASK		(0x1 << PMU_TM_DISABLE_OFFS)
22 
23 /* Dove Theraml Diode Control 0 Register */
24 #define PMU_TDC0_SW_RST_MASK		(0x1 << 1)
25 #define PMU_TDC0_SEL_VCAL_OFFS		5
26 #define PMU_TDC0_SEL_VCAL_MASK		(0x3 << PMU_TDC0_SEL_VCAL_OFFS)
27 #define PMU_TDC0_REF_CAL_CNT_OFFS	11
28 #define PMU_TDC0_REF_CAL_CNT_MASK	(0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
29 #define PMU_TDC0_AVG_NUM_OFFS		25
30 #define PMU_TDC0_AVG_NUM_MASK		(0x7 << PMU_TDC0_AVG_NUM_OFFS)
31 
32 /* Dove Thermal Diode Control 1 Register */
33 #define PMU_TEMP_DIOD_CTRL1_REG		0x04
34 #define PMU_TDC1_TEMP_VALID_MASK	(0x1 << 10)
35 
36 /* Dove Thermal Sensor Dev Structure */
37 struct dove_thermal_priv {
38 	void __iomem *sensor;
39 	void __iomem *control;
40 };
41 
42 static int dove_init_sensor(const struct dove_thermal_priv *priv)
43 {
44 	u32 reg;
45 	u32 i;
46 
47 	/* Configure the Diode Control Register #0 */
48 	reg = readl_relaxed(priv->control);
49 
50 	/* Use average of 2 */
51 	reg &= ~PMU_TDC0_AVG_NUM_MASK;
52 	reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
53 
54 	/* Reference calibration value */
55 	reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
56 	reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
57 
58 	/* Set the high level reference for calibration */
59 	reg &= ~PMU_TDC0_SEL_VCAL_MASK;
60 	reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
61 	writel(reg, priv->control);
62 
63 	/* Reset the sensor */
64 	reg = readl_relaxed(priv->control);
65 	writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
66 	writel(reg, priv->control);
67 
68 	/* Enable the sensor */
69 	reg = readl_relaxed(priv->sensor);
70 	reg &= ~PMU_TM_DISABLE_MASK;
71 	writel(reg, priv->sensor);
72 
73 	/* Poll the sensor for the first reading */
74 	for (i = 0; i < 1000000; i++) {
75 		reg = readl_relaxed(priv->sensor);
76 		if (reg & DOVE_THERMAL_TEMP_MASK)
77 			break;
78 	}
79 
80 	if (i == 1000000)
81 		return -EIO;
82 
83 	return 0;
84 }
85 
86 static int dove_get_temp(struct thermal_zone_device *thermal,
87 			  int *temp)
88 {
89 	unsigned long reg;
90 	struct dove_thermal_priv *priv = thermal_zone_device_priv(thermal);
91 
92 	/* Valid check */
93 	reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
94 	if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0)
95 		return -EIO;
96 
97 	/*
98 	 * Calculate temperature. According to Marvell internal
99 	 * documentation the formula for this is:
100 	 * Celsius = (322-reg)/1.3625
101 	 */
102 	reg = readl_relaxed(priv->sensor);
103 	reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
104 	*temp = ((3220000000UL - (10000000UL * reg)) / 13625);
105 
106 	return 0;
107 }
108 
109 static struct thermal_zone_device_ops ops = {
110 	.get_temp = dove_get_temp,
111 };
112 
113 static const struct of_device_id dove_thermal_id_table[] = {
114 	{ .compatible = "marvell,dove-thermal" },
115 	{}
116 };
117 
118 static int dove_thermal_probe(struct platform_device *pdev)
119 {
120 	struct thermal_zone_device *thermal = NULL;
121 	struct dove_thermal_priv *priv;
122 	int ret;
123 
124 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
125 	if (!priv)
126 		return -ENOMEM;
127 
128 	priv->sensor = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
129 	if (IS_ERR(priv->sensor))
130 		return PTR_ERR(priv->sensor);
131 
132 	priv->control = devm_platform_get_and_ioremap_resource(pdev, 1, NULL);
133 	if (IS_ERR(priv->control))
134 		return PTR_ERR(priv->control);
135 
136 	ret = dove_init_sensor(priv);
137 	if (ret) {
138 		dev_err(&pdev->dev, "Failed to initialize sensor\n");
139 		return ret;
140 	}
141 
142 	thermal = thermal_tripless_zone_device_register("dove_thermal", priv,
143 							&ops, NULL);
144 	if (IS_ERR(thermal)) {
145 		dev_err(&pdev->dev,
146 			"Failed to register thermal zone device\n");
147 		return PTR_ERR(thermal);
148 	}
149 
150 	ret = thermal_zone_device_enable(thermal);
151 	if (ret) {
152 		thermal_zone_device_unregister(thermal);
153 		return ret;
154 	}
155 
156 	platform_set_drvdata(pdev, thermal);
157 
158 	return 0;
159 }
160 
161 static int dove_thermal_exit(struct platform_device *pdev)
162 {
163 	struct thermal_zone_device *dove_thermal =
164 		platform_get_drvdata(pdev);
165 
166 	thermal_zone_device_unregister(dove_thermal);
167 
168 	return 0;
169 }
170 
171 MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
172 
173 static struct platform_driver dove_thermal_driver = {
174 	.probe = dove_thermal_probe,
175 	.remove = dove_thermal_exit,
176 	.driver = {
177 		.name = "dove_thermal",
178 		.of_match_table = dove_thermal_id_table,
179 	},
180 };
181 
182 module_platform_driver(dove_thermal_driver);
183 
184 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
185 MODULE_DESCRIPTION("Dove thermal driver");
186 MODULE_LICENSE("GPL");
187