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