xref: /linux/drivers/thermal/st/st_thermal_memmap.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ST Thermal Sensor Driver for memory mapped sensors.
4  * Author: Ajit Pal Singh <ajitpal.singh@st.com>
5  *
6  * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
7  */
8 
9 #include <linux/of.h>
10 #include <linux/module.h>
11 
12 #include "st_thermal.h"
13 
14 #define STIH416_MPE_CONF			0x0
15 #define STIH416_MPE_STATUS			0x4
16 #define STIH416_MPE_INT_THRESH			0x8
17 #define STIH416_MPE_INT_EN			0xC
18 
19 /* Power control bits for the memory mapped thermal sensor */
20 #define THERMAL_PDN				BIT(4)
21 #define THERMAL_SRSTN				BIT(10)
22 
23 static const struct reg_field st_mmap_thermal_regfields[MAX_REGFIELDS] = {
24 	/*
25 	 * According to the STIH416 MPE temp sensor data sheet -
26 	 * the PDN (Power Down Bit) and SRSTN (Soft Reset Bit) need to be
27 	 * written simultaneously for powering on and off the temperature
28 	 * sensor. regmap_update_bits() will be used to update the register.
29 	 */
30 	[INT_THRESH_HI]	= REG_FIELD(STIH416_MPE_INT_THRESH, 	0,  7),
31 	[DCORRECT]	= REG_FIELD(STIH416_MPE_CONF,		5,  9),
32 	[OVERFLOW]	= REG_FIELD(STIH416_MPE_STATUS,		9,  9),
33 	[DATA]		= REG_FIELD(STIH416_MPE_STATUS,		11, 18),
34 	[INT_ENABLE]	= REG_FIELD(STIH416_MPE_INT_EN,		0,  0),
35 };
36 
37 static irqreturn_t st_mmap_thermal_trip_handler(int irq, void *sdata)
38 {
39 	struct st_thermal_sensor *sensor = sdata;
40 
41 	thermal_zone_device_update(sensor->thermal_dev,
42 				   THERMAL_EVENT_UNSPECIFIED);
43 
44 	return IRQ_HANDLED;
45 }
46 
47 /* Private ops for the Memory Mapped based thermal sensors */
48 static int st_mmap_power_ctrl(struct st_thermal_sensor *sensor,
49 			      enum st_thermal_power_state power_state)
50 {
51 	const unsigned int mask = (THERMAL_PDN | THERMAL_SRSTN);
52 	const unsigned int val = power_state ? mask : 0;
53 
54 	return regmap_update_bits(sensor->regmap, STIH416_MPE_CONF, mask, val);
55 }
56 
57 static int st_mmap_alloc_regfields(struct st_thermal_sensor *sensor)
58 {
59 	struct device *dev = sensor->dev;
60 	struct regmap *regmap = sensor->regmap;
61 	const struct reg_field *reg_fields = sensor->cdata->reg_fields;
62 
63 	sensor->int_thresh_hi = devm_regmap_field_alloc(dev, regmap,
64 						reg_fields[INT_THRESH_HI]);
65 	sensor->int_enable = devm_regmap_field_alloc(dev, regmap,
66 						reg_fields[INT_ENABLE]);
67 
68 	if (IS_ERR(sensor->int_thresh_hi) || IS_ERR(sensor->int_enable)) {
69 		dev_err(dev, "failed to alloc mmap regfields\n");
70 		return -EINVAL;
71 	}
72 
73 	return 0;
74 }
75 
76 static int st_mmap_enable_irq(struct st_thermal_sensor *sensor)
77 {
78 	int ret;
79 
80 	/* Set upper critical threshold */
81 	ret = regmap_field_write(sensor->int_thresh_hi,
82 				 sensor->cdata->crit_temp -
83 				 sensor->cdata->temp_adjust_val);
84 	if (ret)
85 		return ret;
86 
87 	return regmap_field_write(sensor->int_enable, 1);
88 }
89 
90 static int st_mmap_register_enable_irq(struct st_thermal_sensor *sensor)
91 {
92 	struct device *dev = sensor->dev;
93 	struct platform_device *pdev = to_platform_device(dev);
94 	int ret;
95 
96 	sensor->irq = platform_get_irq(pdev, 0);
97 	if (sensor->irq < 0) {
98 		dev_err(dev, "failed to register IRQ\n");
99 		return sensor->irq;
100 	}
101 
102 	ret = devm_request_threaded_irq(dev, sensor->irq,
103 					NULL, st_mmap_thermal_trip_handler,
104 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
105 					dev->driver->name, sensor);
106 	if (ret) {
107 		dev_err(dev, "failed to register IRQ %d\n", sensor->irq);
108 		return ret;
109 	}
110 
111 	return st_mmap_enable_irq(sensor);
112 }
113 
114 static const struct regmap_config st_416mpe_regmap_config = {
115 	.reg_bits = 32,
116 	.val_bits = 32,
117 	.reg_stride = 4,
118 };
119 
120 static int st_mmap_regmap_init(struct st_thermal_sensor *sensor)
121 {
122 	struct device *dev = sensor->dev;
123 	struct platform_device *pdev = to_platform_device(dev);
124 	struct resource *res;
125 
126 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127 	if (!res) {
128 		dev_err(dev, "no memory resources defined\n");
129 		return -ENODEV;
130 	}
131 
132 	sensor->mmio_base = devm_ioremap_resource(dev, res);
133 	if (IS_ERR(sensor->mmio_base)) {
134 		dev_err(dev, "failed to remap IO\n");
135 		return PTR_ERR(sensor->mmio_base);
136 	}
137 
138 	sensor->regmap = devm_regmap_init_mmio(dev, sensor->mmio_base,
139 				&st_416mpe_regmap_config);
140 	if (IS_ERR(sensor->regmap)) {
141 		dev_err(dev, "failed to initialise regmap\n");
142 		return PTR_ERR(sensor->regmap);
143 	}
144 
145 	return 0;
146 }
147 
148 static const struct st_thermal_sensor_ops st_mmap_sensor_ops = {
149 	.power_ctrl		= st_mmap_power_ctrl,
150 	.alloc_regfields	= st_mmap_alloc_regfields,
151 	.regmap_init		= st_mmap_regmap_init,
152 	.register_enable_irq	= st_mmap_register_enable_irq,
153 	.enable_irq		= st_mmap_enable_irq,
154 };
155 
156 /* Compatible device data stih416 mpe thermal sensor */
157 static const struct st_thermal_compat_data st_416mpe_cdata = {
158 	.reg_fields		= st_mmap_thermal_regfields,
159 	.ops			= &st_mmap_sensor_ops,
160 	.calibration_val	= 14,
161 	.temp_adjust_val	= -95,
162 	.crit_temp		= 120,
163 };
164 
165 /* Compatible device data stih407 thermal sensor */
166 static const struct st_thermal_compat_data st_407_cdata = {
167 	.reg_fields		= st_mmap_thermal_regfields,
168 	.ops			= &st_mmap_sensor_ops,
169 	.calibration_val	= 16,
170 	.temp_adjust_val	= -95,
171 	.crit_temp		= 120,
172 };
173 
174 static const struct of_device_id st_mmap_thermal_of_match[] = {
175 	{ .compatible = "st,stih416-mpe-thermal", .data = &st_416mpe_cdata },
176 	{ .compatible = "st,stih407-thermal",     .data = &st_407_cdata },
177 	{ /* sentinel */ }
178 };
179 MODULE_DEVICE_TABLE(of, st_mmap_thermal_of_match);
180 
181 static int st_mmap_probe(struct platform_device *pdev)
182 {
183 	return st_thermal_register(pdev,  st_mmap_thermal_of_match);
184 }
185 
186 static int st_mmap_remove(struct platform_device *pdev)
187 {
188 	return st_thermal_unregister(pdev);
189 }
190 
191 static struct platform_driver st_mmap_thermal_driver = {
192 	.driver = {
193 		.name	= "st_thermal_mmap",
194 		.pm     = &st_thermal_pm_ops,
195 		.of_match_table = st_mmap_thermal_of_match,
196 	},
197 	.probe		= st_mmap_probe,
198 	.remove		= st_mmap_remove,
199 };
200 
201 module_platform_driver(st_mmap_thermal_driver);
202 
203 MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
204 MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
205 MODULE_LICENSE("GPL v2");
206