blob: 06c6bbd69a1a7a34a95bba2de0cacf7b5626f76f [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
6#include <linux/err.h>
7#include <linux/module.h>
8#include <linux/of.h>
9#include <linux/platform_device.h>
10#include <linux/pm.h>
11#include <linux/slab.h>
12#include <linux/thermal.h>
13#include "tsens.h"
14
15static int tsens_get_temp(void *data, int *temp)
16{
Amit Kucheria8b71bce2019-11-01 00:07:25 +053017 struct tsens_sensor *s = data;
Amit Kucheria69b628a2019-03-20 18:47:44 +053018 struct tsens_priv *priv = s->priv;
Rajendra Nayak90660732016-05-05 14:21:39 +053019
Amit Kucheria8b71bce2019-11-01 00:07:25 +053020 return priv->ops->get_temp(s, temp);
Rajendra Nayak90660732016-05-05 14:21:39 +053021}
22
Amit Kucheria2cbcd2e2019-03-20 18:47:45 +053023static int tsens_get_trend(void *data, int trip, enum thermal_trend *trend)
Rajendra Nayak90660732016-05-05 14:21:39 +053024{
Amit Kucheria8b71bce2019-11-01 00:07:25 +053025 struct tsens_sensor *s = data;
Amit Kucheria69b628a2019-03-20 18:47:44 +053026 struct tsens_priv *priv = s->priv;
Rajendra Nayak90660732016-05-05 14:21:39 +053027
Amit Kucheria69b628a2019-03-20 18:47:44 +053028 if (priv->ops->get_trend)
Amit Kucheria8b71bce2019-11-01 00:07:25 +053029 return priv->ops->get_trend(s, trend);
Rajendra Nayak90660732016-05-05 14:21:39 +053030
31 return -ENOTSUPP;
32}
33
Arnd Bergmann5b974692016-07-04 15:12:28 +020034static int __maybe_unused tsens_suspend(struct device *dev)
Rajendra Nayak90660732016-05-05 14:21:39 +053035{
Amit Kucheria69b628a2019-03-20 18:47:44 +053036 struct tsens_priv *priv = dev_get_drvdata(dev);
Rajendra Nayak90660732016-05-05 14:21:39 +053037
Amit Kucheria69b628a2019-03-20 18:47:44 +053038 if (priv->ops && priv->ops->suspend)
39 return priv->ops->suspend(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +053040
41 return 0;
42}
43
Arnd Bergmann5b974692016-07-04 15:12:28 +020044static int __maybe_unused tsens_resume(struct device *dev)
Rajendra Nayak90660732016-05-05 14:21:39 +053045{
Amit Kucheria69b628a2019-03-20 18:47:44 +053046 struct tsens_priv *priv = dev_get_drvdata(dev);
Rajendra Nayak90660732016-05-05 14:21:39 +053047
Amit Kucheria69b628a2019-03-20 18:47:44 +053048 if (priv->ops && priv->ops->resume)
49 return priv->ops->resume(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +053050
51 return 0;
52}
53
54static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
55
56static const struct of_device_id tsens_table[] = {
57 {
58 .compatible = "qcom,msm8916-tsens",
Rajendra Nayak840a5bd2016-05-05 14:21:40 +053059 .data = &data_8916,
Rajendra Nayak90660732016-05-05 14:21:39 +053060 }, {
61 .compatible = "qcom,msm8974-tsens",
Rajendra Nayak5e6703b2016-05-05 14:21:41 +053062 .data = &data_8974,
Rajendra Nayakd059c732016-05-05 14:21:44 +053063 }, {
64 .compatible = "qcom,msm8996-tsens",
65 .data = &data_8996,
Amit Kucheria191dc742018-07-18 12:13:12 +053066 }, {
Amit Kucheriae8c24c6f2019-03-20 18:48:00 +053067 .compatible = "qcom,tsens-v1",
68 .data = &data_tsens_v1,
69 }, {
Amit Kucheria191dc742018-07-18 12:13:12 +053070 .compatible = "qcom,tsens-v2",
71 .data = &data_tsens_v2,
Rajendra Nayak90660732016-05-05 14:21:39 +053072 },
73 {}
74};
75MODULE_DEVICE_TABLE(of, tsens_table);
76
77static const struct thermal_zone_of_device_ops tsens_of_ops = {
78 .get_temp = tsens_get_temp,
79 .get_trend = tsens_get_trend,
80};
81
Amit Kucheria69b628a2019-03-20 18:47:44 +053082static int tsens_register(struct tsens_priv *priv)
Rajendra Nayak90660732016-05-05 14:21:39 +053083{
84 int i;
85 struct thermal_zone_device *tzd;
Rajendra Nayak90660732016-05-05 14:21:39 +053086
Amit Kucheria69b628a2019-03-20 18:47:44 +053087 for (i = 0; i < priv->num_sensors; i++) {
88 priv->sensor[i].priv = priv;
Amit Kucheria8b71bce2019-11-01 00:07:25 +053089 tzd = devm_thermal_zone_of_sensor_register(priv->dev, priv->sensor[i].hw_id,
Amit Kucheria69b628a2019-03-20 18:47:44 +053090 &priv->sensor[i],
Rajendra Nayak90660732016-05-05 14:21:39 +053091 &tsens_of_ops);
92 if (IS_ERR(tzd))
93 continue;
Amit Kucheria69b628a2019-03-20 18:47:44 +053094 priv->sensor[i].tzd = tzd;
95 if (priv->ops->enable)
96 priv->ops->enable(priv, i);
Rajendra Nayak90660732016-05-05 14:21:39 +053097 }
98 return 0;
99}
100
101static int tsens_probe(struct platform_device *pdev)
102{
103 int ret, i;
104 struct device *dev;
105 struct device_node *np;
Amit Kucheria69b628a2019-03-20 18:47:44 +0530106 struct tsens_priv *priv;
Amit Kucheria3c040ce2019-03-20 18:47:42 +0530107 const struct tsens_plat_data *data;
Rajendra Nayak90660732016-05-05 14:21:39 +0530108 const struct of_device_id *id;
Bjorn Andersson6d7c70d2018-05-07 16:53:39 -0700109 u32 num_sensors;
Rajendra Nayak90660732016-05-05 14:21:39 +0530110
111 if (pdev->dev.of_node)
112 dev = &pdev->dev;
113 else
114 dev = pdev->dev.parent;
115
116 np = dev->of_node;
117
118 id = of_match_node(tsens_table, np);
Rajendra Nayak20d4fd82016-05-05 14:21:43 +0530119 if (id)
120 data = id->data;
121 else
122 data = &data_8960;
Rajendra Nayak90660732016-05-05 14:21:39 +0530123
Bjorn Andersson6d7c70d2018-05-07 16:53:39 -0700124 num_sensors = data->num_sensors;
125
126 if (np)
127 of_property_read_u32(np, "#qcom,sensors", &num_sensors);
128
129 if (num_sensors <= 0) {
Amit Kucheria3795ad52019-11-01 00:07:27 +0530130 dev_err(dev, "%s: invalid number of sensors\n", __func__);
Rajendra Nayak90660732016-05-05 14:21:39 +0530131 return -EINVAL;
132 }
133
Amit Kucheria69b628a2019-03-20 18:47:44 +0530134 priv = devm_kzalloc(dev,
135 struct_size(priv, sensor, num_sensors),
Kees Cook0ed2dd02018-05-08 16:08:53 -0700136 GFP_KERNEL);
Amit Kucheria69b628a2019-03-20 18:47:44 +0530137 if (!priv)
Rajendra Nayak90660732016-05-05 14:21:39 +0530138 return -ENOMEM;
139
Amit Kucheria69b628a2019-03-20 18:47:44 +0530140 priv->dev = dev;
141 priv->num_sensors = num_sensors;
142 priv->ops = data->ops;
143 for (i = 0; i < priv->num_sensors; i++) {
Rajendra Nayak90660732016-05-05 14:21:39 +0530144 if (data->hw_ids)
Amit Kucheria69b628a2019-03-20 18:47:44 +0530145 priv->sensor[i].hw_id = data->hw_ids[i];
Rajendra Nayak90660732016-05-05 14:21:39 +0530146 else
Amit Kucheria69b628a2019-03-20 18:47:44 +0530147 priv->sensor[i].hw_id = i;
Rajendra Nayak90660732016-05-05 14:21:39 +0530148 }
Amit Kucheriac1997052019-03-20 18:47:50 +0530149 priv->feat = data->feat;
150 priv->fields = data->fields;
Rajendra Nayak90660732016-05-05 14:21:39 +0530151
Amit Kucheria0e9c0bc2019-11-01 00:07:26 +0530152 platform_set_drvdata(pdev, priv);
153
Amit Kucheria69b628a2019-03-20 18:47:44 +0530154 if (!priv->ops || !priv->ops->init || !priv->ops->get_temp)
Rajendra Nayak90660732016-05-05 14:21:39 +0530155 return -EINVAL;
156
Amit Kucheria69b628a2019-03-20 18:47:44 +0530157 ret = priv->ops->init(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +0530158 if (ret < 0) {
Amit Kucheria3795ad52019-11-01 00:07:27 +0530159 dev_err(dev, "%s: init failed\n", __func__);
Rajendra Nayak90660732016-05-05 14:21:39 +0530160 return ret;
161 }
162
Amit Kucheria69b628a2019-03-20 18:47:44 +0530163 if (priv->ops->calibrate) {
164 ret = priv->ops->calibrate(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +0530165 if (ret < 0) {
Amit Kucheriafc7d18c2019-03-20 18:47:52 +0530166 if (ret != -EPROBE_DEFER)
Amit Kucheria3795ad52019-11-01 00:07:27 +0530167 dev_err(dev, "%s: calibration failed\n", __func__);
Rajendra Nayak90660732016-05-05 14:21:39 +0530168 return ret;
169 }
170 }
171
Amit Kucheria0e9c0bc2019-11-01 00:07:26 +0530172 return tsens_register(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +0530173}
174
175static int tsens_remove(struct platform_device *pdev)
176{
Amit Kucheria69b628a2019-03-20 18:47:44 +0530177 struct tsens_priv *priv = platform_get_drvdata(pdev);
Rajendra Nayak90660732016-05-05 14:21:39 +0530178
Amit Kucheria69b628a2019-03-20 18:47:44 +0530179 if (priv->ops->disable)
180 priv->ops->disable(priv);
Rajendra Nayak90660732016-05-05 14:21:39 +0530181
182 return 0;
183}
184
185static struct platform_driver tsens_driver = {
186 .probe = tsens_probe,
187 .remove = tsens_remove,
188 .driver = {
189 .name = "qcom-tsens",
190 .pm = &tsens_pm_ops,
191 .of_match_table = tsens_table,
192 },
193};
194module_platform_driver(tsens_driver);
195
196MODULE_LICENSE("GPL v2");
197MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
198MODULE_ALIAS("platform:qcom-tsens");