xref: /linux/drivers/acpi/sbshc.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SMBus driver for ACPI Embedded Controller (v0.1)
4  *
5  * Copyright (c) 2007 Alexey Starikovskiy
6  */
7 
8 #define pr_fmt(fmt) "ACPI: " fmt
9 
10 #include <linux/acpi.h>
11 #include <linux/wait.h>
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 
18 #include "sbshc.h"
19 #include "internal.h"
20 
21 #define ACPI_SMB_HC_CLASS	"smbus_host_ctl"
22 #define ACPI_SMB_HC_DEVICE_NAME	"ACPI SMBus HC"
23 
24 struct acpi_smb_hc {
25 	struct acpi_ec *ec;
26 	struct mutex lock;
27 	wait_queue_head_t wait;
28 	u8 offset;
29 	u8 query_bit;
30 	smbus_alarm_callback callback;
31 	void *context;
32 	bool done;
33 };
34 
35 static int acpi_smbus_hc_probe(struct platform_device *pdev);
36 static void acpi_smbus_hc_remove(struct platform_device *pdev);
37 
38 static const struct acpi_device_id sbs_device_ids[] = {
39 	{"ACPI0001", 0},
40 	{"ACPI0005", 0},
41 	{"", 0},
42 };
43 
44 MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
45 
46 static struct platform_driver acpi_smb_hc_driver = {
47 	.probe = acpi_smbus_hc_probe,
48 	.remove = acpi_smbus_hc_remove,
49 	.driver = {
50 		.name = "acpi-smbus-hc",
51 		.acpi_match_table = sbs_device_ids,
52 	},
53 };
54 
55 union acpi_smb_status {
56 	u8 raw;
57 	struct {
58 		u8 status:5;
59 		u8 reserved:1;
60 		u8 alarm:1;
61 		u8 done:1;
62 	} fields;
63 };
64 
65 enum acpi_smb_status_codes {
66 	SMBUS_OK = 0,
67 	SMBUS_UNKNOWN_FAILURE = 0x07,
68 	SMBUS_DEVICE_ADDRESS_NACK = 0x10,
69 	SMBUS_DEVICE_ERROR = 0x11,
70 	SMBUS_DEVICE_COMMAND_ACCESS_DENIED = 0x12,
71 	SMBUS_UNKNOWN_ERROR = 0x13,
72 	SMBUS_DEVICE_ACCESS_DENIED = 0x17,
73 	SMBUS_TIMEOUT = 0x18,
74 	SMBUS_HOST_UNSUPPORTED_PROTOCOL = 0x19,
75 	SMBUS_BUSY = 0x1a,
76 	SMBUS_PEC_ERROR = 0x1f,
77 };
78 
79 enum acpi_smb_offset {
80 	ACPI_SMB_PROTOCOL = 0,	/* protocol, PEC */
81 	ACPI_SMB_STATUS = 1,	/* status */
82 	ACPI_SMB_ADDRESS = 2,	/* address */
83 	ACPI_SMB_COMMAND = 3,	/* command */
84 	ACPI_SMB_DATA = 4,	/* 32 data registers */
85 	ACPI_SMB_BLOCK_COUNT = 0x24,	/* number of data bytes */
86 	ACPI_SMB_ALARM_ADDRESS = 0x25,	/* alarm address */
87 	ACPI_SMB_ALARM_DATA = 0x26,	/* 2 bytes alarm data */
88 };
89 
90 static inline int smb_hc_read(struct acpi_smb_hc *hc, u8 address, u8 *data)
91 {
92 	return ec_read(hc->offset + address, data);
93 }
94 
95 static inline int smb_hc_write(struct acpi_smb_hc *hc, u8 address, u8 data)
96 {
97 	return ec_write(hc->offset + address, data);
98 }
99 
100 static int wait_transaction_complete(struct acpi_smb_hc *hc, int timeout)
101 {
102 	if (wait_event_timeout(hc->wait, hc->done, msecs_to_jiffies(timeout)))
103 		return 0;
104 	return -ETIME;
105 }
106 
107 static int acpi_smbus_transaction(struct acpi_smb_hc *hc, u8 protocol,
108 				  u8 address, u8 command, u8 *data, u8 length)
109 {
110 	int ret = -EFAULT, i;
111 	u8 temp, sz = 0;
112 
113 	if (!hc) {
114 		pr_err("host controller is not configured\n");
115 		return ret;
116 	}
117 
118 	mutex_lock(&hc->lock);
119 	hc->done = false;
120 	if (smb_hc_read(hc, ACPI_SMB_PROTOCOL, &temp))
121 		goto end;
122 	if (temp) {
123 		ret = -EBUSY;
124 		goto end;
125 	}
126 	smb_hc_write(hc, ACPI_SMB_COMMAND, command);
127 	if (!(protocol & 0x01)) {
128 		smb_hc_write(hc, ACPI_SMB_BLOCK_COUNT, length);
129 		for (i = 0; i < length; ++i)
130 			smb_hc_write(hc, ACPI_SMB_DATA + i, data[i]);
131 	}
132 	smb_hc_write(hc, ACPI_SMB_ADDRESS, address << 1);
133 	smb_hc_write(hc, ACPI_SMB_PROTOCOL, protocol);
134 	/*
135 	 * Wait for completion. Save the status code, data size,
136 	 * and data into the return package (if required by the protocol).
137 	 */
138 	ret = wait_transaction_complete(hc, 1000);
139 	if (ret || !(protocol & 0x01))
140 		goto end;
141 	switch (protocol) {
142 	case SMBUS_RECEIVE_BYTE:
143 	case SMBUS_READ_BYTE:
144 		sz = 1;
145 		break;
146 	case SMBUS_READ_WORD:
147 		sz = 2;
148 		break;
149 	case SMBUS_READ_BLOCK:
150 		if (smb_hc_read(hc, ACPI_SMB_BLOCK_COUNT, &sz)) {
151 			ret = -EFAULT;
152 			goto end;
153 		}
154 		sz &= 0x1f;
155 		break;
156 	}
157 	for (i = 0; i < sz; ++i)
158 		smb_hc_read(hc, ACPI_SMB_DATA + i, &data[i]);
159       end:
160 	mutex_unlock(&hc->lock);
161 	return ret;
162 }
163 
164 int acpi_smbus_read(struct acpi_smb_hc *hc, u8 protocol, u8 address,
165 		    u8 command, u8 *data)
166 {
167 	return acpi_smbus_transaction(hc, protocol, address, command, data, 0);
168 }
169 
170 EXPORT_SYMBOL_GPL(acpi_smbus_read);
171 
172 int acpi_smbus_write(struct acpi_smb_hc *hc, u8 protocol, u8 address,
173 		     u8 command, u8 *data, u8 length)
174 {
175 	return acpi_smbus_transaction(hc, protocol, address, command, data, length);
176 }
177 
178 EXPORT_SYMBOL_GPL(acpi_smbus_write);
179 
180 int acpi_smbus_register_callback(struct acpi_smb_hc *hc,
181 				 smbus_alarm_callback callback, void *context)
182 {
183 	mutex_lock(&hc->lock);
184 	hc->callback = callback;
185 	hc->context = context;
186 	mutex_unlock(&hc->lock);
187 	return 0;
188 }
189 
190 EXPORT_SYMBOL_GPL(acpi_smbus_register_callback);
191 
192 int acpi_smbus_unregister_callback(struct acpi_smb_hc *hc)
193 {
194 	mutex_lock(&hc->lock);
195 	hc->callback = NULL;
196 	hc->context = NULL;
197 	mutex_unlock(&hc->lock);
198 	acpi_os_wait_events_complete();
199 	return 0;
200 }
201 
202 EXPORT_SYMBOL_GPL(acpi_smbus_unregister_callback);
203 
204 static inline void acpi_smbus_callback(void *context)
205 {
206 	struct acpi_smb_hc *hc = context;
207 	if (hc->callback)
208 		hc->callback(hc->context);
209 }
210 
211 static int smbus_alarm(void *context)
212 {
213 	struct acpi_smb_hc *hc = context;
214 	union acpi_smb_status status;
215 	u8 address;
216 	if (smb_hc_read(hc, ACPI_SMB_STATUS, &status.raw))
217 		return 0;
218 	/* Check if it is only a completion notify */
219 	if (status.fields.done && status.fields.status == SMBUS_OK) {
220 		hc->done = true;
221 		wake_up(&hc->wait);
222 	}
223 	if (!status.fields.alarm)
224 		return 0;
225 	mutex_lock(&hc->lock);
226 	smb_hc_read(hc, ACPI_SMB_ALARM_ADDRESS, &address);
227 	status.fields.alarm = 0;
228 	smb_hc_write(hc, ACPI_SMB_STATUS, status.raw);
229 	/* We are only interested in events coming from known devices */
230 	switch (address >> 1) {
231 		case ACPI_SBS_CHARGER:
232 		case ACPI_SBS_MANAGER:
233 		case ACPI_SBS_BATTERY:
234 			acpi_os_execute(OSL_NOTIFY_HANDLER,
235 					acpi_smbus_callback, hc);
236 	}
237 	mutex_unlock(&hc->lock);
238 	return 0;
239 }
240 
241 static int acpi_smbus_hc_probe(struct platform_device *pdev)
242 {
243 	struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
244 	int status;
245 	unsigned long long val;
246 	struct acpi_smb_hc *hc;
247 
248 	status = acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
249 	if (ACPI_FAILURE(status)) {
250 		pr_err("error obtaining _EC.\n");
251 		return -EIO;
252 	}
253 
254 	strscpy(acpi_device_name(device), ACPI_SMB_HC_DEVICE_NAME);
255 	strscpy(acpi_device_class(device), ACPI_SMB_HC_CLASS);
256 
257 	hc = kzalloc_obj(struct acpi_smb_hc);
258 	if (!hc)
259 		return -ENOMEM;
260 	mutex_init(&hc->lock);
261 	init_waitqueue_head(&hc->wait);
262 
263 	platform_set_drvdata(pdev, hc);
264 
265 	hc->ec = dev_get_drvdata(pdev->dev.parent);
266 	hc->offset = (val >> 8) & 0xff;
267 	hc->query_bit = val & 0xff;
268 
269 	acpi_ec_add_query_handler(hc->ec, hc->query_bit, NULL, smbus_alarm, hc);
270 	dev_info(&device->dev, "SBS HC: offset = 0x%0x, query_bit = 0x%0x\n",
271 		 hc->offset, hc->query_bit);
272 
273 	return 0;
274 }
275 
276 static void acpi_smbus_hc_remove(struct platform_device *pdev)
277 {
278 	struct acpi_smb_hc *hc = platform_get_drvdata(pdev);
279 
280 	acpi_ec_remove_query_handler(hc->ec, hc->query_bit);
281 	acpi_os_wait_events_complete();
282 	kfree(hc);
283 }
284 
285 module_platform_driver(acpi_smb_hc_driver);
286 
287 MODULE_LICENSE("GPL");
288 MODULE_AUTHOR("Alexey Starikovskiy");
289 MODULE_DESCRIPTION("ACPI SMBus HC driver");
290