1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ST Thermal Sensor Driver for syscfg based 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 #include <linux/mfd/syscon.h>
12 
13 #include "st_thermal.h"
14 
15 /* STiH415 */
16 #define STIH415_SYSCFG_FRONT(num)		((num - 100) * 4)
17 #define STIH415_SAS_THSENS_CONF			STIH415_SYSCFG_FRONT(178)
18 #define STIH415_SAS_THSENS_STATUS		STIH415_SYSCFG_FRONT(198)
19 #define STIH415_SYSCFG_MPE(num)			((num - 600) * 4)
20 #define STIH415_MPE_THSENS_CONF			STIH415_SYSCFG_MPE(607)
21 #define STIH415_MPE_THSENS_STATUS		STIH415_SYSCFG_MPE(667)
22 
23 /* STiH416 */
24 #define STIH416_SYSCFG_FRONT(num)		((num - 1000) * 4)
25 #define STIH416_SAS_THSENS_CONF			STIH416_SYSCFG_FRONT(1552)
26 #define STIH416_SAS_THSENS_STATUS1		STIH416_SYSCFG_FRONT(1554)
27 #define STIH416_SAS_THSENS_STATUS2		STIH416_SYSCFG_FRONT(1594)
28 
29 /* STiD127 */
30 #define STID127_SYSCFG_CPU(num)			((num - 700) * 4)
31 #define STID127_THSENS_CONF			STID127_SYSCFG_CPU(743)
32 #define STID127_THSENS_STATUS			STID127_SYSCFG_CPU(767)
33 
34 static const struct reg_field st_415sas_regfields[MAX_REGFIELDS] = {
35 	[TEMP_PWR] = REG_FIELD(STIH415_SAS_THSENS_CONF,   9,  9),
36 	[DCORRECT] = REG_FIELD(STIH415_SAS_THSENS_CONF,   4,  8),
37 	[OVERFLOW] = REG_FIELD(STIH415_SAS_THSENS_STATUS, 8,  8),
38 	[DATA] 	   = REG_FIELD(STIH415_SAS_THSENS_STATUS, 10, 16),
39 };
40 
41 static const struct reg_field st_415mpe_regfields[MAX_REGFIELDS] = {
42 	[TEMP_PWR] = REG_FIELD(STIH415_MPE_THSENS_CONF,   8,  8),
43 	[DCORRECT] = REG_FIELD(STIH415_MPE_THSENS_CONF,   3,  7),
44 	[OVERFLOW] = REG_FIELD(STIH415_MPE_THSENS_STATUS, 9,  9),
45 	[DATA]     = REG_FIELD(STIH415_MPE_THSENS_STATUS, 11, 18),
46 };
47 
48 static const struct reg_field st_416sas_regfields[MAX_REGFIELDS] = {
49 	[TEMP_PWR] = REG_FIELD(STIH416_SAS_THSENS_CONF,    9,  9),
50 	[DCORRECT] = REG_FIELD(STIH416_SAS_THSENS_CONF,    4,  8),
51 	[OVERFLOW] = REG_FIELD(STIH416_SAS_THSENS_STATUS1, 8,  8),
52 	[DATA]     = REG_FIELD(STIH416_SAS_THSENS_STATUS2, 10, 16),
53 };
54 
55 static const struct reg_field st_127_regfields[MAX_REGFIELDS] = {
56 	[TEMP_PWR] = REG_FIELD(STID127_THSENS_CONF,   7,  7),
57 	[DCORRECT] = REG_FIELD(STID127_THSENS_CONF,   2,  6),
58 	[OVERFLOW] = REG_FIELD(STID127_THSENS_STATUS, 9,  9),
59 	[DATA]     = REG_FIELD(STID127_THSENS_STATUS, 11, 18),
60 };
61 
62 /* Private OPs for System Configuration Register based thermal sensors */
st_syscfg_power_ctrl(struct st_thermal_sensor * sensor,enum st_thermal_power_state power_state)63 static int st_syscfg_power_ctrl(struct st_thermal_sensor *sensor,
64 				enum st_thermal_power_state power_state)
65 {
66 	return regmap_field_write(sensor->pwr, power_state);
67 }
68 
st_syscfg_alloc_regfields(struct st_thermal_sensor * sensor)69 static int st_syscfg_alloc_regfields(struct st_thermal_sensor *sensor)
70 {
71 	struct device *dev = sensor->dev;
72 
73 	sensor->pwr = devm_regmap_field_alloc(dev, sensor->regmap,
74 					sensor->cdata->reg_fields[TEMP_PWR]);
75 
76 	if (IS_ERR(sensor->pwr)) {
77 		dev_err(dev, "failed to alloc syscfg regfields\n");
78 		return PTR_ERR(sensor->pwr);
79 	}
80 
81 	return 0;
82 }
83 
st_syscfg_regmap_init(struct st_thermal_sensor * sensor)84 static int st_syscfg_regmap_init(struct st_thermal_sensor *sensor)
85 {
86 	sensor->regmap =
87 		syscon_regmap_lookup_by_compatible(sensor->cdata->sys_compat);
88 	if (IS_ERR(sensor->regmap)) {
89 		dev_err(sensor->dev, "failed to find syscfg regmap\n");
90 		return PTR_ERR(sensor->regmap);
91 	}
92 
93 	return 0;
94 }
95 
96 static const struct st_thermal_sensor_ops st_syscfg_sensor_ops = {
97 	.power_ctrl		= st_syscfg_power_ctrl,
98 	.alloc_regfields	= st_syscfg_alloc_regfields,
99 	.regmap_init		= st_syscfg_regmap_init,
100 };
101 
102 /* Compatible device data for stih415 sas thermal sensor */
103 static const struct st_thermal_compat_data st_415sas_cdata = {
104 	.sys_compat		= "st,stih415-front-syscfg",
105 	.reg_fields		= st_415sas_regfields,
106 	.ops			= &st_syscfg_sensor_ops,
107 	.calibration_val	= 16,
108 	.temp_adjust_val	= 20,
109 	.crit_temp		= 120,
110 };
111 
112 /* Compatible device data for stih415 mpe thermal sensor */
113 static const struct st_thermal_compat_data st_415mpe_cdata = {
114 	.sys_compat		= "st,stih415-system-syscfg",
115 	.reg_fields		= st_415mpe_regfields,
116 	.ops			= &st_syscfg_sensor_ops,
117 	.calibration_val	= 16,
118 	.temp_adjust_val	= -103,
119 	.crit_temp		= 120,
120 };
121 
122 /* Compatible device data for stih416 sas thermal sensor */
123 static const struct st_thermal_compat_data st_416sas_cdata = {
124 	.sys_compat		= "st,stih416-front-syscfg",
125 	.reg_fields		= st_416sas_regfields,
126 	.ops			= &st_syscfg_sensor_ops,
127 	.calibration_val	= 16,
128 	.temp_adjust_val	= 20,
129 	.crit_temp		= 120,
130 };
131 
132 /* Compatible device data for stid127 thermal sensor */
133 static const struct st_thermal_compat_data st_127_cdata = {
134 	.sys_compat		= "st,stid127-cpu-syscfg",
135 	.reg_fields		= st_127_regfields,
136 	.ops			= &st_syscfg_sensor_ops,
137 	.calibration_val	= 8,
138 	.temp_adjust_val	= -103,
139 	.crit_temp		= 120,
140 };
141 
142 static const struct of_device_id st_syscfg_thermal_of_match[] = {
143 	{ .compatible = "st,stih415-sas-thermal", .data = &st_415sas_cdata },
144 	{ .compatible = "st,stih415-mpe-thermal", .data = &st_415mpe_cdata },
145 	{ .compatible = "st,stih416-sas-thermal", .data = &st_416sas_cdata },
146 	{ .compatible = "st,stid127-thermal",     .data = &st_127_cdata },
147 	{ /* sentinel */ }
148 };
149 MODULE_DEVICE_TABLE(of, st_syscfg_thermal_of_match);
150 
st_syscfg_probe(struct platform_device * pdev)151 static int st_syscfg_probe(struct platform_device *pdev)
152 {
153 	return st_thermal_register(pdev, st_syscfg_thermal_of_match);
154 }
155 
st_syscfg_remove(struct platform_device * pdev)156 static int st_syscfg_remove(struct platform_device *pdev)
157 {
158 	return st_thermal_unregister(pdev);
159 }
160 
161 static struct platform_driver st_syscfg_thermal_driver = {
162 	.driver = {
163 		.name	= "st_syscfg_thermal",
164 		.pm     = &st_thermal_pm_ops,
165 		.of_match_table =  st_syscfg_thermal_of_match,
166 	},
167 	.probe		= st_syscfg_probe,
168 	.remove		= st_syscfg_remove,
169 };
170 module_platform_driver(st_syscfg_thermal_driver);
171 
172 MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
173 MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
174 MODULE_LICENSE("GPL v2");
175