xref: /OK3568_Linux_fs/kernel/drivers/hwmon/xgene-hwmon.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * APM X-Gene SoC Hardware Monitoring Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2016, Applied Micro Circuits Corporation
6*4882a593Smuzhiyun  * Author: Loc Ho <lho@apm.com>
7*4882a593Smuzhiyun  *         Hoan Tran <hotran@apm.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This driver provides the following features:
10*4882a593Smuzhiyun  *  - Retrieve CPU total power (uW)
11*4882a593Smuzhiyun  *  - Retrieve IO total power (uW)
12*4882a593Smuzhiyun  *  - Retrieve SoC temperature (milli-degree C) and alarm
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun #include <linux/acpi.h>
15*4882a593Smuzhiyun #include <linux/dma-mapping.h>
16*4882a593Smuzhiyun #include <linux/hwmon.h>
17*4882a593Smuzhiyun #include <linux/hwmon-sysfs.h>
18*4882a593Smuzhiyun #include <linux/io.h>
19*4882a593Smuzhiyun #include <linux/interrupt.h>
20*4882a593Smuzhiyun #include <linux/kfifo.h>
21*4882a593Smuzhiyun #include <linux/mailbox_controller.h>
22*4882a593Smuzhiyun #include <linux/mailbox_client.h>
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/of.h>
25*4882a593Smuzhiyun #include <linux/platform_device.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <acpi/pcc.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* SLIMpro message defines */
30*4882a593Smuzhiyun #define MSG_TYPE_DBG			0
31*4882a593Smuzhiyun #define MSG_TYPE_ERR			7
32*4882a593Smuzhiyun #define MSG_TYPE_PWRMGMT		9
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define MSG_TYPE(v)			(((v) & 0xF0000000) >> 28)
35*4882a593Smuzhiyun #define MSG_TYPE_SET(v)			(((v) << 28) & 0xF0000000)
36*4882a593Smuzhiyun #define MSG_SUBTYPE(v)			(((v) & 0x0F000000) >> 24)
37*4882a593Smuzhiyun #define MSG_SUBTYPE_SET(v)		(((v) << 24) & 0x0F000000)
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define DBG_SUBTYPE_SENSOR_READ		4
40*4882a593Smuzhiyun #define SENSOR_RD_MSG			0x04FFE902
41*4882a593Smuzhiyun #define SENSOR_RD_EN_ADDR(a)		((a) & 0x000FFFFF)
42*4882a593Smuzhiyun #define PMD_PWR_REG			0x20
43*4882a593Smuzhiyun #define PMD_PWR_MW_REG			0x26
44*4882a593Smuzhiyun #define SOC_PWR_REG			0x21
45*4882a593Smuzhiyun #define SOC_PWR_MW_REG			0x27
46*4882a593Smuzhiyun #define SOC_TEMP_REG			0x10
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define TEMP_NEGATIVE_BIT		8
49*4882a593Smuzhiyun #define SENSOR_INVALID_DATA		BIT(15)
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define PWRMGMT_SUBTYPE_TPC		1
52*4882a593Smuzhiyun #define TPC_ALARM			2
53*4882a593Smuzhiyun #define TPC_GET_ALARM			3
54*4882a593Smuzhiyun #define TPC_CMD(v)			(((v) & 0x00FF0000) >> 16)
55*4882a593Smuzhiyun #define TPC_CMD_SET(v)			(((v) << 16) & 0x00FF0000)
56*4882a593Smuzhiyun #define TPC_EN_MSG(hndl, cmd, type) \
57*4882a593Smuzhiyun 	(MSG_TYPE_SET(MSG_TYPE_PWRMGMT) | \
58*4882a593Smuzhiyun 	MSG_SUBTYPE_SET(hndl) | TPC_CMD_SET(cmd) | type)
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /* PCC defines */
61*4882a593Smuzhiyun #define PCC_SIGNATURE_MASK		0x50424300
62*4882a593Smuzhiyun #define PCCC_GENERATE_DB_INT		BIT(15)
63*4882a593Smuzhiyun #define PCCS_CMD_COMPLETE		BIT(0)
64*4882a593Smuzhiyun #define PCCS_SCI_DOORBEL		BIT(1)
65*4882a593Smuzhiyun #define PCCS_PLATFORM_NOTIFICATION	BIT(3)
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun  * Arbitrary retries in case the remote processor is slow to respond
68*4882a593Smuzhiyun  * to PCC commands
69*4882a593Smuzhiyun  */
70*4882a593Smuzhiyun #define PCC_NUM_RETRIES			500
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #define ASYNC_MSG_FIFO_SIZE		16
73*4882a593Smuzhiyun #define MBOX_OP_TIMEOUTMS		1000
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #define WATT_TO_mWATT(x)		((x) * 1000)
76*4882a593Smuzhiyun #define mWATT_TO_uWATT(x)		((x) * 1000)
77*4882a593Smuzhiyun #define CELSIUS_TO_mCELSIUS(x)		((x) * 1000)
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #define to_xgene_hwmon_dev(cl)		\
80*4882a593Smuzhiyun 	container_of(cl, struct xgene_hwmon_dev, mbox_client)
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun enum xgene_hwmon_version {
83*4882a593Smuzhiyun 	XGENE_HWMON_V1 = 0,
84*4882a593Smuzhiyun 	XGENE_HWMON_V2 = 1,
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun struct slimpro_resp_msg {
88*4882a593Smuzhiyun 	u32 msg;
89*4882a593Smuzhiyun 	u32 param1;
90*4882a593Smuzhiyun 	u32 param2;
91*4882a593Smuzhiyun } __packed;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun struct xgene_hwmon_dev {
94*4882a593Smuzhiyun 	struct device		*dev;
95*4882a593Smuzhiyun 	struct mbox_chan	*mbox_chan;
96*4882a593Smuzhiyun 	struct mbox_client	mbox_client;
97*4882a593Smuzhiyun 	int			mbox_idx;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	spinlock_t		kfifo_lock;
100*4882a593Smuzhiyun 	struct mutex		rd_mutex;
101*4882a593Smuzhiyun 	struct completion	rd_complete;
102*4882a593Smuzhiyun 	int			resp_pending;
103*4882a593Smuzhiyun 	struct slimpro_resp_msg sync_msg;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	struct work_struct	workq;
106*4882a593Smuzhiyun 	struct kfifo_rec_ptr_1	async_msg_fifo;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	struct device		*hwmon_dev;
109*4882a593Smuzhiyun 	bool			temp_critical_alarm;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	phys_addr_t		comm_base_addr;
112*4882a593Smuzhiyun 	void			*pcc_comm_addr;
113*4882a593Smuzhiyun 	u64			usecs_lat;
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun  * This function tests and clears a bitmask then returns its old value
118*4882a593Smuzhiyun  */
xgene_word_tst_and_clr(u16 * addr,u16 mask)119*4882a593Smuzhiyun static u16 xgene_word_tst_and_clr(u16 *addr, u16 mask)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	u16 ret, val;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	val = le16_to_cpu(READ_ONCE(*addr));
124*4882a593Smuzhiyun 	ret = val & mask;
125*4882a593Smuzhiyun 	val &= ~mask;
126*4882a593Smuzhiyun 	WRITE_ONCE(*addr, cpu_to_le16(val));
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	return ret;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
xgene_hwmon_pcc_rd(struct xgene_hwmon_dev * ctx,u32 * msg)131*4882a593Smuzhiyun static int xgene_hwmon_pcc_rd(struct xgene_hwmon_dev *ctx, u32 *msg)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
134*4882a593Smuzhiyun 	u32 *ptr = (void *)(generic_comm_base + 1);
135*4882a593Smuzhiyun 	int rc, i;
136*4882a593Smuzhiyun 	u16 val;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	mutex_lock(&ctx->rd_mutex);
139*4882a593Smuzhiyun 	init_completion(&ctx->rd_complete);
140*4882a593Smuzhiyun 	ctx->resp_pending = true;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* Write signature for subspace */
143*4882a593Smuzhiyun 	WRITE_ONCE(generic_comm_base->signature,
144*4882a593Smuzhiyun 		   cpu_to_le32(PCC_SIGNATURE_MASK | ctx->mbox_idx));
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/* Write to the shared command region */
147*4882a593Smuzhiyun 	WRITE_ONCE(generic_comm_base->command,
148*4882a593Smuzhiyun 		   cpu_to_le16(MSG_TYPE(msg[0]) | PCCC_GENERATE_DB_INT));
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/* Flip CMD COMPLETE bit */
151*4882a593Smuzhiyun 	val = le16_to_cpu(READ_ONCE(generic_comm_base->status));
152*4882a593Smuzhiyun 	val &= ~PCCS_CMD_COMPLETE;
153*4882a593Smuzhiyun 	WRITE_ONCE(generic_comm_base->status, cpu_to_le16(val));
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/* Copy the message to the PCC comm space */
156*4882a593Smuzhiyun 	for (i = 0; i < sizeof(struct slimpro_resp_msg) / 4; i++)
157*4882a593Smuzhiyun 		WRITE_ONCE(ptr[i], cpu_to_le32(msg[i]));
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* Ring the doorbell */
160*4882a593Smuzhiyun 	rc = mbox_send_message(ctx->mbox_chan, msg);
161*4882a593Smuzhiyun 	if (rc < 0) {
162*4882a593Smuzhiyun 		dev_err(ctx->dev, "Mailbox send error %d\n", rc);
163*4882a593Smuzhiyun 		goto err;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 	if (!wait_for_completion_timeout(&ctx->rd_complete,
166*4882a593Smuzhiyun 					 usecs_to_jiffies(ctx->usecs_lat))) {
167*4882a593Smuzhiyun 		dev_err(ctx->dev, "Mailbox operation timed out\n");
168*4882a593Smuzhiyun 		rc = -ETIMEDOUT;
169*4882a593Smuzhiyun 		goto err;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	/* Check for error message */
173*4882a593Smuzhiyun 	if (MSG_TYPE(ctx->sync_msg.msg) == MSG_TYPE_ERR) {
174*4882a593Smuzhiyun 		rc = -EINVAL;
175*4882a593Smuzhiyun 		goto err;
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	msg[0] = ctx->sync_msg.msg;
179*4882a593Smuzhiyun 	msg[1] = ctx->sync_msg.param1;
180*4882a593Smuzhiyun 	msg[2] = ctx->sync_msg.param2;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun err:
183*4882a593Smuzhiyun 	mbox_chan_txdone(ctx->mbox_chan, 0);
184*4882a593Smuzhiyun 	ctx->resp_pending = false;
185*4882a593Smuzhiyun 	mutex_unlock(&ctx->rd_mutex);
186*4882a593Smuzhiyun 	return rc;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
xgene_hwmon_rd(struct xgene_hwmon_dev * ctx,u32 * msg)189*4882a593Smuzhiyun static int xgene_hwmon_rd(struct xgene_hwmon_dev *ctx, u32 *msg)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	int rc;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	mutex_lock(&ctx->rd_mutex);
194*4882a593Smuzhiyun 	init_completion(&ctx->rd_complete);
195*4882a593Smuzhiyun 	ctx->resp_pending = true;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	rc = mbox_send_message(ctx->mbox_chan, msg);
198*4882a593Smuzhiyun 	if (rc < 0) {
199*4882a593Smuzhiyun 		dev_err(ctx->dev, "Mailbox send error %d\n", rc);
200*4882a593Smuzhiyun 		goto err;
201*4882a593Smuzhiyun 	}
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	if (!wait_for_completion_timeout(&ctx->rd_complete,
204*4882a593Smuzhiyun 					 msecs_to_jiffies(MBOX_OP_TIMEOUTMS))) {
205*4882a593Smuzhiyun 		dev_err(ctx->dev, "Mailbox operation timed out\n");
206*4882a593Smuzhiyun 		rc = -ETIMEDOUT;
207*4882a593Smuzhiyun 		goto err;
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* Check for error message */
211*4882a593Smuzhiyun 	if (MSG_TYPE(ctx->sync_msg.msg) == MSG_TYPE_ERR) {
212*4882a593Smuzhiyun 		rc = -EINVAL;
213*4882a593Smuzhiyun 		goto err;
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	msg[0] = ctx->sync_msg.msg;
217*4882a593Smuzhiyun 	msg[1] = ctx->sync_msg.param1;
218*4882a593Smuzhiyun 	msg[2] = ctx->sync_msg.param2;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun err:
221*4882a593Smuzhiyun 	ctx->resp_pending = false;
222*4882a593Smuzhiyun 	mutex_unlock(&ctx->rd_mutex);
223*4882a593Smuzhiyun 	return rc;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
xgene_hwmon_reg_map_rd(struct xgene_hwmon_dev * ctx,u32 addr,u32 * data)226*4882a593Smuzhiyun static int xgene_hwmon_reg_map_rd(struct xgene_hwmon_dev *ctx, u32 addr,
227*4882a593Smuzhiyun 				  u32 *data)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	u32 msg[3];
230*4882a593Smuzhiyun 	int rc;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	msg[0] = SENSOR_RD_MSG;
233*4882a593Smuzhiyun 	msg[1] = SENSOR_RD_EN_ADDR(addr);
234*4882a593Smuzhiyun 	msg[2] = 0;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (acpi_disabled)
237*4882a593Smuzhiyun 		rc = xgene_hwmon_rd(ctx, msg);
238*4882a593Smuzhiyun 	else
239*4882a593Smuzhiyun 		rc = xgene_hwmon_pcc_rd(ctx, msg);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	if (rc < 0)
242*4882a593Smuzhiyun 		return rc;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/*
245*4882a593Smuzhiyun 	 * Check if sensor data is valid.
246*4882a593Smuzhiyun 	 */
247*4882a593Smuzhiyun 	if (msg[1] & SENSOR_INVALID_DATA)
248*4882a593Smuzhiyun 		return -ENODATA;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	*data = msg[1];
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	return rc;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
xgene_hwmon_get_notification_msg(struct xgene_hwmon_dev * ctx,u32 * amsg)255*4882a593Smuzhiyun static int xgene_hwmon_get_notification_msg(struct xgene_hwmon_dev *ctx,
256*4882a593Smuzhiyun 					    u32 *amsg)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun 	u32 msg[3];
259*4882a593Smuzhiyun 	int rc;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	msg[0] = TPC_EN_MSG(PWRMGMT_SUBTYPE_TPC, TPC_GET_ALARM, 0);
262*4882a593Smuzhiyun 	msg[1] = 0;
263*4882a593Smuzhiyun 	msg[2] = 0;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	rc = xgene_hwmon_pcc_rd(ctx, msg);
266*4882a593Smuzhiyun 	if (rc < 0)
267*4882a593Smuzhiyun 		return rc;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	amsg[0] = msg[0];
270*4882a593Smuzhiyun 	amsg[1] = msg[1];
271*4882a593Smuzhiyun 	amsg[2] = msg[2];
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return rc;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
xgene_hwmon_get_cpu_pwr(struct xgene_hwmon_dev * ctx,u32 * val)276*4882a593Smuzhiyun static int xgene_hwmon_get_cpu_pwr(struct xgene_hwmon_dev *ctx, u32 *val)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	u32 watt, mwatt;
279*4882a593Smuzhiyun 	int rc;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	rc = xgene_hwmon_reg_map_rd(ctx, PMD_PWR_REG, &watt);
282*4882a593Smuzhiyun 	if (rc < 0)
283*4882a593Smuzhiyun 		return rc;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	rc = xgene_hwmon_reg_map_rd(ctx, PMD_PWR_MW_REG, &mwatt);
286*4882a593Smuzhiyun 	if (rc < 0)
287*4882a593Smuzhiyun 		return rc;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	*val = WATT_TO_mWATT(watt) + mwatt;
290*4882a593Smuzhiyun 	return 0;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
xgene_hwmon_get_io_pwr(struct xgene_hwmon_dev * ctx,u32 * val)293*4882a593Smuzhiyun static int xgene_hwmon_get_io_pwr(struct xgene_hwmon_dev *ctx, u32 *val)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	u32 watt, mwatt;
296*4882a593Smuzhiyun 	int rc;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	rc = xgene_hwmon_reg_map_rd(ctx, SOC_PWR_REG, &watt);
299*4882a593Smuzhiyun 	if (rc < 0)
300*4882a593Smuzhiyun 		return rc;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	rc = xgene_hwmon_reg_map_rd(ctx, SOC_PWR_MW_REG, &mwatt);
303*4882a593Smuzhiyun 	if (rc < 0)
304*4882a593Smuzhiyun 		return rc;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	*val = WATT_TO_mWATT(watt) + mwatt;
307*4882a593Smuzhiyun 	return 0;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun 
xgene_hwmon_get_temp(struct xgene_hwmon_dev * ctx,u32 * val)310*4882a593Smuzhiyun static int xgene_hwmon_get_temp(struct xgene_hwmon_dev *ctx, u32 *val)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun 	return xgene_hwmon_reg_map_rd(ctx, SOC_TEMP_REG, val);
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun  * Sensor temperature/power functions
317*4882a593Smuzhiyun  */
temp1_input_show(struct device * dev,struct device_attribute * attr,char * buf)318*4882a593Smuzhiyun static ssize_t temp1_input_show(struct device *dev,
319*4882a593Smuzhiyun 				struct device_attribute *attr,
320*4882a593Smuzhiyun 				char *buf)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
323*4882a593Smuzhiyun 	int rc, temp;
324*4882a593Smuzhiyun 	u32 val;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	rc = xgene_hwmon_get_temp(ctx, &val);
327*4882a593Smuzhiyun 	if (rc < 0)
328*4882a593Smuzhiyun 		return rc;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	temp = sign_extend32(val, TEMP_NEGATIVE_BIT);
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", CELSIUS_TO_mCELSIUS(temp));
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
temp1_label_show(struct device * dev,struct device_attribute * attr,char * buf)335*4882a593Smuzhiyun static ssize_t temp1_label_show(struct device *dev,
336*4882a593Smuzhiyun 				struct device_attribute *attr,
337*4882a593Smuzhiyun 				char *buf)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "SoC Temperature\n");
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun 
temp1_critical_alarm_show(struct device * dev,struct device_attribute * devattr,char * buf)342*4882a593Smuzhiyun static ssize_t temp1_critical_alarm_show(struct device *dev,
343*4882a593Smuzhiyun 					 struct device_attribute *devattr,
344*4882a593Smuzhiyun 					 char *buf)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n", ctx->temp_critical_alarm);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
power1_label_show(struct device * dev,struct device_attribute * attr,char * buf)351*4882a593Smuzhiyun static ssize_t power1_label_show(struct device *dev,
352*4882a593Smuzhiyun 				 struct device_attribute *attr,
353*4882a593Smuzhiyun 				 char *buf)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "CPU power\n");
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
power2_label_show(struct device * dev,struct device_attribute * attr,char * buf)358*4882a593Smuzhiyun static ssize_t power2_label_show(struct device *dev,
359*4882a593Smuzhiyun 				 struct device_attribute *attr,
360*4882a593Smuzhiyun 				 char *buf)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "IO power\n");
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
power1_input_show(struct device * dev,struct device_attribute * attr,char * buf)365*4882a593Smuzhiyun static ssize_t power1_input_show(struct device *dev,
366*4882a593Smuzhiyun 				 struct device_attribute *attr,
367*4882a593Smuzhiyun 				 char *buf)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
370*4882a593Smuzhiyun 	u32 val;
371*4882a593Smuzhiyun 	int rc;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	rc = xgene_hwmon_get_cpu_pwr(ctx, &val);
374*4882a593Smuzhiyun 	if (rc < 0)
375*4882a593Smuzhiyun 		return rc;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%u\n", mWATT_TO_uWATT(val));
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
power2_input_show(struct device * dev,struct device_attribute * attr,char * buf)380*4882a593Smuzhiyun static ssize_t power2_input_show(struct device *dev,
381*4882a593Smuzhiyun 				 struct device_attribute *attr,
382*4882a593Smuzhiyun 				 char *buf)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
385*4882a593Smuzhiyun 	u32 val;
386*4882a593Smuzhiyun 	int rc;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	rc = xgene_hwmon_get_io_pwr(ctx, &val);
389*4882a593Smuzhiyun 	if (rc < 0)
390*4882a593Smuzhiyun 		return rc;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%u\n", mWATT_TO_uWATT(val));
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun static DEVICE_ATTR_RO(temp1_label);
396*4882a593Smuzhiyun static DEVICE_ATTR_RO(temp1_input);
397*4882a593Smuzhiyun static DEVICE_ATTR_RO(temp1_critical_alarm);
398*4882a593Smuzhiyun static DEVICE_ATTR_RO(power1_label);
399*4882a593Smuzhiyun static DEVICE_ATTR_RO(power1_input);
400*4882a593Smuzhiyun static DEVICE_ATTR_RO(power2_label);
401*4882a593Smuzhiyun static DEVICE_ATTR_RO(power2_input);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun static struct attribute *xgene_hwmon_attrs[] = {
404*4882a593Smuzhiyun 	&dev_attr_temp1_label.attr,
405*4882a593Smuzhiyun 	&dev_attr_temp1_input.attr,
406*4882a593Smuzhiyun 	&dev_attr_temp1_critical_alarm.attr,
407*4882a593Smuzhiyun 	&dev_attr_power1_label.attr,
408*4882a593Smuzhiyun 	&dev_attr_power1_input.attr,
409*4882a593Smuzhiyun 	&dev_attr_power2_label.attr,
410*4882a593Smuzhiyun 	&dev_attr_power2_input.attr,
411*4882a593Smuzhiyun 	NULL,
412*4882a593Smuzhiyun };
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun ATTRIBUTE_GROUPS(xgene_hwmon);
415*4882a593Smuzhiyun 
xgene_hwmon_tpc_alarm(struct xgene_hwmon_dev * ctx,struct slimpro_resp_msg * amsg)416*4882a593Smuzhiyun static int xgene_hwmon_tpc_alarm(struct xgene_hwmon_dev *ctx,
417*4882a593Smuzhiyun 				 struct slimpro_resp_msg *amsg)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	ctx->temp_critical_alarm = !!amsg->param2;
420*4882a593Smuzhiyun 	sysfs_notify(&ctx->dev->kobj, NULL, "temp1_critical_alarm");
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	return 0;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
xgene_hwmon_process_pwrmsg(struct xgene_hwmon_dev * ctx,struct slimpro_resp_msg * amsg)425*4882a593Smuzhiyun static void xgene_hwmon_process_pwrmsg(struct xgene_hwmon_dev *ctx,
426*4882a593Smuzhiyun 				       struct slimpro_resp_msg *amsg)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	if ((MSG_SUBTYPE(amsg->msg) == PWRMGMT_SUBTYPE_TPC) &&
429*4882a593Smuzhiyun 	    (TPC_CMD(amsg->msg) == TPC_ALARM))
430*4882a593Smuzhiyun 		xgene_hwmon_tpc_alarm(ctx, amsg);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun  * This function is called to process async work queue
435*4882a593Smuzhiyun  */
xgene_hwmon_evt_work(struct work_struct * work)436*4882a593Smuzhiyun static void xgene_hwmon_evt_work(struct work_struct *work)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	struct slimpro_resp_msg amsg;
439*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx;
440*4882a593Smuzhiyun 	int ret;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	ctx = container_of(work, struct xgene_hwmon_dev, workq);
443*4882a593Smuzhiyun 	while (kfifo_out_spinlocked(&ctx->async_msg_fifo, &amsg,
444*4882a593Smuzhiyun 				    sizeof(struct slimpro_resp_msg),
445*4882a593Smuzhiyun 				    &ctx->kfifo_lock)) {
446*4882a593Smuzhiyun 		/*
447*4882a593Smuzhiyun 		 * If PCC, send a consumer command to Platform to get info
448*4882a593Smuzhiyun 		 * If Slimpro Mailbox, get message from specific FIFO
449*4882a593Smuzhiyun 		 */
450*4882a593Smuzhiyun 		if (!acpi_disabled) {
451*4882a593Smuzhiyun 			ret = xgene_hwmon_get_notification_msg(ctx,
452*4882a593Smuzhiyun 							       (u32 *)&amsg);
453*4882a593Smuzhiyun 			if (ret < 0)
454*4882a593Smuzhiyun 				continue;
455*4882a593Smuzhiyun 		}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 		if (MSG_TYPE(amsg.msg) == MSG_TYPE_PWRMGMT)
458*4882a593Smuzhiyun 			xgene_hwmon_process_pwrmsg(ctx, &amsg);
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun 
xgene_hwmon_rx_ready(struct xgene_hwmon_dev * ctx,void * msg)462*4882a593Smuzhiyun static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(ctx->hwmon_dev) && !ctx->resp_pending) {
465*4882a593Smuzhiyun 		/* Enqueue to the FIFO */
466*4882a593Smuzhiyun 		kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
467*4882a593Smuzhiyun 				    sizeof(struct slimpro_resp_msg),
468*4882a593Smuzhiyun 				    &ctx->kfifo_lock);
469*4882a593Smuzhiyun 		return -ENODEV;
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	return 0;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun /*
476*4882a593Smuzhiyun  * This function is called when the SLIMpro Mailbox received a message
477*4882a593Smuzhiyun  */
xgene_hwmon_rx_cb(struct mbox_client * cl,void * msg)478*4882a593Smuzhiyun static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	/*
483*4882a593Smuzhiyun 	 * While the driver registers with the mailbox framework, an interrupt
484*4882a593Smuzhiyun 	 * can be pending before the probe function completes its
485*4882a593Smuzhiyun 	 * initialization. If such condition occurs, just queue up the message
486*4882a593Smuzhiyun 	 * as the driver is not ready for servicing the callback.
487*4882a593Smuzhiyun 	 */
488*4882a593Smuzhiyun 	if (xgene_hwmon_rx_ready(ctx, msg) < 0)
489*4882a593Smuzhiyun 		return;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/*
492*4882a593Smuzhiyun 	 * Response message format:
493*4882a593Smuzhiyun 	 * msg[0] is the return code of the operation
494*4882a593Smuzhiyun 	 * msg[1] is the first parameter word
495*4882a593Smuzhiyun 	 * msg[2] is the second parameter word
496*4882a593Smuzhiyun 	 *
497*4882a593Smuzhiyun 	 * As message only supports dword size, just assign it.
498*4882a593Smuzhiyun 	 */
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	/* Check for sync query */
501*4882a593Smuzhiyun 	if (ctx->resp_pending &&
502*4882a593Smuzhiyun 	    ((MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_ERR) ||
503*4882a593Smuzhiyun 	     (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_DBG &&
504*4882a593Smuzhiyun 	      MSG_SUBTYPE(((u32 *)msg)[0]) == DBG_SUBTYPE_SENSOR_READ) ||
505*4882a593Smuzhiyun 	     (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_PWRMGMT &&
506*4882a593Smuzhiyun 	      MSG_SUBTYPE(((u32 *)msg)[0]) == PWRMGMT_SUBTYPE_TPC &&
507*4882a593Smuzhiyun 	      TPC_CMD(((u32 *)msg)[0]) == TPC_ALARM))) {
508*4882a593Smuzhiyun 		ctx->sync_msg.msg = ((u32 *)msg)[0];
509*4882a593Smuzhiyun 		ctx->sync_msg.param1 = ((u32 *)msg)[1];
510*4882a593Smuzhiyun 		ctx->sync_msg.param2 = ((u32 *)msg)[2];
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 		/* Operation waiting for response */
513*4882a593Smuzhiyun 		complete(&ctx->rd_complete);
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 		return;
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/* Enqueue to the FIFO */
519*4882a593Smuzhiyun 	kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
520*4882a593Smuzhiyun 			    sizeof(struct slimpro_resp_msg), &ctx->kfifo_lock);
521*4882a593Smuzhiyun 	/* Schedule the bottom handler */
522*4882a593Smuzhiyun 	schedule_work(&ctx->workq);
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun /*
526*4882a593Smuzhiyun  * This function is called when the PCC Mailbox received a message
527*4882a593Smuzhiyun  */
xgene_hwmon_pcc_rx_cb(struct mbox_client * cl,void * msg)528*4882a593Smuzhiyun static void xgene_hwmon_pcc_rx_cb(struct mbox_client *cl, void *msg)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
531*4882a593Smuzhiyun 	struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
532*4882a593Smuzhiyun 	struct slimpro_resp_msg amsg;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	/*
535*4882a593Smuzhiyun 	 * While the driver registers with the mailbox framework, an interrupt
536*4882a593Smuzhiyun 	 * can be pending before the probe function completes its
537*4882a593Smuzhiyun 	 * initialization. If such condition occurs, just queue up the message
538*4882a593Smuzhiyun 	 * as the driver is not ready for servicing the callback.
539*4882a593Smuzhiyun 	 */
540*4882a593Smuzhiyun 	if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
541*4882a593Smuzhiyun 		return;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	msg = generic_comm_base + 1;
544*4882a593Smuzhiyun 	/* Check if platform sends interrupt */
545*4882a593Smuzhiyun 	if (!xgene_word_tst_and_clr(&generic_comm_base->status,
546*4882a593Smuzhiyun 				    PCCS_SCI_DOORBEL))
547*4882a593Smuzhiyun 		return;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	/*
550*4882a593Smuzhiyun 	 * Response message format:
551*4882a593Smuzhiyun 	 * msg[0] is the return code of the operation
552*4882a593Smuzhiyun 	 * msg[1] is the first parameter word
553*4882a593Smuzhiyun 	 * msg[2] is the second parameter word
554*4882a593Smuzhiyun 	 *
555*4882a593Smuzhiyun 	 * As message only supports dword size, just assign it.
556*4882a593Smuzhiyun 	 */
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	/* Check for sync query */
559*4882a593Smuzhiyun 	if (ctx->resp_pending &&
560*4882a593Smuzhiyun 	    ((MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_ERR) ||
561*4882a593Smuzhiyun 	     (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_DBG &&
562*4882a593Smuzhiyun 	      MSG_SUBTYPE(((u32 *)msg)[0]) == DBG_SUBTYPE_SENSOR_READ) ||
563*4882a593Smuzhiyun 	     (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_PWRMGMT &&
564*4882a593Smuzhiyun 	      MSG_SUBTYPE(((u32 *)msg)[0]) == PWRMGMT_SUBTYPE_TPC &&
565*4882a593Smuzhiyun 	      TPC_CMD(((u32 *)msg)[0]) == TPC_ALARM))) {
566*4882a593Smuzhiyun 		/* Check if platform completes command */
567*4882a593Smuzhiyun 		if (xgene_word_tst_and_clr(&generic_comm_base->status,
568*4882a593Smuzhiyun 					   PCCS_CMD_COMPLETE)) {
569*4882a593Smuzhiyun 			ctx->sync_msg.msg = ((u32 *)msg)[0];
570*4882a593Smuzhiyun 			ctx->sync_msg.param1 = ((u32 *)msg)[1];
571*4882a593Smuzhiyun 			ctx->sync_msg.param2 = ((u32 *)msg)[2];
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 			/* Operation waiting for response */
574*4882a593Smuzhiyun 			complete(&ctx->rd_complete);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 			return;
577*4882a593Smuzhiyun 		}
578*4882a593Smuzhiyun 	}
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	/*
581*4882a593Smuzhiyun 	 * Platform notifies interrupt to OSPM.
582*4882a593Smuzhiyun 	 * OPSM schedules a consumer command to get this information
583*4882a593Smuzhiyun 	 * in a workqueue. Platform must wait until OSPM has issued
584*4882a593Smuzhiyun 	 * a consumer command that serves this notification.
585*4882a593Smuzhiyun 	 */
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	/* Enqueue to the FIFO */
588*4882a593Smuzhiyun 	kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
589*4882a593Smuzhiyun 			    sizeof(struct slimpro_resp_msg), &ctx->kfifo_lock);
590*4882a593Smuzhiyun 	/* Schedule the bottom handler */
591*4882a593Smuzhiyun 	schedule_work(&ctx->workq);
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
xgene_hwmon_tx_done(struct mbox_client * cl,void * msg,int ret)594*4882a593Smuzhiyun static void xgene_hwmon_tx_done(struct mbox_client *cl, void *msg, int ret)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun 	if (ret) {
597*4882a593Smuzhiyun 		dev_dbg(cl->dev, "TX did not complete: CMD sent:%x, ret:%d\n",
598*4882a593Smuzhiyun 			*(u16 *)msg, ret);
599*4882a593Smuzhiyun 	} else {
600*4882a593Smuzhiyun 		dev_dbg(cl->dev, "TX completed. CMD sent:%x, ret:%d\n",
601*4882a593Smuzhiyun 			*(u16 *)msg, ret);
602*4882a593Smuzhiyun 	}
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun #ifdef CONFIG_ACPI
606*4882a593Smuzhiyun static const struct acpi_device_id xgene_hwmon_acpi_match[] = {
607*4882a593Smuzhiyun 	{"APMC0D29", XGENE_HWMON_V1},
608*4882a593Smuzhiyun 	{"APMC0D8A", XGENE_HWMON_V2},
609*4882a593Smuzhiyun 	{},
610*4882a593Smuzhiyun };
611*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, xgene_hwmon_acpi_match);
612*4882a593Smuzhiyun #endif
613*4882a593Smuzhiyun 
xgene_hwmon_probe(struct platform_device * pdev)614*4882a593Smuzhiyun static int xgene_hwmon_probe(struct platform_device *pdev)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx;
617*4882a593Smuzhiyun 	struct mbox_client *cl;
618*4882a593Smuzhiyun 	int rc;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
621*4882a593Smuzhiyun 	if (!ctx)
622*4882a593Smuzhiyun 		return -ENOMEM;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	ctx->dev = &pdev->dev;
625*4882a593Smuzhiyun 	platform_set_drvdata(pdev, ctx);
626*4882a593Smuzhiyun 	cl = &ctx->mbox_client;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	spin_lock_init(&ctx->kfifo_lock);
629*4882a593Smuzhiyun 	mutex_init(&ctx->rd_mutex);
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	rc = kfifo_alloc(&ctx->async_msg_fifo,
632*4882a593Smuzhiyun 			 sizeof(struct slimpro_resp_msg) * ASYNC_MSG_FIFO_SIZE,
633*4882a593Smuzhiyun 			 GFP_KERNEL);
634*4882a593Smuzhiyun 	if (rc)
635*4882a593Smuzhiyun 		return -ENOMEM;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	/* Request mailbox channel */
640*4882a593Smuzhiyun 	cl->dev = &pdev->dev;
641*4882a593Smuzhiyun 	cl->tx_done = xgene_hwmon_tx_done;
642*4882a593Smuzhiyun 	cl->tx_block = false;
643*4882a593Smuzhiyun 	cl->tx_tout = MBOX_OP_TIMEOUTMS;
644*4882a593Smuzhiyun 	cl->knows_txdone = false;
645*4882a593Smuzhiyun 	if (acpi_disabled) {
646*4882a593Smuzhiyun 		cl->rx_callback = xgene_hwmon_rx_cb;
647*4882a593Smuzhiyun 		ctx->mbox_chan = mbox_request_channel(cl, 0);
648*4882a593Smuzhiyun 		if (IS_ERR(ctx->mbox_chan)) {
649*4882a593Smuzhiyun 			dev_err(&pdev->dev,
650*4882a593Smuzhiyun 				"SLIMpro mailbox channel request failed\n");
651*4882a593Smuzhiyun 			rc = -ENODEV;
652*4882a593Smuzhiyun 			goto out_mbox_free;
653*4882a593Smuzhiyun 		}
654*4882a593Smuzhiyun 	} else {
655*4882a593Smuzhiyun 		struct acpi_pcct_hw_reduced *cppc_ss;
656*4882a593Smuzhiyun 		const struct acpi_device_id *acpi_id;
657*4882a593Smuzhiyun 		int version;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 		acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
660*4882a593Smuzhiyun 					    &pdev->dev);
661*4882a593Smuzhiyun 		if (!acpi_id)
662*4882a593Smuzhiyun 			return -EINVAL;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 		version = (int)acpi_id->driver_data;
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 		if (device_property_read_u32(&pdev->dev, "pcc-channel",
667*4882a593Smuzhiyun 					     &ctx->mbox_idx)) {
668*4882a593Smuzhiyun 			dev_err(&pdev->dev, "no pcc-channel property\n");
669*4882a593Smuzhiyun 			rc = -ENODEV;
670*4882a593Smuzhiyun 			goto out_mbox_free;
671*4882a593Smuzhiyun 		}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 		cl->rx_callback = xgene_hwmon_pcc_rx_cb;
674*4882a593Smuzhiyun 		ctx->mbox_chan = pcc_mbox_request_channel(cl, ctx->mbox_idx);
675*4882a593Smuzhiyun 		if (IS_ERR(ctx->mbox_chan)) {
676*4882a593Smuzhiyun 			dev_err(&pdev->dev,
677*4882a593Smuzhiyun 				"PPC channel request failed\n");
678*4882a593Smuzhiyun 			rc = -ENODEV;
679*4882a593Smuzhiyun 			goto out_mbox_free;
680*4882a593Smuzhiyun 		}
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 		/*
683*4882a593Smuzhiyun 		 * The PCC mailbox controller driver should
684*4882a593Smuzhiyun 		 * have parsed the PCCT (global table of all
685*4882a593Smuzhiyun 		 * PCC channels) and stored pointers to the
686*4882a593Smuzhiyun 		 * subspace communication region in con_priv.
687*4882a593Smuzhiyun 		 */
688*4882a593Smuzhiyun 		cppc_ss = ctx->mbox_chan->con_priv;
689*4882a593Smuzhiyun 		if (!cppc_ss) {
690*4882a593Smuzhiyun 			dev_err(&pdev->dev, "PPC subspace not found\n");
691*4882a593Smuzhiyun 			rc = -ENODEV;
692*4882a593Smuzhiyun 			goto out;
693*4882a593Smuzhiyun 		}
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 		if (!ctx->mbox_chan->mbox->txdone_irq) {
696*4882a593Smuzhiyun 			dev_err(&pdev->dev, "PCC IRQ not supported\n");
697*4882a593Smuzhiyun 			rc = -ENODEV;
698*4882a593Smuzhiyun 			goto out;
699*4882a593Smuzhiyun 		}
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 		/*
702*4882a593Smuzhiyun 		 * This is the shared communication region
703*4882a593Smuzhiyun 		 * for the OS and Platform to communicate over.
704*4882a593Smuzhiyun 		 */
705*4882a593Smuzhiyun 		ctx->comm_base_addr = cppc_ss->base_address;
706*4882a593Smuzhiyun 		if (ctx->comm_base_addr) {
707*4882a593Smuzhiyun 			if (version == XGENE_HWMON_V2)
708*4882a593Smuzhiyun 				ctx->pcc_comm_addr = (void __force *)ioremap(
709*4882a593Smuzhiyun 							ctx->comm_base_addr,
710*4882a593Smuzhiyun 							cppc_ss->length);
711*4882a593Smuzhiyun 			else
712*4882a593Smuzhiyun 				ctx->pcc_comm_addr = memremap(
713*4882a593Smuzhiyun 							ctx->comm_base_addr,
714*4882a593Smuzhiyun 							cppc_ss->length,
715*4882a593Smuzhiyun 							MEMREMAP_WB);
716*4882a593Smuzhiyun 		} else {
717*4882a593Smuzhiyun 			dev_err(&pdev->dev, "Failed to get PCC comm region\n");
718*4882a593Smuzhiyun 			rc = -ENODEV;
719*4882a593Smuzhiyun 			goto out;
720*4882a593Smuzhiyun 		}
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 		if (!ctx->pcc_comm_addr) {
723*4882a593Smuzhiyun 			dev_err(&pdev->dev,
724*4882a593Smuzhiyun 				"Failed to ioremap PCC comm region\n");
725*4882a593Smuzhiyun 			rc = -ENOMEM;
726*4882a593Smuzhiyun 			goto out;
727*4882a593Smuzhiyun 		}
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 		/*
730*4882a593Smuzhiyun 		 * cppc_ss->latency is just a Nominal value. In reality
731*4882a593Smuzhiyun 		 * the remote processor could be much slower to reply.
732*4882a593Smuzhiyun 		 * So add an arbitrary amount of wait on top of Nominal.
733*4882a593Smuzhiyun 		 */
734*4882a593Smuzhiyun 		ctx->usecs_lat = PCC_NUM_RETRIES * cppc_ss->latency;
735*4882a593Smuzhiyun 	}
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
738*4882a593Smuzhiyun 							   "apm_xgene",
739*4882a593Smuzhiyun 							   ctx,
740*4882a593Smuzhiyun 							   xgene_hwmon_groups);
741*4882a593Smuzhiyun 	if (IS_ERR(ctx->hwmon_dev)) {
742*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to register HW monitor device\n");
743*4882a593Smuzhiyun 		rc = PTR_ERR(ctx->hwmon_dev);
744*4882a593Smuzhiyun 		goto out;
745*4882a593Smuzhiyun 	}
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	/*
748*4882a593Smuzhiyun 	 * Schedule the bottom handler if there is a pending message.
749*4882a593Smuzhiyun 	 */
750*4882a593Smuzhiyun 	schedule_work(&ctx->workq);
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver registered\n");
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	return 0;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun out:
757*4882a593Smuzhiyun 	if (acpi_disabled)
758*4882a593Smuzhiyun 		mbox_free_channel(ctx->mbox_chan);
759*4882a593Smuzhiyun 	else
760*4882a593Smuzhiyun 		pcc_mbox_free_channel(ctx->mbox_chan);
761*4882a593Smuzhiyun out_mbox_free:
762*4882a593Smuzhiyun 	kfifo_free(&ctx->async_msg_fifo);
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	return rc;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
xgene_hwmon_remove(struct platform_device * pdev)767*4882a593Smuzhiyun static int xgene_hwmon_remove(struct platform_device *pdev)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun 	struct xgene_hwmon_dev *ctx = platform_get_drvdata(pdev);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	hwmon_device_unregister(ctx->hwmon_dev);
772*4882a593Smuzhiyun 	kfifo_free(&ctx->async_msg_fifo);
773*4882a593Smuzhiyun 	if (acpi_disabled)
774*4882a593Smuzhiyun 		mbox_free_channel(ctx->mbox_chan);
775*4882a593Smuzhiyun 	else
776*4882a593Smuzhiyun 		pcc_mbox_free_channel(ctx->mbox_chan);
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	return 0;
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun static const struct of_device_id xgene_hwmon_of_match[] = {
782*4882a593Smuzhiyun 	{.compatible = "apm,xgene-slimpro-hwmon"},
783*4882a593Smuzhiyun 	{}
784*4882a593Smuzhiyun };
785*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, xgene_hwmon_of_match);
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun static struct platform_driver xgene_hwmon_driver __refdata = {
788*4882a593Smuzhiyun 	.probe = xgene_hwmon_probe,
789*4882a593Smuzhiyun 	.remove = xgene_hwmon_remove,
790*4882a593Smuzhiyun 	.driver = {
791*4882a593Smuzhiyun 		.name = "xgene-slimpro-hwmon",
792*4882a593Smuzhiyun 		.of_match_table = xgene_hwmon_of_match,
793*4882a593Smuzhiyun 		.acpi_match_table = ACPI_PTR(xgene_hwmon_acpi_match),
794*4882a593Smuzhiyun 	},
795*4882a593Smuzhiyun };
796*4882a593Smuzhiyun module_platform_driver(xgene_hwmon_driver);
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun MODULE_DESCRIPTION("APM X-Gene SoC hardware monitor");
799*4882a593Smuzhiyun MODULE_LICENSE("GPL");
800