summaryrefslogtreecommitdiffstats
path: root/arch/powerpc/platforms/powernv/opal-sensor.c
blob: 3192c614a1e17134be2ed56874c95a207ceacc25 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * PowerNV sensor code
 *
 * Copyright (C) 2013 IBM
 */

#include <linux/delay.h>
#include <linux/of_platform.h>
#include <asm/opal.h>
#include <asm/machdep.h>

/*
 * This will return sensor information to driver based on the requested sensor
 * handle. A handle is an opaque id for the powernv, read by the driver from the
 * device tree..
 */
int opal_get_sensor_data(u32 sensor_hndl, u32 *sensor_data)
{
	int ret, token;
	struct opal_msg msg;
	__be32 data;

	token = opal_async_get_token_interruptible();
	if (token < 0)
		return token;

	ret = opal_sensor_read(sensor_hndl, token, &data);
	switch (ret) {
	case OPAL_ASYNC_COMPLETION:
		ret = opal_async_wait_response(token, &msg);
		if (ret) {
			pr_err("%s: Failed to wait for the async response, %d\n",
			       __func__, ret);
			goto out;
		}

		ret = opal_error_code(opal_get_async_rc(msg));
		*sensor_data = be32_to_cpu(data);
		break;

	case OPAL_SUCCESS:
		ret = 0;
		*sensor_data = be32_to_cpu(data);
		break;

	case OPAL_WRONG_STATE:
		ret = -EIO;
		break;

	default:
		ret = opal_error_code(ret);
		break;
	}

out:
	opal_async_release_token(token);
	return ret;
}
EXPORT_SYMBOL_GPL(opal_get_sensor_data);

int opal_get_sensor_data_u64(u32 sensor_hndl, u64 *sensor_data)
{
	int ret, token;
	struct opal_msg msg;
	__be64 data;

	if (!opal_check_token(OPAL_SENSOR_READ_U64)) {
		u32 sdata;

		ret = opal_get_sensor_data(sensor_hndl, &sdata);
		if (!ret)
			*sensor_data = sdata;
		return ret;
	}

	token = opal_async_get_token_interruptible();
	if (token < 0)
		return token;

	ret = opal_sensor_read_u64(sensor_hndl, token, &data);
	switch (ret) {
	case OPAL_ASYNC_COMPLETION:
		ret = opal_async_wait_response(token, &msg);
		if (ret) {
			pr_err("%s: Failed to wait for the async response, %d\n",
			       __func__, ret);
			goto out_token;
		}

		ret = opal_error_code(opal_get_async_rc(msg));
		*sensor_data = be64_to_cpu(data);
		break;

	case OPAL_SUCCESS:
		ret = 0;
		*sensor_data = be64_to_cpu(data);
		break;

	case OPAL_WRONG_STATE:
		ret = -EIO;
		break;

	default:
		ret = opal_error_code(ret);
		break;
	}

out_token:
	opal_async_release_token(token);
	return ret;
}
EXPORT_SYMBOL_GPL(opal_get_sensor_data_u64);

int __init opal_sensor_init(void)
{
	struct platform_device *pdev;
	struct device_node *sensor;

	sensor = of_find_node_by_path("/ibm,opal/sensors");
	if (!sensor) {
		pr_err("Opal node 'sensors' not found\n");
		return -ENODEV;
	}

	pdev = of_platform_device_create(sensor, "opal-sensor", NULL);
	of_node_put(sensor);

	return PTR_ERR_OR_ZERO(pdev);
}