blob: c992c068a16b2bae88802f89ddc1bd464b6d0e8e [file] [log] [blame]
Jishnu Prakashb5203662019-03-08 13:06:23 +05301/* Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -08002 *
Siddartha Mohanadosscd8aa0b2017-04-06 15:17:46 -07003 * 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.
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -08006 *
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.
Siddartha Mohanadosscd8aa0b2017-04-06 15:17:46 -070011 *
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080012 */
13#ifndef __QCOM_TSENS_H__
14#define __QCOM_TSENS_H__
15
16#include <linux/kernel.h>
17#include <linux/thermal.h>
18#include <linux/interrupt.h>
19#include <linux/types.h>
20#include <linux/workqueue.h>
21#include <linux/io.h>
22#include <linux/delay.h>
23
24#define DEBUG_SIZE 10
25#define TSENS_MAX_SENSORS 16
Chinkit Kumar,Kirti Kumar Parmar253de882018-06-06 00:11:56 +053026#define TSENS_NUM_SENSORS_8937 11
27#define TSENS_NUM_SENSORS_8909 5
Siddartha Mohanadoss07b10612017-04-07 14:53:45 -070028#define TSENS_CONTROLLER_ID(n) (n)
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080029#define TSENS_CTRL_ADDR(n) (n)
Siddartha Mohanadoss07b10612017-04-07 14:53:45 -070030#define TSENS_TM_SN_STATUS(n) ((n) + 0xa0)
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080031
Rama Krishna Phani Ae677c1c2018-01-31 18:51:43 +053032#define ONE_PT_CALIB 0x1
33#define ONE_PT_CALIB2 0x2
34#define TWO_PT_CALIB 0x3
35
Chinkit Kumar,Kirti Kumar Parmar253de882018-06-06 00:11:56 +053036#define SLOPE_FACTOR 1000
37#define SLOPE_DEFAULT 3200
38
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080039enum tsens_dbg_type {
40 TSENS_DBG_POLL,
41 TSENS_DBG_LOG_TEMP_READS,
42 TSENS_DBG_LOG_INTERRUPT_TIMESTAMP,
Siddartha Mohanadoss64a5da32017-05-09 11:09:51 -070043 TSENS_DBG_LOG_BUS_ID_DATA,
Ashok Jammigumpulaa1b7ec12017-11-23 15:37:44 +053044 TSENS_DBG_MTC_DATA,
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080045 TSENS_DBG_LOG_MAX
46};
47
48#define tsens_sec_to_msec_value 1000
49
50struct tsens_device;
51
52#if defined(CONFIG_THERMAL_TSENS)
53int tsens2xxx_dbg(struct tsens_device *data, u32 id, u32 dbg_type, int *temp);
54#else
55static inline int tsens2xxx_dbg(struct tsens_device *data, u32 id,
56 u32 dbg_type, int *temp)
57{ return -ENXIO; }
58#endif
59
60struct tsens_dbg {
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080061 u32 idx;
62 unsigned long long time_stmp[DEBUG_SIZE];
63 unsigned long temp[DEBUG_SIZE];
64};
65
66struct tsens_dbg_context {
67 struct tsens_device *tmdev;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080068 struct tsens_dbg sensor_dbg_info[TSENS_MAX_SENSORS];
69 int tsens_critical_wd_cnt;
Rama Krishna Phani A6ad66332017-10-25 21:20:04 +053070 u32 irq_idx;
71 unsigned long long irq_time_stmp[DEBUG_SIZE];
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080072 struct delayed_work tsens_critical_poll_test;
73};
74
75struct tsens_context {
Jishnu Prakashb5203662019-03-08 13:06:23 +053076 enum thermal_trip_activation_mode high_th_state;
77 enum thermal_trip_activation_mode low_th_state;
78 enum thermal_trip_activation_mode crit_th_state;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080079 int high_temp;
80 int low_temp;
81 int crit_temp;
Rama Krishna Phani Ae677c1c2018-01-31 18:51:43 +053082 int high_adc_code;
83 int low_adc_code;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080084};
85
86struct tsens_sensor {
87 struct tsens_device *tmdev;
88 struct thermal_zone_device *tzd;
89 u32 hw_id;
90 u32 id;
91 const char *sensor_name;
92 struct tsens_context thr_state;
Rama Krishna Phani Ae677c1c2018-01-31 18:51:43 +053093 int offset;
94 int slope;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -080095};
96
97/**
98 * struct tsens_ops - operations as supported by the tsens device
99 * @init: Function to initialize the tsens device
100 * @get_temp: Function which returns the temp in millidegC
101 */
102struct tsens_ops {
103 int (*hw_init)(struct tsens_device *);
104 int (*get_temp)(struct tsens_sensor *, int *);
Siddartha Mohanadoss07b10612017-04-07 14:53:45 -0700105 int (*set_trips)(struct tsens_sensor *, int, int);
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800106 int (*interrupts_reg)(struct tsens_device *);
107 int (*dbg)(struct tsens_device *, u32, u32, int *);
Siddartha Mohanadossf86413d2017-07-18 13:44:31 -0700108 int (*sensor_en)(struct tsens_device *, u32);
Rama Krishna Phani Ae677c1c2018-01-31 18:51:43 +0530109 int (*calibrate)(struct tsens_device *);
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800110};
111
112struct tsens_irqs {
113 const char *name;
114 irqreturn_t (*handler)(int, void *);
115};
116
117/**
118 * struct tsens_data - tsens instance specific data
119 * @num_sensors: Max number of sensors supported by platform
120 * @ops: operations the tsens instance supports
121 * @hw_ids: Subset of sensors ids supported by platform, if not the first n
122 */
123struct tsens_data {
124 const u32 num_sensors;
125 const struct tsens_ops *ops;
126 unsigned int *hw_ids;
127 u32 temp_factor;
128 bool cycle_monitor;
Siddartha Mohanadoss64a5da32017-05-09 11:09:51 -0700129 u32 cycle_compltn_monitor_mask;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800130 bool wd_bark;
Siddartha Mohanadoss64a5da32017-05-09 11:09:51 -0700131 u32 wd_bark_mask;
Ashok Jammigumpulaa1b7ec12017-11-23 15:37:44 +0530132 bool mtc;
Rama Krishna Phani Ae677c1c2018-01-31 18:51:43 +0530133 bool valid_status_check;
Siva Kumar Akkireddiee8aed92018-04-09 17:06:05 +0530134 u32 ver_major;
135 u32 ver_minor;
Ashok Jammigumpulaa1b7ec12017-11-23 15:37:44 +0530136};
137
138struct tsens_mtc_sysfs {
Rama Krishna Phani Ae677c1c2018-01-31 18:51:43 +0530139 u32 zone_log;
Ashok Jammigumpulaa1b7ec12017-11-23 15:37:44 +0530140 int zone_mtc;
141 int th1;
142 int th2;
Rama Krishna Phani Ae677c1c2018-01-31 18:51:43 +0530143 u32 zone_hist;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800144};
145
146struct tsens_device {
147 struct device *dev;
148 struct platform_device *pdev;
149 struct list_head list;
Chinkit Kumar,Kirti Kumar Parmar253de882018-06-06 00:11:56 +0530150 bool prev_reading_avail;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800151 struct regmap *map;
152 struct regmap_field *status_field;
Siddartha Mohanadoss07b10612017-04-07 14:53:45 -0700153 void __iomem *tsens_srot_addr;
154 void __iomem *tsens_tm_addr;
Rama Krishna Phani Ae677c1c2018-01-31 18:51:43 +0530155 void __iomem *tsens_calib_addr;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800156 const struct tsens_ops *ops;
157 struct tsens_dbg_context tsens_dbg;
158 spinlock_t tsens_crit_lock;
159 spinlock_t tsens_upp_low_lock;
160 const struct tsens_data *ctrl_data;
Jishnu Prakash53c21242018-05-16 10:42:15 +0530161 struct tsens_mtc_sysfs mtcsys;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800162 struct tsens_sensor sensor[0];
163};
164
165extern const struct tsens_data data_tsens2xxx, data_tsens23xx, data_tsens24xx;
Chinkit Kumar,Kirti Kumar Parmar253de882018-06-06 00:11:56 +0530166extern const struct tsens_data data_tsens14xx, data_tsens1xxx_8909;
Ashok Jammigumpulaa1b7ec12017-11-23 15:37:44 +0530167extern struct list_head tsens_device_list;
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800168
Chinkit Kumar,Kirti Kumar Parmar253de882018-06-06 00:11:56 +0530169extern int calibrate_8937(struct tsens_device *tmdev);
170extern int calibrate_8909(struct tsens_device *tmdev);
171
Siddartha Mohanadoss41b4cd92017-02-21 14:34:23 -0800172#endif /* __QCOM_TSENS_H__ */