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