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