blob: e1f22a3ceb991b29485ca1eda6a3c616b39d56a0 [file] [log] [blame]
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +02001/*
David Collins85c416b2017-05-03 14:39:40 -07002 * Copyright (c) 2011-2015, 2017, The Linux Foundation. All rights reserved.
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +02003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
David Collins85c416b2017-05-03 14:39:40 -070014#include <linux/bitops.h>
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020015#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/iio/consumer.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/platform_device.h>
23#include <linux/regmap.h>
24#include <linux/thermal.h>
David Collins01409b02017-05-15 15:33:46 -070025#include "thermal_core.h"
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020026
27#define QPNP_TM_REG_TYPE 0x04
28#define QPNP_TM_REG_SUBTYPE 0x05
29#define QPNP_TM_REG_STATUS 0x08
30#define QPNP_TM_REG_SHUTDOWN_CTRL1 0x40
31#define QPNP_TM_REG_ALARM_CTRL 0x46
32
33#define QPNP_TM_TYPE 0x09
David Collins85c416b2017-05-03 14:39:40 -070034#define QPNP_TM_SUBTYPE_GEN1 0x08
35#define QPNP_TM_SUBTYPE_GEN2 0x09
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020036
David Collins85c416b2017-05-03 14:39:40 -070037#define STATUS_GEN1_STAGE_MASK GENMASK(1, 0)
38#define STATUS_GEN2_STATE_MASK GENMASK(6, 4)
39#define STATUS_GEN2_STATE_SHIFT 4
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020040
David Collins85c416b2017-05-03 14:39:40 -070041#define SHUTDOWN_CTRL1_OVERRIDE_MASK GENMASK(7, 6)
42#define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020043
David Collins85c416b2017-05-03 14:39:40 -070044#define ALARM_CTRL_FORCE_ENABLE BIT(7)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020045
46/*
47 * Trip point values based on threshold control
48 * 0 = {105 C, 125 C, 145 C}
49 * 1 = {110 C, 130 C, 150 C}
50 * 2 = {115 C, 135 C, 155 C}
51 * 3 = {120 C, 140 C, 160 C}
52*/
53#define TEMP_STAGE_STEP 20000 /* Stage step: 20.000 C */
54#define TEMP_STAGE_HYSTERESIS 2000
55
56#define TEMP_THRESH_MIN 105000 /* Threshold Min: 105 C */
57#define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
58
59#define THRESH_MIN 0
60
61/* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
62#define DEFAULT_TEMP 37000
63
64struct qpnp_tm_chip {
65 struct regmap *map;
66 struct thermal_zone_device *tz_dev;
David Collins85c416b2017-05-03 14:39:40 -070067 unsigned int subtype;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020068 long temp;
69 unsigned int thresh;
70 unsigned int stage;
71 unsigned int prev_stage;
72 unsigned int base;
73 struct iio_channel *adc;
74};
75
David Collins85c416b2017-05-03 14:39:40 -070076/* This array maps from GEN2 alarm state to GEN1 alarm stage */
77static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
78
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020079static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
80{
81 unsigned int val;
82 int ret;
83
84 ret = regmap_read(chip->map, chip->base + addr, &val);
85 if (ret < 0)
86 return ret;
87
88 *data = val;
89 return 0;
90}
91
92static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
93{
94 return regmap_write(chip->map, chip->base + addr, data);
95}
96
David Collins85c416b2017-05-03 14:39:40 -070097/**
98 * qpnp_tm_get_temp_stage() - return over-temperature stage
99 * @chip: Pointer to the qpnp_tm chip
100 *
101 * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200102 */
David Collins85c416b2017-05-03 14:39:40 -0700103static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200104{
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200105 int ret;
106 u8 reg = 0;
107
108 ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
109 if (ret < 0)
110 return ret;
111
David Collins85c416b2017-05-03 14:39:40 -0700112 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
113 ret = reg & STATUS_GEN1_STAGE_MASK;
114 else
115 ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200116
David Collins85c416b2017-05-03 14:39:40 -0700117 return ret;
118}
119
120/*
121 * This function updates the internal temp value based on the
122 * current thermal stage and threshold as well as the previous stage
123 */
124static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
125{
126 unsigned int stage, stage_new, stage_old;
127 int ret;
128
129 ret = qpnp_tm_get_temp_stage(chip);
130 if (ret < 0)
131 return ret;
132 stage = ret;
133
134 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
135 stage_new = stage;
136 stage_old = chip->stage;
137 } else {
138 stage_new = alarm_state_map[stage];
139 stage_old = alarm_state_map[chip->stage];
140 }
141
142 if (stage_new > stage_old) {
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200143 /* increasing stage, use lower bound */
David Collins85c416b2017-05-03 14:39:40 -0700144 chip->temp = (stage_new - 1) * TEMP_STAGE_STEP +
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200145 chip->thresh * TEMP_THRESH_STEP +
146 TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
David Collins85c416b2017-05-03 14:39:40 -0700147 } else if (stage_new < stage_old) {
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200148 /* decreasing stage, use upper bound */
David Collins85c416b2017-05-03 14:39:40 -0700149 chip->temp = stage_new * TEMP_STAGE_STEP +
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200150 chip->thresh * TEMP_THRESH_STEP -
151 TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
152 }
153
154 chip->stage = stage;
155
156 return 0;
157}
158
Sascha Hauer17e83512015-07-24 08:12:54 +0200159static int qpnp_tm_get_temp(void *data, int *temp)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200160{
161 struct qpnp_tm_chip *chip = data;
162 int ret, mili_celsius;
163
164 if (!temp)
165 return -EINVAL;
166
167 if (IS_ERR(chip->adc)) {
168 ret = qpnp_tm_update_temp_no_adc(chip);
169 if (ret < 0)
170 return ret;
171 } else {
172 ret = iio_read_channel_processed(chip->adc, &mili_celsius);
173 if (ret < 0)
174 return ret;
175
176 chip->temp = mili_celsius;
177 }
178
179 *temp = chip->temp < 0 ? 0 : chip->temp;
180
181 return 0;
182}
183
184static const struct thermal_zone_of_device_ops qpnp_tm_sensor_ops = {
185 .get_temp = qpnp_tm_get_temp,
186};
187
188static irqreturn_t qpnp_tm_isr(int irq, void *data)
189{
190 struct qpnp_tm_chip *chip = data;
191
David Collins01409b02017-05-15 15:33:46 -0700192 of_thermal_handle_trip(chip->tz_dev);
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200193
194 return IRQ_HANDLED;
195}
196
197/*
198 * This function initializes the internal temp value based on only the
199 * current thermal stage and threshold. Setup threshold control and
200 * disable shutdown override.
201 */
202static int qpnp_tm_init(struct qpnp_tm_chip *chip)
203{
David Collins85c416b2017-05-03 14:39:40 -0700204 unsigned int stage;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200205 int ret;
David Collins85c416b2017-05-03 14:39:40 -0700206 u8 reg = 0;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200207
David Collins85c416b2017-05-03 14:39:40 -0700208 ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg);
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200209 if (ret < 0)
210 return ret;
211
David Collins85c416b2017-05-03 14:39:40 -0700212 chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
213 chip->temp = DEFAULT_TEMP;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200214
David Collins85c416b2017-05-03 14:39:40 -0700215 ret = qpnp_tm_get_temp_stage(chip);
216 if (ret < 0)
217 return ret;
218 chip->stage = ret;
219
220 stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
221 ? chip->stage : alarm_state_map[chip->stage];
222
223 if (stage)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200224 chip->temp = chip->thresh * TEMP_THRESH_STEP +
David Collins85c416b2017-05-03 14:39:40 -0700225 (stage - 1) * TEMP_STAGE_STEP +
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200226 TEMP_THRESH_MIN;
227
228 /*
229 * Set threshold and disable software override of stage 2 and 3
230 * shutdowns.
231 */
David Collins85c416b2017-05-03 14:39:40 -0700232 chip->thresh = THRESH_MIN;
233 reg &= ~(SHUTDOWN_CTRL1_OVERRIDE_MASK | SHUTDOWN_CTRL1_THRESHOLD_MASK);
234 reg |= chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200235 ret = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
236 if (ret < 0)
237 return ret;
238
239 /* Enable the thermal alarm PMIC module in always-on mode. */
240 reg = ALARM_CTRL_FORCE_ENABLE;
241 ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
242
243 return ret;
244}
245
246static int qpnp_tm_probe(struct platform_device *pdev)
247{
248 struct qpnp_tm_chip *chip;
249 struct device_node *node;
250 u8 type, subtype;
251 u32 res[2];
252 int ret, irq;
253
254 node = pdev->dev.of_node;
255
256 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
257 if (!chip)
258 return -ENOMEM;
259
260 dev_set_drvdata(&pdev->dev, chip);
261
262 chip->map = dev_get_regmap(pdev->dev.parent, NULL);
263 if (!chip->map)
264 return -ENXIO;
265
266 ret = of_property_read_u32_array(node, "reg", res, 2);
267 if (ret < 0)
268 return ret;
269
270 irq = platform_get_irq(pdev, 0);
271 if (irq < 0)
272 return irq;
273
274 /* ADC based measurements are optional */
275 chip->adc = iio_channel_get(&pdev->dev, "thermal");
276 if (PTR_ERR(chip->adc) == -EPROBE_DEFER)
277 return PTR_ERR(chip->adc);
278
279 chip->base = res[0];
280
281 ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
282 if (ret < 0) {
283 dev_err(&pdev->dev, "could not read type\n");
284 goto fail;
285 }
286
287 ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
288 if (ret < 0) {
289 dev_err(&pdev->dev, "could not read subtype\n");
290 goto fail;
291 }
292
David Collins85c416b2017-05-03 14:39:40 -0700293 if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
294 && subtype != QPNP_TM_SUBTYPE_GEN2)) {
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200295 dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
296 type, subtype);
297 ret = -ENODEV;
298 goto fail;
299 }
300
David Collins85c416b2017-05-03 14:39:40 -0700301 chip->subtype = subtype;
302
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200303 ret = qpnp_tm_init(chip);
304 if (ret < 0) {
305 dev_err(&pdev->dev, "init failed\n");
306 goto fail;
307 }
308
309 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
310 IRQF_ONESHOT, node->name, chip);
311 if (ret < 0)
312 goto fail;
313
Eduardo Valentine9364912016-03-09 13:08:48 -0800314 chip->tz_dev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, chip,
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200315 &qpnp_tm_sensor_ops);
316 if (IS_ERR(chip->tz_dev)) {
317 dev_err(&pdev->dev, "failed to register sensor\n");
318 ret = PTR_ERR(chip->tz_dev);
319 goto fail;
320 }
321
322 return 0;
323
324fail:
325 if (!IS_ERR(chip->adc))
326 iio_channel_release(chip->adc);
327
328 return ret;
329}
330
331static int qpnp_tm_remove(struct platform_device *pdev)
332{
333 struct qpnp_tm_chip *chip = dev_get_drvdata(&pdev->dev);
334
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200335 if (!IS_ERR(chip->adc))
336 iio_channel_release(chip->adc);
337
338 return 0;
339}
340
341static const struct of_device_id qpnp_tm_match_table[] = {
342 { .compatible = "qcom,spmi-temp-alarm" },
343 { }
344};
345MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
346
347static struct platform_driver qpnp_tm_driver = {
348 .driver = {
349 .name = "spmi-temp-alarm",
350 .of_match_table = qpnp_tm_match_table,
351 },
352 .probe = qpnp_tm_probe,
353 .remove = qpnp_tm_remove,
354};
355module_platform_driver(qpnp_tm_driver);
356
357MODULE_ALIAS("platform:spmi-temp-alarm");
358MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
359MODULE_LICENSE("GPL v2");