2018-09-12 17:52:47 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-05-05 16:51:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, The Linux Foundation. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-11-01 02:37:28 +08:00
|
|
|
#include <linux/debugfs.h>
|
2016-05-05 16:51:39 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
2019-11-01 02:37:39 +08:00
|
|
|
#include <linux/of_platform.h>
|
2016-05-05 16:51:39 +08:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/thermal.h>
|
|
|
|
#include "tsens.h"
|
|
|
|
|
|
|
|
static int tsens_get_temp(void *data, int *temp)
|
|
|
|
{
|
2019-11-01 02:37:25 +08:00
|
|
|
struct tsens_sensor *s = data;
|
2019-03-20 21:17:44 +08:00
|
|
|
struct tsens_priv *priv = s->priv;
|
2016-05-05 16:51:39 +08:00
|
|
|
|
2019-11-01 02:37:25 +08:00
|
|
|
return priv->ops->get_temp(s, temp);
|
2016-05-05 16:51:39 +08:00
|
|
|
}
|
|
|
|
|
2019-03-20 21:17:45 +08:00
|
|
|
static int tsens_get_trend(void *data, int trip, enum thermal_trend *trend)
|
2016-05-05 16:51:39 +08:00
|
|
|
{
|
2019-11-01 02:37:25 +08:00
|
|
|
struct tsens_sensor *s = data;
|
2019-03-20 21:17:44 +08:00
|
|
|
struct tsens_priv *priv = s->priv;
|
2016-05-05 16:51:39 +08:00
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
if (priv->ops->get_trend)
|
2019-11-01 02:37:25 +08:00
|
|
|
return priv->ops->get_trend(s, trend);
|
2016-05-05 16:51:39 +08:00
|
|
|
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2016-07-04 21:12:28 +08:00
|
|
|
static int __maybe_unused tsens_suspend(struct device *dev)
|
2016-05-05 16:51:39 +08:00
|
|
|
{
|
2019-03-20 21:17:44 +08:00
|
|
|
struct tsens_priv *priv = dev_get_drvdata(dev);
|
2016-05-05 16:51:39 +08:00
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
if (priv->ops && priv->ops->suspend)
|
|
|
|
return priv->ops->suspend(priv);
|
2016-05-05 16:51:39 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-04 21:12:28 +08:00
|
|
|
static int __maybe_unused tsens_resume(struct device *dev)
|
2016-05-05 16:51:39 +08:00
|
|
|
{
|
2019-03-20 21:17:44 +08:00
|
|
|
struct tsens_priv *priv = dev_get_drvdata(dev);
|
2016-05-05 16:51:39 +08:00
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
if (priv->ops && priv->ops->resume)
|
|
|
|
return priv->ops->resume(priv);
|
2016-05-05 16:51:39 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
|
|
|
|
|
|
|
|
static const struct of_device_id tsens_table[] = {
|
|
|
|
{
|
|
|
|
.compatible = "qcom,msm8916-tsens",
|
2016-05-05 16:51:40 +08:00
|
|
|
.data = &data_8916,
|
2016-05-05 16:51:39 +08:00
|
|
|
}, {
|
|
|
|
.compatible = "qcom,msm8974-tsens",
|
2016-05-05 16:51:41 +08:00
|
|
|
.data = &data_8974,
|
2019-10-05 18:41:31 +08:00
|
|
|
}, {
|
|
|
|
.compatible = "qcom,msm8976-tsens",
|
|
|
|
.data = &data_8976,
|
2016-05-05 16:51:44 +08:00
|
|
|
}, {
|
|
|
|
.compatible = "qcom,msm8996-tsens",
|
|
|
|
.data = &data_8996,
|
2019-03-20 21:18:00 +08:00
|
|
|
}, {
|
|
|
|
.compatible = "qcom,tsens-v1",
|
|
|
|
.data = &data_tsens_v1,
|
2018-07-18 14:43:12 +08:00
|
|
|
}, {
|
|
|
|
.compatible = "qcom,tsens-v2",
|
|
|
|
.data = &data_tsens_v2,
|
2016-05-05 16:51:39 +08:00
|
|
|
},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, tsens_table);
|
|
|
|
|
|
|
|
static const struct thermal_zone_of_device_ops tsens_of_ops = {
|
|
|
|
.get_temp = tsens_get_temp,
|
|
|
|
.get_trend = tsens_get_trend,
|
2019-11-01 02:37:39 +08:00
|
|
|
.set_trips = tsens_set_trips,
|
2016-05-05 16:51:39 +08:00
|
|
|
};
|
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
static int tsens_register(struct tsens_priv *priv)
|
2016-05-05 16:51:39 +08:00
|
|
|
{
|
2019-11-01 02:37:39 +08:00
|
|
|
int i, ret, irq;
|
2016-05-05 16:51:39 +08:00
|
|
|
struct thermal_zone_device *tzd;
|
2019-11-01 02:37:39 +08:00
|
|
|
struct platform_device *pdev;
|
2016-05-05 16:51:39 +08:00
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
for (i = 0; i < priv->num_sensors; i++) {
|
|
|
|
priv->sensor[i].priv = priv;
|
2019-11-01 02:37:25 +08:00
|
|
|
tzd = devm_thermal_zone_of_sensor_register(priv->dev, priv->sensor[i].hw_id,
|
2019-03-20 21:17:44 +08:00
|
|
|
&priv->sensor[i],
|
2016-05-05 16:51:39 +08:00
|
|
|
&tsens_of_ops);
|
|
|
|
if (IS_ERR(tzd))
|
|
|
|
continue;
|
2019-03-20 21:17:44 +08:00
|
|
|
priv->sensor[i].tzd = tzd;
|
|
|
|
if (priv->ops->enable)
|
|
|
|
priv->ops->enable(priv, i);
|
2016-05-05 16:51:39 +08:00
|
|
|
}
|
2019-11-01 02:37:39 +08:00
|
|
|
|
|
|
|
pdev = of_find_device_by_node(priv->dev->of_node);
|
|
|
|
if (!pdev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
irq = platform_get_irq_byname(pdev, "uplow");
|
|
|
|
if (irq < 0) {
|
|
|
|
ret = irq;
|
2019-12-12 18:38:14 +08:00
|
|
|
/* For old DTs with no IRQ defined */
|
|
|
|
if (irq == -ENXIO)
|
|
|
|
ret = 0;
|
2019-11-01 02:37:39 +08:00
|
|
|
goto err_put_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = devm_request_threaded_irq(&pdev->dev, irq,
|
|
|
|
NULL, tsens_irq_thread,
|
|
|
|
IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
|
|
|
|
dev_name(&pdev->dev), priv);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "%s: failed to get irq\n", __func__);
|
|
|
|
goto err_put_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_irq_wake(irq);
|
|
|
|
|
|
|
|
err_put_device:
|
|
|
|
put_device(&pdev->dev);
|
|
|
|
return ret;
|
2016-05-05 16:51:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int tsens_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
struct device *dev;
|
|
|
|
struct device_node *np;
|
2019-03-20 21:17:44 +08:00
|
|
|
struct tsens_priv *priv;
|
2019-03-20 21:17:42 +08:00
|
|
|
const struct tsens_plat_data *data;
|
2016-05-05 16:51:39 +08:00
|
|
|
const struct of_device_id *id;
|
2018-05-08 07:53:39 +08:00
|
|
|
u32 num_sensors;
|
2016-05-05 16:51:39 +08:00
|
|
|
|
|
|
|
if (pdev->dev.of_node)
|
|
|
|
dev = &pdev->dev;
|
|
|
|
else
|
|
|
|
dev = pdev->dev.parent;
|
|
|
|
|
|
|
|
np = dev->of_node;
|
|
|
|
|
|
|
|
id = of_match_node(tsens_table, np);
|
2016-05-05 16:51:43 +08:00
|
|
|
if (id)
|
|
|
|
data = id->data;
|
|
|
|
else
|
|
|
|
data = &data_8960;
|
2016-05-05 16:51:39 +08:00
|
|
|
|
2018-05-08 07:53:39 +08:00
|
|
|
num_sensors = data->num_sensors;
|
|
|
|
|
|
|
|
if (np)
|
|
|
|
of_property_read_u32(np, "#qcom,sensors", &num_sensors);
|
|
|
|
|
|
|
|
if (num_sensors <= 0) {
|
2019-11-01 02:37:27 +08:00
|
|
|
dev_err(dev, "%s: invalid number of sensors\n", __func__);
|
2016-05-05 16:51:39 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
priv = devm_kzalloc(dev,
|
|
|
|
struct_size(priv, sensor, num_sensors),
|
treewide: Use struct_size() for devm_kmalloc() and friends
Replaces open-coded struct size calculations with struct_size() for
devm_*, f2fs_*, and sock_* allocations. Automatically generated (and
manually adjusted) from the following Coccinelle script:
// Direct reference to struct field.
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@
- alloc(HANDLE, sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT), GFP)
+ alloc(HANDLE, struct_size(VAR, ELEMENT, COUNT), GFP)
// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@
- alloc(HANDLE, sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]), GFP)
+ alloc(HANDLE, struct_size(VAR, ELEMENT, COUNT), GFP)
// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
expression SOMETHING, COUNT, ELEMENT;
@@
- alloc(HANDLE, sizeof(SOMETHING) + COUNT * sizeof(ELEMENT), GFP)
+ alloc(HANDLE, CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT), GFP)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-05-09 07:08:53 +08:00
|
|
|
GFP_KERNEL);
|
2019-03-20 21:17:44 +08:00
|
|
|
if (!priv)
|
2016-05-05 16:51:39 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
priv->dev = dev;
|
|
|
|
priv->num_sensors = num_sensors;
|
|
|
|
priv->ops = data->ops;
|
|
|
|
for (i = 0; i < priv->num_sensors; i++) {
|
2016-05-05 16:51:39 +08:00
|
|
|
if (data->hw_ids)
|
2019-03-20 21:17:44 +08:00
|
|
|
priv->sensor[i].hw_id = data->hw_ids[i];
|
2016-05-05 16:51:39 +08:00
|
|
|
else
|
2019-03-20 21:17:44 +08:00
|
|
|
priv->sensor[i].hw_id = i;
|
2016-05-05 16:51:39 +08:00
|
|
|
}
|
2019-03-20 21:17:50 +08:00
|
|
|
priv->feat = data->feat;
|
|
|
|
priv->fields = data->fields;
|
2016-05-05 16:51:39 +08:00
|
|
|
|
2019-11-01 02:37:26 +08:00
|
|
|
platform_set_drvdata(pdev, priv);
|
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
if (!priv->ops || !priv->ops->init || !priv->ops->get_temp)
|
2016-05-05 16:51:39 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
ret = priv->ops->init(priv);
|
2016-05-05 16:51:39 +08:00
|
|
|
if (ret < 0) {
|
2019-11-01 02:37:27 +08:00
|
|
|
dev_err(dev, "%s: init failed\n", __func__);
|
2016-05-05 16:51:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-20 21:17:44 +08:00
|
|
|
if (priv->ops->calibrate) {
|
|
|
|
ret = priv->ops->calibrate(priv);
|
2016-05-05 16:51:39 +08:00
|
|
|
if (ret < 0) {
|
2019-03-20 21:17:52 +08:00
|
|
|
if (ret != -EPROBE_DEFER)
|
2019-11-01 02:37:27 +08:00
|
|
|
dev_err(dev, "%s: calibration failed\n", __func__);
|
2016-05-05 16:51:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 02:37:26 +08:00
|
|
|
return tsens_register(priv);
|
2016-05-05 16:51:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int tsens_remove(struct platform_device *pdev)
|
|
|
|
{
|
2019-03-20 21:17:44 +08:00
|
|
|
struct tsens_priv *priv = platform_get_drvdata(pdev);
|
2016-05-05 16:51:39 +08:00
|
|
|
|
2019-11-01 02:37:28 +08:00
|
|
|
debugfs_remove_recursive(priv->debug_root);
|
2019-11-01 02:37:39 +08:00
|
|
|
tsens_disable_irq(priv);
|
2019-03-20 21:17:44 +08:00
|
|
|
if (priv->ops->disable)
|
|
|
|
priv->ops->disable(priv);
|
2016-05-05 16:51:39 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver tsens_driver = {
|
|
|
|
.probe = tsens_probe,
|
|
|
|
.remove = tsens_remove,
|
|
|
|
.driver = {
|
|
|
|
.name = "qcom-tsens",
|
|
|
|
.pm = &tsens_pm_ops,
|
|
|
|
.of_match_table = tsens_table,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
module_platform_driver(tsens_driver);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
|
|
|
|
MODULE_ALIAS("platform:qcom-tsens");
|