smbus_acpi.c revision 1.9.2.2 1 1.9.2.2 yamt /* $NetBSD: smbus_acpi.c,v 1.9.2.2 2010/03/11 15:03:23 yamt Exp $ */
2 1.9.2.2 yamt
3 1.9.2.2 yamt /*-
4 1.9.2.2 yamt * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.9.2.2 yamt * All rights reserved.
6 1.9.2.2 yamt *
7 1.9.2.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.9.2.2 yamt * by Paul Goyette
9 1.9.2.2 yamt *
10 1.9.2.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.9.2.2 yamt * modification, are permitted provided that the following conditions
12 1.9.2.2 yamt * are met:
13 1.9.2.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.9.2.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.9.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.9.2.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.9.2.2 yamt * documentation and/or other materials provided with the distribution.
18 1.9.2.2 yamt *
19 1.9.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.9.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.9.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.9.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.9.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.9.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.9.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.9.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.9.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.9.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.9.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.9.2.2 yamt */
31 1.9.2.2 yamt
32 1.9.2.2 yamt /*
33 1.9.2.2 yamt * ACPI SMBus Controller driver
34 1.9.2.2 yamt *
35 1.9.2.2 yamt * See http://smbus.org/specs/smbus_cmi10.pdf for specifications
36 1.9.2.2 yamt */
37 1.9.2.2 yamt
38 1.9.2.2 yamt #include <sys/cdefs.h>
39 1.9.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: smbus_acpi.c,v 1.9.2.2 2010/03/11 15:03:23 yamt Exp $");
40 1.9.2.2 yamt
41 1.9.2.2 yamt #include <sys/param.h>
42 1.9.2.2 yamt #include <sys/device.h>
43 1.9.2.2 yamt #include <sys/callout.h>
44 1.9.2.2 yamt #include <sys/kernel.h>
45 1.9.2.2 yamt #include <sys/mutex.h>
46 1.9.2.2 yamt #include <sys/systm.h>
47 1.9.2.2 yamt
48 1.9.2.2 yamt #include <dev/acpi/acpireg.h>
49 1.9.2.2 yamt #include <dev/acpi/acpivar.h>
50 1.9.2.2 yamt
51 1.9.2.2 yamt #include <dev/i2c/i2cvar.h>
52 1.9.2.2 yamt
53 1.9.2.2 yamt #define _COMPONENT ACPI_BUS_COMPONENT
54 1.9.2.2 yamt ACPI_MODULE_NAME ("smbus_acpi")
55 1.9.2.2 yamt
56 1.9.2.2 yamt /*
57 1.9.2.2 yamt * ACPI SMBus CMI protocol codes
58 1.9.2.2 yamt */
59 1.9.2.2 yamt #define ACPI_SMBUS_RD_QUICK 0x03
60 1.9.2.2 yamt #define ACPI_SMBUS_RCV_BYTE 0x05
61 1.9.2.2 yamt #define ACPI_SMBUS_RD_BYTE 0x07
62 1.9.2.2 yamt #define ACPI_SMBUS_RD_WORD 0x09
63 1.9.2.2 yamt #define ACPI_SMBUS_RD_BLOCK 0x0B
64 1.9.2.2 yamt #define ACPI_SMBUS_WR_QUICK 0x02
65 1.9.2.2 yamt #define ACPI_SMBUS_SND_BYTE 0x04
66 1.9.2.2 yamt #define ACPI_SMBUS_WR_BYTE 0x06
67 1.9.2.2 yamt #define ACPI_SMBUS_WR_WORD 0x08
68 1.9.2.2 yamt #define ACPI_SMBUS_WR_BLOCK 0x0A
69 1.9.2.2 yamt #define ACPI_SMBUS_PROCESS_CALL 0x0C
70 1.9.2.2 yamt
71 1.9.2.2 yamt struct acpi_smbus_softc {
72 1.9.2.2 yamt struct acpi_devnode *sc_devnode;
73 1.9.2.2 yamt struct callout sc_callout;
74 1.9.2.2 yamt struct i2c_controller sc_i2c_tag;
75 1.9.2.2 yamt device_t sc_dv;
76 1.9.2.2 yamt kmutex_t sc_i2c_mutex;
77 1.9.2.2 yamt int sc_poll_alert;
78 1.9.2.2 yamt };
79 1.9.2.2 yamt
80 1.9.2.2 yamt static int acpi_smbus_match(device_t, cfdata_t, void *);
81 1.9.2.2 yamt static void acpi_smbus_attach(device_t, device_t, void *);
82 1.9.2.2 yamt static int acpi_smbus_detach(device_t, int);
83 1.9.2.2 yamt static int acpi_smbus_acquire_bus(void *, int);
84 1.9.2.2 yamt static void acpi_smbus_release_bus(void *, int);
85 1.9.2.2 yamt static int acpi_smbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
86 1.9.2.2 yamt size_t, void *, size_t, int);
87 1.9.2.2 yamt static void acpi_smbus_alerts(struct acpi_smbus_softc *);
88 1.9.2.2 yamt static void acpi_smbus_tick(void *);
89 1.9.2.2 yamt static void acpi_smbus_notify_handler(ACPI_HANDLE, uint32_t, void *);
90 1.9.2.2 yamt
91 1.9.2.2 yamt struct SMB_UDID {
92 1.9.2.2 yamt uint8_t dev_cap;
93 1.9.2.2 yamt uint8_t vers_rev;
94 1.9.2.2 yamt uint16_t vendor;
95 1.9.2.2 yamt uint16_t device;
96 1.9.2.2 yamt uint16_t interface;
97 1.9.2.2 yamt uint16_t subsys_vendor;
98 1.9.2.2 yamt uint16_t subsys_device;
99 1.9.2.2 yamt uint8_t reserved[4];
100 1.9.2.2 yamt };
101 1.9.2.2 yamt
102 1.9.2.2 yamt struct SMB_DEVICE {
103 1.9.2.2 yamt uint8_t slave_addr;
104 1.9.2.2 yamt uint8_t reserved;
105 1.9.2.2 yamt struct SMB_UDID dev_id;
106 1.9.2.2 yamt };
107 1.9.2.2 yamt
108 1.9.2.2 yamt struct SMB_INFO {
109 1.9.2.2 yamt uint8_t struct_ver;
110 1.9.2.2 yamt uint8_t spec_ver;
111 1.9.2.2 yamt uint8_t hw_cap;
112 1.9.2.2 yamt uint8_t poll_int;
113 1.9.2.2 yamt uint8_t dev_count;
114 1.9.2.2 yamt struct SMB_DEVICE device[1];
115 1.9.2.2 yamt };
116 1.9.2.2 yamt
117 1.9.2.2 yamt static const char * const smbus_acpi_ids[] = {
118 1.9.2.2 yamt "SMBUS01", /* SMBus CMI v1.0 */
119 1.9.2.2 yamt NULL
120 1.9.2.2 yamt };
121 1.9.2.2 yamt
122 1.9.2.2 yamt static const char * const pcibus_acpi_ids[] = {
123 1.9.2.2 yamt "PNP0A03",
124 1.9.2.2 yamt "PNP0A08",
125 1.9.2.2 yamt NULL
126 1.9.2.2 yamt };
127 1.9.2.2 yamt
128 1.9.2.2 yamt CFATTACH_DECL_NEW(acpismbus, sizeof(struct acpi_smbus_softc),
129 1.9.2.2 yamt acpi_smbus_match, acpi_smbus_attach, acpi_smbus_detach, NULL);
130 1.9.2.2 yamt
131 1.9.2.2 yamt /*
132 1.9.2.2 yamt * acpi_smbus_match: autoconf(9) match routine
133 1.9.2.2 yamt */
134 1.9.2.2 yamt static int
135 1.9.2.2 yamt acpi_smbus_match(device_t parent, cfdata_t match, void *aux)
136 1.9.2.2 yamt {
137 1.9.2.2 yamt struct acpi_attach_args *aa = aux;
138 1.9.2.2 yamt int r = 0;
139 1.9.2.2 yamt ACPI_STATUS rv;
140 1.9.2.2 yamt ACPI_BUFFER smi_buf;
141 1.9.2.2 yamt ACPI_OBJECT *e, *p;
142 1.9.2.2 yamt
143 1.9.2.2 yamt if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
144 1.9.2.2 yamt return 0;
145 1.9.2.2 yamt
146 1.9.2.2 yamt if (acpi_match_hid(aa->aa_node->ad_devinfo, smbus_acpi_ids) == 0)
147 1.9.2.2 yamt return 0;
148 1.9.2.2 yamt
149 1.9.2.2 yamt /* Ensure that device's CMI version is supported */
150 1.9.2.2 yamt rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
151 1.9.2.2 yamt if (ACPI_FAILURE(rv))
152 1.9.2.2 yamt goto done;
153 1.9.2.2 yamt
154 1.9.2.2 yamt p = smi_buf.Pointer;
155 1.9.2.2 yamt if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
156 1.9.2.2 yamt p->Package.Count >= 1) {
157 1.9.2.2 yamt e = p->Package.Elements;
158 1.9.2.2 yamt if (e[0].Type == ACPI_TYPE_INTEGER &&
159 1.9.2.2 yamt e[0].Integer.Value == 0x10)
160 1.9.2.2 yamt r = 1;
161 1.9.2.2 yamt }
162 1.9.2.2 yamt done:
163 1.9.2.2 yamt if (smi_buf.Pointer != NULL)
164 1.9.2.2 yamt ACPI_FREE(smi_buf.Pointer);
165 1.9.2.2 yamt
166 1.9.2.2 yamt return r;
167 1.9.2.2 yamt }
168 1.9.2.2 yamt
169 1.9.2.2 yamt /*
170 1.9.2.2 yamt * acpitz_attach: autoconf(9) attach routine
171 1.9.2.2 yamt */
172 1.9.2.2 yamt static void
173 1.9.2.2 yamt acpi_smbus_attach(device_t parent, device_t self, void *aux)
174 1.9.2.2 yamt {
175 1.9.2.2 yamt struct acpi_smbus_softc *sc = device_private(self);
176 1.9.2.2 yamt struct acpi_attach_args *aa = aux;
177 1.9.2.2 yamt struct i2cbus_attach_args iba;
178 1.9.2.2 yamt ACPI_STATUS rv;
179 1.9.2.2 yamt ACPI_HANDLE native_dev, native_bus;
180 1.9.2.2 yamt ACPI_DEVICE_INFO *native_dev_info, *native_bus_info;
181 1.9.2.2 yamt ACPI_BUFFER smi_buf;
182 1.9.2.2 yamt ACPI_OBJECT *e, *p;
183 1.9.2.2 yamt struct SMB_INFO *info;
184 1.9.2.2 yamt int pci_bus, pci_dev, pci_func;
185 1.9.2.2 yamt
186 1.9.2.2 yamt aprint_naive("\n");
187 1.9.2.2 yamt
188 1.9.2.2 yamt sc->sc_devnode = aa->aa_node;
189 1.9.2.2 yamt sc->sc_dv = self;
190 1.9.2.2 yamt sc->sc_poll_alert = 2;
191 1.9.2.2 yamt
192 1.9.2.2 yamt /* Attach I2C bus */
193 1.9.2.2 yamt mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
194 1.9.2.2 yamt sc->sc_i2c_tag.ic_cookie = sc;
195 1.9.2.2 yamt sc->sc_i2c_tag.ic_acquire_bus = acpi_smbus_acquire_bus;
196 1.9.2.2 yamt sc->sc_i2c_tag.ic_release_bus = acpi_smbus_release_bus;
197 1.9.2.2 yamt sc->sc_i2c_tag.ic_exec = acpi_smbus_exec;
198 1.9.2.2 yamt
199 1.9.2.2 yamt /* Retrieve polling interval for SMBus Alerts */
200 1.9.2.2 yamt rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
201 1.9.2.2 yamt if (ACPI_SUCCESS(rv)) {
202 1.9.2.2 yamt p = smi_buf.Pointer;
203 1.9.2.2 yamt if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
204 1.9.2.2 yamt p->Package.Count >= 2) {
205 1.9.2.2 yamt e = p->Package.Elements;
206 1.9.2.2 yamt if (e[1].Type == ACPI_TYPE_BUFFER) {
207 1.9.2.2 yamt info = (struct SMB_INFO *)(e[1].Buffer.Pointer);
208 1.9.2.2 yamt sc->sc_poll_alert = info->poll_int;
209 1.9.2.2 yamt }
210 1.9.2.2 yamt }
211 1.9.2.2 yamt }
212 1.9.2.2 yamt if (smi_buf.Pointer != NULL)
213 1.9.2.2 yamt ACPI_FREE(smi_buf.Pointer);
214 1.9.2.2 yamt
215 1.9.2.2 yamt /* Install notify handler if possible */
216 1.9.2.2 yamt rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
217 1.9.2.2 yamt ACPI_DEVICE_NOTIFY, acpi_smbus_notify_handler, self);
218 1.9.2.2 yamt if (ACPI_FAILURE(rv)) {
219 1.9.2.2 yamt aprint_error(": unable to install DEVICE NOTIFY handler: %s\n",
220 1.9.2.2 yamt AcpiFormatException(rv));
221 1.9.2.2 yamt sc->sc_poll_alert = 2; /* If failed, fall-back to polling */
222 1.9.2.2 yamt }
223 1.9.2.2 yamt
224 1.9.2.2 yamt callout_init(&sc->sc_callout, 0);
225 1.9.2.2 yamt callout_setfunc(&sc->sc_callout, acpi_smbus_tick, self);
226 1.9.2.2 yamt
227 1.9.2.2 yamt if (!pmf_device_register(self, NULL, NULL))
228 1.9.2.2 yamt aprint_error(": couldn't establish power handler\n");
229 1.9.2.2 yamt
230 1.9.2.2 yamt if (sc->sc_poll_alert != 0) {
231 1.9.2.2 yamt aprint_debug(" alert_poll %d sec", sc->sc_poll_alert);
232 1.9.2.2 yamt callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
233 1.9.2.2 yamt }
234 1.9.2.2 yamt aprint_normal("\n");
235 1.9.2.2 yamt
236 1.9.2.2 yamt /*
237 1.9.2.2 yamt * Retrieve and display native controller info
238 1.9.2.2 yamt */
239 1.9.2.2 yamt rv = AcpiGetParent(sc->sc_devnode->ad_handle, &native_dev);
240 1.9.2.2 yamt
241 1.9.2.2 yamt native_bus_info = native_dev_info = NULL;
242 1.9.2.2 yamt
243 1.9.2.2 yamt if (ACPI_SUCCESS(rv))
244 1.9.2.2 yamt rv = AcpiGetParent(native_dev, &native_bus);
245 1.9.2.2 yamt
246 1.9.2.2 yamt if (ACPI_SUCCESS(rv))
247 1.9.2.2 yamt rv = AcpiGetObjectInfo(native_bus, &native_bus_info);
248 1.9.2.2 yamt
249 1.9.2.2 yamt if (ACPI_SUCCESS(rv) &&
250 1.9.2.2 yamt acpi_match_hid(native_bus_info, pcibus_acpi_ids) != 0) {
251 1.9.2.2 yamt
252 1.9.2.2 yamt rv = AcpiGetObjectInfo(native_dev, &native_dev_info);
253 1.9.2.2 yamt
254 1.9.2.2 yamt if (ACPI_SUCCESS(rv)) {
255 1.9.2.2 yamt pci_bus = native_bus_info->Address;
256 1.9.2.2 yamt pci_dev = ACPI_ADR_PCI_DEV(native_dev_info->Address);
257 1.9.2.2 yamt pci_func = ACPI_ADR_PCI_FUNC(native_dev_info->Address);
258 1.9.2.2 yamt aprint_debug_dev(self, "Native i2c host controller"
259 1.9.2.2 yamt " is on PCI bus %d dev %d func %d\n",
260 1.9.2.2 yamt pci_bus, pci_dev, pci_func);
261 1.9.2.2 yamt /*
262 1.9.2.2 yamt * XXX We really need a mechanism to prevent the
263 1.9.2.2 yamt * XXX native controller from attaching
264 1.9.2.2 yamt */
265 1.9.2.2 yamt }
266 1.9.2.2 yamt }
267 1.9.2.2 yamt
268 1.9.2.2 yamt if (native_bus_info != NULL)
269 1.9.2.2 yamt ACPI_FREE(native_bus_info);
270 1.9.2.2 yamt
271 1.9.2.2 yamt if (native_dev_info != NULL)
272 1.9.2.2 yamt ACPI_FREE(native_dev_info);
273 1.9.2.2 yamt
274 1.9.2.2 yamt memset(&iba, 0, sizeof(iba));
275 1.9.2.2 yamt iba.iba_tag = &sc->sc_i2c_tag;
276 1.9.2.2 yamt config_found_ia(self, "i2cbus", &iba, iicbus_print);
277 1.9.2.2 yamt }
278 1.9.2.2 yamt
279 1.9.2.2 yamt static int
280 1.9.2.2 yamt acpi_smbus_detach(device_t self, int flags)
281 1.9.2.2 yamt {
282 1.9.2.2 yamt struct acpi_smbus_softc *sc = device_private(self);
283 1.9.2.2 yamt ACPI_STATUS rv;
284 1.9.2.2 yamt
285 1.9.2.2 yamt rv = AcpiRemoveNotifyHandler(sc->sc_devnode->ad_handle,
286 1.9.2.2 yamt ACPI_DEVICE_NOTIFY, acpi_smbus_notify_handler);
287 1.9.2.2 yamt
288 1.9.2.2 yamt if (ACPI_FAILURE(rv))
289 1.9.2.2 yamt return EBUSY;
290 1.9.2.2 yamt
291 1.9.2.2 yamt pmf_device_deregister(self);
292 1.9.2.2 yamt
293 1.9.2.2 yamt callout_halt(&sc->sc_callout, NULL);
294 1.9.2.2 yamt callout_destroy(&sc->sc_callout);
295 1.9.2.2 yamt
296 1.9.2.2 yamt mutex_destroy(&sc->sc_i2c_mutex);
297 1.9.2.2 yamt
298 1.9.2.2 yamt return 0;
299 1.9.2.2 yamt }
300 1.9.2.2 yamt
301 1.9.2.2 yamt static int
302 1.9.2.2 yamt acpi_smbus_acquire_bus(void *cookie, int flags)
303 1.9.2.2 yamt {
304 1.9.2.2 yamt struct acpi_smbus_softc *sc = cookie;
305 1.9.2.2 yamt
306 1.9.2.2 yamt mutex_enter(&sc->sc_i2c_mutex);
307 1.9.2.2 yamt return 0;
308 1.9.2.2 yamt }
309 1.9.2.2 yamt
310 1.9.2.2 yamt static void
311 1.9.2.2 yamt acpi_smbus_release_bus(void *cookie, int flags)
312 1.9.2.2 yamt {
313 1.9.2.2 yamt struct acpi_smbus_softc *sc = cookie;
314 1.9.2.2 yamt
315 1.9.2.2 yamt mutex_exit(&sc->sc_i2c_mutex);
316 1.9.2.2 yamt }
317 1.9.2.2 yamt static int
318 1.9.2.2 yamt acpi_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
319 1.9.2.2 yamt const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
320 1.9.2.2 yamt {
321 1.9.2.2 yamt struct acpi_smbus_softc *sc = cookie;
322 1.9.2.2 yamt const uint8_t *c = cmdbuf;
323 1.9.2.2 yamt uint8_t *b = buf, *xb;
324 1.9.2.2 yamt int xlen;
325 1.9.2.2 yamt int r = 0;
326 1.9.2.2 yamt ACPI_BUFFER smbuf;
327 1.9.2.2 yamt ACPI_STATUS rv;
328 1.9.2.2 yamt ACPI_OBJECT_LIST args;
329 1.9.2.2 yamt ACPI_OBJECT arg[5];
330 1.9.2.2 yamt ACPI_OBJECT *p, *e;
331 1.9.2.2 yamt
332 1.9.2.2 yamt smbuf.Pointer = NULL;
333 1.9.2.2 yamt smbuf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
334 1.9.2.2 yamt args.Pointer = arg;
335 1.9.2.2 yamt arg[0].Type = ACPI_TYPE_INTEGER; /* Protocol */
336 1.9.2.2 yamt arg[1].Type = ACPI_TYPE_INTEGER; /* Slave Addr */
337 1.9.2.2 yamt arg[1].Integer.Value = addr;
338 1.9.2.2 yamt arg[2].Type = ACPI_TYPE_INTEGER; /* Command */
339 1.9.2.2 yamt if (I2C_OP_READ_P(op)) {
340 1.9.2.2 yamt args.Count = 3;
341 1.9.2.2 yamt if (len == 0) {
342 1.9.2.2 yamt arg[2].Integer.Value = 0;
343 1.9.2.2 yamt if (cmdlen == 0)
344 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_RD_QUICK;
345 1.9.2.2 yamt else
346 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_RCV_BYTE;
347 1.9.2.2 yamt } else
348 1.9.2.2 yamt arg[2].Integer.Value = *c;
349 1.9.2.2 yamt if (len == 1)
350 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_RD_BYTE;
351 1.9.2.2 yamt else if (len == 2)
352 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_RD_WORD;
353 1.9.2.2 yamt else if (len > 2)
354 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_RD_BLOCK;
355 1.9.2.2 yamt rv = AcpiEvaluateObject(sc->sc_devnode->ad_handle, "_SBR",
356 1.9.2.2 yamt &args, &smbuf);
357 1.9.2.2 yamt } else {
358 1.9.2.2 yamt args.Count = 5;
359 1.9.2.2 yamt arg[3].Type = ACPI_TYPE_INTEGER; /* Data Len */
360 1.9.2.2 yamt arg[3].Integer.Value = len;
361 1.9.2.2 yamt arg[4].Type = ACPI_TYPE_INTEGER; /* Data */
362 1.9.2.2 yamt if (len == 0) {
363 1.9.2.2 yamt arg[4].Integer.Value = 0;
364 1.9.2.2 yamt if (cmdlen == 0) {
365 1.9.2.2 yamt arg[2].Integer.Value = 0;
366 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_WR_QUICK;
367 1.9.2.2 yamt } else {
368 1.9.2.2 yamt arg[2].Integer.Value = *c;
369 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_SND_BYTE;
370 1.9.2.2 yamt }
371 1.9.2.2 yamt } else
372 1.9.2.2 yamt arg[2].Integer.Value = *c;
373 1.9.2.2 yamt if (len == 1) {
374 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_WR_BYTE;
375 1.9.2.2 yamt arg[4].Integer.Value = *b;
376 1.9.2.2 yamt } else if (len == 2) {
377 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_WR_WORD;
378 1.9.2.2 yamt arg[4].Integer.Value = *b++;
379 1.9.2.2 yamt arg[4].Integer.Value += (*b--) << 8;
380 1.9.2.2 yamt } else if (len > 2) {
381 1.9.2.2 yamt arg[0].Integer.Value = ACPI_SMBUS_WR_BLOCK;
382 1.9.2.2 yamt arg[4].Type = ACPI_TYPE_BUFFER;
383 1.9.2.2 yamt arg[4].Buffer.Pointer = buf;
384 1.9.2.2 yamt arg[4].Buffer.Length = (len < 32?len:32);
385 1.9.2.2 yamt }
386 1.9.2.2 yamt rv = AcpiEvaluateObject(sc->sc_devnode->ad_handle, "_SBW",
387 1.9.2.2 yamt &args, &smbuf);
388 1.9.2.2 yamt }
389 1.9.2.2 yamt if (ACPI_FAILURE(rv))
390 1.9.2.2 yamt r = 1;
391 1.9.2.2 yamt else {
392 1.9.2.2 yamt p = smbuf.Pointer;
393 1.9.2.2 yamt if (p == NULL || p->Type != ACPI_TYPE_PACKAGE ||
394 1.9.2.2 yamt p->Package.Count < 1)
395 1.9.2.2 yamt r = 1;
396 1.9.2.2 yamt else {
397 1.9.2.2 yamt e = p->Package.Elements;
398 1.9.2.2 yamt if (e->Type == ACPI_TYPE_INTEGER)
399 1.9.2.2 yamt r = e[0].Integer.Value;
400 1.9.2.2 yamt else
401 1.9.2.2 yamt r = 1;
402 1.9.2.2 yamt }
403 1.9.2.2 yamt if (r != 0)
404 1.9.2.2 yamt r = 1;
405 1.9.2.2 yamt
406 1.9.2.2 yamt /* For read operations, copy data to user buffer */
407 1.9.2.2 yamt if (r == 0 && I2C_OP_READ_P(op)) {
408 1.9.2.2 yamt if (p->Package.Count >= 3 &&
409 1.9.2.2 yamt e[1].Type == ACPI_TYPE_INTEGER) {
410 1.9.2.2 yamt xlen = e[1].Integer.Value;
411 1.9.2.2 yamt if (xlen > len)
412 1.9.2.2 yamt xlen = len;
413 1.9.2.2 yamt if (xlen != 0 &&
414 1.9.2.2 yamt e[2].Type == ACPI_TYPE_BUFFER) {
415 1.9.2.2 yamt xb = e[2].Buffer.Pointer;
416 1.9.2.2 yamt if (xb != NULL)
417 1.9.2.2 yamt memcpy(b, xb, xlen);
418 1.9.2.2 yamt else
419 1.9.2.2 yamt r = 1;
420 1.9.2.2 yamt } else if (e[2].Type == ACPI_TYPE_INTEGER) {
421 1.9.2.2 yamt if (xlen > 0)
422 1.9.2.2 yamt *b++ = e[2].Integer.Value &
423 1.9.2.2 yamt 0xff;
424 1.9.2.2 yamt if (xlen > 1)
425 1.9.2.2 yamt *b = (e[2].Integer.Value >> 8);
426 1.9.2.2 yamt } else
427 1.9.2.2 yamt r = 1;
428 1.9.2.2 yamt } else
429 1.9.2.2 yamt r = 1;
430 1.9.2.2 yamt }
431 1.9.2.2 yamt }
432 1.9.2.2 yamt if (smbuf.Pointer)
433 1.9.2.2 yamt ACPI_FREE(smbuf.Pointer);
434 1.9.2.2 yamt
435 1.9.2.2 yamt return r;
436 1.9.2.2 yamt }
437 1.9.2.2 yamt
438 1.9.2.2 yamt /*
439 1.9.2.2 yamt * acpi_smbus_alerts
440 1.9.2.2 yamt *
441 1.9.2.2 yamt * Whether triggered by periodic polling or an AcpiNotify, retrieve
442 1.9.2.2 yamt * all pending SMBus device alerts
443 1.9.2.2 yamt */
444 1.9.2.2 yamt static void
445 1.9.2.2 yamt acpi_smbus_alerts(struct acpi_smbus_softc *sc)
446 1.9.2.2 yamt {
447 1.9.2.2 yamt int status = 0;
448 1.9.2.2 yamt uint8_t slave_addr;
449 1.9.2.2 yamt ACPI_STATUS rv;
450 1.9.2.2 yamt ACPI_BUFFER alert;
451 1.9.2.2 yamt ACPI_OBJECT *e, *p;
452 1.9.2.2 yamt
453 1.9.2.2 yamt do {
454 1.9.2.2 yamt rv = acpi_eval_struct(sc->sc_devnode->ad_handle, "_SBA",
455 1.9.2.2 yamt &alert);
456 1.9.2.2 yamt if (ACPI_FAILURE(rv)) {
457 1.9.2.2 yamt status = 1;
458 1.9.2.2 yamt goto done;
459 1.9.2.2 yamt }
460 1.9.2.2 yamt
461 1.9.2.2 yamt p = alert.Pointer;
462 1.9.2.2 yamt if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
463 1.9.2.2 yamt p->Package.Count >= 2) {
464 1.9.2.2 yamt e = p->Package.Elements;
465 1.9.2.2 yamt if (e[0].Type == ACPI_TYPE_INTEGER)
466 1.9.2.2 yamt status = e[0].Integer.Value;
467 1.9.2.2 yamt else
468 1.9.2.2 yamt status = 1;
469 1.9.2.2 yamt if (status == 0x0 && e[1].Type == ACPI_TYPE_INTEGER) {
470 1.9.2.2 yamt slave_addr = e[1].Integer.Value;
471 1.9.2.2 yamt aprint_debug_dev(sc->sc_dv, "Alert for 0x%x\n",
472 1.9.2.2 yamt slave_addr);
473 1.9.2.2 yamt (void)iic_smbus_intr(&sc->sc_i2c_tag);
474 1.9.2.2 yamt }
475 1.9.2.2 yamt }
476 1.9.2.2 yamt done:
477 1.9.2.2 yamt if (alert.Pointer != NULL)
478 1.9.2.2 yamt ACPI_FREE(alert.Pointer);
479 1.9.2.2 yamt } while (status == 0);
480 1.9.2.2 yamt }
481 1.9.2.2 yamt
482 1.9.2.2 yamt static void
483 1.9.2.2 yamt acpi_smbus_tick(void *opaque)
484 1.9.2.2 yamt {
485 1.9.2.2 yamt device_t dv = opaque;
486 1.9.2.2 yamt struct acpi_smbus_softc *sc = device_private(dv);
487 1.9.2.2 yamt
488 1.9.2.2 yamt acpi_smbus_alerts(sc);
489 1.9.2.2 yamt
490 1.9.2.2 yamt callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
491 1.9.2.2 yamt }
492 1.9.2.2 yamt
493 1.9.2.2 yamt static void
494 1.9.2.2 yamt acpi_smbus_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
495 1.9.2.2 yamt {
496 1.9.2.2 yamt device_t dv = opaque;
497 1.9.2.2 yamt struct acpi_smbus_softc *sc = device_private(dv);
498 1.9.2.2 yamt
499 1.9.2.2 yamt aprint_debug_dev(dv, "received notify message 0x%x\n", notify);
500 1.9.2.2 yamt acpi_smbus_alerts(sc);
501 1.9.2.2 yamt }
502