blob: 57cdd49d570280c323492b07d03e747389e5690b [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>
25
26#define QPNP_TM_REG_TYPE 0x04
27#define QPNP_TM_REG_SUBTYPE 0x05
28#define QPNP_TM_REG_STATUS 0x08
29#define QPNP_TM_REG_SHUTDOWN_CTRL1 0x40
30#define QPNP_TM_REG_ALARM_CTRL 0x46
31
32#define QPNP_TM_TYPE 0x09
David Collins85c416b2017-05-03 14:39:40 -070033#define QPNP_TM_SUBTYPE_GEN1 0x08
34#define QPNP_TM_SUBTYPE_GEN2 0x09
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020035
David Collins85c416b2017-05-03 14:39:40 -070036#define STATUS_GEN1_STAGE_MASK GENMASK(1, 0)
37#define STATUS_GEN2_STATE_MASK GENMASK(6, 4)
38#define STATUS_GEN2_STATE_SHIFT 4
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020039
David Collins85c416b2017-05-03 14:39:40 -070040#define SHUTDOWN_CTRL1_OVERRIDE_MASK GENMASK(7, 6)
41#define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020042
David Collins85c416b2017-05-03 14:39:40 -070043#define ALARM_CTRL_FORCE_ENABLE BIT(7)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020044
45/*
46 * Trip point values based on threshold control
47 * 0 = {105 C, 125 C, 145 C}
48 * 1 = {110 C, 130 C, 150 C}
49 * 2 = {115 C, 135 C, 155 C}
50 * 3 = {120 C, 140 C, 160 C}
51*/
52#define TEMP_STAGE_STEP 20000 /* Stage step: 20.000 C */
53#define TEMP_STAGE_HYSTERESIS 2000
54
55#define TEMP_THRESH_MIN 105000 /* Threshold Min: 105 C */
56#define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
57
58#define THRESH_MIN 0
59
60/* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
61#define DEFAULT_TEMP 37000
62
63struct qpnp_tm_chip {
64 struct regmap *map;
65 struct thermal_zone_device *tz_dev;
David Collins85c416b2017-05-03 14:39:40 -070066 unsigned int subtype;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020067 long temp;
68 unsigned int thresh;
69 unsigned int stage;
70 unsigned int prev_stage;
71 unsigned int base;
72 struct iio_channel *adc;
73};
74
David Collins85c416b2017-05-03 14:39:40 -070075/* This array maps from GEN2 alarm state to GEN1 alarm stage */
76static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
77
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +020078static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
79{
80 unsigned int val;
81 int ret;
82
83 ret = regmap_read(chip->map, chip->base + addr, &val);
84 if (ret < 0)
85 return ret;
86
87 *data = val;
88 return 0;
89}
90
91static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
92{
93 return regmap_write(chip->map, chip->base + addr, data);
94}
95
David Collins85c416b2017-05-03 14:39:40 -070096/**
97 * qpnp_tm_get_temp_stage() - return over-temperature stage
98 * @chip: Pointer to the qpnp_tm chip
99 *
100 * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200101 */
David Collins85c416b2017-05-03 14:39:40 -0700102static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200103{
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200104 int ret;
105 u8 reg = 0;
106
107 ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
108 if (ret < 0)
109 return ret;
110
David Collins85c416b2017-05-03 14:39:40 -0700111 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
112 ret = reg & STATUS_GEN1_STAGE_MASK;
113 else
114 ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200115
David Collins85c416b2017-05-03 14:39:40 -0700116 return ret;
117}
118
119/*
120 * This function updates the internal temp value based on the
121 * current thermal stage and threshold as well as the previous stage
122 */
123static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
124{
125 unsigned int stage, stage_new, stage_old;
126 int ret;
127
128 ret = qpnp_tm_get_temp_stage(chip);
129 if (ret < 0)
130 return ret;
131 stage = ret;
132
133 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
134 stage_new = stage;
135 stage_old = chip->stage;
136 } else {
137 stage_new = alarm_state_map[stage];
138 stage_old = alarm_state_map[chip->stage];
139 }
140
141 if (stage_new > stage_old) {
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200142 /* increasing stage, use lower bound */
David Collins85c416b2017-05-03 14:39:40 -0700143 chip->temp = (stage_new - 1) * TEMP_STAGE_STEP +
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200144 chip->thresh * TEMP_THRESH_STEP +
145 TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
David Collins85c416b2017-05-03 14:39:40 -0700146 } else if (stage_new < stage_old) {
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200147 /* decreasing stage, use upper bound */
David Collins85c416b2017-05-03 14:39:40 -0700148 chip->temp = stage_new * TEMP_STAGE_STEP +
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200149 chip->thresh * TEMP_THRESH_STEP -
150 TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
151 }
152
153 chip->stage = stage;
154
155 return 0;
156}
157
Sascha Hauer17e83512015-07-24 08:12:54 +0200158static int qpnp_tm_get_temp(void *data, int *temp)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200159{
160 struct qpnp_tm_chip *chip = data;
161 int ret, mili_celsius;
162
163 if (!temp)
164 return -EINVAL;
165
166 if (IS_ERR(chip->adc)) {
167 ret = qpnp_tm_update_temp_no_adc(chip);
168 if (ret < 0)
169 return ret;
170 } else {
171 ret = iio_read_channel_processed(chip->adc, &mili_celsius);
172 if (ret < 0)
173 return ret;
174
175 chip->temp = mili_celsius;
176 }
177
178 *temp = chip->temp < 0 ? 0 : chip->temp;
179
180 return 0;
181}
182
183static const struct thermal_zone_of_device_ops qpnp_tm_sensor_ops = {
184 .get_temp = qpnp_tm_get_temp,
185};
186
187static irqreturn_t qpnp_tm_isr(int irq, void *data)
188{
189 struct qpnp_tm_chip *chip = data;
190
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700191 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200192
193 return IRQ_HANDLED;
194}
195
196/*
197 * This function initializes the internal temp value based on only the
198 * current thermal stage and threshold. Setup threshold control and
199 * disable shutdown override.
200 */
201static int qpnp_tm_init(struct qpnp_tm_chip *chip)
202{
David Collins85c416b2017-05-03 14:39:40 -0700203 unsigned int stage;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200204 int ret;
David Collins85c416b2017-05-03 14:39:40 -0700205 u8 reg = 0;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200206
David Collins85c416b2017-05-03 14:39:40 -0700207 ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg);
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200208 if (ret < 0)
209 return ret;
210
David Collins85c416b2017-05-03 14:39:40 -0700211 chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
212 chip->temp = DEFAULT_TEMP;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200213
David Collins85c416b2017-05-03 14:39:40 -0700214 ret = qpnp_tm_get_temp_stage(chip);
215 if (ret < 0)
216 return ret;
217 chip->stage = ret;
218
219 stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
220 ? chip->stage : alarm_state_map[chip->stage];
221
222 if (stage)
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200223 chip->temp = chip->thresh * TEMP_THRESH_STEP +
David Collins85c416b2017-05-03 14:39:40 -0700224 (stage - 1) * TEMP_STAGE_STEP +
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200225 TEMP_THRESH_MIN;
226
227 /*
228 * Set threshold and disable software override of stage 2 and 3
229 * shutdowns.
230 */
David Collins85c416b2017-05-03 14:39:40 -0700231 chip->thresh = THRESH_MIN;
232 reg &= ~(SHUTDOWN_CTRL1_OVERRIDE_MASK | SHUTDOWN_CTRL1_THRESHOLD_MASK);
233 reg |= chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK;
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200234 ret = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
235 if (ret < 0)
236 return ret;
237
238 /* Enable the thermal alarm PMIC module in always-on mode. */
239 reg = ALARM_CTRL_FORCE_ENABLE;
240 ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
241
242 return ret;
243}
244
245static int qpnp_tm_probe(struct platform_device *pdev)
246{
247 struct qpnp_tm_chip *chip;
248 struct device_node *node;
249 u8 type, subtype;
250 u32 res[2];
251 int ret, irq;
252
253 node = pdev->dev.of_node;
254
255 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
256 if (!chip)
257 return -ENOMEM;
258
259 dev_set_drvdata(&pdev->dev, chip);
260
261 chip->map = dev_get_regmap(pdev->dev.parent, NULL);
262 if (!chip->map)
263 return -ENXIO;
264
265 ret = of_property_read_u32_array(node, "reg", res, 2);
266 if (ret < 0)
267 return ret;
268
269 irq = platform_get_irq(pdev, 0);
270 if (irq < 0)
271 return irq;
272
273 /* ADC based measurements are optional */
274 chip->adc = iio_channel_get(&pdev->dev, "thermal");
275 if (PTR_ERR(chip->adc) == -EPROBE_DEFER)
276 return PTR_ERR(chip->adc);
277
278 chip->base = res[0];
279
280 ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
281 if (ret < 0) {
282 dev_err(&pdev->dev, "could not read type\n");
283 goto fail;
284 }
285
286 ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
287 if (ret < 0) {
288 dev_err(&pdev->dev, "could not read subtype\n");
289 goto fail;
290 }
291
David Collins85c416b2017-05-03 14:39:40 -0700292 if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
293 && subtype != QPNP_TM_SUBTYPE_GEN2)) {
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200294 dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
295 type, subtype);
296 ret = -ENODEV;
297 goto fail;
298 }
299
David Collins85c416b2017-05-03 14:39:40 -0700300 chip->subtype = subtype;
301
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200302 ret = qpnp_tm_init(chip);
303 if (ret < 0) {
304 dev_err(&pdev->dev, "init failed\n");
305 goto fail;
306 }
307
308 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
309 IRQF_ONESHOT, node->name, chip);
310 if (ret < 0)
311 goto fail;
312
Eduardo Valentine9364912016-03-09 13:08:48 -0800313 chip->tz_dev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, chip,
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200314 &qpnp_tm_sensor_ops);
315 if (IS_ERR(chip->tz_dev)) {
316 dev_err(&pdev->dev, "failed to register sensor\n");
317 ret = PTR_ERR(chip->tz_dev);
318 goto fail;
319 }
320
321 return 0;
322
323fail:
324 if (!IS_ERR(chip->adc))
325 iio_channel_release(chip->adc);
326
327 return ret;
328}
329
330static int qpnp_tm_remove(struct platform_device *pdev)
331{
332 struct qpnp_tm_chip *chip = dev_get_drvdata(&pdev->dev);
333
Ivan T. Ivanovc610afa2015-02-05 19:12:56 +0200334 if (!IS_ERR(chip->adc))
335 iio_channel_release(chip->adc);
336
337 return 0;
338}
339
340static const struct of_device_id qpnp_tm_match_table[] = {
341 { .compatible = "qcom,spmi-temp-alarm" },
342 { }
343};
344MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
345
346static struct platform_driver qpnp_tm_driver = {
347 .driver = {
348 .name = "spmi-temp-alarm",
349 .of_match_table = qpnp_tm_match_table,
350 },
351 .probe = qpnp_tm_probe,
352 .remove = qpnp_tm_remove,
353};
354module_platform_driver(qpnp_tm_driver);
355
356MODULE_ALIAS("platform:spmi-temp-alarm");
357MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
358MODULE_LICENSE("GPL v2");