blob: 2f77d235cf73539b58fdc54a3ee231a732b86e7a [file] [log] [blame]
Amit Kucheria2d71d8d2018-09-12 15:22:47 +05301// SPDX-License-Identifier: GPL-2.0
Rajendra Nayak90660732016-05-05 14:21:39 +05302/*
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
Rajendra Nayak90660732016-05-05 14:21:39 +05304 */
5
Amit Kucheria7c938f42019-11-01 00:07:28 +05306#include <linux/debugfs.h>
Rajendra Nayak90660732016-05-05 14:21:39 +05307#include <linux/err.h>
8#include <linux/module.h>
9#include <linux/of.h>
Amit Kucheria634e11d2019-11-01 00:07:39 +053010#include <linux/of_platform.h>
Rajendra Nayak90660732016-05-05 14:21:39 +053011#include <linux/platform_device.h>
12#include <linux/pm.h>
13#include <linux/slab.h>
14#include <linux/thermal.h>
15#include "tsens.h"
16
17static int tsens_get_temp(void *data, int *temp)
18{
Amit Kucheria8b71bce2019-11-01 00:07:25 +053019 struct tsens_sensor *s = data;
Amit Kucheria69b628a2019-03-20 18:47:44 +053020 struct tsens_priv *priv = s->priv;
Rajendra Nayak90660732016-05-05 14:21:39 +053021
Amit Kucheria8b71bce2019-11-01 00:07:25 +053022 return priv->ops->get_temp(s, temp);
Rajendra Nayak90660732016-05-05 14:21:39 +053023}
24
Amit Kucheria2cbcd2e2019-03-20 18:47:45 +053025static int tsens_get_trend(void *data, int trip, enum thermal_trend *trend)
Rajendra Nayak90660732016-05-05 14:21:39 +053026{
Amit Kucheria8b71bce2019-11-01 00:07:25 +053027 struct tsens_sensor *s = data;
Amit Kucheria69b628a2019-03-20 18:47:44 +053028 struct tsens_priv *priv = s->priv;
Rajendra Nayak90660732016-05-05 14:21:39 +053029
Amit Kucheria69b628a2019-03-20 18:47:44 +053030 if (priv->ops->get_trend)
Amit Kucheria8b71bce2019-11-01 00:07:25 +053031 return priv->ops->get_trend(s, trend);
Rajendra Nayak90660732016-05-05 14:21:39 +053032
33 return -ENOTSUPP;
34}
35
Arnd Bergmann5b974692016-07-04 15:12:28 +020036static int __maybe_unused tsens_suspend(struct device *dev)
Rajendra Nayak90660732016-05-05 14:21:39 +053037{
Amit Kucheria69b628a2019-03-20 18:47:44 +053038 struct tsens_priv *priv = dev_get_drvdata(dev);
Rajendra Nayak90660732016-05-05 14:21:39 +053039
Amit Kucheria69b628a2019-03-20 18:47:44 +053040 if (priv->ops && priv->ops->suspend)
41 return priv->ops->suspend(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +053042
43 return 0;
44}
45
Arnd Bergmann5b974692016-07-04 15:12:28 +020046static int __maybe_unused tsens_resume(struct device *dev)
Rajendra Nayak90660732016-05-05 14:21:39 +053047{
Amit Kucheria69b628a2019-03-20 18:47:44 +053048 struct tsens_priv *priv = dev_get_drvdata(dev);
Rajendra Nayak90660732016-05-05 14:21:39 +053049
Amit Kucheria69b628a2019-03-20 18:47:44 +053050 if (priv->ops && priv->ops->resume)
51 return priv->ops->resume(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +053052
53 return 0;
54}
55
56static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
57
58static const struct of_device_id tsens_table[] = {
59 {
60 .compatible = "qcom,msm8916-tsens",
Rajendra Nayak840a5bd2016-05-05 14:21:40 +053061 .data = &data_8916,
Rajendra Nayak90660732016-05-05 14:21:39 +053062 }, {
63 .compatible = "qcom,msm8974-tsens",
Rajendra Nayak5e6703b2016-05-05 14:21:41 +053064 .data = &data_8974,
Rajendra Nayakd059c732016-05-05 14:21:44 +053065 }, {
AngeloGioacchino Del Regno0e580292019-10-05 12:41:31 +020066 .compatible = "qcom,msm8976-tsens",
67 .data = &data_8976,
68 }, {
Rajendra Nayakd059c732016-05-05 14:21:44 +053069 .compatible = "qcom,msm8996-tsens",
70 .data = &data_8996,
Amit Kucheria191dc742018-07-18 12:13:12 +053071 }, {
Amit Kucheriae8c24c6f2019-03-20 18:48:00 +053072 .compatible = "qcom,tsens-v1",
73 .data = &data_tsens_v1,
74 }, {
Amit Kucheria191dc742018-07-18 12:13:12 +053075 .compatible = "qcom,tsens-v2",
76 .data = &data_tsens_v2,
Rajendra Nayak90660732016-05-05 14:21:39 +053077 },
78 {}
79};
80MODULE_DEVICE_TABLE(of, tsens_table);
81
82static const struct thermal_zone_of_device_ops tsens_of_ops = {
83 .get_temp = tsens_get_temp,
84 .get_trend = tsens_get_trend,
Amit Kucheria634e11d2019-11-01 00:07:39 +053085 .set_trips = tsens_set_trips,
Rajendra Nayak90660732016-05-05 14:21:39 +053086};
87
Amit Kucheria79125e02020-03-12 18:07:02 +053088static int tsens_register_irq(struct tsens_priv *priv, char *irqname,
89 irq_handler_t thread_fn)
90{
91 struct platform_device *pdev;
92 int ret, irq;
93
94 pdev = of_find_device_by_node(priv->dev->of_node);
95 if (!pdev)
96 return -ENODEV;
97
98 irq = platform_get_irq_byname(pdev, irqname);
99 if (irq < 0) {
100 ret = irq;
101 /* For old DTs with no IRQ defined */
102 if (irq == -ENXIO)
103 ret = 0;
104 } else {
105 ret = devm_request_threaded_irq(&pdev->dev, irq,
106 NULL, thread_fn,
Amit Kucheria8d3a6d42020-03-12 18:07:05 +0530107 IRQF_ONESHOT,
Amit Kucheria79125e02020-03-12 18:07:02 +0530108 dev_name(&pdev->dev), priv);
109 if (ret)
110 dev_err(&pdev->dev, "%s: failed to get irq\n",
111 __func__);
112 else
113 enable_irq_wake(irq);
114 }
115
116 put_device(&pdev->dev);
117 return ret;
118}
119
Amit Kucheria69b628a2019-03-20 18:47:44 +0530120static int tsens_register(struct tsens_priv *priv)
Rajendra Nayak90660732016-05-05 14:21:39 +0530121{
Amit Kucheria79125e02020-03-12 18:07:02 +0530122 int i, ret;
Rajendra Nayak90660732016-05-05 14:21:39 +0530123 struct thermal_zone_device *tzd;
Rajendra Nayak90660732016-05-05 14:21:39 +0530124
Amit Kucheria69b628a2019-03-20 18:47:44 +0530125 for (i = 0; i < priv->num_sensors; i++) {
126 priv->sensor[i].priv = priv;
Amit Kucheria8b71bce2019-11-01 00:07:25 +0530127 tzd = devm_thermal_zone_of_sensor_register(priv->dev, priv->sensor[i].hw_id,
Amit Kucheria69b628a2019-03-20 18:47:44 +0530128 &priv->sensor[i],
Rajendra Nayak90660732016-05-05 14:21:39 +0530129 &tsens_of_ops);
130 if (IS_ERR(tzd))
131 continue;
Amit Kucheria69b628a2019-03-20 18:47:44 +0530132 priv->sensor[i].tzd = tzd;
133 if (priv->ops->enable)
134 priv->ops->enable(priv, i);
Rajendra Nayak90660732016-05-05 14:21:39 +0530135 }
Amit Kucheria634e11d2019-11-01 00:07:39 +0530136
Amit Kucheria79125e02020-03-12 18:07:02 +0530137 ret = tsens_register_irq(priv, "uplow", tsens_irq_thread);
138 if (ret < 0)
139 return ret;
Amit Kucheria634e11d2019-11-01 00:07:39 +0530140
Amit Kucheria79125e02020-03-12 18:07:02 +0530141 if (priv->feat->crit_int)
142 ret = tsens_register_irq(priv, "critical",
143 tsens_critical_irq_thread);
Amit Kucheria634e11d2019-11-01 00:07:39 +0530144
Amit Kucheria634e11d2019-11-01 00:07:39 +0530145 return ret;
Rajendra Nayak90660732016-05-05 14:21:39 +0530146}
147
148static int tsens_probe(struct platform_device *pdev)
149{
150 int ret, i;
151 struct device *dev;
152 struct device_node *np;
Amit Kucheria69b628a2019-03-20 18:47:44 +0530153 struct tsens_priv *priv;
Amit Kucheria3c040ce2019-03-20 18:47:42 +0530154 const struct tsens_plat_data *data;
Rajendra Nayak90660732016-05-05 14:21:39 +0530155 const struct of_device_id *id;
Bjorn Andersson6d7c70d2018-05-07 16:53:39 -0700156 u32 num_sensors;
Rajendra Nayak90660732016-05-05 14:21:39 +0530157
158 if (pdev->dev.of_node)
159 dev = &pdev->dev;
160 else
161 dev = pdev->dev.parent;
162
163 np = dev->of_node;
164
165 id = of_match_node(tsens_table, np);
Rajendra Nayak20d4fd82016-05-05 14:21:43 +0530166 if (id)
167 data = id->data;
168 else
169 data = &data_8960;
Rajendra Nayak90660732016-05-05 14:21:39 +0530170
Bjorn Andersson6d7c70d2018-05-07 16:53:39 -0700171 num_sensors = data->num_sensors;
172
173 if (np)
174 of_property_read_u32(np, "#qcom,sensors", &num_sensors);
175
176 if (num_sensors <= 0) {
Amit Kucheria3795ad52019-11-01 00:07:27 +0530177 dev_err(dev, "%s: invalid number of sensors\n", __func__);
Rajendra Nayak90660732016-05-05 14:21:39 +0530178 return -EINVAL;
179 }
180
Amit Kucheria69b628a2019-03-20 18:47:44 +0530181 priv = devm_kzalloc(dev,
182 struct_size(priv, sensor, num_sensors),
Kees Cook0ed2dd02018-05-08 16:08:53 -0700183 GFP_KERNEL);
Amit Kucheria69b628a2019-03-20 18:47:44 +0530184 if (!priv)
Rajendra Nayak90660732016-05-05 14:21:39 +0530185 return -ENOMEM;
186
Amit Kucheria69b628a2019-03-20 18:47:44 +0530187 priv->dev = dev;
188 priv->num_sensors = num_sensors;
189 priv->ops = data->ops;
190 for (i = 0; i < priv->num_sensors; i++) {
Rajendra Nayak90660732016-05-05 14:21:39 +0530191 if (data->hw_ids)
Amit Kucheria69b628a2019-03-20 18:47:44 +0530192 priv->sensor[i].hw_id = data->hw_ids[i];
Rajendra Nayak90660732016-05-05 14:21:39 +0530193 else
Amit Kucheria69b628a2019-03-20 18:47:44 +0530194 priv->sensor[i].hw_id = i;
Rajendra Nayak90660732016-05-05 14:21:39 +0530195 }
Amit Kucheriac1997052019-03-20 18:47:50 +0530196 priv->feat = data->feat;
197 priv->fields = data->fields;
Rajendra Nayak90660732016-05-05 14:21:39 +0530198
Amit Kucheria0e9c0bc2019-11-01 00:07:26 +0530199 platform_set_drvdata(pdev, priv);
200
Amit Kucheria69b628a2019-03-20 18:47:44 +0530201 if (!priv->ops || !priv->ops->init || !priv->ops->get_temp)
Rajendra Nayak90660732016-05-05 14:21:39 +0530202 return -EINVAL;
203
Amit Kucheria69b628a2019-03-20 18:47:44 +0530204 ret = priv->ops->init(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +0530205 if (ret < 0) {
Amit Kucheria3795ad52019-11-01 00:07:27 +0530206 dev_err(dev, "%s: init failed\n", __func__);
Rajendra Nayak90660732016-05-05 14:21:39 +0530207 return ret;
208 }
209
Amit Kucheria69b628a2019-03-20 18:47:44 +0530210 if (priv->ops->calibrate) {
211 ret = priv->ops->calibrate(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +0530212 if (ret < 0) {
Amit Kucheriafc7d18c2019-03-20 18:47:52 +0530213 if (ret != -EPROBE_DEFER)
Amit Kucheria3795ad52019-11-01 00:07:27 +0530214 dev_err(dev, "%s: calibration failed\n", __func__);
Rajendra Nayak90660732016-05-05 14:21:39 +0530215 return ret;
216 }
217 }
218
Amit Kucheria0e9c0bc2019-11-01 00:07:26 +0530219 return tsens_register(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +0530220}
221
222static int tsens_remove(struct platform_device *pdev)
223{
Amit Kucheria69b628a2019-03-20 18:47:44 +0530224 struct tsens_priv *priv = platform_get_drvdata(pdev);
Rajendra Nayak90660732016-05-05 14:21:39 +0530225
Amit Kucheria7c938f42019-11-01 00:07:28 +0530226 debugfs_remove_recursive(priv->debug_root);
Amit Kucheria634e11d2019-11-01 00:07:39 +0530227 tsens_disable_irq(priv);
Amit Kucheria69b628a2019-03-20 18:47:44 +0530228 if (priv->ops->disable)
229 priv->ops->disable(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +0530230
231 return 0;
232}
233
234static struct platform_driver tsens_driver = {
235 .probe = tsens_probe,
236 .remove = tsens_remove,
237 .driver = {
238 .name = "qcom-tsens",
239 .pm = &tsens_pm_ops,
240 .of_match_table = tsens_table,
241 },
242};
243module_platform_driver(tsens_driver);
244
245MODULE_LICENSE("GPL v2");
246MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
247MODULE_ALIAS("platform:qcom-tsens");