36b0b52db524cb78114b912b7e6d0b8514844592
[sfrench/cifs-2.6.git] / drivers / thermal / qcom / tsens.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4  */
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
15 static int tsens_get_temp(void *data, int *temp)
16 {
17         const struct tsens_sensor *s = data;
18         struct tsens_priv *priv = s->priv;
19
20         return priv->ops->get_temp(priv, s->id, temp);
21 }
22
23 static int tsens_get_trend(void *data, int trip, enum thermal_trend *trend)
24 {
25         const struct tsens_sensor *s = data;
26         struct tsens_priv *priv = s->priv;
27
28         if (priv->ops->get_trend)
29                 return priv->ops->get_trend(priv, s->id, trend);
30
31         return -ENOTSUPP;
32 }
33
34 static int  __maybe_unused tsens_suspend(struct device *dev)
35 {
36         struct tsens_priv *priv = dev_get_drvdata(dev);
37
38         if (priv->ops && priv->ops->suspend)
39                 return priv->ops->suspend(priv);
40
41         return 0;
42 }
43
44 static int __maybe_unused tsens_resume(struct device *dev)
45 {
46         struct tsens_priv *priv = dev_get_drvdata(dev);
47
48         if (priv->ops && priv->ops->resume)
49                 return priv->ops->resume(priv);
50
51         return 0;
52 }
53
54 static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
55
56 static const struct of_device_id tsens_table[] = {
57         {
58                 .compatible = "qcom,msm8916-tsens",
59                 .data = &data_8916,
60         }, {
61                 .compatible = "qcom,msm8974-tsens",
62                 .data = &data_8974,
63         }, {
64                 .compatible = "qcom,msm8996-tsens",
65                 .data = &data_8996,
66         }, {
67                 .compatible = "qcom,tsens-v1",
68                 .data = &data_tsens_v1,
69         }, {
70                 .compatible = "qcom,tsens-v2",
71                 .data = &data_tsens_v2,
72         },
73         {}
74 };
75 MODULE_DEVICE_TABLE(of, tsens_table);
76
77 static const struct thermal_zone_of_device_ops tsens_of_ops = {
78         .get_temp = tsens_get_temp,
79         .get_trend = tsens_get_trend,
80 };
81
82 static int tsens_register(struct tsens_priv *priv)
83 {
84         int i;
85         struct thermal_zone_device *tzd;
86
87         for (i = 0;  i < priv->num_sensors; i++) {
88                 if (!is_sensor_enabled(priv, priv->sensor[i].hw_id)) {
89                         dev_err(priv->dev, "sensor %d: disabled\n",
90                                 priv->sensor[i].hw_id);
91                         continue;
92                 }
93                 priv->sensor[i].priv = priv;
94                 priv->sensor[i].id = i;
95                 tzd = devm_thermal_zone_of_sensor_register(priv->dev, i,
96                                                            &priv->sensor[i],
97                                                            &tsens_of_ops);
98                 if (IS_ERR(tzd))
99                         continue;
100                 priv->sensor[i].tzd = tzd;
101                 if (priv->ops->enable)
102                         priv->ops->enable(priv, i);
103         }
104         return 0;
105 }
106
107 static int tsens_probe(struct platform_device *pdev)
108 {
109         int ret, i;
110         struct device *dev;
111         struct device_node *np;
112         struct tsens_priv *priv;
113         const struct tsens_plat_data *data;
114         const struct of_device_id *id;
115         u32 num_sensors;
116
117         if (pdev->dev.of_node)
118                 dev = &pdev->dev;
119         else
120                 dev = pdev->dev.parent;
121
122         np = dev->of_node;
123
124         id = of_match_node(tsens_table, np);
125         if (id)
126                 data = id->data;
127         else
128                 data = &data_8960;
129
130         num_sensors = data->num_sensors;
131
132         if (np)
133                 of_property_read_u32(np, "#qcom,sensors", &num_sensors);
134
135         if (num_sensors <= 0) {
136                 dev_err(dev, "invalid number of sensors\n");
137                 return -EINVAL;
138         }
139
140         priv = devm_kzalloc(dev,
141                              struct_size(priv, sensor, num_sensors),
142                              GFP_KERNEL);
143         if (!priv)
144                 return -ENOMEM;
145
146         priv->dev = dev;
147         priv->num_sensors = num_sensors;
148         priv->ops = data->ops;
149         for (i = 0;  i < priv->num_sensors; i++) {
150                 if (data->hw_ids)
151                         priv->sensor[i].hw_id = data->hw_ids[i];
152                 else
153                         priv->sensor[i].hw_id = i;
154         }
155         priv->feat = data->feat;
156         priv->fields = data->fields;
157
158         if (!priv->ops || !priv->ops->init || !priv->ops->get_temp)
159                 return -EINVAL;
160
161         ret = priv->ops->init(priv);
162         if (ret < 0) {
163                 dev_err(dev, "tsens init failed\n");
164                 return ret;
165         }
166
167         if (priv->ops->calibrate) {
168                 ret = priv->ops->calibrate(priv);
169                 if (ret < 0) {
170                         if (ret != -EPROBE_DEFER)
171                                 dev_err(dev, "tsens calibration failed\n");
172                         return ret;
173                 }
174         }
175
176         ret = tsens_register(priv);
177
178         platform_set_drvdata(pdev, priv);
179
180         return ret;
181 }
182
183 static int tsens_remove(struct platform_device *pdev)
184 {
185         struct tsens_priv *priv = platform_get_drvdata(pdev);
186
187         if (priv->ops->disable)
188                 priv->ops->disable(priv);
189
190         return 0;
191 }
192
193 static struct platform_driver tsens_driver = {
194         .probe = tsens_probe,
195         .remove = tsens_remove,
196         .driver = {
197                 .name = "qcom-tsens",
198                 .pm     = &tsens_pm_ops,
199                 .of_match_table = tsens_table,
200         },
201 };
202 module_platform_driver(tsens_driver);
203
204 MODULE_LICENSE("GPL v2");
205 MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
206 MODULE_ALIAS("platform:qcom-tsens");