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
23static 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
37static 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 */
48static 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
57static 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
76static 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
90static 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		return sensor->irq;
99
100	ret = devm_request_threaded_irq(dev, sensor->irq,
101					NULL, st_mmap_thermal_trip_handler,
102					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
103					dev->driver->name, sensor);
104	if (ret) {
105		dev_err(dev, "failed to register IRQ %d\n", sensor->irq);
106		return ret;
107	}
108
109	return st_mmap_enable_irq(sensor);
110}
111
112static const struct regmap_config st_416mpe_regmap_config = {
113	.reg_bits = 32,
114	.val_bits = 32,
115	.reg_stride = 4,
116};
117
118static int st_mmap_regmap_init(struct st_thermal_sensor *sensor)
119{
120	struct device *dev = sensor->dev;
121	struct platform_device *pdev = to_platform_device(dev);
122	struct resource *res;
123
124	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
125	if (!res) {
126		dev_err(dev, "no memory resources defined\n");
127		return -ENODEV;
128	}
129
130	sensor->mmio_base = devm_ioremap_resource(dev, res);
131	if (IS_ERR(sensor->mmio_base)) {
132		dev_err(dev, "failed to remap IO\n");
133		return PTR_ERR(sensor->mmio_base);
134	}
135
136	sensor->regmap = devm_regmap_init_mmio(dev, sensor->mmio_base,
137				&st_416mpe_regmap_config);
138	if (IS_ERR(sensor->regmap)) {
139		dev_err(dev, "failed to initialise regmap\n");
140		return PTR_ERR(sensor->regmap);
141	}
142
143	return 0;
144}
145
146static const struct st_thermal_sensor_ops st_mmap_sensor_ops = {
147	.power_ctrl		= st_mmap_power_ctrl,
148	.alloc_regfields	= st_mmap_alloc_regfields,
149	.regmap_init		= st_mmap_regmap_init,
150	.register_enable_irq	= st_mmap_register_enable_irq,
151	.enable_irq		= st_mmap_enable_irq,
152};
153
154/* Compatible device data stih416 mpe thermal sensor */
155static const struct st_thermal_compat_data st_416mpe_cdata = {
156	.reg_fields		= st_mmap_thermal_regfields,
157	.ops			= &st_mmap_sensor_ops,
158	.calibration_val	= 14,
159	.temp_adjust_val	= -95,
160	.crit_temp		= 120,
161};
162
163/* Compatible device data stih407 thermal sensor */
164static const struct st_thermal_compat_data st_407_cdata = {
165	.reg_fields		= st_mmap_thermal_regfields,
166	.ops			= &st_mmap_sensor_ops,
167	.calibration_val	= 16,
168	.temp_adjust_val	= -95,
169	.crit_temp		= 120,
170};
171
172static const struct of_device_id st_mmap_thermal_of_match[] = {
173	{ .compatible = "st,stih416-mpe-thermal", .data = &st_416mpe_cdata },
174	{ .compatible = "st,stih407-thermal",     .data = &st_407_cdata },
175	{ /* sentinel */ }
176};
177MODULE_DEVICE_TABLE(of, st_mmap_thermal_of_match);
178
179static int st_mmap_probe(struct platform_device *pdev)
180{
181	return st_thermal_register(pdev,  st_mmap_thermal_of_match);
182}
183
184static int st_mmap_remove(struct platform_device *pdev)
185{
186	return st_thermal_unregister(pdev);
187}
188
189static struct platform_driver st_mmap_thermal_driver = {
190	.driver = {
191		.name	= "st_thermal_mmap",
192		.pm     = &st_thermal_pm_ops,
193		.of_match_table = st_mmap_thermal_of_match,
194	},
195	.probe		= st_mmap_probe,
196	.remove		= st_mmap_remove,
197};
198
199module_platform_driver(st_mmap_thermal_driver);
200
201MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
202MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
203MODULE_LICENSE("GPL v2");
204