summaryrefslogtreecommitdiff
path: root/drivers/hwmon/fp9931-hwmon.c
blob: babb8291051a9f5a4854506181f305d5255b2b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Fitipower FP9931 PMIC temperature monitor driver
 *
 * Copyright 2021 NXP
 */

#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/mfd/fp9931.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>

struct fp9931_hwmon {
	struct device *dev;
	struct device *hwmon_dev;
	struct fp9931 *fp9931;
	struct regulator *en_ts;
};

static ssize_t fp9931_hwmon_temp_show(struct device *dev,
		struct device_attribute *attr, char *buf);

static SENSOR_DEVICE_ATTR_RO(temp1_input, fp9931_hwmon_temp, 0);

static struct attribute *fp9931_hwmon_attrs[] = {
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	NULL
};
ATTRIBUTE_GROUPS(fp9931_hwmon);

static ssize_t fp9931_hwmon_temp_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	int ret;
	u8 tmst_value;
	struct fp9931_hwmon *hwmon = dev_get_drvdata(dev);

	ret = fp9931_reg_read(hwmon->fp9931->client,
			      FP9931_TMST_VALUE,
			      &tmst_value);
	if (ret)
		return ret;

	return sprintf(buf, "%d\n", (s32)((s8)tmst_value));
}

static int fp9931_hwmon_probe(struct platform_device *pdev)
{
	int ret;
	struct fp9931_hwmon *hwmon;

	hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
	if (!hwmon)
		return -ENOMEM;
	hwmon->dev = &pdev->dev;

	hwmon->en_ts = devm_regulator_get(pdev->dev.parent, "en-ts");
	if (IS_ERR(hwmon->en_ts)) {
		ret = PTR_ERR(hwmon->en_ts);
		dev_err(&pdev->dev, "failed to get en-ts regulator : %d\n", ret);

		return ret;
	}

	hwmon->hwmon_dev = devm_hwmon_device_register_with_groups(hwmon->dev,
							"fp9931_hwmon", hwmon,
							fp9931_hwmon_groups);
	if (IS_ERR(hwmon->hwmon_dev)) {
		ret = PTR_ERR(hwmon->hwmon_dev);
		dev_err(hwmon->dev, "failed to register hwmon for fp9931 : %d\n", ret);

		return ret;
	}

	hwmon->fp9931 = dev_get_drvdata(pdev->dev.parent);
	WARN_ON(IS_ERR_OR_NULL(hwmon->fp9931));
	platform_set_drvdata(pdev, hwmon);

	return regulator_enable(hwmon->en_ts);
}

static int fp9931_hwmon_remove(struct platform_device *pdev)
{
	struct fp9931_hwmon *hwmon = platform_get_drvdata(pdev);

	return regulator_disable(hwmon->en_ts);
}

static const struct platform_device_id fp9931_hwmon_id[] = {
	{ "fp9931-hwmon", 0 },
	{ /* sentinel */    },
};

static struct platform_driver fp9931_hwmon_driver = {
	.probe	= fp9931_hwmon_probe,
	.remove	= fp9931_hwmon_remove,
	.id_table = fp9931_hwmon_id,
	.driver	= {
		.name = "fp9931_hwmon",
	},
};

module_platform_driver(fp9931_hwmon_driver);

MODULE_DESCRIPTION("FP9931 hardware monitor driver");
MODULE_AUTHOR("Fancy Fang <chen.fang@nxp.com>");
MODULE_LICENSE("GPL");