blob: eb7832a4d2a08db5a7fe65eb371fc1cdb178fae2 [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Surendar Karka809cb8e2018-11-28 16:51:54 +05302/* Copyright (c) 2015, 2017-2019 The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05303 */
4
5#include <linux/bitops.h>
6#include <linux/kernel.h>
Sudheer Papothic9dd3be2018-04-06 00:51:48 +05307#include <linux/suspend.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05308#include <linux/errno.h>
9#include <linux/delay.h>
10#include <linux/thermal.h>
11#include <sound/soc.h>
12#include "wsa881x-temp-sensor.h"
13
14#define T1_TEMP -10
15#define T2_TEMP 150
16#define LOW_TEMP_THRESHOLD 5
17#define HIGH_TEMP_THRESHOLD 45
18#define TEMP_INVALID 0xFFFF
19#define WSA881X_TEMP_RETRY 3
20/*
21 * wsa881x_get_temp - get wsa temperature
22 * @thermal: thermal zone device
23 * @temp: temperature value
24 *
25 * Get the temperature of wsa881x.
26 *
27 * Return: 0 on success or negative error code on failure.
28 */
29int wsa881x_get_temp(struct thermal_zone_device *thermal,
30 int *temp)
31{
32 struct wsa881x_tz_priv *pdata;
Meng Wang15c825d2018-09-06 10:49:18 +080033 struct snd_soc_component *component;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053034 struct wsa_temp_register reg;
35 int dmeas, d1, d2;
36 int ret = 0;
37 int temp_val;
38 int t1 = T1_TEMP;
39 int t2 = T2_TEMP;
40 u8 retry = WSA881X_TEMP_RETRY;
41
42 if (!thermal)
43 return -EINVAL;
44
45 if (thermal->devdata) {
46 pdata = thermal->devdata;
Meng Wang15c825d2018-09-06 10:49:18 +080047 if (pdata->component) {
48 component = pdata->component;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053049 } else {
50 pr_err("%s: codec is NULL\n", __func__);
51 return -EINVAL;
52 }
53 } else {
54 pr_err("%s: pdata is NULL\n", __func__);
55 return -EINVAL;
56 }
Sudheer Papothic9dd3be2018-04-06 00:51:48 +053057 if (atomic_cmpxchg(&pdata->is_suspend_spk, 1, 0)) {
58 /*
59 * get_temp query happens as part of POST_PM_SUSPEND
60 * from thermal core. To avoid calls to slimbus
61 * as part of this thermal query, return default temp
62 * and reset the suspend flag.
63 */
64 if (!pdata->t0_init) {
65 if (temp)
66 *temp = pdata->curr_temp;
67 return 0;
68 }
69 }
70
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053071temp_retry:
72 if (pdata->wsa_temp_reg_read) {
Meng Wang15c825d2018-09-06 10:49:18 +080073 ret = pdata->wsa_temp_reg_read(component, &reg);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053074 if (ret) {
Sudheer Papothi4b9d12f2018-08-18 05:02:58 +053075 pr_err("%s: temp read failed: %d, current temp: %d\n",
76 __func__, ret, pdata->curr_temp);
77 if (temp)
78 *temp = pdata->curr_temp;
79 return 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053080 }
81 } else {
82 pr_err("%s: wsa_temp_reg_read is NULL\n", __func__);
83 return -EINVAL;
84 }
85 /*
86 * Temperature register values are expected to be in the
87 * following range.
88 * d1_msb = 68 - 92 and d1_lsb = 0, 64, 128, 192
89 * d2_msb = 185 -218 and d2_lsb = 0, 64, 128, 192
90 */
91 if ((reg.d1_msb < 68 || reg.d1_msb > 92) ||
92 (!(reg.d1_lsb == 0 || reg.d1_lsb == 64 || reg.d1_lsb == 128 ||
93 reg.d1_lsb == 192)) ||
94 (reg.d2_msb < 185 || reg.d2_msb > 218) ||
95 (!(reg.d2_lsb == 0 || reg.d2_lsb == 64 || reg.d2_lsb == 128 ||
96 reg.d2_lsb == 192))) {
97 printk_ratelimited("%s: Temperature registers[%d %d %d %d] are out of range\n",
98 __func__, reg.d1_msb, reg.d1_lsb, reg.d2_msb,
99 reg.d2_lsb);
100 }
101 dmeas = ((reg.dmeas_msb << 0x8) | reg.dmeas_lsb) >> 0x6;
102 d1 = ((reg.d1_msb << 0x8) | reg.d1_lsb) >> 0x6;
103 d2 = ((reg.d2_msb << 0x8) | reg.d2_lsb) >> 0x6;
104
105 if (d1 == d2)
106 temp_val = TEMP_INVALID;
107 else
108 temp_val = t1 + (((dmeas - d1) * (t2 - t1))/(d2 - d1));
109
110 if (temp_val <= LOW_TEMP_THRESHOLD ||
111 temp_val >= HIGH_TEMP_THRESHOLD) {
Surendar Karka809cb8e2018-11-28 16:51:54 +0530112 pr_debug("%s: T0: %d is out of range[%d, %d]\n", __func__,
113 temp_val, LOW_TEMP_THRESHOLD, HIGH_TEMP_THRESHOLD);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530114 if (retry--) {
115 msleep(20);
116 goto temp_retry;
117 }
118 }
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530119 pdata->curr_temp = temp_val;
120
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530121 if (temp)
122 *temp = temp_val;
123 pr_debug("%s: t0 measured: %d dmeas = %d, d1 = %d, d2 = %d\n",
124 __func__, temp_val, dmeas, d1, d2);
125 return ret;
126}
127EXPORT_SYMBOL(wsa881x_get_temp);
128
129static struct thermal_zone_device_ops wsa881x_thermal_ops = {
130 .get_temp = wsa881x_get_temp,
131};
132
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530133
134static int wsa881x_pm_notify(struct notifier_block *nb,
135 unsigned long mode, void *_unused)
136{
137 struct wsa881x_tz_priv *pdata =
138 container_of(nb, struct wsa881x_tz_priv, pm_nb);
139
140 switch (mode) {
141 case PM_SUSPEND_PREPARE:
142 atomic_set(&pdata->is_suspend_spk, 1);
143 break;
144 default:
145 break;
146 }
147 return 0;
148}
149
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530150int wsa881x_init_thermal(struct wsa881x_tz_priv *tz_pdata)
151{
152 struct thermal_zone_device *tz_dev;
153
154 if (tz_pdata == NULL) {
155 pr_err("%s: thermal pdata is NULL\n", __func__);
156 return -EINVAL;
157 }
158 /* Register with the thermal zone */
159 tz_dev = thermal_zone_device_register(tz_pdata->name,
160 0, 0, tz_pdata,
161 &wsa881x_thermal_ops, NULL, 0, 0);
162 if (IS_ERR(tz_dev)) {
163 pr_err("%s: thermal device register failed.\n", __func__);
164 return -EINVAL;
165 }
166 tz_pdata->tz_dev = tz_dev;
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530167 tz_pdata->pm_nb.notifier_call = wsa881x_pm_notify;
168 register_pm_notifier(&tz_pdata->pm_nb);
169 atomic_set(&tz_pdata->is_suspend_spk, 0);
170
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530171 return 0;
172}
173EXPORT_SYMBOL(wsa881x_init_thermal);
174
175void wsa881x_deinit_thermal(struct thermal_zone_device *tz_dev)
176{
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530177 struct wsa881x_tz_priv *pdata;
178
179 if (tz_dev && tz_dev->devdata) {
180 pdata = tz_dev->devdata;
181 if (pdata)
182 unregister_pm_notifier(&pdata->pm_nb);
183 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530184 if (tz_dev)
185 thermal_zone_device_unregister(tz_dev);
186}
187EXPORT_SYMBOL(wsa881x_deinit_thermal);