blob: 5d345cceb7028504288656bd9d51ad37975049cf [file] [log] [blame]
Siddartha Mohanadosscb813fd2017-12-20 15:06:42 -08001/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/kernel.h>
17#include <linux/regmap.h>
18#include <linux/of.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/delay.h>
23#include <linux/mutex.h>
24#include <linux/types.h>
25#include <linux/hwmon.h>
26#include <linux/module.h>
27#include <linux/debugfs.h>
28#include <linux/spmi.h>
29#include <linux/platform_device.h>
30#include <linux/of_irq.h>
31#include <linux/interrupt.h>
32#include <linux/completion.h>
33#include <linux/hwmon-sysfs.h>
34#include <linux/qpnp/qpnp-adc.h>
35#include <linux/thermal.h>
36#include <linux/platform_device.h>
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -070037#include "thermal_core.h"
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -080038
39/* QPNP VADC TM register definition */
40#define QPNP_REVISION3 0x2
41#define QPNP_PERPH_SUBTYPE 0x5
42#define QPNP_PERPH_TYPE2 0x2
43#define QPNP_REVISION_EIGHT_CHANNEL_SUPPORT 2
44#define QPNP_PERPH_SUBTYPE_TWO_CHANNEL_SUPPORT 0x22
Prasad Malisetty358dda32018-02-20 06:14:26 +053045#define QPNP_STATUS1 0x8
46#define QPNP_STATUS1_OP_MODE 4
47#define QPNP_STATUS1_MEAS_INTERVAL_EN_STS BIT(2)
48#define QPNP_STATUS1_REQ_STS BIT(1)
49#define QPNP_STATUS1_EOC BIT(0)
50#define QPNP_STATUS2 0x9
51#define QPNP_STATUS2_CONV_SEQ_STATE 6
52#define QPNP_STATUS2_FIFO_NOT_EMPTY_FLAG BIT(1)
53#define QPNP_STATUS2_CONV_SEQ_TIMEOUT_STS BIT(0)
54#define QPNP_CONV_TIMEOUT_ERR 2
55
56#define QPNP_MODE_CTL 0x40
57#define QPNP_OP_MODE_SHIFT 3
58#define QPNP_VREF_XO_THM_FORCE BIT(2)
59#define QPNP_AMUX_TRIM_EN BIT(1)
60#define QPNP_ADC_TRIM_EN BIT(0)
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -080061#define QPNP_EN_CTL1 0x46
62#define QPNP_ADC_TM_EN BIT(7)
63#define QPNP_BTM_CONV_REQ 0x47
64#define QPNP_ADC_CONV_REQ_EN BIT(7)
65
Prasad Malisetty358dda32018-02-20 06:14:26 +053066#define QPNP_ADC_DIG_PARAM 0x50
67#define QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT 3
68#define QPNP_HW_SETTLE_DELAY 0x51
69#define QPNP_CONV_SEQ_CTL 0x54
70#define QPNP_CONV_SEQ_HOLDOFF_SHIFT 4
71#define QPNP_CONV_SEQ_TRIG_CTL 0x55
72#define QPNP_ADC_TM_MEAS_INTERVAL_CTL 0x57
73#define QPNP_ADC_TM_MEAS_INTERVAL_TIME_SHIFT 0x3
74#define QPNP_ADC_TM_MEAS_INTERVAL_CTL2 0x58
75#define QPNP_ADC_TM_MEAS_INTERVAL_CTL2_SHIFT 0x4
76#define QPNP_ADC_TM_MEAS_INTERVAL_CTL2_MASK 0xf0
77#define QPNP_ADC_TM_MEAS_INTERVAL_CTL3_MASK 0xf
78
79#define QPNP_ADC_MEAS_INTERVAL_OP_CTL 0x59
80#define QPNP_ADC_MEAS_INTERVAL_OP BIT(7)
81
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -070082#define QPNP_OP_MODE_SHIFT 3
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -080083#define QPNP_CONV_REQ 0x52
84#define QPNP_CONV_REQ_SET BIT(7)
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -080085
Prasad Malisetty358dda32018-02-20 06:14:26 +053086#define QPNP_FAST_AVG_CTL 0x5a
87#define QPNP_FAST_AVG_EN 0x5b
88#define QPNP_FAST_AVG_ENABLED BIT(7)
89
90#define QPNP_M0_LOW_THR_LSB 0x5c
91#define QPNP_M0_LOW_THR_MSB 0x5d
92#define QPNP_M0_HIGH_THR_LSB 0x5e
93#define QPNP_M0_HIGH_THR_MSB 0x5f
94#define QPNP_M1_ADC_CH_SEL_CTL 0x68
95#define QPNP_M1_LOW_THR_LSB 0x69
96#define QPNP_M1_LOW_THR_MSB 0x6a
97#define QPNP_M1_HIGH_THR_LSB 0x6b
98#define QPNP_M1_HIGH_THR_MSB 0x6c
99#define QPNP_M2_ADC_CH_SEL_CTL 0x70
100#define QPNP_M2_LOW_THR_LSB 0x71
101#define QPNP_M2_LOW_THR_MSB 0x72
102#define QPNP_M2_HIGH_THR_LSB 0x73
103#define QPNP_M2_HIGH_THR_MSB 0x74
104#define QPNP_M3_ADC_CH_SEL_CTL 0x78
105#define QPNP_M3_LOW_THR_LSB 0x79
106#define QPNP_M3_LOW_THR_MSB 0x7a
107#define QPNP_M3_HIGH_THR_LSB 0x7b
108#define QPNP_M3_HIGH_THR_MSB 0x7c
109#define QPNP_M4_ADC_CH_SEL_CTL 0x80
110#define QPNP_M4_LOW_THR_LSB 0x81
111#define QPNP_M4_LOW_THR_MSB 0x82
112#define QPNP_M4_HIGH_THR_LSB 0x83
113#define QPNP_M4_HIGH_THR_MSB 0x84
114#define QPNP_M5_ADC_CH_SEL_CTL 0x88
115#define QPNP_M5_LOW_THR_LSB 0x89
116#define QPNP_M5_LOW_THR_MSB 0x8a
117#define QPNP_M5_HIGH_THR_LSB 0x8b
118#define QPNP_M5_HIGH_THR_MSB 0x8c
119#define QPNP_M6_ADC_CH_SEL_CTL 0x90
120#define QPNP_M6_LOW_THR_LSB 0x91
121#define QPNP_M6_LOW_THR_MSB 0x92
122#define QPNP_M6_HIGH_THR_LSB 0x93
123#define QPNP_M6_HIGH_THR_MSB 0x94
124#define QPNP_M7_ADC_CH_SEL_CTL 0x98
125#define QPNP_M7_LOW_THR_LSB 0x99
126#define QPNP_M7_LOW_THR_MSB 0x9a
127#define QPNP_M7_HIGH_THR_LSB 0x9b
128#define QPNP_M7_HIGH_THR_MSB 0x9c
129
130#define QPNP_ADC_TM_MULTI_MEAS_EN 0x41
131#define QPNP_ADC_TM_MULTI_MEAS_EN_M0 BIT(0)
132#define QPNP_ADC_TM_MULTI_MEAS_EN_M1 BIT(1)
133#define QPNP_ADC_TM_MULTI_MEAS_EN_M2 BIT(2)
134#define QPNP_ADC_TM_MULTI_MEAS_EN_M3 BIT(3)
135#define QPNP_ADC_TM_MULTI_MEAS_EN_M4 BIT(4)
136#define QPNP_ADC_TM_MULTI_MEAS_EN_M5 BIT(5)
137#define QPNP_ADC_TM_MULTI_MEAS_EN_M6 BIT(6)
138#define QPNP_ADC_TM_MULTI_MEAS_EN_M7 BIT(7)
139#define QPNP_ADC_TM_LOW_THR_INT_EN 0x42
140#define QPNP_ADC_TM_LOW_THR_INT_EN_M0 BIT(0)
141#define QPNP_ADC_TM_LOW_THR_INT_EN_M1 BIT(1)
142#define QPNP_ADC_TM_LOW_THR_INT_EN_M2 BIT(2)
143#define QPNP_ADC_TM_LOW_THR_INT_EN_M3 BIT(3)
144#define QPNP_ADC_TM_LOW_THR_INT_EN_M4 BIT(4)
145#define QPNP_ADC_TM_LOW_THR_INT_EN_M5 BIT(5)
146#define QPNP_ADC_TM_LOW_THR_INT_EN_M6 BIT(6)
147#define QPNP_ADC_TM_LOW_THR_INT_EN_M7 BIT(7)
148#define QPNP_ADC_TM_HIGH_THR_INT_EN 0x43
149#define QPNP_ADC_TM_HIGH_THR_INT_EN_M0 BIT(0)
150#define QPNP_ADC_TM_HIGH_THR_INT_EN_M1 BIT(1)
151#define QPNP_ADC_TM_HIGH_THR_INT_EN_M2 BIT(2)
152#define QPNP_ADC_TM_HIGH_THR_INT_EN_M3 BIT(3)
153#define QPNP_ADC_TM_HIGH_THR_INT_EN_M4 BIT(4)
154#define QPNP_ADC_TM_HIGH_THR_INT_EN_M5 BIT(5)
155#define QPNP_ADC_TM_HIGH_THR_INT_EN_M6 BIT(6)
156#define QPNP_ADC_TM_HIGH_THR_INT_EN_M7 BIT(7)
157
158#define QPNP_ADC_TM_M0_MEAS_INTERVAL_CTL 0x59
159#define QPNP_ADC_TM_M1_MEAS_INTERVAL_CTL 0x6d
160#define QPNP_ADC_TM_M2_MEAS_INTERVAL_CTL 0x75
161#define QPNP_ADC_TM_M3_MEAS_INTERVAL_CTL 0x7d
162#define QPNP_ADC_TM_M4_MEAS_INTERVAL_CTL 0x85
163#define QPNP_ADC_TM_M5_MEAS_INTERVAL_CTL 0x8d
164#define QPNP_ADC_TM_M6_MEAS_INTERVAL_CTL 0x95
165#define QPNP_ADC_TM_M7_MEAS_INTERVAL_CTL 0x9d
166
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800167#define QPNP_ADC_TM_STATUS1 0x8
168#define QPNP_ADC_TM_STATUS_LOW 0xa
169#define QPNP_ADC_TM_STATUS_HIGH 0xb
170
171#define QPNP_ADC_TM_M0_LOW_THR 0x5d5c
172#define QPNP_ADC_TM_M0_HIGH_THR 0x5f5e
173#define QPNP_ADC_TM_MEAS_INTERVAL 0x0
174
175#define QPNP_ADC_TM_THR_LSB_MASK(val) (val & 0xff)
176#define QPNP_ADC_TM_THR_MSB_MASK(val) ((val & 0xff00) >> 8)
177
Prasad Malisetty358dda32018-02-20 06:14:26 +0530178#define QPNP_MIN_TIME 2000
179#define QPNP_MAX_TIME 2100
180#define QPNP_RETRY 1000
181
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800182/* QPNP ADC TM HC start */
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -0700183#define QPNP_BTM_HC_STATUS1 0x08
184#define QPNP_BTM_HC_STATUS_LOW 0x0a
185#define QPNP_BTM_HC_STATUS_HIGH 0x0b
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800186
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -0700187#define QPNP_BTM_HC_ADC_DIG_PARAM 0x42
188#define QPNP_BTM_HC_FAST_AVG_CTL 0x43
189#define QPNP_BTM_EN_CTL1 0x46
190#define QPNP_BTM_CONV_REQ 0x47
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800191
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -0700192#define QPNP_BTM_MEAS_INTERVAL_CTL 0x50
193#define QPNP_BTM_MEAS_INTERVAL_CTL2 0x51
Jishnu Prakashd09bc692018-05-02 10:54:28 +0530194#define QPNP_BTM_MEAS_INTERVAL_CTL_PM5 0x44
195#define QPNP_BTM_MEAS_INTERVAL_CTL2_PM5 0x45
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -0700196#define QPNP_ADC_TM_MEAS_INTERVAL_TIME_SHIFT 0x3
197#define QPNP_ADC_TM_MEAS_INTERVAL_CTL2_SHIFT 0x4
198#define QPNP_ADC_TM_MEAS_INTERVAL_CTL2_MASK 0xf0
199#define QPNP_ADC_TM_MEAS_INTERVAL_CTL3_MASK 0xf
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800200
201#define QPNP_BTM_Mn_ADC_CH_SEL_CTL(n) ((n * 8) + 0x60)
202#define QPNP_BTM_Mn_LOW_THR0(n) ((n * 8) + 0x61)
203#define QPNP_BTM_Mn_LOW_THR1(n) ((n * 8) + 0x62)
204#define QPNP_BTM_Mn_HIGH_THR0(n) ((n * 8) + 0x63)
205#define QPNP_BTM_Mn_HIGH_THR1(n) ((n * 8) + 0x64)
206#define QPNP_BTM_Mn_MEAS_INTERVAL_CTL(n) ((n * 8) + 0x65)
207#define QPNP_BTM_Mn_CTL(n) ((n * 8) + 0x66)
208#define QPNP_BTM_CTL_HW_SETTLE_DELAY_MASK 0xf
209#define QPNP_BTM_CTL_CAL_SEL 0x30
210#define QPNP_BTM_CTL_CAL_SEL_MASK_SHIFT 4
211#define QPNP_BTM_CTL_CAL_VAL 0x40
212
213#define QPNP_BTM_Mn_EN(n) ((n * 8) + 0x67)
214#define QPNP_BTM_Mn_MEAS_EN BIT(7)
215#define QPNP_BTM_Mn_HIGH_THR_INT_EN BIT(1)
216#define QPNP_BTM_Mn_LOW_THR_INT_EN BIT(0)
217
218#define QPNP_BTM_Mn_DATA0(n) ((n * 2) + 0xa0)
219#define QPNP_BTM_Mn_DATA1(n) ((n * 2) + 0xa1)
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -0700220#define QPNP_BTM_CHANNELS 8
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800221
222/* QPNP ADC TM HC end */
223
224struct qpnp_adc_thr_info {
225 u8 status_low;
226 u8 status_high;
227 u8 qpnp_adc_tm_meas_en;
228 u8 adc_tm_low_enable;
229 u8 adc_tm_high_enable;
230 u8 adc_tm_low_thr_set;
231 u8 adc_tm_high_thr_set;
Prasad Malisetty358dda32018-02-20 06:14:26 +0530232 spinlock_t adc_tm_low_lock;
233 spinlock_t adc_tm_high_lock;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800234};
235
236struct qpnp_adc_thr_client_info {
237 struct list_head list;
238 struct qpnp_adc_tm_btm_param *btm_param;
239 int32_t low_thr_requested;
240 int32_t high_thr_requested;
241 enum qpnp_state_request state_requested;
242 enum qpnp_state_request state_req_copy;
243 bool low_thr_set;
244 bool high_thr_set;
245 bool notify_low_thr;
246 bool notify_high_thr;
247};
248
249struct qpnp_adc_tm_sensor {
250 struct thermal_zone_device *tz_dev;
251 struct qpnp_adc_tm_chip *chip;
252 enum thermal_device_mode mode;
253 uint32_t sensor_num;
254 enum qpnp_adc_meas_timer_select timer_select;
255 uint32_t meas_interval;
256 uint32_t low_thr;
257 uint32_t high_thr;
258 uint32_t btm_channel_num;
259 uint32_t vadc_channel_num;
260 struct workqueue_struct *req_wq;
261 struct work_struct work;
262 bool thermal_node;
263 uint32_t scale_type;
264 struct list_head thr_list;
265 bool high_thr_triggered;
266 bool low_thr_triggered;
267};
268
269struct qpnp_adc_tm_chip {
270 struct device *dev;
271 struct qpnp_adc_drv *adc;
272 struct list_head list;
273 bool adc_tm_initialized;
274 bool adc_tm_recalib_check;
275 int max_channels_available;
276 atomic_t wq_cnt;
277 struct qpnp_vadc_chip *vadc_dev;
278 struct workqueue_struct *high_thr_wq;
279 struct workqueue_struct *low_thr_wq;
280 struct workqueue_struct *thr_wq;
281 struct work_struct trigger_high_thr_work;
282 struct work_struct trigger_low_thr_work;
283 struct work_struct trigger_thr_work;
284 bool adc_vote_enable;
285 struct qpnp_adc_thr_info th_info;
286 bool adc_tm_hc;
287 struct qpnp_adc_tm_sensor sensor[0];
288};
289
290LIST_HEAD(qpnp_adc_tm_device_list);
291
Prasad Malisetty358dda32018-02-20 06:14:26 +0530292struct qpnp_adc_tm_trip_reg_type {
293 enum qpnp_adc_tm_channel_select btm_amux_chan;
294 uint16_t low_thr_lsb_addr;
295 uint16_t low_thr_msb_addr;
296 uint16_t high_thr_lsb_addr;
297 uint16_t high_thr_msb_addr;
298 u8 multi_meas_en;
299 u8 low_thr_int_chan_en;
300 u8 high_thr_int_chan_en;
301 u8 meas_interval_ctl;
302};
303
304static struct qpnp_adc_tm_trip_reg_type adc_tm_data[] = {
305 [QPNP_ADC_TM_CHAN0] = {QPNP_ADC_TM_M0_ADC_CH_SEL_CTL,
306 QPNP_M0_LOW_THR_LSB,
307 QPNP_M0_LOW_THR_MSB, QPNP_M0_HIGH_THR_LSB,
308 QPNP_M0_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M0,
309 QPNP_ADC_TM_LOW_THR_INT_EN_M0, QPNP_ADC_TM_HIGH_THR_INT_EN_M0,
310 QPNP_ADC_TM_M0_MEAS_INTERVAL_CTL},
311 [QPNP_ADC_TM_CHAN1] = {QPNP_ADC_TM_M1_ADC_CH_SEL_CTL,
312 QPNP_M1_LOW_THR_LSB,
313 QPNP_M1_LOW_THR_MSB, QPNP_M1_HIGH_THR_LSB,
314 QPNP_M1_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M1,
315 QPNP_ADC_TM_LOW_THR_INT_EN_M1, QPNP_ADC_TM_HIGH_THR_INT_EN_M1,
316 QPNP_ADC_TM_M1_MEAS_INTERVAL_CTL},
317 [QPNP_ADC_TM_CHAN2] = {QPNP_ADC_TM_M2_ADC_CH_SEL_CTL,
318 QPNP_M2_LOW_THR_LSB,
319 QPNP_M2_LOW_THR_MSB, QPNP_M2_HIGH_THR_LSB,
320 QPNP_M2_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M2,
321 QPNP_ADC_TM_LOW_THR_INT_EN_M2, QPNP_ADC_TM_HIGH_THR_INT_EN_M2,
322 QPNP_ADC_TM_M2_MEAS_INTERVAL_CTL},
323 [QPNP_ADC_TM_CHAN3] = {QPNP_ADC_TM_M3_ADC_CH_SEL_CTL,
324 QPNP_M3_LOW_THR_LSB,
325 QPNP_M3_LOW_THR_MSB, QPNP_M3_HIGH_THR_LSB,
326 QPNP_M3_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M3,
327 QPNP_ADC_TM_LOW_THR_INT_EN_M3, QPNP_ADC_TM_HIGH_THR_INT_EN_M3,
328 QPNP_ADC_TM_M3_MEAS_INTERVAL_CTL},
329 [QPNP_ADC_TM_CHAN4] = {QPNP_ADC_TM_M4_ADC_CH_SEL_CTL,
330 QPNP_M4_LOW_THR_LSB,
331 QPNP_M4_LOW_THR_MSB, QPNP_M4_HIGH_THR_LSB,
332 QPNP_M4_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M4,
333 QPNP_ADC_TM_LOW_THR_INT_EN_M4, QPNP_ADC_TM_HIGH_THR_INT_EN_M4,
334 QPNP_ADC_TM_M4_MEAS_INTERVAL_CTL},
335 [QPNP_ADC_TM_CHAN5] = {QPNP_ADC_TM_M5_ADC_CH_SEL_CTL,
336 QPNP_M5_LOW_THR_LSB,
337 QPNP_M5_LOW_THR_MSB, QPNP_M5_HIGH_THR_LSB,
338 QPNP_M5_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M5,
339 QPNP_ADC_TM_LOW_THR_INT_EN_M5, QPNP_ADC_TM_HIGH_THR_INT_EN_M5,
340 QPNP_ADC_TM_M5_MEAS_INTERVAL_CTL},
341 [QPNP_ADC_TM_CHAN6] = {QPNP_ADC_TM_M6_ADC_CH_SEL_CTL,
342 QPNP_M6_LOW_THR_LSB,
343 QPNP_M6_LOW_THR_MSB, QPNP_M6_HIGH_THR_LSB,
344 QPNP_M6_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M6,
345 QPNP_ADC_TM_LOW_THR_INT_EN_M6, QPNP_ADC_TM_HIGH_THR_INT_EN_M6,
346 QPNP_ADC_TM_M6_MEAS_INTERVAL_CTL},
347 [QPNP_ADC_TM_CHAN7] = {QPNP_ADC_TM_M7_ADC_CH_SEL_CTL,
348 QPNP_M7_LOW_THR_LSB,
349 QPNP_M7_LOW_THR_MSB, QPNP_M7_HIGH_THR_LSB,
350 QPNP_M7_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M7,
351 QPNP_ADC_TM_LOW_THR_INT_EN_M7, QPNP_ADC_TM_HIGH_THR_INT_EN_M7,
352 QPNP_ADC_TM_M7_MEAS_INTERVAL_CTL},
353};
354
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800355static struct qpnp_adc_tm_reverse_scale_fn adc_tm_rscale_fn[] = {
356 [SCALE_R_VBATT] = {qpnp_adc_vbatt_rscaler},
357 [SCALE_RBATT_THERM] = {qpnp_adc_btm_scaler},
358 [SCALE_R_USB_ID] = {qpnp_adc_usb_scaler},
359 [SCALE_RPMIC_THERM] = {qpnp_adc_scale_millidegc_pmic_voltage_thr},
360 [SCALE_R_SMB_BATT_THERM] = {qpnp_adc_smb_btm_rscaler},
361 [SCALE_R_ABSOLUTE] = {qpnp_adc_absolute_rthr},
362 [SCALE_QRD_SKUH_RBATT_THERM] = {qpnp_adc_qrd_skuh_btm_scaler},
363 [SCALE_QRD_SKUT1_RBATT_THERM] = {qpnp_adc_qrd_skut1_btm_scaler},
364};
365
366static int32_t qpnp_adc_tm_read_reg(struct qpnp_adc_tm_chip *chip,
367 int16_t reg, u8 *data, int len)
368{
369 int rc = 0;
370
371 rc = regmap_bulk_read(chip->adc->regmap, (chip->adc->offset + reg),
372 data, len);
373 if (rc < 0)
374 pr_err("adc-tm read reg %d failed with %d\n", reg, rc);
375
376 return rc;
377}
378
379static int32_t qpnp_adc_tm_write_reg(struct qpnp_adc_tm_chip *chip,
380 int16_t reg, u8 data, int len)
381{
382 int rc = 0;
383 u8 *buf;
384
385 buf = &data;
386
387 rc = regmap_bulk_write(chip->adc->regmap, (chip->adc->offset + reg),
388 buf, len);
389 if (rc < 0)
390 pr_err("adc-tm write reg %d failed with %d\n", reg, rc);
391
392 return rc;
393}
394
Prasad Malisetty358dda32018-02-20 06:14:26 +0530395static int32_t qpnp_adc_tm_fast_avg_en(struct qpnp_adc_tm_chip *chip,
396 uint32_t *fast_avg_sample)
397{
398 int rc = 0, version = 0;
399 u8 fast_avg_en = 0;
400
401 version = qpnp_adc_get_revid_version(chip->dev);
402 if (!((version == QPNP_REV_ID_8916_1_0) ||
403 (version == QPNP_REV_ID_8916_1_1) ||
404 (version == QPNP_REV_ID_8916_2_0))) {
405 pr_debug("fast-avg-en not required for this version\n");
406 return rc;
407 }
408
409 fast_avg_en = QPNP_FAST_AVG_ENABLED;
410 rc = qpnp_adc_tm_write_reg(chip, QPNP_FAST_AVG_EN, fast_avg_en, 1);
411 if (rc < 0) {
412 pr_err("adc-tm fast-avg enable err\n");
413 return rc;
414 }
415
416 if (*fast_avg_sample >= 3)
417 *fast_avg_sample = 2;
418
419 return rc;
420}
421
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800422static int qpnp_adc_tm_check_vreg_vote(struct qpnp_adc_tm_chip *chip)
423{
424 int rc = 0;
425
426 if (!chip->adc_vote_enable) {
427 if (chip->adc->hkadc_ldo && chip->adc->hkadc_ldo_ok) {
428 rc = qpnp_adc_enable_voltage(chip->adc);
429 if (rc) {
430 pr_err("failed enabling VADC LDO\n");
431 return rc;
432 }
433 chip->adc_vote_enable = true;
434 }
435 }
436
437 return rc;
438}
439
440static int32_t qpnp_adc_tm_enable(struct qpnp_adc_tm_chip *chip)
441{
442 int rc = 0;
443 u8 data = 0;
444
445 rc = qpnp_adc_tm_check_vreg_vote(chip);
446 if (rc) {
447 pr_err("ADC TM VREG enable failed:%d\n", rc);
448 return rc;
449 }
450
451 data = QPNP_ADC_TM_EN;
452 rc = qpnp_adc_tm_write_reg(chip, QPNP_EN_CTL1, data, 1);
453 if (rc < 0) {
454 pr_err("adc-tm enable failed\n");
455 return rc;
456 }
457
Prasad Malisetty358dda32018-02-20 06:14:26 +0530458 if (chip->adc_tm_hc) {
459 data = QPNP_ADC_CONV_REQ_EN;
460 rc = qpnp_adc_tm_write_reg(chip, QPNP_BTM_CONV_REQ, data, 1);
461 if (rc < 0) {
462 pr_err("adc-tm enable failed\n");
463 return rc;
464 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800465 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800466 return rc;
467}
468
469static int32_t qpnp_adc_tm_disable(struct qpnp_adc_tm_chip *chip)
470{
Prasad Malisetty358dda32018-02-20 06:14:26 +0530471 u8 data = 0;
472 int rc = 0;
473
Rama Krishna Phani A66e48b42018-03-13 18:29:08 +0530474 if (!chip->adc_tm_hc) {
475 rc = qpnp_adc_tm_write_reg(chip, QPNP_EN_CTL1, data, 1);
Prasad Malisetty358dda32018-02-20 06:14:26 +0530476 if (rc < 0) {
Rama Krishna Phani A66e48b42018-03-13 18:29:08 +0530477 pr_err("adc-tm disable failed\n");
Prasad Malisetty358dda32018-02-20 06:14:26 +0530478 return rc;
479 }
480 }
481
Prasad Malisetty358dda32018-02-20 06:14:26 +0530482 return rc;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800483}
484
485static int qpnp_adc_tm_is_valid(struct qpnp_adc_tm_chip *chip)
486{
487 struct qpnp_adc_tm_chip *adc_tm_chip = NULL;
488
489 list_for_each_entry(adc_tm_chip, &qpnp_adc_tm_device_list, list)
490 if (chip == adc_tm_chip)
491 return 0;
492
493 return -EINVAL;
494}
495
496static int32_t qpnp_adc_tm_rc_check_channel_en(struct qpnp_adc_tm_chip *chip)
497{
498 u8 adc_tm_ctl = 0, status_low = 0, status_high = 0;
499 int rc = 0, i = 0;
500 bool ldo_en = false;
501
502 for (i = 0; i < chip->max_channels_available; i++) {
503 rc = qpnp_adc_tm_read_reg(chip, QPNP_BTM_Mn_CTL(i),
504 &adc_tm_ctl, 1);
505 if (rc) {
506 pr_err("adc-tm-tm read ctl failed with %d\n", rc);
507 return rc;
508 }
509
510 adc_tm_ctl &= QPNP_BTM_Mn_MEAS_EN;
511 status_low = adc_tm_ctl & QPNP_BTM_Mn_LOW_THR_INT_EN;
512 status_high = adc_tm_ctl & QPNP_BTM_Mn_HIGH_THR_INT_EN;
513
514 /* Enable only if there are pending measurement requests */
515 if ((adc_tm_ctl && status_high) ||
516 (adc_tm_ctl && status_low)) {
517 qpnp_adc_tm_enable(chip);
518 ldo_en = true;
519
520 /* Request conversion */
521 rc = qpnp_adc_tm_write_reg(chip, QPNP_CONV_REQ,
522 QPNP_CONV_REQ_SET, 1);
523 if (rc < 0) {
524 pr_err("adc-tm request conversion failed\n");
525 return rc;
526 }
527 }
528 break;
529 }
530
531 if (!ldo_en) {
532 /* disable the vote if applicable */
533 if (chip->adc_vote_enable && chip->adc->hkadc_ldo &&
534 chip->adc->hkadc_ldo_ok) {
535 qpnp_adc_disable_voltage(chip->adc);
536 chip->adc_vote_enable = false;
537 }
538 }
539
540 return rc;
541}
542
543static int32_t qpnp_adc_tm_enable_if_channel_meas(
544 struct qpnp_adc_tm_chip *chip)
545{
Prasad Malisetty358dda32018-02-20 06:14:26 +0530546 u8 adc_tm_meas_en = 0, status_low = 0, status_high = 0;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800547 int rc = 0;
548
Prasad Malisetty358dda32018-02-20 06:14:26 +0530549 if (chip->adc_tm_hc) {
550 rc = qpnp_adc_tm_rc_check_channel_en(chip);
551 if (rc) {
552 pr_err("adc_tm channel check failed\n");
553 return rc;
554 }
555 } else {
556 /* Check if a measurement request is still required */
557 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
558 &adc_tm_meas_en, 1);
559 if (rc) {
560 pr_err("read status high failed with %d\n", rc);
561 return rc;
562 }
563 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
564 &status_low, 1);
565 if (rc) {
566 pr_err("read status low failed with %d\n", rc);
567 return rc;
568 }
569
570 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_HIGH_THR_INT_EN,
571 &status_high, 1);
572 if (rc) {
573 pr_err("read status high failed with %d\n", rc);
574 return rc;
575 }
576
577 /* Enable only if there are pending measurement requests */
578 if ((adc_tm_meas_en && status_high) ||
579 (adc_tm_meas_en && status_low)) {
580 qpnp_adc_tm_enable(chip);
581 /* Request conversion */
582 rc = qpnp_adc_tm_write_reg(chip, QPNP_CONV_REQ,
583 QPNP_CONV_REQ_SET, 1);
584 if (rc < 0) {
585 pr_err("adc-tm request conversion failed\n");
586 return rc;
587 }
588 } else {
589 /* disable the vote if applicable */
590 if (chip->adc_vote_enable && chip->adc->hkadc_ldo &&
591 chip->adc->hkadc_ldo_ok) {
592 qpnp_adc_disable_voltage(chip->adc);
593 chip->adc_vote_enable = false;
594 }
595 }
596 }
597 return rc;
598}
599
600static int32_t qpnp_adc_tm_mode_select(struct qpnp_adc_tm_chip *chip,
601 u8 mode_ctl)
602{
603 int rc;
604
605 mode_ctl |= (QPNP_ADC_TRIM_EN | QPNP_AMUX_TRIM_EN);
606
607 /* VADC_BTM current sets mode to recurring measurements */
608 rc = qpnp_adc_tm_write_reg(chip, QPNP_MODE_CTL, mode_ctl, 1);
609 if (rc < 0)
610 pr_err("adc-tm write mode selection err\n");
611
612 return rc;
613}
614
615static int32_t qpnp_adc_tm_req_sts_check(struct qpnp_adc_tm_chip *chip)
616{
617 u8 status1 = 0, mode_ctl = 0;
618 int rc, count = 0;
619
620 /* Re-enable the peripheral */
621 rc = qpnp_adc_tm_enable(chip);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800622 if (rc) {
Prasad Malisetty358dda32018-02-20 06:14:26 +0530623 pr_err("adc-tm re-enable peripheral failed\n");
624 return rc;
625 }
626
627 /* The VADC_TM bank needs to be disabled for new conversion request */
628 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS1, &status1, 1);
629 if (rc) {
630 pr_err("adc-tm read status1 failed\n");
631 return rc;
632 }
633
634 /* Disable the bank if a conversion is occurring */
635 while (status1 & QPNP_STATUS1_REQ_STS) {
636 if (count > QPNP_RETRY) {
637 pr_err("retry error=%d with 0x%x\n", count, status1);
638 break;
639 }
640 /*
641 * Wait time is based on the optimum sampling rate
642 * and adding enough time buffer to account for ADC conversions
643 * occurring on different peripheral banks
644 */
645 usleep_range(QPNP_MIN_TIME, QPNP_MAX_TIME);
646 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS1,
647 &status1, 1);
648 if (rc < 0) {
649 pr_err("adc-tm disable failed\n");
650 return rc;
651 }
652 count++;
653 }
654
655 if (!chip->adc_tm_hc) {
656 /* Change the mode back to recurring measurement mode */
657 mode_ctl = ADC_OP_MEASUREMENT_INTERVAL << QPNP_OP_MODE_SHIFT;
658 rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
659 if (rc < 0) {
660 pr_err("adc-tm mode change to recurring failed\n");
661 return rc;
662 }
663 }
664
665 /* Disable the peripheral */
666 rc = qpnp_adc_tm_disable(chip);
667 if (rc < 0) {
668 pr_err("adc-tm peripheral disable failed\n");
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800669 return rc;
670 }
671
672 return rc;
673}
674
675static int32_t qpnp_adc_tm_get_btm_idx(struct qpnp_adc_tm_chip *chip,
676 uint32_t btm_chan, uint32_t *btm_chan_idx)
677{
678 int rc = 0, i;
679 bool chan_found = false;
680
Prasad Malisetty358dda32018-02-20 06:14:26 +0530681 if (!chip->adc_tm_hc) {
682 for (i = 0; i < QPNP_ADC_TM_CHAN_NONE; i++) {
683 if (adc_tm_data[i].btm_amux_chan == btm_chan) {
684 *btm_chan_idx = i;
685 chan_found = true;
686 }
687 }
688 } else {
689 for (i = 0; i < chip->max_channels_available; i++) {
690 if (chip->sensor[i].btm_channel_num == btm_chan) {
691 *btm_chan_idx = i;
692 chan_found = true;
693 break;
694 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800695 }
696 }
697
698 if (!chan_found)
699 return -EINVAL;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800700 return rc;
701}
702
703static int32_t qpnp_adc_tm_check_revision(struct qpnp_adc_tm_chip *chip,
704 uint32_t btm_chan_num)
705{
706 u8 rev, perph_subtype;
707 int rc = 0;
708
709 rc = qpnp_adc_tm_read_reg(chip, QPNP_REVISION3, &rev, 1);
710 if (rc) {
711 pr_err("adc-tm revision read failed\n");
712 return rc;
713 }
714
715 rc = qpnp_adc_tm_read_reg(chip, QPNP_PERPH_SUBTYPE, &perph_subtype, 1);
716 if (rc) {
717 pr_err("adc-tm perph_subtype read failed\n");
718 return rc;
719 }
720
721 if (perph_subtype == QPNP_PERPH_TYPE2) {
722 if ((rev < QPNP_REVISION_EIGHT_CHANNEL_SUPPORT) &&
723 (btm_chan_num > QPNP_ADC_TM_M4_ADC_CH_SEL_CTL)) {
724 pr_debug("Version does not support more than 5 channels\n");
725 return -EINVAL;
726 }
727 }
728
729 if (perph_subtype == QPNP_PERPH_SUBTYPE_TWO_CHANNEL_SUPPORT) {
730 if (btm_chan_num > QPNP_ADC_TM_M1_ADC_CH_SEL_CTL) {
731 pr_debug("Version does not support more than 2 channels\n");
732 return -EINVAL;
733 }
734 }
735
736 return rc;
737}
738
739static int32_t qpnp_adc_tm_timer_interval_select(
740 struct qpnp_adc_tm_chip *chip, uint32_t btm_chan,
741 struct qpnp_vadc_chan_properties *chan_prop)
742{
743 int rc, chan_idx = 0, i = 0;
744 bool chan_found = false;
745 u8 meas_interval_timer2 = 0, timer_interval_store = 0;
746 uint32_t btm_chan_idx = 0;
Jishnu Prakashd09bc692018-05-02 10:54:28 +0530747 bool is_pmic_5 = chip->adc->adc_prop->is_pmic_5;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800748
749 while (i < chip->max_channels_available) {
750 if (chip->sensor[i].btm_channel_num == btm_chan) {
751 chan_idx = i;
752 chan_found = true;
753 i++;
754 } else
755 i++;
756 }
757
758 if (!chan_found) {
759 pr_err("Channel not found\n");
760 return -EINVAL;
761 }
762
763 switch (chip->sensor[chan_idx].timer_select) {
764 case ADC_MEAS_TIMER_SELECT1:
Prasad Malisetty358dda32018-02-20 06:14:26 +0530765 if (!chip->adc_tm_hc)
766 rc = qpnp_adc_tm_write_reg(chip,
767 QPNP_ADC_TM_MEAS_INTERVAL_CTL,
768 chip->sensor[chan_idx].meas_interval, 1);
Jishnu Prakashd09bc692018-05-02 10:54:28 +0530769 else {
770 if (!is_pmic_5)
771 rc = qpnp_adc_tm_write_reg(chip,
772 QPNP_BTM_MEAS_INTERVAL_CTL,
773 chip->sensor[chan_idx].meas_interval,
774 1);
775 else
776 rc = qpnp_adc_tm_write_reg(chip,
777 QPNP_BTM_MEAS_INTERVAL_CTL_PM5,
778 chip->sensor[chan_idx].meas_interval,
779 1);
780 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800781 if (rc < 0) {
782 pr_err("timer1 configure failed\n");
783 return rc;
784 }
Prasad Malisetty358dda32018-02-20 06:14:26 +0530785 break;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800786 case ADC_MEAS_TIMER_SELECT2:
787 /* Thermal channels uses timer2, default to 1 second */
Prasad Malisetty358dda32018-02-20 06:14:26 +0530788 if (!chip->adc_tm_hc)
789 rc = qpnp_adc_tm_read_reg(chip,
790 QPNP_ADC_TM_MEAS_INTERVAL_CTL2,
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800791 &meas_interval_timer2, 1);
Jishnu Prakashd09bc692018-05-02 10:54:28 +0530792 else {
793 if (!is_pmic_5)
794 rc = qpnp_adc_tm_read_reg(chip,
Prasad Malisetty358dda32018-02-20 06:14:26 +0530795 QPNP_BTM_MEAS_INTERVAL_CTL2,
796 &meas_interval_timer2, 1);
Jishnu Prakashd09bc692018-05-02 10:54:28 +0530797 else
798 rc = qpnp_adc_tm_read_reg(chip,
799 QPNP_BTM_MEAS_INTERVAL_CTL2_PM5,
800 &meas_interval_timer2, 1);
801 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800802 if (rc < 0) {
803 pr_err("timer2 configure read failed\n");
804 return rc;
805 }
806 timer_interval_store = chip->sensor[chan_idx].meas_interval;
807 timer_interval_store <<= QPNP_ADC_TM_MEAS_INTERVAL_CTL2_SHIFT;
808 timer_interval_store &= QPNP_ADC_TM_MEAS_INTERVAL_CTL2_MASK;
809 meas_interval_timer2 |= timer_interval_store;
Prasad Malisetty358dda32018-02-20 06:14:26 +0530810 if (!chip->adc_tm_hc)
811 rc = qpnp_adc_tm_write_reg(chip,
812 QPNP_ADC_TM_MEAS_INTERVAL_CTL2,
813 meas_interval_timer2, 1);
Jishnu Prakashd09bc692018-05-02 10:54:28 +0530814 else {
815 if (!is_pmic_5)
816 rc = qpnp_adc_tm_write_reg(chip,
817 QPNP_BTM_MEAS_INTERVAL_CTL2,
818 meas_interval_timer2, 1);
819 else
820 rc = qpnp_adc_tm_write_reg(chip,
821 QPNP_BTM_MEAS_INTERVAL_CTL2_PM5,
822 meas_interval_timer2, 1);
823 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800824 if (rc < 0) {
825 pr_err("timer2 configure failed\n");
826 return rc;
827 }
828 break;
829 case ADC_MEAS_TIMER_SELECT3:
Prasad Malisetty358dda32018-02-20 06:14:26 +0530830 if (!chip->adc_tm_hc)
831 rc = qpnp_adc_tm_read_reg(chip,
832 QPNP_ADC_TM_MEAS_INTERVAL_CTL2,
833 &meas_interval_timer2, 1);
Jishnu Prakashd09bc692018-05-02 10:54:28 +0530834 else {
835 if (!is_pmic_5)
836 rc = qpnp_adc_tm_read_reg(chip,
837 QPNP_BTM_MEAS_INTERVAL_CTL2,
838 &meas_interval_timer2, 1);
839 else
840 rc = qpnp_adc_tm_read_reg(chip,
841 QPNP_BTM_MEAS_INTERVAL_CTL2_PM5,
842 &meas_interval_timer2, 1);
843 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800844 if (rc < 0) {
845 pr_err("timer3 read failed\n");
846 return rc;
847 }
848 timer_interval_store = chip->sensor[chan_idx].meas_interval;
849 timer_interval_store &= QPNP_ADC_TM_MEAS_INTERVAL_CTL3_MASK;
850 meas_interval_timer2 |= timer_interval_store;
Prasad Malisetty358dda32018-02-20 06:14:26 +0530851 if (!chip->adc_tm_hc)
852 rc = qpnp_adc_tm_write_reg(chip,
853 QPNP_ADC_TM_MEAS_INTERVAL_CTL2,
854 meas_interval_timer2, 1);
Jishnu Prakashd09bc692018-05-02 10:54:28 +0530855 else {
856 if (!is_pmic_5)
857 rc = qpnp_adc_tm_write_reg(chip,
858 QPNP_BTM_MEAS_INTERVAL_CTL2,
859 meas_interval_timer2, 1);
860 else
861 rc = qpnp_adc_tm_write_reg(chip,
862 QPNP_BTM_MEAS_INTERVAL_CTL2_PM5,
863 meas_interval_timer2, 1);
864 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800865 if (rc < 0) {
866 pr_err("timer3 configure failed\n");
867 return rc;
868 }
869 break;
870 default:
871 pr_err("Invalid timer selection\n");
872 return -EINVAL;
873 }
874
875 /* Select the timer to use for the corresponding channel */
876 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
877 if (rc < 0) {
878 pr_err("Invalid btm channel idx\n");
879 return rc;
880 }
Prasad Malisetty358dda32018-02-20 06:14:26 +0530881 if (!chip->adc_tm_hc)
882 rc = qpnp_adc_tm_write_reg(chip,
883 adc_tm_data[btm_chan_idx].meas_interval_ctl,
884 chip->sensor[chan_idx].timer_select, 1);
885 else
886 rc = qpnp_adc_tm_write_reg(chip,
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -0700887 QPNP_BTM_Mn_MEAS_INTERVAL_CTL(btm_chan_idx),
888 chip->sensor[chan_idx].timer_select, 1);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -0800889 if (rc < 0) {
890 pr_err("TM channel timer configure failed\n");
891 return rc;
892 }
893
894 pr_debug("timer select:%d, timer_value_within_select:%d, channel:%x\n",
895 chip->sensor[chan_idx].timer_select,
896 chip->sensor[chan_idx].meas_interval,
897 btm_chan);
898
899 return rc;
900}
901
902static int32_t qpnp_adc_tm_add_to_list(struct qpnp_adc_tm_chip *chip,
903 uint32_t dt_index,
904 struct qpnp_adc_tm_btm_param *param,
905 struct qpnp_vadc_chan_properties *chan_prop)
906{
907 struct qpnp_adc_thr_client_info *client_info = NULL;
908 bool client_info_exists = false;
909
910 list_for_each_entry(client_info,
911 &chip->sensor[dt_index].thr_list, list) {
912 if (client_info->btm_param == param) {
913 client_info->low_thr_requested = chan_prop->low_thr;
914 client_info->high_thr_requested = chan_prop->high_thr;
915 client_info->state_requested = param->state_request;
916 client_info->state_req_copy = param->state_request;
917 client_info->notify_low_thr = false;
918 client_info->notify_high_thr = false;
919 client_info_exists = true;
920 pr_debug("client found\n");
921 }
922 }
923
924 if (!client_info_exists) {
925 client_info = devm_kzalloc(chip->dev,
926 sizeof(struct qpnp_adc_thr_client_info), GFP_KERNEL);
927 if (!client_info)
928 return -ENOMEM;
929
930 pr_debug("new client\n");
931 client_info->btm_param = param;
932 client_info->low_thr_requested = chan_prop->low_thr;
933 client_info->high_thr_requested = chan_prop->high_thr;
934 client_info->state_requested = param->state_request;
935 client_info->state_req_copy = param->state_request;
936
937 list_add_tail(&client_info->list,
938 &chip->sensor[dt_index].thr_list);
939 }
940
941 return 0;
942}
943
944static int32_t qpnp_adc_tm_reg_update(struct qpnp_adc_tm_chip *chip,
945 uint16_t addr, u8 mask, bool state)
946{
947 u8 reg_value = 0;
948 int rc = 0;
949
950 rc = qpnp_adc_tm_read_reg(chip, addr, &reg_value, 1);
951 if (rc < 0) {
952 pr_err("read failed for addr:0x%x\n", addr);
953 return rc;
954 }
955
956 reg_value = reg_value & ~mask;
957 if (state)
958 reg_value |= mask;
959
960 pr_debug("state:%d, reg:0x%x with bits:0x%x and mask:0x%x\n",
961 state, addr, reg_value, ~mask);
962 rc = qpnp_adc_tm_write_reg(chip, addr, reg_value, 1);
963 if (rc < 0) {
964 pr_err("write failed for addr:%x\n", addr);
965 return rc;
966 }
967
968 return rc;
969}
970
Prasad Malisetty358dda32018-02-20 06:14:26 +0530971static int32_t qpnp_adc_tm_read_thr_value(struct qpnp_adc_tm_chip *chip,
972 uint32_t btm_chan)
973{
974 int rc = 0;
975 u8 data_lsb = 0, data_msb = 0;
976 uint32_t btm_chan_idx = 0;
977 int32_t low_thr = 0, high_thr = 0;
978
979 if (!chip->adc_tm_hc) {
980 pr_err("Not applicable for VADC HC peripheral\n");
981 return -EINVAL;
982 }
983
984 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
985 if (rc < 0) {
986 pr_err("Invalid btm channel idx\n");
987 return rc;
988 }
989
990 rc = qpnp_adc_tm_read_reg(chip,
991 adc_tm_data[btm_chan_idx].low_thr_lsb_addr,
992 &data_lsb, 1);
993 if (rc < 0) {
994 pr_err("low threshold lsb setting failed\n");
995 return rc;
996 }
997
998 rc = qpnp_adc_tm_read_reg(chip,
999 adc_tm_data[btm_chan_idx].low_thr_msb_addr,
1000 &data_msb, 1);
1001 if (rc < 0) {
1002 pr_err("low threshold msb setting failed\n");
1003 return rc;
1004 }
1005
1006 low_thr = (data_msb << 8) | data_lsb;
1007
1008 rc = qpnp_adc_tm_read_reg(chip,
1009 adc_tm_data[btm_chan_idx].high_thr_lsb_addr,
1010 &data_lsb, 1);
1011 if (rc < 0) {
1012 pr_err("high threshold lsb setting failed\n");
1013 return rc;
1014 }
1015
1016 rc = qpnp_adc_tm_read_reg(chip,
1017 adc_tm_data[btm_chan_idx].high_thr_msb_addr,
1018 &data_msb, 1);
1019 if (rc < 0) {
1020 pr_err("high threshold msb setting failed\n");
1021 return rc;
1022 }
1023
1024 high_thr = (data_msb << 8) | data_lsb;
1025
1026 pr_debug("configured thresholds high:0x%x and low:0x%x\n",
1027 high_thr, low_thr);
1028
1029 return rc;
1030}
1031
1032
1033
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001034static int32_t qpnp_adc_tm_thr_update(struct qpnp_adc_tm_chip *chip,
1035 uint32_t btm_chan, int32_t high_thr, int32_t low_thr)
1036{
1037 int rc = 0;
1038 uint32_t btm_chan_idx = 0;
1039
1040 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
1041 if (rc < 0) {
1042 pr_err("Invalid btm channel idx\n");
1043 return rc;
1044 }
1045
Prasad Malisetty358dda32018-02-20 06:14:26 +05301046 if (!chip->adc_tm_hc) {
1047 rc = qpnp_adc_tm_write_reg(chip,
1048 adc_tm_data[btm_chan_idx].low_thr_lsb_addr,
1049 QPNP_ADC_TM_THR_LSB_MASK(low_thr), 1);
1050 if (rc < 0) {
1051 pr_err("low threshold lsb setting failed\n");
1052 return rc;
1053 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001054
Prasad Malisetty358dda32018-02-20 06:14:26 +05301055 rc = qpnp_adc_tm_write_reg(chip,
1056 adc_tm_data[btm_chan_idx].low_thr_msb_addr,
1057 QPNP_ADC_TM_THR_MSB_MASK(low_thr), 1);
1058 if (rc < 0) {
1059 pr_err("low threshold msb setting failed\n");
1060 return rc;
1061 }
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001062
Prasad Malisetty358dda32018-02-20 06:14:26 +05301063 rc = qpnp_adc_tm_write_reg(chip,
1064 adc_tm_data[btm_chan_idx].high_thr_lsb_addr,
1065 QPNP_ADC_TM_THR_LSB_MASK(high_thr), 1);
1066 if (rc < 0) {
1067 pr_err("high threshold lsb setting failed\n");
1068 return rc;
1069 }
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001070
Prasad Malisetty358dda32018-02-20 06:14:26 +05301071 rc = qpnp_adc_tm_write_reg(chip,
1072 adc_tm_data[btm_chan_idx].high_thr_msb_addr,
1073 QPNP_ADC_TM_THR_MSB_MASK(high_thr), 1);
1074 if (rc < 0)
1075 pr_err("high threshold msb setting failed\n");
1076 } else {
1077 rc = qpnp_adc_tm_write_reg(chip,
1078 QPNP_BTM_Mn_LOW_THR0(btm_chan_idx),
1079 QPNP_ADC_TM_THR_LSB_MASK(low_thr), 1);
1080 if (rc < 0) {
1081 pr_err("low threshold lsb setting failed\n");
1082 return rc;
1083 }
1084
1085 rc = qpnp_adc_tm_write_reg(chip,
1086 QPNP_BTM_Mn_LOW_THR1(btm_chan_idx),
1087 QPNP_ADC_TM_THR_MSB_MASK(low_thr), 1);
1088 if (rc < 0) {
1089 pr_err("low threshold msb setting failed\n");
1090 return rc;
1091 }
1092
1093 rc = qpnp_adc_tm_write_reg(chip,
1094 QPNP_BTM_Mn_HIGH_THR0(btm_chan_idx),
1095 QPNP_ADC_TM_THR_LSB_MASK(high_thr), 1);
1096 if (rc < 0) {
1097 pr_err("high threshold lsb setting failed\n");
1098 return rc;
1099 }
1100
1101 rc = qpnp_adc_tm_write_reg(chip,
1102 QPNP_BTM_Mn_HIGH_THR1(btm_chan_idx),
1103 QPNP_ADC_TM_THR_MSB_MASK(high_thr), 1);
1104 if (rc < 0)
1105 pr_err("high threshold msb setting failed\n");
1106
1107 }
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001108
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001109 pr_debug("client requested high:%d and low:%d\n",
Prasad Malisetty358dda32018-02-20 06:14:26 +05301110 high_thr, low_thr);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001111
1112 return rc;
1113}
1114
1115static int32_t qpnp_adc_tm_manage_thresholds(struct qpnp_adc_tm_chip *chip,
1116 uint32_t dt_index, uint32_t btm_chan)
1117{
1118 struct qpnp_adc_thr_client_info *client_info = NULL;
1119 struct list_head *thr_list;
1120 int high_thr = 0, low_thr = 0, rc = 0;
1121
1122
1123 /*
1124 * high_thr/low_thr starting point and reset the high_thr_set and
1125 * low_thr_set back to reset since the thresholds will be
1126 * recomputed.
1127 */
1128 list_for_each(thr_list,
1129 &chip->sensor[dt_index].thr_list) {
1130 client_info = list_entry(thr_list,
1131 struct qpnp_adc_thr_client_info, list);
1132 high_thr = client_info->high_thr_requested;
1133 low_thr = client_info->low_thr_requested;
1134 client_info->high_thr_set = false;
1135 client_info->low_thr_set = false;
1136 }
1137
1138 pr_debug("init threshold is high:%d and low:%d\n", high_thr, low_thr);
1139
1140 /* Find the min of high_thr and max of low_thr */
1141 list_for_each(thr_list,
1142 &chip->sensor[dt_index].thr_list) {
1143 client_info = list_entry(thr_list,
1144 struct qpnp_adc_thr_client_info, list);
1145 if ((client_info->state_req_copy == ADC_TM_HIGH_THR_ENABLE) ||
1146 (client_info->state_req_copy ==
1147 ADC_TM_HIGH_LOW_THR_ENABLE))
1148 if (client_info->high_thr_requested < high_thr)
1149 high_thr = client_info->high_thr_requested;
1150
1151 if ((client_info->state_req_copy == ADC_TM_LOW_THR_ENABLE) ||
1152 (client_info->state_req_copy ==
1153 ADC_TM_HIGH_LOW_THR_ENABLE))
1154 if (client_info->low_thr_requested > low_thr)
1155 low_thr = client_info->low_thr_requested;
1156
1157 pr_debug("threshold compared is high:%d and low:%d\n",
1158 client_info->high_thr_requested,
1159 client_info->low_thr_requested);
1160 pr_debug("current threshold is high:%d and low:%d\n",
1161 high_thr, low_thr);
1162 }
1163
1164 /* Check which of the high_thr and low_thr got set */
1165 list_for_each(thr_list,
1166 &chip->sensor[dt_index].thr_list) {
1167 client_info = list_entry(thr_list,
1168 struct qpnp_adc_thr_client_info, list);
1169 if ((client_info->state_req_copy == ADC_TM_HIGH_THR_ENABLE) ||
1170 (client_info->state_req_copy ==
1171 ADC_TM_HIGH_LOW_THR_ENABLE))
1172 if (high_thr == client_info->high_thr_requested)
1173 client_info->high_thr_set = true;
1174
1175 if ((client_info->state_req_copy == ADC_TM_LOW_THR_ENABLE) ||
1176 (client_info->state_req_copy ==
1177 ADC_TM_HIGH_LOW_THR_ENABLE))
1178 if (low_thr == client_info->low_thr_requested)
1179 client_info->low_thr_set = true;
1180 }
1181
1182 rc = qpnp_adc_tm_thr_update(chip, btm_chan, high_thr, low_thr);
1183 if (rc < 0)
1184 pr_err("setting chan:%d threshold failed\n", btm_chan);
1185
1186 pr_debug("threshold written is high:%d and low:%d\n",
1187 high_thr, low_thr);
1188
1189 return 0;
1190}
1191
1192static int32_t qpnp_adc_tm_channel_configure(struct qpnp_adc_tm_chip *chip,
1193 uint32_t btm_chan,
1194 struct qpnp_vadc_chan_properties *chan_prop,
1195 uint32_t amux_channel)
1196{
1197 int rc = 0, i = 0, chan_idx = 0;
1198 bool chan_found = false, high_thr_set = false, low_thr_set = false;
1199 u8 sensor_mask = 0;
1200 struct qpnp_adc_thr_client_info *client_info = NULL;
1201 uint32_t btm_chan_idx = 0;
1202
1203 while (i < chip->max_channels_available) {
1204 if (chip->sensor[i].btm_channel_num == btm_chan) {
1205 chan_idx = i;
1206 chan_found = true;
1207 i++;
1208 } else
1209 i++;
1210 }
1211
1212 if (!chan_found) {
1213 pr_err("Channel not found\n");
1214 return -EINVAL;
1215 }
1216
1217 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
1218 if (rc < 0) {
1219 pr_err("Invalid btm channel idx\n");
1220 return rc;
1221 }
1222
1223 sensor_mask = 1 << chan_idx;
1224 if (!chip->sensor[chan_idx].thermal_node) {
1225 /* Update low and high notification thresholds */
1226 rc = qpnp_adc_tm_manage_thresholds(chip, chan_idx,
1227 btm_chan);
1228 if (rc < 0) {
1229 pr_err("setting chan:%d threshold failed\n", btm_chan);
1230 return rc;
1231 }
1232
1233 list_for_each_entry(client_info,
1234 &chip->sensor[chan_idx].thr_list, list) {
1235 if (client_info->high_thr_set == true)
1236 high_thr_set = true;
1237 if (client_info->low_thr_set == true)
1238 low_thr_set = true;
1239 }
1240
1241 if (low_thr_set) {
1242 pr_debug("low sensor mask:%x with state:%d\n",
1243 sensor_mask, chan_prop->state_request);
1244 /* Enable low threshold's interrupt */
Prasad Malisetty358dda32018-02-20 06:14:26 +05301245 if (!chip->adc_tm_hc)
1246 rc = qpnp_adc_tm_reg_update(chip,
1247 QPNP_ADC_TM_LOW_THR_INT_EN,
1248 sensor_mask, true);
1249 else
1250 rc = qpnp_adc_tm_reg_update(chip,
1251 QPNP_BTM_Mn_EN(btm_chan_idx),
1252 QPNP_BTM_Mn_LOW_THR_INT_EN, true);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001253 if (rc < 0) {
1254 pr_err("low thr enable err:%d\n", btm_chan);
1255 return rc;
1256 }
1257 }
1258
1259 if (high_thr_set) {
1260 /* Enable high threshold's interrupt */
1261 pr_debug("high sensor mask:%x\n", sensor_mask);
Prasad Malisetty358dda32018-02-20 06:14:26 +05301262 if (!chip->adc_tm_hc)
1263 rc = qpnp_adc_tm_reg_update(chip,
1264 QPNP_ADC_TM_HIGH_THR_INT_EN,
1265 sensor_mask, true);
1266 else
1267 rc = qpnp_adc_tm_reg_update(chip,
1268 QPNP_BTM_Mn_EN(btm_chan_idx),
1269 QPNP_BTM_Mn_HIGH_THR_INT_EN, true);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001270 if (rc < 0) {
1271 pr_err("high thr enable err:%d\n", btm_chan);
1272 return rc;
1273 }
1274 }
1275 }
1276
1277 /* Enable corresponding BTM channel measurement */
Prasad Malisetty358dda32018-02-20 06:14:26 +05301278 if (!chip->adc_tm_hc)
1279 rc = qpnp_adc_tm_reg_update(chip,
1280 QPNP_ADC_TM_MULTI_MEAS_EN, sensor_mask, true);
1281 else
1282 rc = qpnp_adc_tm_reg_update(chip, QPNP_BTM_Mn_EN(btm_chan_idx),
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001283 QPNP_BTM_Mn_MEAS_EN, true);
1284 if (rc < 0) {
1285 pr_err("multi measurement en failed\n");
1286 return rc;
1287 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001288 return rc;
1289}
1290
1291static int32_t qpnp_adc_tm_hc_configure(struct qpnp_adc_tm_chip *chip,
1292 struct qpnp_adc_amux_properties *chan_prop)
1293{
1294 u8 decimation = 0, fast_avg_ctl = 0;
1295 u8 buf[8];
1296 int rc = 0;
1297 uint32_t btm_chan = 0, cal_type = 0, btm_chan_idx = 0;
1298
1299 /* Disable bank */
1300 rc = qpnp_adc_tm_disable(chip);
1301 if (rc)
1302 return rc;
1303
1304 /* Decimation setup */
1305 decimation = chan_prop->decimation;
1306 rc = qpnp_adc_tm_write_reg(chip, QPNP_BTM_HC_ADC_DIG_PARAM,
1307 decimation, 1);
1308 if (rc < 0) {
1309 pr_err("adc-tm digital parameter setup err\n");
1310 return rc;
1311 }
1312
1313 /* Fast averaging setup/enable */
1314 rc = qpnp_adc_tm_read_reg(chip, QPNP_BTM_HC_FAST_AVG_CTL,
1315 &fast_avg_ctl, 1);
1316 if (rc < 0) {
1317 pr_err("adc-tm fast-avg enable read err\n");
1318 return rc;
1319 }
1320 fast_avg_ctl |= chan_prop->fast_avg_setup;
1321 rc = qpnp_adc_tm_write_reg(chip, QPNP_BTM_HC_FAST_AVG_CTL,
1322 fast_avg_ctl, 1);
1323 if (rc < 0) {
1324 pr_err("adc-tm fast-avg enable write err\n");
1325 return rc;
1326 }
1327
1328 /* Read block registers for respective BTM channel */
1329 btm_chan = chan_prop->chan_prop->tm_channel_select;
1330 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
1331 if (rc < 0) {
1332 pr_err("Invalid btm channel idx\n");
1333 return rc;
1334 }
1335
1336 rc = qpnp_adc_tm_read_reg(chip,
1337 QPNP_BTM_Mn_ADC_CH_SEL_CTL(btm_chan_idx), buf, 8);
1338 if (rc < 0) {
1339 pr_err("qpnp adc configure block read failed\n");
1340 return rc;
1341 }
1342
1343 /* Update ADC channel sel */
1344 rc = qpnp_adc_tm_write_reg(chip,
1345 QPNP_BTM_Mn_ADC_CH_SEL_CTL(btm_chan_idx),
1346 chan_prop->amux_channel, 1);
1347 if (rc < 0) {
1348 pr_err("adc-tm channel amux select failed\n");
1349 return rc;
1350 }
1351
1352 /* Manage thresholds */
1353 rc = qpnp_adc_tm_channel_configure(chip, btm_chan,
1354 chan_prop->chan_prop, chan_prop->amux_channel);
1355 if (rc < 0) {
1356 pr_err("adc-tm channel threshold configure failed\n");
1357 return rc;
1358 }
1359
1360 /* Measurement interval setup */
1361 rc = qpnp_adc_tm_timer_interval_select(chip, btm_chan,
1362 chan_prop->chan_prop);
1363 if (rc < 0) {
1364 pr_err("adc-tm timer select failed\n");
1365 return rc;
1366 }
1367
1368 /* Set calibration select, hw_settle delay */
1369 cal_type |= (chan_prop->calib_type << QPNP_BTM_CTL_CAL_SEL_MASK_SHIFT);
1370 buf[6] &= ~QPNP_BTM_CTL_HW_SETTLE_DELAY_MASK;
1371 buf[6] |= chan_prop->hw_settle_time;
1372 buf[6] &= ~QPNP_BTM_CTL_CAL_SEL;
1373 buf[6] |= cal_type;
1374 rc = qpnp_adc_tm_write_reg(chip, QPNP_BTM_Mn_CTL(btm_chan_idx),
1375 buf[6], 1);
1376 if (rc < 0) {
1377 pr_err("adc-tm hw-settle, calib sel failed\n");
1378 return rc;
1379 }
1380
1381 /* Enable bank */
1382 rc = qpnp_adc_tm_enable(chip);
1383 if (rc)
1384 return rc;
1385
1386 /* Request conversion */
1387 rc = qpnp_adc_tm_write_reg(chip, QPNP_CONV_REQ, QPNP_CONV_REQ_SET, 1);
1388 if (rc < 0) {
1389 pr_err("adc-tm request conversion failed\n");
1390 return rc;
1391 }
1392
1393 return 0;
1394}
1395
Prasad Malisetty358dda32018-02-20 06:14:26 +05301396static int32_t qpnp_adc_tm_configure(struct qpnp_adc_tm_chip *chip,
1397 struct qpnp_adc_amux_properties *chan_prop)
1398{
1399 u8 decimation = 0, op_cntrl = 0, mode_ctl = 0;
1400 int rc = 0;
1401 uint32_t btm_chan = 0;
1402
1403 /* Set measurement in single measurement mode */
1404 mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
1405 rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
1406 if (rc < 0) {
1407 pr_err("adc-tm single mode select failed\n");
1408 return rc;
1409 }
1410
1411 /* Disable bank */
1412 rc = qpnp_adc_tm_disable(chip);
1413 if (rc)
1414 return rc;
1415
1416 /* Check if a conversion is in progress */
1417 rc = qpnp_adc_tm_req_sts_check(chip);
1418 if (rc < 0) {
1419 pr_err("adc-tm req_sts check failed\n");
1420 return rc;
1421 }
1422
1423 /* Configure AMUX channel select for the corresponding BTM channel*/
1424 btm_chan = chan_prop->chan_prop->tm_channel_select;
1425 rc = qpnp_adc_tm_write_reg(chip, btm_chan, chan_prop->amux_channel, 1);
1426 if (rc < 0) {
1427 pr_err("adc-tm channel selection err\n");
1428 return rc;
1429 }
1430
1431 /* Digital parameter setup */
1432 decimation |= chan_prop->decimation <<
1433 QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT;
1434 rc = qpnp_adc_tm_write_reg(chip, QPNP_ADC_DIG_PARAM, decimation, 1);
1435 if (rc < 0) {
1436 pr_err("adc-tm digital parameter setup err\n");
1437 return rc;
1438 }
1439
1440 /* Hardware setting time */
1441 rc = qpnp_adc_tm_write_reg(chip, QPNP_HW_SETTLE_DELAY,
1442 chan_prop->hw_settle_time, 1);
1443 if (rc < 0) {
1444 pr_err("adc-tm hw settling time setup err\n");
1445 return rc;
1446 }
1447
1448 /* Fast averaging setup/enable */
1449 rc = qpnp_adc_tm_fast_avg_en(chip, &chan_prop->fast_avg_setup);
1450 if (rc < 0) {
1451 pr_err("adc-tm fast-avg enable err\n");
1452 return rc;
1453 }
1454
1455 rc = qpnp_adc_tm_write_reg(chip, QPNP_FAST_AVG_CTL,
1456 chan_prop->fast_avg_setup, 1);
1457 if (rc < 0) {
1458 pr_err("adc-tm fast-avg setup err\n");
1459 return rc;
1460 }
1461
1462 /* Measurement interval setup */
1463 rc = qpnp_adc_tm_timer_interval_select(chip, btm_chan,
1464 chan_prop->chan_prop);
1465 if (rc < 0) {
1466 pr_err("adc-tm timer select failed\n");
1467 return rc;
1468 }
1469
1470 /* Channel configuration setup */
1471 rc = qpnp_adc_tm_channel_configure(chip, btm_chan,
1472 chan_prop->chan_prop, chan_prop->amux_channel);
1473 if (rc < 0) {
1474 pr_err("adc-tm channel configure failed\n");
1475 return rc;
1476 }
1477
1478 /* Recurring interval measurement enable */
1479 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_MEAS_INTERVAL_OP_CTL,
1480 &op_cntrl, 1);
1481 op_cntrl |= QPNP_ADC_MEAS_INTERVAL_OP;
1482 rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_MEAS_INTERVAL_OP_CTL,
1483 op_cntrl, true);
1484 if (rc < 0) {
1485 pr_err("adc-tm meas interval op configure failed\n");
1486 return rc;
1487 }
1488
1489 /* Enable bank */
1490 rc = qpnp_adc_tm_enable(chip);
1491 if (rc)
1492 return rc;
1493
1494 /* Request conversion */
1495 rc = qpnp_adc_tm_write_reg(chip, QPNP_CONV_REQ, QPNP_CONV_REQ_SET, 1);
1496 if (rc < 0) {
1497 pr_err("adc-tm request conversion failed\n");
1498 return rc;
1499 }
1500
1501 return 0;
1502}
1503
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001504static int qpnp_adc_tm_set_mode(struct qpnp_adc_tm_sensor *adc_tm,
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001505 enum thermal_device_mode mode)
1506{
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001507 struct qpnp_adc_tm_chip *chip = adc_tm->chip;
1508 int rc = 0, channel;
Prasad Malisetty358dda32018-02-20 06:14:26 +05301509 u8 sensor_mask = 0, mode_ctl = 0;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001510 uint32_t btm_chan_idx = 0, btm_chan = 0;
1511
1512 if (qpnp_adc_tm_is_valid(chip)) {
1513 pr_err("invalid device\n");
1514 return -ENODEV;
1515 }
1516
1517 if (qpnp_adc_tm_check_revision(chip, adc_tm->btm_channel_num))
1518 return -EINVAL;
1519
1520 mutex_lock(&chip->adc->adc_lock);
1521
1522 btm_chan = adc_tm->btm_channel_num;
1523 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
1524 if (rc < 0) {
1525 pr_err("Invalid btm channel idx\n");
1526 goto fail;
1527 }
1528
1529 if (mode == THERMAL_DEVICE_ENABLED) {
1530 chip->adc->amux_prop->amux_channel =
1531 adc_tm->vadc_channel_num;
1532 channel = adc_tm->sensor_num;
1533 chip->adc->amux_prop->decimation =
1534 chip->adc->adc_channels[channel].adc_decimation;
1535 chip->adc->amux_prop->hw_settle_time =
1536 chip->adc->adc_channels[channel].hw_settle_time;
1537 chip->adc->amux_prop->fast_avg_setup =
1538 chip->adc->adc_channels[channel].fast_avg_setup;
1539 chip->adc->amux_prop->mode_sel =
1540 ADC_OP_MEASUREMENT_INTERVAL << QPNP_OP_MODE_SHIFT;
1541 chip->adc->amux_prop->chan_prop->low_thr = adc_tm->low_thr;
1542 chip->adc->amux_prop->chan_prop->high_thr = adc_tm->high_thr;
1543 chip->adc->amux_prop->chan_prop->tm_channel_select =
1544 adc_tm->btm_channel_num;
1545 chip->adc->amux_prop->calib_type =
1546 chip->adc->adc_channels[channel].calib_type;
1547
Prasad Malisetty358dda32018-02-20 06:14:26 +05301548 if (!chip->adc_tm_hc) {
1549 rc = qpnp_adc_tm_configure(chip, chip->adc->amux_prop);
1550 if (rc) {
1551 pr_err("adc-tm configure failed with %d\n", rc);
1552 goto fail;
1553 }
1554 } else {
1555 rc = qpnp_adc_tm_hc_configure(chip,
1556 chip->adc->amux_prop);
1557 if (rc) {
1558 pr_err("hc configure failed with %d\n", rc);
1559 goto fail;
1560 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001561 }
1562 } else if (mode == THERMAL_DEVICE_DISABLED) {
1563 sensor_mask = 1 << adc_tm->sensor_num;
Prasad Malisetty358dda32018-02-20 06:14:26 +05301564 if (!chip->adc_tm_hc) {
1565 mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
1566 rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
1567 if (rc < 0) {
1568 pr_err("adc-tm single mode select failed\n");
1569 goto fail;
1570 }
1571 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001572
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001573 /* Disable bank */
1574 rc = qpnp_adc_tm_disable(chip);
1575 if (rc < 0) {
1576 pr_err("adc-tm disable failed\n");
1577 goto fail;
1578 }
1579
Prasad Malisetty358dda32018-02-20 06:14:26 +05301580 if (!chip->adc_tm_hc) {
1581 /* Check if a conversion is in progress */
1582 rc = qpnp_adc_tm_req_sts_check(chip);
1583 if (rc < 0) {
1584 pr_err("adc-tm req_sts check failed\n");
1585 goto fail;
1586 }
1587 rc = qpnp_adc_tm_reg_update(chip,
1588 QPNP_ADC_TM_MULTI_MEAS_EN,
1589 sensor_mask, false);
1590 if (rc < 0) {
1591 pr_err("multi measurement update failed\n");
1592 goto fail;
1593 }
1594 } else {
1595 rc = qpnp_adc_tm_reg_update(chip,
1596 QPNP_BTM_Mn_EN(btm_chan_idx),
1597 QPNP_BTM_Mn_MEAS_EN, false);
1598 if (rc < 0) {
1599 pr_err("multi measurement disable failed\n");
1600 goto fail;
1601 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001602 }
1603
1604 rc = qpnp_adc_tm_enable_if_channel_meas(chip);
1605 if (rc < 0) {
1606 pr_err("re-enabling measurement failed\n");
1607 goto fail;
1608 }
1609 }
1610
1611 adc_tm->mode = mode;
1612
1613fail:
1614 mutex_unlock(&chip->adc->adc_lock);
1615
1616 return 0;
1617}
1618
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001619static int qpnp_adc_tm_activate_trip_type(struct qpnp_adc_tm_sensor *adc_tm,
1620 int trip, enum thermal_trip_activation_mode mode)
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001621{
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001622 struct qpnp_adc_tm_chip *chip = adc_tm->chip;
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001623 int rc = 0, sensor_mask = 0;
Prasad Malisetty358dda32018-02-20 06:14:26 +05301624 u8 thr_int_en = 0;
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001625 bool state = false;
1626 uint32_t btm_chan_idx = 0, btm_chan = 0;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001627
1628 if (qpnp_adc_tm_is_valid(chip))
1629 return -ENODEV;
1630
1631 if (qpnp_adc_tm_check_revision(chip, adc_tm->btm_channel_num))
1632 return -EINVAL;
1633
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001634 if (mode == THERMAL_TRIP_ACTIVATION_ENABLED)
1635 state = true;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001636
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001637 sensor_mask = 1 << adc_tm->sensor_num;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001638
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001639 pr_debug("Sensor number:%x with state:%d\n",
1640 adc_tm->sensor_num, state);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001641
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001642 btm_chan = adc_tm->btm_channel_num;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001643 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
1644 if (rc < 0) {
1645 pr_err("Invalid btm channel idx\n");
1646 return rc;
1647 }
1648
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001649 switch (trip) {
1650 case ADC_TM_TRIP_HIGH_WARM:
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001651 /* low_thr (lower voltage) for higher temp */
Prasad Malisetty358dda32018-02-20 06:14:26 +05301652 thr_int_en = adc_tm_data[btm_chan_idx].low_thr_int_chan_en;
1653 if (!chip->adc_tm_hc)
1654 rc = qpnp_adc_tm_reg_update(chip,
1655 QPNP_ADC_TM_LOW_THR_INT_EN,
1656 sensor_mask, state);
1657 else
1658 rc = qpnp_adc_tm_reg_update(chip,
1659 QPNP_BTM_Mn_EN(btm_chan_idx),
1660 QPNP_BTM_Mn_LOW_THR_INT_EN, state);
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001661 if (rc)
1662 pr_err("channel:%x failed\n", btm_chan);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001663 break;
1664 case ADC_TM_TRIP_LOW_COOL:
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001665 /* high_thr (higher voltage) for cooler temp */
Prasad Malisetty358dda32018-02-20 06:14:26 +05301666 thr_int_en = adc_tm_data[btm_chan_idx].high_thr_int_chan_en;
1667 if (!chip->adc_tm_hc)
1668 rc = qpnp_adc_tm_reg_update(chip,
1669 QPNP_ADC_TM_HIGH_THR_INT_EN,
1670 sensor_mask, state);
1671 else
1672 rc = qpnp_adc_tm_reg_update(chip,
1673 QPNP_BTM_Mn_EN(btm_chan_idx),
1674 QPNP_BTM_Mn_HIGH_THR_INT_EN, state);
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001675 if (rc)
1676 pr_err("channel:%x failed\n", btm_chan);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001677 break;
1678 default:
1679 return -EINVAL;
1680 }
1681
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001682 return rc;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001683}
1684
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001685static int qpnp_adc_tm_set_trip_temp(void *data, int low_temp, int high_temp)
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001686{
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001687 struct qpnp_adc_tm_sensor *adc_tm = data;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001688 struct qpnp_adc_tm_chip *chip = adc_tm->chip;
1689 struct qpnp_adc_tm_config tm_config;
1690 u8 trip_cool_thr0, trip_cool_thr1, trip_warm_thr0, trip_warm_thr1;
1691 uint16_t reg_low_thr_lsb, reg_low_thr_msb;
1692 uint16_t reg_high_thr_lsb, reg_high_thr_msb;
1693 int rc = 0;
1694 uint32_t btm_chan = 0, btm_chan_idx = 0;
1695
1696 if (qpnp_adc_tm_is_valid(chip))
1697 return -ENODEV;
1698
1699 if (qpnp_adc_tm_check_revision(chip, adc_tm->btm_channel_num))
1700 return -EINVAL;
1701
1702 tm_config.channel = adc_tm->vadc_channel_num;
1703 tm_config.high_thr_temp = tm_config.low_thr_temp = 0;
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001704 if (high_temp != INT_MAX)
1705 tm_config.high_thr_temp = high_temp;
1706 if (low_temp != INT_MIN)
1707 tm_config.low_thr_temp = low_temp;
1708
1709 if ((high_temp == INT_MAX) && (low_temp == INT_MIN)) {
1710 pr_err("No trips to set\n");
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001711 return -EINVAL;
1712 }
1713
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001714 pr_debug("requested a high - %d and low - %d\n",
1715 tm_config.high_thr_temp, tm_config.low_thr_temp);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001716 rc = qpnp_adc_tm_scale_therm_voltage_pu2(chip->vadc_dev,
1717 chip->adc->adc_prop, &tm_config);
1718 if (rc < 0) {
1719 pr_err("Failed to lookup the adc-tm thresholds\n");
1720 return rc;
1721 }
1722
1723 trip_warm_thr0 = ((tm_config.low_thr_voltage << 24) >> 24);
1724 trip_warm_thr1 = ((tm_config.low_thr_voltage << 16) >> 24);
1725 trip_cool_thr0 = ((tm_config.high_thr_voltage << 24) >> 24);
1726 trip_cool_thr1 = ((tm_config.high_thr_voltage << 16) >> 24);
1727
1728 pr_debug("low_thr:0x%llx, high_thr:0x%llx\n", tm_config.low_thr_voltage,
1729 tm_config.high_thr_voltage);
1730
1731 btm_chan = adc_tm->btm_channel_num;
1732 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
1733 if (rc < 0) {
1734 pr_err("Invalid btm channel idx\n");
1735 return rc;
1736 }
1737
Prasad Malisetty358dda32018-02-20 06:14:26 +05301738 if (!chip->adc_tm_hc) {
1739 reg_low_thr_lsb = adc_tm_data[btm_chan_idx].low_thr_lsb_addr;
1740 reg_low_thr_msb = adc_tm_data[btm_chan_idx].low_thr_msb_addr;
1741 reg_high_thr_lsb = adc_tm_data[btm_chan_idx].high_thr_lsb_addr;
1742 reg_high_thr_msb = adc_tm_data[btm_chan_idx].high_thr_msb_addr;
1743 } else {
1744 reg_low_thr_lsb = QPNP_BTM_Mn_LOW_THR0(btm_chan_idx);
1745 reg_low_thr_msb = QPNP_BTM_Mn_LOW_THR1(btm_chan_idx);
1746 reg_high_thr_lsb = QPNP_BTM_Mn_HIGH_THR0(btm_chan_idx);
1747 reg_high_thr_msb = QPNP_BTM_Mn_HIGH_THR1(btm_chan_idx);
1748 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001749
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001750 if (high_temp != INT_MAX) {
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001751 rc = qpnp_adc_tm_write_reg(chip, reg_low_thr_lsb,
1752 trip_cool_thr0, 1);
1753 if (rc) {
1754 pr_err("adc-tm_tm read threshold err\n");
1755 return rc;
1756 }
1757
1758 rc = qpnp_adc_tm_write_reg(chip, reg_low_thr_msb,
1759 trip_cool_thr1, 1);
1760 if (rc) {
1761 pr_err("adc-tm_tm read threshold err\n");
1762 return rc;
1763 }
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001764 adc_tm->low_thr = tm_config.high_thr_voltage;
1765
1766 rc = qpnp_adc_tm_activate_trip_type(adc_tm,
1767 ADC_TM_TRIP_HIGH_WARM,
1768 THERMAL_TRIP_ACTIVATION_ENABLED);
1769 if (rc) {
1770 pr_err("adc-tm warm activation failed\n");
1771 return rc;
1772 }
1773 } else {
1774 rc = qpnp_adc_tm_activate_trip_type(adc_tm,
1775 ADC_TM_TRIP_HIGH_WARM,
1776 THERMAL_TRIP_ACTIVATION_DISABLED);
1777 if (rc) {
1778 pr_err("adc-tm warm deactivation failed\n");
1779 return rc;
1780 }
1781 }
1782
1783 if (low_temp != INT_MIN) {
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001784 rc = qpnp_adc_tm_write_reg(chip, reg_high_thr_lsb,
1785 trip_warm_thr0, 1);
1786 if (rc) {
1787 pr_err("adc-tm_tm read threshold err\n");
1788 return rc;
1789 }
1790
1791 rc = qpnp_adc_tm_write_reg(chip, reg_high_thr_msb,
1792 trip_warm_thr1, 1);
1793 if (rc) {
1794 pr_err("adc-tm_tm read threshold err\n");
1795 return rc;
1796 }
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001797 adc_tm->high_thr = tm_config.low_thr_voltage;
1798
1799 rc = qpnp_adc_tm_activate_trip_type(adc_tm,
1800 ADC_TM_TRIP_LOW_COOL,
1801 THERMAL_TRIP_ACTIVATION_ENABLED);
1802 if (rc) {
1803 pr_err("adc-tm cool activation failed\n");
1804 return rc;
1805 }
1806 } else {
1807 rc = qpnp_adc_tm_activate_trip_type(adc_tm,
1808 ADC_TM_TRIP_LOW_COOL,
1809 THERMAL_TRIP_ACTIVATION_DISABLED);
1810 if (rc) {
1811 pr_err("adc-tm cool deactivation failed\n");
1812 return rc;
1813 }
1814 }
1815
1816 if ((high_temp != INT_MAX) || (low_temp != INT_MIN)) {
1817 rc = qpnp_adc_tm_set_mode(adc_tm, THERMAL_DEVICE_ENABLED);
1818 if (rc) {
1819 pr_err("sensor enabled failed\n");
1820 return rc;
1821 }
1822 } else {
1823 rc = qpnp_adc_tm_set_mode(adc_tm, THERMAL_DEVICE_DISABLED);
1824 if (rc) {
1825 pr_err("sensor disable failed\n");
1826 return rc;
1827 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001828 }
1829
1830 return 0;
1831}
1832
1833static void notify_battery_therm(struct qpnp_adc_tm_sensor *adc_tm)
1834{
1835 struct qpnp_adc_thr_client_info *client_info = NULL;
1836
1837 list_for_each_entry(client_info,
1838 &adc_tm->thr_list, list) {
1839 /* Batt therm's warm temperature translates to low voltage */
1840 if (client_info->notify_low_thr) {
1841 /* HIGH_STATE = WARM_TEMP for battery client */
1842 client_info->btm_param->threshold_notification(
1843 ADC_TM_WARM_STATE, client_info->btm_param->btm_ctx);
1844 client_info->notify_low_thr = false;
1845 }
1846
1847 /* Batt therm's cool temperature translates to high voltage */
1848 if (client_info->notify_high_thr) {
1849 /* LOW_STATE = COOL_TEMP for battery client */
1850 client_info->btm_param->threshold_notification(
1851 ADC_TM_COOL_STATE, client_info->btm_param->btm_ctx);
1852 client_info->notify_high_thr = false;
1853 }
1854 }
1855}
1856
1857static void notify_clients(struct qpnp_adc_tm_sensor *adc_tm)
1858{
1859 struct qpnp_adc_thr_client_info *client_info = NULL;
1860
1861 list_for_each_entry(client_info,
1862 &adc_tm->thr_list, list) {
1863 /* For non batt therm clients */
1864 if (client_info->notify_low_thr) {
1865 if (client_info->btm_param->threshold_notification
1866 != NULL) {
1867 pr_debug("notify kernel with low state\n");
1868 client_info->btm_param->threshold_notification(
1869 ADC_TM_LOW_STATE,
1870 client_info->btm_param->btm_ctx);
1871 client_info->notify_low_thr = false;
1872 }
1873 }
1874
1875 if (client_info->notify_high_thr) {
1876 if (client_info->btm_param->threshold_notification
1877 != NULL) {
1878 pr_debug("notify kernel with high state\n");
1879 client_info->btm_param->threshold_notification(
1880 ADC_TM_HIGH_STATE,
1881 client_info->btm_param->btm_ctx);
1882 client_info->notify_high_thr = false;
1883 }
1884 }
1885 }
1886}
1887
1888static void notify_adc_tm_fn(struct work_struct *work)
1889{
1890 struct qpnp_adc_tm_sensor *adc_tm = container_of(work,
1891 struct qpnp_adc_tm_sensor, work);
1892 struct qpnp_adc_tm_chip *chip = adc_tm->chip;
1893
1894 if (adc_tm->thermal_node) {
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001895 pr_debug("notifying uspace client\n");
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07001896 of_thermal_handle_trip(adc_tm->tz_dev);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08001897 } else {
1898 if (adc_tm->scale_type == SCALE_RBATT_THERM)
1899 notify_battery_therm(adc_tm);
1900 else
1901 notify_clients(adc_tm);
1902 }
1903
1904 atomic_dec(&chip->wq_cnt);
1905}
1906
Prasad Malisetty358dda32018-02-20 06:14:26 +05301907static int qpnp_adc_tm_recalib_request_check(struct qpnp_adc_tm_chip *chip,
1908 int sensor_num, u8 status_high, u8 *notify_check)
1909{
1910 int rc = 0;
1911 u8 sensor_mask = 0, mode_ctl = 0;
1912 int32_t old_thr = 0, new_thr = 0;
1913 uint32_t channel, btm_chan_num, scale_type;
1914 struct qpnp_vadc_result result;
1915 struct qpnp_adc_thr_client_info *client_info = NULL;
1916 struct list_head *thr_list;
1917 bool status = false;
1918
1919 if (!chip->adc_tm_recalib_check) {
1920 *notify_check = 1;
1921 return rc;
1922 }
1923
1924 list_for_each(thr_list, &chip->sensor[sensor_num].thr_list) {
1925 client_info = list_entry(thr_list,
1926 struct qpnp_adc_thr_client_info, list);
1927 channel = client_info->btm_param->channel;
1928 btm_chan_num = chip->sensor[sensor_num].btm_channel_num;
1929 sensor_mask = 1 << sensor_num;
1930
1931 rc = qpnp_vadc_read(chip->vadc_dev, channel, &result);
1932 if (rc < 0) {
1933 pr_err("failure to read vadc channel=%d\n",
1934 client_info->btm_param->channel);
1935 goto fail;
1936 }
1937 new_thr = result.physical;
1938
1939 if (status_high)
1940 old_thr = client_info->btm_param->high_thr;
1941 else
1942 old_thr = client_info->btm_param->low_thr;
1943
1944 if (new_thr > old_thr)
1945 status = (status_high) ? true : false;
1946 else
1947 status = (status_high) ? false : true;
1948
1949 pr_debug(
1950 "recalib:sen=%d, new_thr=%d, new_thr_adc_code=0x%x, old_thr=%d status=%d valid_status=%d\n",
1951 sensor_num, new_thr, result.adc_code,
1952 old_thr, status_high, status);
1953
1954 rc = qpnp_adc_tm_read_thr_value(chip, btm_chan_num);
1955 if (rc < 0) {
1956 pr_err("adc-tm thresholds read failed\n");
1957 goto fail;
1958 }
1959
1960 if (status) {
1961 *notify_check = 1;
1962 pr_debug("Client can be notify\n");
1963 return rc;
1964 }
1965
1966 pr_debug("Client can not be notify, restart measurement\n");
1967 /* Set measurement in single measurement mode */
1968 mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
1969 rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
1970 if (rc < 0) {
1971 pr_err("adc-tm single mode select failed\n");
1972 goto fail;
1973 }
1974
1975 /* Disable bank */
1976 rc = qpnp_adc_tm_disable(chip);
1977 if (rc < 0) {
1978 pr_err("adc-tm disable failed\n");
1979 goto fail;
1980 }
1981
1982 /* Check if a conversion is in progress */
1983 rc = qpnp_adc_tm_req_sts_check(chip);
1984 if (rc < 0) {
1985 pr_err("adc-tm req_sts check failed\n");
1986 goto fail;
1987 }
1988
1989 rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
1990 sensor_mask, false);
1991 if (rc < 0) {
1992 pr_err("low threshold int write failed\n");
1993 goto fail;
1994 }
1995
1996 rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_HIGH_THR_INT_EN,
1997 sensor_mask, false);
1998 if (rc < 0) {
1999 pr_err("high threshold int enable failed\n");
2000 goto fail;
2001 }
2002
2003 rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
2004 sensor_mask, false);
2005 if (rc < 0) {
2006 pr_err("multi measurement en failed\n");
2007 goto fail;
2008 }
2009
2010 /* restart measurement */
2011 scale_type = chip->sensor[sensor_num].scale_type;
2012 chip->adc->amux_prop->amux_channel = channel;
2013 chip->adc->amux_prop->decimation =
2014 chip->adc->adc_channels[sensor_num].adc_decimation;
2015 chip->adc->amux_prop->hw_settle_time =
2016 chip->adc->adc_channels[sensor_num].hw_settle_time;
2017 chip->adc->amux_prop->fast_avg_setup =
2018 chip->adc->adc_channels[sensor_num].fast_avg_setup;
2019 chip->adc->amux_prop->mode_sel =
2020 ADC_OP_MEASUREMENT_INTERVAL << QPNP_OP_MODE_SHIFT;
2021 adc_tm_rscale_fn[scale_type].chan(chip->vadc_dev,
2022 client_info->btm_param,
2023 &chip->adc->amux_prop->chan_prop->low_thr,
2024 &chip->adc->amux_prop->chan_prop->high_thr);
2025 qpnp_adc_tm_add_to_list(chip, sensor_num,
2026 client_info->btm_param,
2027 chip->adc->amux_prop->chan_prop);
2028 chip->adc->amux_prop->chan_prop->tm_channel_select =
2029 chip->sensor[sensor_num].btm_channel_num;
2030 chip->adc->amux_prop->chan_prop->state_request =
2031 client_info->btm_param->state_request;
2032
2033 rc = qpnp_adc_tm_configure(chip, chip->adc->amux_prop);
2034 if (rc) {
2035 pr_err("adc-tm configure failed with %d\n", rc);
2036 goto fail;
2037 }
2038 *notify_check = 0;
2039 pr_debug("BTM channel reconfigured for measuremnt\n");
2040 }
2041fail:
2042 return rc;
2043}
2044
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002045static int qpnp_adc_tm_disable_rearm_high_thresholds(
2046 struct qpnp_adc_tm_chip *chip, int sensor_num)
2047{
2048
2049 struct qpnp_adc_thr_client_info *client_info = NULL;
2050 struct list_head *thr_list;
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002051 uint32_t btm_chan_num = 0, btm_chan_idx = 0;
Prasad Malisetty358dda32018-02-20 06:14:26 +05302052 u8 sensor_mask = 0, notify_check = 0;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002053 int rc = 0;
2054
2055 btm_chan_num = chip->sensor[sensor_num].btm_channel_num;
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002056 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan_num, &btm_chan_idx);
2057 if (rc < 0) {
2058 pr_err("Invalid btm channel idx\n");
2059 return rc;
2060 }
2061
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002062 pr_debug("high:sen:%d, hs:0x%x, ls:0x%x, meas_en:0x%x\n",
2063 sensor_num, chip->th_info.adc_tm_high_enable,
2064 chip->th_info.adc_tm_low_enable,
2065 chip->th_info.qpnp_adc_tm_meas_en);
2066 if (!chip->sensor[sensor_num].thermal_node) {
2067 /*
2068 * For non thermal registered clients such as usb_id,
2069 * vbatt, pmic_therm
2070 */
2071 sensor_mask = 1 << sensor_num;
2072 pr_debug("non thermal node - mask:%x\n", sensor_mask);
Prasad Malisetty358dda32018-02-20 06:14:26 +05302073 if (!chip->adc_tm_hc) {
2074 rc = qpnp_adc_tm_recalib_request_check(chip,
2075 sensor_num, true, &notify_check);
2076 if (rc < 0 || !notify_check) {
2077 pr_debug("Calib recheck re-armed rc=%d\n", rc);
2078 chip->th_info.adc_tm_high_enable = 0;
2079 return rc;
2080 }
2081 } else {
2082 rc = qpnp_adc_tm_reg_update(chip,
2083 QPNP_BTM_Mn_EN(btm_chan_idx),
2084 QPNP_BTM_Mn_HIGH_THR_INT_EN, false);
2085 if (rc < 0) {
2086 pr_err("high threshold int update failed\n");
2087 return rc;
2088 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002089 }
Prasad Malisetty358dda32018-02-20 06:14:26 +05302090 } else {
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002091 /*
2092 * Uses the thermal sysfs registered device to disable
2093 * the corresponding high voltage threshold which
2094 * is triggered by low temp
2095 */
2096 sensor_mask = 1 << sensor_num;
2097 pr_debug("thermal node with mask:%x\n", sensor_mask);
2098 rc = qpnp_adc_tm_activate_trip_type(
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002099 &chip->sensor[sensor_num],
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002100 ADC_TM_TRIP_LOW_COOL,
2101 THERMAL_TRIP_ACTIVATION_DISABLED);
2102 if (rc < 0) {
2103 pr_err("notify error:%d\n", sensor_num);
2104 return rc;
2105 }
2106 }
2107 list_for_each(thr_list, &chip->sensor[sensor_num].thr_list) {
2108 client_info = list_entry(thr_list,
2109 struct qpnp_adc_thr_client_info, list);
2110 if (client_info->high_thr_set) {
2111 client_info->high_thr_set = false;
2112 client_info->notify_high_thr = true;
2113 if (client_info->state_req_copy ==
2114 ADC_TM_HIGH_LOW_THR_ENABLE)
2115 client_info->state_req_copy =
2116 ADC_TM_LOW_THR_ENABLE;
2117 else
2118 client_info->state_req_copy =
2119 ADC_TM_HIGH_THR_DISABLE;
2120 }
2121 }
2122 qpnp_adc_tm_manage_thresholds(chip, sensor_num, btm_chan_num);
2123
Prasad Malisetty358dda32018-02-20 06:14:26 +05302124 if (!chip->adc_tm_hc) {
2125 rc = qpnp_adc_tm_reg_update(chip,
2126 QPNP_ADC_TM_MULTI_MEAS_EN,
2127 sensor_mask, false);
2128 if (rc < 0) {
2129 pr_err("multi meas disable failed\n");
2130 return rc;
2131 }
2132 } else {
2133 rc = qpnp_adc_tm_reg_update(chip,
2134 QPNP_BTM_Mn_EN(sensor_num),
2135 QPNP_BTM_Mn_MEAS_EN, false);
2136 if (rc < 0) {
2137 pr_err("multi meas disable failed\n");
2138 return rc;
2139 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002140 }
2141
2142 rc = qpnp_adc_tm_enable_if_channel_meas(chip);
2143 if (rc < 0) {
2144 pr_err("re-enabling measurement failed\n");
2145 return rc;
2146 }
2147
Siddartha Mohanadoss336fff42017-12-11 16:51:00 -08002148 if (!queue_work(chip->sensor[sensor_num].req_wq,
2149 &chip->sensor[sensor_num].work)) {
2150 /* The item is already queued, reduce the count */
2151 atomic_dec(&chip->wq_cnt);
2152 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002153
2154 return rc;
2155}
2156
2157static int qpnp_adc_tm_disable_rearm_low_thresholds(
2158 struct qpnp_adc_tm_chip *chip, int sensor_num)
2159{
2160 struct qpnp_adc_thr_client_info *client_info = NULL;
2161 struct list_head *thr_list;
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002162 uint32_t btm_chan_num = 0, btm_chan_idx = 0;
Prasad Malisetty358dda32018-02-20 06:14:26 +05302163 u8 sensor_mask = 0, notify_check = 0;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002164 int rc = 0;
2165
2166 btm_chan_num = chip->sensor[sensor_num].btm_channel_num;
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002167 rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan_num, &btm_chan_idx);
2168 if (rc < 0) {
2169 pr_err("Invalid btm channel idx\n");
2170 return rc;
2171 }
2172
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002173 pr_debug("low:sen:%d, hs:0x%x, ls:0x%x, meas_en:0x%x\n",
2174 sensor_num, chip->th_info.adc_tm_high_enable,
2175 chip->th_info.adc_tm_low_enable,
2176 chip->th_info.qpnp_adc_tm_meas_en);
2177 if (!chip->sensor[sensor_num].thermal_node) {
2178 /*
2179 * For non thermal registered clients such as usb_id,
2180 * vbatt, pmic_therm
2181 */
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002182 sensor_mask = 1 << sensor_num;
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002183 pr_debug("non thermal node - mask:%x\n", sensor_mask);
Prasad Malisetty358dda32018-02-20 06:14:26 +05302184 if (!chip->adc_tm_hc) {
2185 rc = qpnp_adc_tm_recalib_request_check(chip,
2186 sensor_num, false, &notify_check);
2187 if (rc < 0 || !notify_check) {
2188 pr_debug("Calib recheck re-armed rc=%d\n", rc);
2189 chip->th_info.adc_tm_low_enable = 0;
2190 return rc;
2191 }
2192 } else {
2193 rc = qpnp_adc_tm_reg_update(chip,
2194 QPNP_BTM_Mn_EN(btm_chan_idx),
2195 QPNP_BTM_Mn_LOW_THR_INT_EN, false);
2196 if (rc < 0) {
2197 pr_err("low threshold int update failed\n");
2198 return rc;
2199 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002200 }
2201 } else {
2202 /*
2203 * Uses the thermal sysfs registered device to disable
2204 * the corresponding high voltage threshold which
2205 * is triggered by low temp
2206 */
2207 sensor_mask = 1 << sensor_num;
2208 pr_debug("thermal node with mask:%x\n", sensor_mask);
2209 rc = qpnp_adc_tm_activate_trip_type(
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002210 &chip->sensor[sensor_num],
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002211 ADC_TM_TRIP_HIGH_WARM,
2212 THERMAL_TRIP_ACTIVATION_DISABLED);
2213 if (rc < 0) {
2214 pr_err("notify error:%d\n", sensor_num);
2215 return rc;
2216 }
2217 }
2218 list_for_each(thr_list, &chip->sensor[sensor_num].thr_list) {
2219 client_info = list_entry(thr_list,
2220 struct qpnp_adc_thr_client_info, list);
2221 if (client_info->low_thr_set) {
2222 client_info->low_thr_set = false;
2223 client_info->notify_low_thr = true;
2224 if (client_info->state_req_copy ==
2225 ADC_TM_HIGH_LOW_THR_ENABLE)
2226 client_info->state_req_copy =
2227 ADC_TM_HIGH_THR_ENABLE;
2228 else
2229 client_info->state_req_copy =
2230 ADC_TM_LOW_THR_DISABLE;
2231 }
2232 }
2233 qpnp_adc_tm_manage_thresholds(chip, sensor_num, btm_chan_num);
2234
Prasad Malisetty358dda32018-02-20 06:14:26 +05302235 if (!chip->adc_tm_hc) {
2236 rc = qpnp_adc_tm_reg_update(chip,
2237 QPNP_ADC_TM_MULTI_MEAS_EN,
2238 sensor_mask, false);
2239 if (rc < 0) {
2240 pr_err("multi meas disable failed\n");
2241 return rc;
2242 }
2243 } else {
2244 rc = qpnp_adc_tm_reg_update(chip,
2245 QPNP_BTM_Mn_EN(sensor_num),
2246 QPNP_BTM_Mn_MEAS_EN, false);
2247 if (rc < 0) {
2248 pr_err("multi meas disable failed\n");
2249 return rc;
2250 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002251 }
2252
2253 rc = qpnp_adc_tm_enable_if_channel_meas(chip);
2254 if (rc < 0) {
2255 pr_err("re-enabling measurement failed\n");
2256 return rc;
2257 }
2258
Siddartha Mohanadoss336fff42017-12-11 16:51:00 -08002259 if (!queue_work(chip->sensor[sensor_num].req_wq,
2260 &chip->sensor[sensor_num].work)) {
2261 /* The item is already queued, reduce the count */
2262 atomic_dec(&chip->wq_cnt);
2263 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002264
2265 return rc;
2266}
2267
2268static int qpnp_adc_tm_read_status(struct qpnp_adc_tm_chip *chip)
2269{
Jishnu Prakash71c39e32018-04-02 19:16:39 +05302270 int rc = 0, sensor_notify_num = 0, i = 0, sensor_num = 0;
2271 unsigned long flags;
2272
2273 if (qpnp_adc_tm_is_valid(chip))
2274 return -ENODEV;
2275
2276 mutex_lock(&chip->adc->adc_lock);
2277
2278 rc = qpnp_adc_tm_req_sts_check(chip);
2279 if (rc) {
2280 pr_err("adc-tm-tm req sts check failed with %d\n", rc);
2281 goto fail;
2282 }
2283
2284 if (chip->th_info.adc_tm_high_enable) {
2285 spin_lock_irqsave(&chip->th_info.adc_tm_high_lock, flags);
2286 sensor_notify_num = chip->th_info.adc_tm_high_enable;
2287 chip->th_info.adc_tm_high_enable = 0;
2288 spin_unlock_irqrestore(&chip->th_info.adc_tm_high_lock, flags);
2289 while (i < chip->max_channels_available) {
2290 if ((sensor_notify_num & 0x1) == 1) {
2291 sensor_num = i;
2292 rc = qpnp_adc_tm_disable_rearm_high_thresholds(
2293 chip, sensor_num);
2294 if (rc < 0) {
2295 pr_err("rearm threshold failed\n");
2296 goto fail;
2297 }
2298 }
2299 sensor_notify_num >>= 1;
2300 i++;
2301 }
2302 }
2303
2304 if (chip->th_info.adc_tm_low_enable) {
2305 spin_lock_irqsave(&chip->th_info.adc_tm_low_lock, flags);
2306 sensor_notify_num = chip->th_info.adc_tm_low_enable;
2307 chip->th_info.adc_tm_low_enable = 0;
2308 spin_unlock_irqrestore(&chip->th_info.adc_tm_low_lock, flags);
2309 i = 0;
2310 while (i < chip->max_channels_available) {
2311 if ((sensor_notify_num & 0x1) == 1) {
2312 sensor_num = i;
2313 rc = qpnp_adc_tm_disable_rearm_low_thresholds(
2314 chip, sensor_num);
2315 if (rc < 0) {
2316 pr_err("rearm threshold failed\n");
2317 goto fail;
2318 }
2319 }
2320 sensor_notify_num >>= 1;
2321 i++;
2322 }
2323 }
2324
2325fail:
2326 mutex_unlock(&chip->adc->adc_lock);
2327 if (rc < 0)
2328 atomic_dec(&chip->wq_cnt);
2329
2330 return rc;
2331}
2332
2333static int qpnp_adc_tm_hc_read_status(struct qpnp_adc_tm_chip *chip)
2334{
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002335 int rc = 0, sensor_num = 0;
2336
2337 if (qpnp_adc_tm_is_valid(chip))
2338 return -ENODEV;
2339
2340 pr_debug("%s\n", __func__);
2341
2342 mutex_lock(&chip->adc->adc_lock);
2343
Prasad Malisetty358dda32018-02-20 06:14:26 +05302344 if (!chip->adc_tm_hc) {
2345 rc = qpnp_adc_tm_req_sts_check(chip);
2346 if (rc) {
2347 pr_err("adc-tm-tm req sts check failed with %d\n", rc);
2348 goto fail;
2349 }
2350 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002351 while (sensor_num < chip->max_channels_available) {
2352 if (chip->sensor[sensor_num].high_thr_triggered) {
2353 rc = qpnp_adc_tm_disable_rearm_high_thresholds(
2354 chip, sensor_num);
2355 if (rc) {
2356 pr_err("rearm threshold failed\n");
2357 goto fail;
2358 }
2359 chip->sensor[sensor_num].high_thr_triggered = false;
2360 }
2361 sensor_num++;
2362 }
2363
2364 sensor_num = 0;
2365 while (sensor_num < chip->max_channels_available) {
2366 if (chip->sensor[sensor_num].low_thr_triggered) {
2367 rc = qpnp_adc_tm_disable_rearm_low_thresholds(
2368 chip, sensor_num);
2369 if (rc) {
2370 pr_err("rearm threshold failed\n");
2371 goto fail;
2372 }
2373 chip->sensor[sensor_num].low_thr_triggered = false;
2374 }
2375 sensor_num++;
2376 }
2377
2378fail:
2379 mutex_unlock(&chip->adc->adc_lock);
2380
2381 if (rc < 0 || (!chip->th_info.adc_tm_high_enable &&
2382 !chip->th_info.adc_tm_low_enable))
2383 atomic_dec(&chip->wq_cnt);
2384
2385 return rc;
2386}
2387
2388static void qpnp_adc_tm_high_thr_work(struct work_struct *work)
2389{
2390 struct qpnp_adc_tm_chip *chip = container_of(work,
2391 struct qpnp_adc_tm_chip, trigger_high_thr_work);
2392 int rc;
2393
2394 /* disable the vote if applicable */
2395 if (chip->adc_vote_enable && chip->adc->hkadc_ldo &&
2396 chip->adc->hkadc_ldo_ok) {
2397 qpnp_adc_disable_voltage(chip->adc);
2398 chip->adc_vote_enable = false;
2399 }
2400
2401 pr_debug("thr:0x%x\n", chip->th_info.adc_tm_high_enable);
2402
Jishnu Prakash71c39e32018-04-02 19:16:39 +05302403 if (!chip->adc_tm_hc) {
2404 rc = qpnp_adc_tm_read_status(chip);
2405 if (rc < 0)
2406 pr_err("adc-tm high thr work failed\n");
2407 } else {
2408 rc = qpnp_adc_tm_hc_read_status(chip);
2409 if (rc < 0)
2410 pr_err("adc-tm-hc high thr work failed\n");
2411 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002412}
2413
Prasad Malisetty358dda32018-02-20 06:14:26 +05302414static irqreturn_t qpnp_adc_tm_high_thr_isr(int irq, void *data)
2415{
2416 struct qpnp_adc_tm_chip *chip = data;
2417 u8 mode_ctl = 0, status1 = 0, sensor_mask = 0;
2418 int rc = 0, sensor_notify_num = 0, i = 0, sensor_num = 0;
2419
2420 mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
2421 /* Set measurement in single measurement mode */
2422 qpnp_adc_tm_mode_select(chip, mode_ctl);
2423
2424 qpnp_adc_tm_disable(chip);
2425
2426 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS1, &status1, 1);
2427 if (rc) {
2428 pr_err("adc-tm read status1 failed\n");
2429 return IRQ_HANDLED;
2430 }
2431
2432 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS_HIGH,
2433 &chip->th_info.status_high, 1);
2434 if (rc) {
2435 pr_err("adc-tm-tm read status high failed with %d\n", rc);
2436 return IRQ_HANDLED;
2437 }
2438
2439 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_HIGH_THR_INT_EN,
2440 &chip->th_info.adc_tm_high_thr_set, 1);
2441 if (rc) {
2442 pr_err("adc-tm-tm read high thr failed with %d\n", rc);
2443 return IRQ_HANDLED;
2444 }
2445
2446 /* Check which interrupt threshold is lower and measure against the
2447 * enabled channel
2448 */
2449 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
2450 &chip->th_info.qpnp_adc_tm_meas_en, 1);
2451 if (rc) {
2452 pr_err("adc-tm-tm read status high failed with %d\n", rc);
2453 return IRQ_HANDLED;
2454 }
2455
2456 chip->th_info.adc_tm_high_enable = chip->th_info.qpnp_adc_tm_meas_en &
2457 chip->th_info.status_high;
2458 chip->th_info.adc_tm_high_enable &= chip->th_info.adc_tm_high_thr_set;
2459
2460 sensor_notify_num = chip->th_info.adc_tm_high_enable;
2461 while (i < chip->max_channels_available) {
2462 if ((sensor_notify_num & 0x1) == 1)
2463 sensor_num = i;
2464 sensor_notify_num >>= 1;
2465 i++;
2466 }
2467
2468 if (!chip->sensor[sensor_num].thermal_node) {
2469 sensor_mask = 1 << sensor_num;
2470 rc = qpnp_adc_tm_reg_update(chip,
2471 QPNP_ADC_TM_HIGH_THR_INT_EN,
2472 sensor_mask, false);
2473 if (rc < 0) {
2474 pr_err("high threshold int read failed\n");
2475 return IRQ_HANDLED;
2476 }
2477 } else {
2478 /*
2479 * Uses the thermal sysfs registered device to disable
2480 * the corresponding high voltage threshold which
2481 * is triggered by low temp
2482 */
2483 pr_debug("thermal node with mask:%x\n", sensor_mask);
2484 rc = qpnp_adc_tm_activate_trip_type(
2485 &chip->sensor[sensor_num],
2486 ADC_TM_TRIP_LOW_COOL,
2487 THERMAL_TRIP_ACTIVATION_DISABLED);
2488 if (rc < 0) {
2489 pr_err("notify error:%d\n", sensor_num);
2490 return IRQ_HANDLED;
2491 }
2492 }
2493
2494 atomic_inc(&chip->wq_cnt);
2495 queue_work(chip->high_thr_wq, &chip->trigger_high_thr_work);
2496
2497 return IRQ_HANDLED;
2498}
2499
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002500static void qpnp_adc_tm_low_thr_work(struct work_struct *work)
2501{
2502 struct qpnp_adc_tm_chip *chip = container_of(work,
2503 struct qpnp_adc_tm_chip, trigger_low_thr_work);
2504 int rc;
2505
2506 /* disable the vote if applicable */
2507 if (chip->adc_vote_enable && chip->adc->hkadc_ldo &&
2508 chip->adc->hkadc_ldo_ok) {
2509 qpnp_adc_disable_voltage(chip->adc);
2510 chip->adc_vote_enable = false;
2511 }
2512
2513 pr_debug("thr:0x%x\n", chip->th_info.adc_tm_low_enable);
2514
Jishnu Prakash71c39e32018-04-02 19:16:39 +05302515 if (!chip->adc_tm_hc) {
2516 rc = qpnp_adc_tm_read_status(chip);
2517 if (rc < 0)
2518 pr_err("adc-tm low thr work failed\n");
2519 } else {
2520 rc = qpnp_adc_tm_hc_read_status(chip);
2521 if (rc < 0)
2522 pr_err("adc-tm-hc low thr work failed\n");
2523 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002524}
2525
Prasad Malisetty358dda32018-02-20 06:14:26 +05302526static irqreturn_t qpnp_adc_tm_low_thr_isr(int irq, void *data)
2527{
2528 struct qpnp_adc_tm_chip *chip = data;
2529 u8 mode_ctl = 0, status1 = 0, sensor_mask = 0;
2530 int rc = 0, sensor_notify_num = 0, i = 0, sensor_num = 0;
2531
2532 mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
2533 /* Set measurement in single measurement mode */
2534 qpnp_adc_tm_mode_select(chip, mode_ctl);
2535
2536 qpnp_adc_tm_disable(chip);
2537
2538 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS1, &status1, 1);
2539 if (rc) {
2540 pr_err("adc-tm read status1 failed\n");
2541 return IRQ_HANDLED;
2542 }
2543
2544 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS_LOW,
2545 &chip->th_info.status_low, 1);
2546 if (rc) {
2547 pr_err("adc-tm-tm read status low failed with %d\n", rc);
2548 return IRQ_HANDLED;
2549 }
2550
2551 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
2552 &chip->th_info.adc_tm_low_thr_set, 1);
2553 if (rc) {
2554 pr_err("adc-tm-tm read low thr failed with %d\n", rc);
2555 return IRQ_HANDLED;
2556 }
2557
2558 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
2559 &chip->th_info.qpnp_adc_tm_meas_en, 1);
2560 if (rc) {
2561 pr_err("adc-tm-tm read status high failed with %d\n", rc);
2562 return IRQ_HANDLED;
2563 }
2564
2565 chip->th_info.adc_tm_low_enable = chip->th_info.qpnp_adc_tm_meas_en &
2566 chip->th_info.status_low;
2567 chip->th_info.adc_tm_low_enable &= chip->th_info.adc_tm_low_thr_set;
2568
2569 sensor_notify_num = chip->th_info.adc_tm_low_enable;
2570 while (i < chip->max_channels_available) {
2571 if ((sensor_notify_num & 0x1) == 1)
2572 sensor_num = i;
2573 sensor_notify_num >>= 1;
2574 i++;
2575 }
2576
2577 if (!chip->sensor[sensor_num].thermal_node) {
2578 sensor_mask = 1 << sensor_num;
2579 rc = qpnp_adc_tm_reg_update(chip,
2580 QPNP_ADC_TM_LOW_THR_INT_EN,
2581 sensor_mask, false);
2582 if (rc < 0) {
2583 pr_err("low threshold int read failed\n");
2584 return IRQ_HANDLED;
2585 }
2586 } else {
2587 /*
2588 * Uses the thermal sysfs registered device to disable
2589 * the corresponding low voltage threshold which
2590 * is triggered by high temp
2591 */
2592 pr_debug("thermal node with mask:%x\n", sensor_mask);
2593 rc = qpnp_adc_tm_activate_trip_type(
2594 &chip->sensor[sensor_num],
2595 ADC_TM_TRIP_HIGH_WARM,
2596 THERMAL_TRIP_ACTIVATION_DISABLED);
2597 if (rc < 0) {
2598 pr_err("notify error:%d\n", sensor_num);
2599 return IRQ_HANDLED;
2600 }
2601 }
2602
2603 atomic_inc(&chip->wq_cnt);
2604 queue_work(chip->low_thr_wq, &chip->trigger_low_thr_work);
2605
2606 return IRQ_HANDLED;
2607}
2608
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002609static int qpnp_adc_tm_rc_check_sensor_trip(struct qpnp_adc_tm_chip *chip,
2610 u8 status_low, u8 status_high, int i,
2611 int *sensor_low_notify_num, int *sensor_high_notify_num)
2612{
2613 int rc = 0;
2614 u8 ctl = 0, sensor_mask = 0;
2615
2616 if (((status_low & 0x1) == 1) || ((status_high & 0x1) == 1)) {
2617 rc = qpnp_adc_tm_read_reg(chip,
2618 QPNP_BTM_Mn_EN(i), &ctl, 1);
2619 if (rc) {
2620 pr_err("ctl read failed with %d\n", rc);
2621 return IRQ_HANDLED;
2622 }
2623
2624 if ((status_low & 0x1) && (ctl & QPNP_BTM_Mn_MEAS_EN)
2625 && (ctl & QPNP_BTM_Mn_LOW_THR_INT_EN)) {
2626 /* Mask the corresponding low threshold interrupt en */
2627 if (!chip->sensor[i].thermal_node) {
2628 rc = qpnp_adc_tm_reg_update(chip,
2629 QPNP_BTM_Mn_EN(i),
2630 QPNP_BTM_Mn_LOW_THR_INT_EN, false);
2631 if (rc < 0) {
2632 pr_err("low thr_int en failed\n");
2633 return IRQ_HANDLED;
2634 }
2635 } else {
2636 /*
2637 * Uses the thermal sysfs registered device to disable
2638 * the corresponding low voltage threshold which
2639 * is triggered by high temp
2640 */
2641 pr_debug("thermal node with mask:%x\n", sensor_mask);
2642 rc = qpnp_adc_tm_activate_trip_type(
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002643 &chip->sensor[i],
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002644 ADC_TM_TRIP_HIGH_WARM,
2645 THERMAL_TRIP_ACTIVATION_DISABLED);
2646 if (rc < 0) {
2647 pr_err("notify error:%d\n", i);
2648 return IRQ_HANDLED;
2649 }
2650 }
2651 *sensor_low_notify_num |= (status_low & 0x1);
2652 chip->sensor[i].low_thr_triggered = true;
2653 }
2654
2655 if ((status_high & 0x1) && (ctl & QPNP_BTM_Mn_MEAS_EN) &&
2656 (ctl & QPNP_BTM_Mn_HIGH_THR_INT_EN)) {
2657 /* Mask the corresponding high threshold interrupt en */
2658 if (!chip->sensor[i].thermal_node) {
2659 rc = qpnp_adc_tm_reg_update(chip,
2660 QPNP_BTM_Mn_EN(i),
2661 QPNP_BTM_Mn_HIGH_THR_INT_EN, false);
2662 if (rc < 0) {
2663 pr_err("high thr_int en failed\n");
2664 return IRQ_HANDLED;
2665 }
2666 } else {
2667 /*
2668 * Uses the thermal sysfs registered device to disable
2669 * the corresponding high voltage threshold which
2670 * is triggered by low temp
2671 */
2672 pr_debug("thermal node with mask:%x\n", i);
2673 rc = qpnp_adc_tm_activate_trip_type(
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002674 &chip->sensor[i],
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002675 ADC_TM_TRIP_LOW_COOL,
2676 THERMAL_TRIP_ACTIVATION_DISABLED);
2677 if (rc < 0) {
2678 pr_err("notify error:%d\n", i);
2679 return IRQ_HANDLED;
2680 }
2681 }
2682 *sensor_high_notify_num |= (status_high & 0x1);
2683 chip->sensor[i].high_thr_triggered = true;
2684 }
2685 }
2686
2687 return rc;
2688}
2689
2690static irqreturn_t qpnp_adc_tm_rc_thr_isr(int irq, void *data)
2691{
2692 struct qpnp_adc_tm_chip *chip = data;
2693 u8 status_low = 0, status_high = 0;
2694 int rc = 0, sensor_low_notify_num = 0, i = 0;
2695 int sensor_high_notify_num = 0;
2696
2697 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS_LOW,
2698 &status_low, 1);
2699 if (rc) {
2700 pr_err("adc-tm-tm read status low failed with %d\n", rc);
2701 return IRQ_HANDLED;
2702 }
2703
2704 if (status_low)
2705 chip->th_info.adc_tm_low_enable = status_low;
2706
2707 rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS_HIGH,
2708 &status_high, 1);
2709 if (rc) {
2710 pr_err("adc-tm-tm read status high failed with %d\n", rc);
2711 return IRQ_HANDLED;
2712 }
2713
2714 if (status_high)
2715 chip->th_info.adc_tm_high_enable = status_high;
2716
2717 while (i < chip->max_channels_available) {
2718 rc = qpnp_adc_tm_rc_check_sensor_trip(chip,
2719 status_low, status_high, i,
2720 &sensor_low_notify_num,
2721 &sensor_high_notify_num);
2722 if (rc) {
2723 pr_err("Sensor trip read failed\n");
2724 return IRQ_HANDLED;
2725 }
2726 status_low >>= 1;
2727 status_high >>= 1;
2728 i++;
2729 }
2730
2731 if (sensor_low_notify_num) {
Siddartha Mohanadoss336fff42017-12-11 16:51:00 -08002732 if (queue_work(chip->low_thr_wq, &chip->trigger_low_thr_work))
2733 atomic_inc(&chip->wq_cnt);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002734 }
2735
2736 if (sensor_high_notify_num) {
Siddartha Mohanadoss336fff42017-12-11 16:51:00 -08002737 if (queue_work(chip->high_thr_wq,
2738 &chip->trigger_high_thr_work))
2739 atomic_inc(&chip->wq_cnt);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002740 }
2741
2742 return IRQ_HANDLED;
2743}
2744
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002745static int qpnp_adc_read_temp(void *data, int *temp)
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002746{
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002747 struct qpnp_adc_tm_sensor *adc_tm_sensor = data;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002748 struct qpnp_adc_tm_chip *chip = adc_tm_sensor->chip;
2749 struct qpnp_vadc_result result;
2750 int rc = 0;
2751
2752 rc = qpnp_vadc_read(chip->vadc_dev,
2753 adc_tm_sensor->vadc_channel_num, &result);
2754 if (rc)
2755 return rc;
2756
2757 *temp = result.physical;
2758
2759 return rc;
2760}
2761
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002762static struct thermal_zone_of_device_ops qpnp_adc_tm_thermal_ops = {
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002763 .get_temp = qpnp_adc_read_temp,
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002764 .set_trips = qpnp_adc_tm_set_trip_temp,
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002765};
2766
2767int32_t qpnp_adc_tm_channel_measure(struct qpnp_adc_tm_chip *chip,
2768 struct qpnp_adc_tm_btm_param *param)
2769{
2770 uint32_t channel, amux_prescaling, dt_index = 0, scale_type = 0;
2771 int rc = 0, i = 0, version = 0;
2772 bool chan_found = false;
2773
2774 if (qpnp_adc_tm_is_valid(chip)) {
2775 pr_err("chip not valid\n");
2776 return -ENODEV;
2777 }
2778
2779 if (param->threshold_notification == NULL) {
2780 pr_debug("No notification for high/low temp??\n");
2781 return -EINVAL;
2782 }
2783
2784 mutex_lock(&chip->adc->adc_lock);
2785
2786 channel = param->channel;
2787
2788 if (channel == VSYS) {
2789 version = qpnp_adc_get_revid_version(chip->dev);
2790 if (version == QPNP_REV_ID_PM8950_1_0) {
2791 pr_debug("Channel not supported\n");
2792 rc = -EINVAL;
2793 goto fail_unlock;
2794 }
2795 }
2796
2797 while (i < chip->max_channels_available) {
2798 if (chip->adc->adc_channels[i].channel_num ==
2799 channel) {
2800 dt_index = i;
2801 chan_found = true;
2802 i++;
2803 } else
2804 i++;
2805 }
2806
2807 if (!chan_found) {
2808 pr_err("not a valid ADC_TM channel\n");
2809 rc = -EINVAL;
2810 goto fail_unlock;
2811 }
2812
2813 rc = qpnp_adc_tm_check_revision(chip,
2814 chip->sensor[dt_index].btm_channel_num);
2815 if (rc < 0)
2816 goto fail_unlock;
2817
2818 scale_type = chip->adc->adc_channels[dt_index].adc_scale_fn;
2819 if (scale_type >= SCALE_RSCALE_NONE) {
2820 rc = -EBADF;
2821 goto fail_unlock;
2822 }
2823
2824
2825 amux_prescaling =
2826 chip->adc->adc_channels[dt_index].chan_path_prescaling;
2827
2828 if (amux_prescaling >= PATH_SCALING_NONE) {
2829 rc = -EINVAL;
2830 goto fail_unlock;
2831 }
2832
2833 pr_debug("channel:%d, scale_type:%d, dt_idx:%d",
2834 channel, scale_type, dt_index);
2835 param->gain_num = qpnp_vadc_amux_scaling_ratio[amux_prescaling].num;
2836 param->gain_den = qpnp_vadc_amux_scaling_ratio[amux_prescaling].den;
2837 param->adc_tm_hc = chip->adc_tm_hc;
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07002838 param->full_scale_code = chip->adc->adc_prop->full_scale_code;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002839 chip->adc->amux_prop->amux_channel = channel;
2840 chip->adc->amux_prop->decimation =
2841 chip->adc->adc_channels[dt_index].adc_decimation;
2842 chip->adc->amux_prop->hw_settle_time =
2843 chip->adc->adc_channels[dt_index].hw_settle_time;
2844 chip->adc->amux_prop->fast_avg_setup =
2845 chip->adc->adc_channels[dt_index].fast_avg_setup;
2846 chip->adc->amux_prop->mode_sel =
2847 ADC_OP_MEASUREMENT_INTERVAL << QPNP_OP_MODE_SHIFT;
2848 adc_tm_rscale_fn[scale_type].chan(chip->vadc_dev, param,
2849 &chip->adc->amux_prop->chan_prop->low_thr,
2850 &chip->adc->amux_prop->chan_prop->high_thr);
2851 qpnp_adc_tm_add_to_list(chip, dt_index, param,
2852 chip->adc->amux_prop->chan_prop);
2853 chip->adc->amux_prop->chan_prop->tm_channel_select =
2854 chip->sensor[dt_index].btm_channel_num;
2855 chip->adc->amux_prop->chan_prop->state_request =
2856 param->state_request;
2857 chip->adc->amux_prop->calib_type =
2858 chip->adc->adc_channels[dt_index].calib_type;
Prasad Malisetty358dda32018-02-20 06:14:26 +05302859 if (!chip->adc_tm_hc) {
2860 rc = qpnp_adc_tm_configure(chip, chip->adc->amux_prop);
2861 if (rc) {
2862 pr_err("adc-tm configure failed with %d\n", rc);
2863 goto fail_unlock;
2864 }
2865 } else {
2866 rc = qpnp_adc_tm_hc_configure(chip, chip->adc->amux_prop);
2867 if (rc) {
2868 pr_err("adc-tm hc configure failed with %d\n", rc);
2869 goto fail_unlock;
2870 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002871 }
2872
2873 chip->sensor[dt_index].scale_type = scale_type;
2874
2875fail_unlock:
2876 mutex_unlock(&chip->adc->adc_lock);
2877
2878 return rc;
2879}
2880EXPORT_SYMBOL(qpnp_adc_tm_channel_measure);
2881
2882int32_t qpnp_adc_tm_disable_chan_meas(struct qpnp_adc_tm_chip *chip,
2883 struct qpnp_adc_tm_btm_param *param)
2884{
2885 uint32_t channel, dt_index = 0, btm_chan_num;
Prasad Malisetty358dda32018-02-20 06:14:26 +05302886 u8 sensor_mask = 0, mode_ctl = 0;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002887 int rc = 0;
2888
2889 if (qpnp_adc_tm_is_valid(chip))
2890 return -ENODEV;
2891
2892 mutex_lock(&chip->adc->adc_lock);
2893
Prasad Malisetty358dda32018-02-20 06:14:26 +05302894 if (!chip->adc_tm_hc) {
2895 /* Set measurement in single measurement mode */
2896 mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
2897 rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
2898 if (rc < 0) {
2899 pr_err("adc-tm single mode select failed\n");
2900 goto fail;
2901 }
2902 }
2903
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002904 /* Disable bank */
2905 rc = qpnp_adc_tm_disable(chip);
2906 if (rc < 0) {
2907 pr_err("adc-tm disable failed\n");
2908 goto fail;
2909 }
2910
Prasad Malisetty358dda32018-02-20 06:14:26 +05302911 if (!chip->adc_tm_hc) {
2912 /* Check if a conversion is in progress */
2913 rc = qpnp_adc_tm_req_sts_check(chip);
2914 if (rc < 0) {
2915 pr_err("adc-tm req_sts check failed\n");
2916 goto fail;
2917 }
2918 }
2919
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002920 channel = param->channel;
2921 while ((chip->adc->adc_channels[dt_index].channel_num
2922 != channel) && (dt_index < chip->max_channels_available))
2923 dt_index++;
2924
2925 if (dt_index >= chip->max_channels_available) {
2926 pr_err("not a valid ADC_TMN channel\n");
2927 rc = -EINVAL;
2928 goto fail;
2929 }
2930
2931 btm_chan_num = chip->sensor[dt_index].btm_channel_num;
2932
Prasad Malisetty358dda32018-02-20 06:14:26 +05302933 if (!chip->adc_tm_hc) {
2934 sensor_mask = 1 << chip->sensor[dt_index].sensor_num;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002935
Prasad Malisetty358dda32018-02-20 06:14:26 +05302936 rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
2937 sensor_mask, false);
2938 if (rc < 0) {
2939 pr_err("high threshold int enable failed\n");
2940 goto fail;
2941 }
2942
2943 rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
2944 sensor_mask, false);
2945 if (rc < 0) {
2946 pr_err("multi measurement en failed\n");
2947 goto fail;
2948 }
2949 } else {
2950 rc = qpnp_adc_tm_reg_update(chip, QPNP_BTM_Mn_EN(btm_chan_num),
2951 QPNP_BTM_Mn_HIGH_THR_INT_EN, false);
2952 if (rc < 0) {
2953 pr_err("high thr disable err:%d\n", btm_chan_num);
2954 return rc;
2955 }
2956
2957 rc = qpnp_adc_tm_reg_update(chip, QPNP_BTM_Mn_EN(btm_chan_num),
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002958 QPNP_BTM_Mn_LOW_THR_INT_EN, false);
Prasad Malisetty358dda32018-02-20 06:14:26 +05302959 if (rc < 0) {
2960 pr_err("low thr disable err:%d\n", btm_chan_num);
2961 return rc;
2962 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002963
Prasad Malisetty358dda32018-02-20 06:14:26 +05302964 rc = qpnp_adc_tm_reg_update(chip, QPNP_BTM_Mn_EN(btm_chan_num),
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07002965 QPNP_BTM_Mn_MEAS_EN, false);
Prasad Malisetty358dda32018-02-20 06:14:26 +05302966 if (rc < 0) {
2967 pr_err("multi measurement disable failed\n");
2968 return rc;
2969 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002970 }
2971
2972 rc = qpnp_adc_tm_enable_if_channel_meas(chip);
2973 if (rc < 0)
2974 pr_err("re-enabling measurement failed\n");
2975
2976fail:
2977 mutex_unlock(&chip->adc->adc_lock);
2978
2979 return rc;
2980}
2981EXPORT_SYMBOL(qpnp_adc_tm_disable_chan_meas);
2982
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08002983struct qpnp_adc_tm_chip *qpnp_get_adc_tm(struct device *dev, const char *name)
2984{
2985 struct qpnp_adc_tm_chip *chip;
2986 struct device_node *node = NULL;
2987 char prop_name[QPNP_MAX_PROP_NAME_LEN];
2988
2989 snprintf(prop_name, QPNP_MAX_PROP_NAME_LEN, "qcom,%s-adc_tm", name);
2990
2991 node = of_parse_phandle(dev->of_node, prop_name, 0);
2992 if (node == NULL)
2993 return ERR_PTR(-ENODEV);
2994
2995 list_for_each_entry(chip, &qpnp_adc_tm_device_list, list)
2996 if (chip->adc->pdev->dev.of_node == node)
2997 return chip;
2998
2999 return ERR_PTR(-EPROBE_DEFER);
3000}
3001EXPORT_SYMBOL(qpnp_get_adc_tm);
3002
Prasad Malisetty358dda32018-02-20 06:14:26 +05303003static int qpnp_adc_tm_initial_setup(struct qpnp_adc_tm_chip *chip)
3004{
3005 u8 thr_init = 0;
3006 int rc = 0;
3007
3008 rc = qpnp_adc_tm_write_reg(chip, QPNP_ADC_TM_HIGH_THR_INT_EN,
3009 thr_init, 1);
3010 if (rc < 0) {
3011 pr_err("high thr init failed\n");
3012 return rc;
3013 }
3014
3015 rc = qpnp_adc_tm_write_reg(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
3016 thr_init, 1);
3017 if (rc < 0) {
3018 pr_err("low thr init failed\n");
3019 return rc;
3020 }
3021
3022 rc = qpnp_adc_tm_write_reg(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
3023 thr_init, 1);
3024 if (rc < 0) {
3025 pr_err("multi meas en failed\n");
3026 return rc;
3027 }
3028
3029 return rc;
3030}
3031
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003032static const struct of_device_id qpnp_adc_tm_match_table[] = {
3033 { .compatible = "qcom,qpnp-adc-tm" },
3034 { .compatible = "qcom,qpnp-adc-tm-hc" },
Jishnu Prakashd09bc692018-05-02 10:54:28 +05303035 { .compatible = "qcom,qpnp-adc-tm-hc-pm5" },
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003036 {}
3037};
3038
3039static int qpnp_adc_tm_probe(struct platform_device *pdev)
3040{
3041 struct device_node *node = pdev->dev.of_node, *child;
3042 struct qpnp_adc_tm_chip *chip;
3043 struct qpnp_adc_drv *adc_qpnp;
3044 int32_t count_adc_channel_list = 0, rc, sen_idx = 0, i = 0;
3045 bool thermal_node = false;
3046 const struct of_device_id *id;
3047
3048 for_each_child_of_node(node, child)
3049 count_adc_channel_list++;
3050
3051 if (!count_adc_channel_list) {
3052 pr_err("No channel listing\n");
3053 return -EINVAL;
3054 }
3055
3056 id = of_match_node(qpnp_adc_tm_match_table, node);
3057 if (id == NULL) {
3058 pr_err("qpnp_adc_tm_match of_node prop not present\n");
3059 return -ENODEV;
3060 }
3061
3062 chip = devm_kzalloc(&pdev->dev, sizeof(struct qpnp_adc_tm_chip) +
3063 (count_adc_channel_list *
3064 sizeof(struct qpnp_adc_tm_sensor)),
3065 GFP_KERNEL);
3066 if (!chip)
3067 return -ENOMEM;
3068
Jishnu Prakash68b87242018-03-15 20:31:49 +05303069 list_add(&chip->list, &qpnp_adc_tm_device_list);
3070 chip->max_channels_available = count_adc_channel_list;
3071
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003072 adc_qpnp = devm_kzalloc(&pdev->dev, sizeof(struct qpnp_adc_drv),
3073 GFP_KERNEL);
3074 if (!adc_qpnp) {
3075 rc = -ENOMEM;
3076 goto fail;
3077 }
3078
3079 chip->dev = &(pdev->dev);
3080 chip->adc = adc_qpnp;
3081 chip->adc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
3082 if (!chip->adc->regmap) {
3083 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
3084 rc = -EINVAL;
3085 goto fail;
3086 }
3087
Prasad Malisetty358dda32018-02-20 06:14:26 +05303088 if (of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc")) {
3089 chip->adc_tm_hc = true;
3090 chip->adc->adc_hc = true;
3091 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003092
3093 rc = qpnp_adc_get_devicetree_data(pdev, chip->adc);
3094 if (rc) {
3095 dev_err(&pdev->dev, "failed to read device tree\n");
3096 goto fail;
3097 }
3098 mutex_init(&chip->adc->adc_lock);
3099
Prasad Malisetty358dda32018-02-20 06:14:26 +05303100 /* Register the ADC peripheral interrupt */
3101 if (!chip->adc_tm_hc) {
3102 chip->adc->adc_high_thr_irq = platform_get_irq_byname(pdev,
3103 "high-thr-en-set");
3104 if (chip->adc->adc_high_thr_irq < 0) {
3105 pr_err("Invalid irq\n");
3106 rc = -ENXIO;
3107 goto fail;
3108 }
3109
3110 chip->adc->adc_low_thr_irq = platform_get_irq_byname(pdev,
3111 "low-thr-en-set");
3112 if (chip->adc->adc_low_thr_irq < 0) {
3113 pr_err("Invalid irq\n");
3114 rc = -ENXIO;
3115 goto fail;
3116 }
3117 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003118 chip->vadc_dev = qpnp_get_vadc(&pdev->dev, "adc_tm");
3119 if (IS_ERR(chip->vadc_dev)) {
3120 rc = PTR_ERR(chip->vadc_dev);
3121 if (rc != -EPROBE_DEFER)
3122 pr_err("vadc property missing, rc=%d\n", rc);
3123 goto fail;
3124 }
3125
3126 chip->adc_tm_recalib_check = of_property_read_bool(node,
3127 "qcom,adc-tm-recalib-check");
3128
3129 for_each_child_of_node(node, child) {
3130 char name[25];
3131 int btm_channel_num, timer_select = 0;
3132
3133 rc = of_property_read_u32(child,
3134 "qcom,btm-channel-number", &btm_channel_num);
3135 if (rc) {
3136 pr_err("Invalid btm channel number\n");
3137 goto fail;
3138 }
3139 rc = of_property_read_u32(child,
3140 "qcom,meas-interval-timer-idx", &timer_select);
3141 if (rc) {
3142 pr_debug("Default to timer2 with interval of 1 sec\n");
3143 chip->sensor[sen_idx].timer_select =
3144 ADC_MEAS_TIMER_SELECT2;
3145 chip->sensor[sen_idx].meas_interval =
3146 ADC_MEAS2_INTERVAL_1S;
3147 } else {
3148 if (timer_select >= ADC_MEAS_TIMER_NUM) {
3149 pr_err("Invalid timer selection number\n");
3150 goto fail;
3151 }
3152 chip->sensor[sen_idx].timer_select = timer_select;
3153 if (timer_select == ADC_MEAS_TIMER_SELECT1)
3154 chip->sensor[sen_idx].meas_interval =
3155 ADC_MEAS1_INTERVAL_3P9MS;
3156 else if (timer_select == ADC_MEAS_TIMER_SELECT3)
3157 chip->sensor[sen_idx].meas_interval =
3158 ADC_MEAS3_INTERVAL_4S;
3159 else if (timer_select == ADC_MEAS_TIMER_SELECT2)
3160 chip->sensor[sen_idx].meas_interval =
3161 ADC_MEAS2_INTERVAL_1S;
3162 }
3163
3164 chip->sensor[sen_idx].btm_channel_num = btm_channel_num;
3165 chip->sensor[sen_idx].vadc_channel_num =
3166 chip->adc->adc_channels[sen_idx].channel_num;
3167 chip->sensor[sen_idx].sensor_num = sen_idx;
3168 chip->sensor[sen_idx].chip = chip;
3169 pr_debug("btm_chan:%x, vadc_chan:%x\n", btm_channel_num,
3170 chip->adc->adc_channels[sen_idx].channel_num);
3171 thermal_node = of_property_read_bool(child,
3172 "qcom,thermal-node");
3173 if (thermal_node) {
3174 /* Register with the thermal zone */
3175 pr_debug("thermal node%x\n", btm_channel_num);
3176 chip->sensor[sen_idx].mode = THERMAL_DEVICE_DISABLED;
3177 chip->sensor[sen_idx].thermal_node = true;
3178 snprintf(name, sizeof(name), "%s",
3179 chip->adc->adc_channels[sen_idx].name);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003180 chip->sensor[sen_idx].low_thr =
3181 QPNP_ADC_TM_M0_LOW_THR;
3182 chip->sensor[sen_idx].high_thr =
3183 QPNP_ADC_TM_M0_HIGH_THR;
3184 chip->sensor[sen_idx].tz_dev =
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07003185 devm_thermal_zone_of_sensor_register(
3186 chip->dev,
3187 chip->sensor[sen_idx].vadc_channel_num,
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003188 &chip->sensor[sen_idx],
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07003189 &qpnp_adc_tm_thermal_ops);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003190 if (IS_ERR(chip->sensor[sen_idx].tz_dev))
3191 pr_err("thermal device register failed.\n");
3192 }
3193 chip->sensor[sen_idx].req_wq = alloc_workqueue(
3194 "qpnp_adc_notify_wq", WQ_HIGHPRI, 0);
3195 if (!chip->sensor[sen_idx].req_wq) {
3196 pr_err("Requesting priority wq failed\n");
3197 goto fail;
3198 }
3199 INIT_WORK(&chip->sensor[sen_idx].work, notify_adc_tm_fn);
3200 INIT_LIST_HEAD(&chip->sensor[sen_idx].thr_list);
3201 sen_idx++;
3202 }
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07003203
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003204 chip->high_thr_wq = alloc_workqueue("qpnp_adc_tm_high_thr_wq",
3205 WQ_HIGHPRI, 0);
3206 if (!chip->high_thr_wq) {
3207 pr_err("Requesting high thr priority wq failed\n");
3208 goto fail;
3209 }
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07003210
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003211 chip->low_thr_wq = alloc_workqueue("qpnp_adc_tm_low_thr_wq",
3212 WQ_HIGHPRI, 0);
3213 if (!chip->low_thr_wq) {
3214 pr_err("Requesting low thr priority wq failed\n");
3215 goto fail;
3216 }
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07003217
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003218 chip->thr_wq = alloc_workqueue("qpnp_adc_tm_thr_wq",
3219 WQ_HIGHPRI, 0);
3220 if (!chip->thr_wq) {
3221 pr_err("Requesting thr priority wq failed\n");
3222 goto fail;
3223 }
3224
3225 INIT_WORK(&chip->trigger_high_thr_work, qpnp_adc_tm_high_thr_work);
3226 INIT_WORK(&chip->trigger_low_thr_work, qpnp_adc_tm_low_thr_work);
3227 atomic_set(&chip->wq_cnt, 0);
3228
Prasad Malisetty358dda32018-02-20 06:14:26 +05303229 if (!chip->adc_tm_hc) {
3230 rc = qpnp_adc_tm_initial_setup(chip);
3231 if (rc)
3232 goto fail;
3233 rc = devm_request_irq(&pdev->dev, chip->adc->adc_high_thr_irq,
3234 qpnp_adc_tm_high_thr_isr,
3235 IRQF_TRIGGER_RISING, "qpnp_adc_tm_high_interrupt", chip);
3236 if (rc) {
3237 dev_err(&pdev->dev, "failed to request adc irq\n");
3238 goto fail;
3239 } else {
3240 enable_irq_wake(chip->adc->adc_high_thr_irq);
3241 }
3242
3243 rc = devm_request_irq(&pdev->dev, chip->adc->adc_low_thr_irq,
3244 qpnp_adc_tm_low_thr_isr,
3245 IRQF_TRIGGER_RISING,
3246 "qpnp_adc_tm_low_interrupt", chip);
3247 if (rc) {
3248 dev_err(&pdev->dev, "failed to request adc irq\n");
3249 goto fail;
3250 } else {
3251 enable_irq_wake(chip->adc->adc_low_thr_irq);
3252 }
3253 } else {
3254 rc = devm_request_irq(&pdev->dev, chip->adc->adc_irq_eoc,
3255 qpnp_adc_tm_rc_thr_isr,
3256 IRQF_TRIGGER_HIGH, "qpnp_adc_tm_interrupt", chip);
3257 if (rc)
3258 dev_err(&pdev->dev, "failed to request adc irq\n");
3259 else
3260 enable_irq_wake(chip->adc->adc_irq_eoc);
3261 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003262
3263 chip->adc_vote_enable = false;
3264 dev_set_drvdata(&pdev->dev, chip);
Prasad Malisetty358dda32018-02-20 06:14:26 +05303265 spin_lock_init(&chip->th_info.adc_tm_low_lock);
3266 spin_lock_init(&chip->th_info.adc_tm_high_lock);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003267
3268 pr_debug("OK\n");
3269 return 0;
3270fail:
3271 for_each_child_of_node(node, child) {
3272 thermal_node = of_property_read_bool(child,
3273 "qcom,thermal-node");
3274 if (thermal_node) {
3275 thermal_zone_device_unregister(chip->sensor[i].tz_dev);
3276 if (chip->sensor[i].req_wq)
3277 destroy_workqueue(chip->sensor[sen_idx].req_wq);
3278 }
3279 }
3280 if (chip->high_thr_wq)
3281 destroy_workqueue(chip->high_thr_wq);
3282 if (chip->low_thr_wq)
3283 destroy_workqueue(chip->low_thr_wq);
Jishnu Prakash68b87242018-03-15 20:31:49 +05303284 list_del(&chip->list);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003285 dev_set_drvdata(&pdev->dev, NULL);
3286 return rc;
3287}
3288
3289static int qpnp_adc_tm_remove(struct platform_device *pdev)
3290{
3291 struct qpnp_adc_tm_chip *chip = dev_get_drvdata(&pdev->dev);
3292 struct device_node *node = pdev->dev.of_node, *child;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003293 int i = 0;
3294
3295 for_each_child_of_node(node, child) {
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07003296 if (chip->sensor[i].req_wq)
3297 destroy_workqueue(chip->sensor[i].req_wq);
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003298 i++;
3299 }
3300
3301 if (chip->high_thr_wq)
3302 destroy_workqueue(chip->high_thr_wq);
3303 if (chip->low_thr_wq)
3304 destroy_workqueue(chip->low_thr_wq);
3305 if (chip->adc->hkadc_ldo && chip->adc->hkadc_ldo_ok)
3306 qpnp_adc_free_voltage_resource(chip->adc);
3307 dev_set_drvdata(&pdev->dev, NULL);
3308
3309 return 0;
3310}
3311
3312static void qpnp_adc_tm_shutdown(struct platform_device *pdev)
3313{
3314 struct qpnp_adc_tm_chip *chip = dev_get_drvdata(&pdev->dev);
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07003315 int rc = 0, i = 0;
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003316
3317 /* Disable bank */
3318 rc = qpnp_adc_tm_disable(chip);
3319 if (rc < 0)
3320 pr_err("adc-tm disable failed\n");
3321
Siddartha Mohanadoss697fe0d2017-04-26 17:10:11 -07003322 for (i = 0; i < QPNP_BTM_CHANNELS; i++) {
3323 rc = qpnp_adc_tm_reg_update(chip,
3324 QPNP_BTM_Mn_EN(i),
3325 QPNP_BTM_Mn_MEAS_EN, false);
3326 if (rc < 0)
3327 pr_err("multi measurement disable failed\n");
3328 }
Siddartha Mohanadoss45a944b2017-01-23 19:46:10 -08003329}
3330
3331static int qpnp_adc_tm_suspend_noirq(struct device *dev)
3332{
3333 struct qpnp_adc_tm_chip *chip = dev_get_drvdata(dev);
3334
3335 if (atomic_read(&chip->wq_cnt) != 0) {
3336 pr_err(
3337 "Aborting suspend, adc_tm notification running while suspending\n");
3338 return -EBUSY;
3339 }
3340 return 0;
3341}
3342
3343static const struct dev_pm_ops qpnp_adc_tm_pm_ops = {
3344 .suspend_noirq = qpnp_adc_tm_suspend_noirq,
3345};
3346
3347static struct platform_driver qpnp_adc_tm_driver = {
3348 .driver = {
3349 .name = "qcom,qpnp-adc-tm",
3350 .of_match_table = qpnp_adc_tm_match_table,
3351 .pm = &qpnp_adc_tm_pm_ops,
3352 },
3353 .probe = qpnp_adc_tm_probe,
3354 .remove = qpnp_adc_tm_remove,
3355 .shutdown = qpnp_adc_tm_shutdown,
3356};
3357
3358static int __init qpnp_adc_tm_init(void)
3359{
3360 return platform_driver_register(&qpnp_adc_tm_driver);
3361}
3362module_init(qpnp_adc_tm_init);
3363
3364static void __exit qpnp_adc_tm_exit(void)
3365{
3366 platform_driver_unregister(&qpnp_adc_tm_driver);
3367}
3368module_exit(qpnp_adc_tm_exit);
3369
3370MODULE_DESCRIPTION("QPNP PMIC ADC Threshold Monitoring driver");
3371MODULE_LICENSE("GPL v2");