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