dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / thermal / qcom / tsens.c
blobf1ec9bbe4717e6f0f5c3aea4fb19b5963a189d19
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4 */
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"
15 static int tsens_get_temp(void *data, int *temp)
17 const struct tsens_sensor *s = data;
18 struct tsens_device *tmdev = s->tmdev;
20 return tmdev->ops->get_temp(tmdev, s->id, temp);
23 static int tsens_get_trend(void *p, int trip, enum thermal_trend *trend)
25 const struct tsens_sensor *s = p;
26 struct tsens_device *tmdev = s->tmdev;
28 if (tmdev->ops->get_trend)
29 return tmdev->ops->get_trend(tmdev, s->id, trend);
31 return -ENOTSUPP;
34 static int __maybe_unused tsens_suspend(struct device *dev)
36 struct tsens_device *tmdev = dev_get_drvdata(dev);
38 if (tmdev->ops && tmdev->ops->suspend)
39 return tmdev->ops->suspend(tmdev);
41 return 0;
44 static int __maybe_unused tsens_resume(struct device *dev)
46 struct tsens_device *tmdev = dev_get_drvdata(dev);
48 if (tmdev->ops && tmdev->ops->resume)
49 return tmdev->ops->resume(tmdev);
51 return 0;
54 static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
56 static const struct of_device_id tsens_table[] = {
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-v2",
68 .data = &data_tsens_v2,
72 MODULE_DEVICE_TABLE(of, tsens_table);
74 static const struct thermal_zone_of_device_ops tsens_of_ops = {
75 .get_temp = tsens_get_temp,
76 .get_trend = tsens_get_trend,
79 static int tsens_register(struct tsens_device *tmdev)
81 int i;
82 struct thermal_zone_device *tzd;
84 for (i = 0; i < tmdev->num_sensors; i++) {
85 tmdev->sensor[i].tmdev = tmdev;
86 tmdev->sensor[i].id = i;
87 tzd = devm_thermal_zone_of_sensor_register(tmdev->dev, i,
88 &tmdev->sensor[i],
89 &tsens_of_ops);
90 if (IS_ERR(tzd))
91 continue;
92 tmdev->sensor[i].tzd = tzd;
93 if (tmdev->ops->enable)
94 tmdev->ops->enable(tmdev, i);
96 return 0;
99 static int tsens_probe(struct platform_device *pdev)
101 int ret, i;
102 struct device *dev;
103 struct device_node *np;
104 struct tsens_device *tmdev;
105 const struct tsens_data *data;
106 const struct of_device_id *id;
107 u32 num_sensors;
109 if (pdev->dev.of_node)
110 dev = &pdev->dev;
111 else
112 dev = pdev->dev.parent;
114 np = dev->of_node;
116 id = of_match_node(tsens_table, np);
117 if (id)
118 data = id->data;
119 else
120 data = &data_8960;
122 num_sensors = data->num_sensors;
124 if (np)
125 of_property_read_u32(np, "#qcom,sensors", &num_sensors);
127 if (num_sensors <= 0) {
128 dev_err(dev, "invalid number of sensors\n");
129 return -EINVAL;
132 tmdev = devm_kzalloc(dev,
133 struct_size(tmdev, sensor, num_sensors),
134 GFP_KERNEL);
135 if (!tmdev)
136 return -ENOMEM;
138 tmdev->dev = dev;
139 tmdev->num_sensors = num_sensors;
140 tmdev->ops = data->ops;
141 for (i = 0; i < tmdev->num_sensors; i++) {
142 if (data->hw_ids)
143 tmdev->sensor[i].hw_id = data->hw_ids[i];
144 else
145 tmdev->sensor[i].hw_id = i;
147 for (i = 0; i < REG_ARRAY_SIZE; i++) {
148 tmdev->reg_offsets[i] = data->reg_offsets[i];
151 if (!tmdev->ops || !tmdev->ops->init || !tmdev->ops->get_temp)
152 return -EINVAL;
154 ret = tmdev->ops->init(tmdev);
155 if (ret < 0) {
156 dev_err(dev, "tsens init failed\n");
157 return ret;
160 if (tmdev->ops->calibrate) {
161 ret = tmdev->ops->calibrate(tmdev);
162 if (ret < 0) {
163 dev_err(dev, "tsens calibration failed\n");
164 return ret;
168 ret = tsens_register(tmdev);
170 platform_set_drvdata(pdev, tmdev);
172 return ret;
175 static int tsens_remove(struct platform_device *pdev)
177 struct tsens_device *tmdev = platform_get_drvdata(pdev);
179 if (tmdev->ops->disable)
180 tmdev->ops->disable(tmdev);
182 return 0;
185 static 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,
194 module_platform_driver(tsens_driver);
196 MODULE_LICENSE("GPL v2");
197 MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
198 MODULE_ALIAS("platform:qcom-tsens");