xref: /openbmc/linux/drivers/thermal/qcom/tsens.c (revision 532c2919)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/debugfs.h>
7 #include <linux/err.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/of_platform.h>
11 #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 
17 static int tsens_get_temp(void *data, int *temp)
18 {
19 	struct tsens_sensor *s = data;
20 	struct tsens_priv *priv = s->priv;
21 
22 	return priv->ops->get_temp(s, temp);
23 }
24 
25 static int tsens_get_trend(void *data, int trip, enum thermal_trend *trend)
26 {
27 	struct tsens_sensor *s = data;
28 	struct tsens_priv *priv = s->priv;
29 
30 	if (priv->ops->get_trend)
31 		return priv->ops->get_trend(s, trend);
32 
33 	return -ENOTSUPP;
34 }
35 
36 static int  __maybe_unused tsens_suspend(struct device *dev)
37 {
38 	struct tsens_priv *priv = dev_get_drvdata(dev);
39 
40 	if (priv->ops && priv->ops->suspend)
41 		return priv->ops->suspend(priv);
42 
43 	return 0;
44 }
45 
46 static int __maybe_unused tsens_resume(struct device *dev)
47 {
48 	struct tsens_priv *priv = dev_get_drvdata(dev);
49 
50 	if (priv->ops && priv->ops->resume)
51 		return priv->ops->resume(priv);
52 
53 	return 0;
54 }
55 
56 static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
57 
58 static const struct of_device_id tsens_table[] = {
59 	{
60 		.compatible = "qcom,msm8916-tsens",
61 		.data = &data_8916,
62 	}, {
63 		.compatible = "qcom,msm8974-tsens",
64 		.data = &data_8974,
65 	}, {
66 		.compatible = "qcom,msm8976-tsens",
67 		.data = &data_8976,
68 	}, {
69 		.compatible = "qcom,msm8996-tsens",
70 		.data = &data_8996,
71 	}, {
72 		.compatible = "qcom,tsens-v1",
73 		.data = &data_tsens_v1,
74 	}, {
75 		.compatible = "qcom,tsens-v2",
76 		.data = &data_tsens_v2,
77 	},
78 	{}
79 };
80 MODULE_DEVICE_TABLE(of, tsens_table);
81 
82 static const struct thermal_zone_of_device_ops tsens_of_ops = {
83 	.get_temp = tsens_get_temp,
84 	.get_trend = tsens_get_trend,
85 	.set_trips = tsens_set_trips,
86 };
87 
88 static int tsens_register(struct tsens_priv *priv)
89 {
90 	int i, ret, irq;
91 	struct thermal_zone_device *tzd;
92 	struct platform_device *pdev;
93 
94 	for (i = 0;  i < priv->num_sensors; i++) {
95 		priv->sensor[i].priv = priv;
96 		tzd = devm_thermal_zone_of_sensor_register(priv->dev, priv->sensor[i].hw_id,
97 							   &priv->sensor[i],
98 							   &tsens_of_ops);
99 		if (IS_ERR(tzd))
100 			continue;
101 		priv->sensor[i].tzd = tzd;
102 		if (priv->ops->enable)
103 			priv->ops->enable(priv, i);
104 	}
105 
106 	pdev = of_find_device_by_node(priv->dev->of_node);
107 	if (!pdev)
108 		return -ENODEV;
109 
110 	irq = platform_get_irq_byname(pdev, "uplow");
111 	if (irq < 0) {
112 		ret = irq;
113 		/* For old DTs with no IRQ defined */
114 		if (irq == -ENXIO)
115 			ret = 0;
116 		goto err_put_device;
117 	}
118 
119 	ret = devm_request_threaded_irq(&pdev->dev, irq,
120 					NULL, tsens_irq_thread,
121 					IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
122 					dev_name(&pdev->dev), priv);
123 	if (ret) {
124 		dev_err(&pdev->dev, "%s: failed to get irq\n", __func__);
125 		goto err_put_device;
126 	}
127 
128 	enable_irq_wake(irq);
129 
130 err_put_device:
131 	put_device(&pdev->dev);
132 	return ret;
133 }
134 
135 static int tsens_probe(struct platform_device *pdev)
136 {
137 	int ret, i;
138 	struct device *dev;
139 	struct device_node *np;
140 	struct tsens_priv *priv;
141 	const struct tsens_plat_data *data;
142 	const struct of_device_id *id;
143 	u32 num_sensors;
144 
145 	if (pdev->dev.of_node)
146 		dev = &pdev->dev;
147 	else
148 		dev = pdev->dev.parent;
149 
150 	np = dev->of_node;
151 
152 	id = of_match_node(tsens_table, np);
153 	if (id)
154 		data = id->data;
155 	else
156 		data = &data_8960;
157 
158 	num_sensors = data->num_sensors;
159 
160 	if (np)
161 		of_property_read_u32(np, "#qcom,sensors", &num_sensors);
162 
163 	if (num_sensors <= 0) {
164 		dev_err(dev, "%s: invalid number of sensors\n", __func__);
165 		return -EINVAL;
166 	}
167 
168 	priv = devm_kzalloc(dev,
169 			     struct_size(priv, sensor, num_sensors),
170 			     GFP_KERNEL);
171 	if (!priv)
172 		return -ENOMEM;
173 
174 	priv->dev = dev;
175 	priv->num_sensors = num_sensors;
176 	priv->ops = data->ops;
177 	for (i = 0;  i < priv->num_sensors; i++) {
178 		if (data->hw_ids)
179 			priv->sensor[i].hw_id = data->hw_ids[i];
180 		else
181 			priv->sensor[i].hw_id = i;
182 	}
183 	priv->feat = data->feat;
184 	priv->fields = data->fields;
185 
186 	platform_set_drvdata(pdev, priv);
187 
188 	if (!priv->ops || !priv->ops->init || !priv->ops->get_temp)
189 		return -EINVAL;
190 
191 	ret = priv->ops->init(priv);
192 	if (ret < 0) {
193 		dev_err(dev, "%s: init failed\n", __func__);
194 		return ret;
195 	}
196 
197 	if (priv->ops->calibrate) {
198 		ret = priv->ops->calibrate(priv);
199 		if (ret < 0) {
200 			if (ret != -EPROBE_DEFER)
201 				dev_err(dev, "%s: calibration failed\n", __func__);
202 			return ret;
203 		}
204 	}
205 
206 	return tsens_register(priv);
207 }
208 
209 static int tsens_remove(struct platform_device *pdev)
210 {
211 	struct tsens_priv *priv = platform_get_drvdata(pdev);
212 
213 	debugfs_remove_recursive(priv->debug_root);
214 	tsens_disable_irq(priv);
215 	if (priv->ops->disable)
216 		priv->ops->disable(priv);
217 
218 	return 0;
219 }
220 
221 static struct platform_driver tsens_driver = {
222 	.probe = tsens_probe,
223 	.remove = tsens_remove,
224 	.driver = {
225 		.name = "qcom-tsens",
226 		.pm	= &tsens_pm_ops,
227 		.of_match_table = tsens_table,
228 	},
229 };
230 module_platform_driver(tsens_driver);
231 
232 MODULE_LICENSE("GPL v2");
233 MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
234 MODULE_ALIAS("platform:qcom-tsens");
235