xref: /onnv-gate/usr/src/uts/sun4u/littleneck/io/pcf8574_lneck.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23930Smathue  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/stat.h>		/* ddi_create_minor_node S_IFCHR */
290Sstevel@tonic-gate #include <sys/modctl.h>		/* for modldrv */
300Sstevel@tonic-gate #include <sys/open.h>		/* for open params.	 */
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate #include <sys/conf.h>		/* req. by dev_ops flags MTSAFE etc. */
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/file.h>
370Sstevel@tonic-gate #include <sys/note.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/i2c/clients/pcf8574_impl.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static void *pcf8574soft_statep;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static int pcf8574_do_attach(dev_info_t *);
440Sstevel@tonic-gate static int pcf8574_do_detach(dev_info_t *);
450Sstevel@tonic-gate static int pcf8574_do_resume(void);
460Sstevel@tonic-gate static int pcf8574_do_suspend(void);
470Sstevel@tonic-gate static int pcf8574_get(struct pcf8574_unit *, uchar_t *);
480Sstevel@tonic-gate static int pcf8574_set(struct pcf8574_unit *, uchar_t);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static void littleneck_abort_seq_handler(char *msg);
510Sstevel@tonic-gate extern void (*abort_seq_handler)();
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static void littleneck_ks_poll(void *);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * cb ops (only need ioctl)
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static int pcf8574_open(dev_t *, int, int, cred_t *);
590Sstevel@tonic-gate static int pcf8574_close(dev_t, int, int, cred_t *);
600Sstevel@tonic-gate static int pcf8574_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static struct cb_ops pcf8574_cbops = {
630Sstevel@tonic-gate 	pcf8574_open,			/* open  */
640Sstevel@tonic-gate 	pcf8574_close,			/* close */
650Sstevel@tonic-gate 	nodev,				/* strategy */
660Sstevel@tonic-gate 	nodev,				/* print */
670Sstevel@tonic-gate 	nodev,				/* dump */
680Sstevel@tonic-gate 	nodev,				/* read */
690Sstevel@tonic-gate 	nodev,				/* write */
700Sstevel@tonic-gate 	pcf8574_ioctl,			/* ioctl */
710Sstevel@tonic-gate 	nodev,				/* devmap */
720Sstevel@tonic-gate 	nodev,				/* mmap */
730Sstevel@tonic-gate 	nodev,				/* segmap */
740Sstevel@tonic-gate 	nochpoll,			/* poll */
750Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
760Sstevel@tonic-gate 	NULL,				/* streamtab */
770Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
780Sstevel@tonic-gate 	CB_REV,				/* rev */
790Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
800Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
810Sstevel@tonic-gate };
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * dev ops
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate static int pcf8574_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
870Sstevel@tonic-gate static int pcf8574_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct dev_ops pcf8574_ops = {
900Sstevel@tonic-gate 	DEVO_REV,
910Sstevel@tonic-gate 	0,
920Sstevel@tonic-gate 	ddi_getinfo_1to1,
930Sstevel@tonic-gate 	nulldev,
940Sstevel@tonic-gate 	nulldev,
950Sstevel@tonic-gate 	pcf8574_attach,
960Sstevel@tonic-gate 	pcf8574_detach,
970Sstevel@tonic-gate 	nodev,
980Sstevel@tonic-gate 	&pcf8574_cbops,
99*7656SSherry.Moore@Sun.COM 	NULL,			/* bus_ops */
100*7656SSherry.Moore@Sun.COM 	NULL,			/* power */
101*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static struct modldrv pcf8574_modldrv = {
1070Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
108*7656SSherry.Moore@Sun.COM 	"PCF8574 i2c device driver: v1.9",
1090Sstevel@tonic-gate 	&pcf8574_ops
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static struct modlinkage pcf8574_modlinkage = {
1130Sstevel@tonic-gate 	MODREV_1,
1140Sstevel@tonic-gate 	&pcf8574_modldrv,
1150Sstevel@tonic-gate 	0
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #define	LNECK_KEY_POLL_BIT 5
1190Sstevel@tonic-gate #define	LNECK_KEY_POLL_INTVL 10		/* 10 seconds poll interval */
1200Sstevel@tonic-gate static timeout_id_t keypoll_timeout_id;
1210Sstevel@tonic-gate static clock_t keypoll_timeout_hz;
1220Sstevel@tonic-gate static boolean_t key_locked_bit;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int
_init(void)1260Sstevel@tonic-gate _init(void)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	int error;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	error = mod_install(&pcf8574_modlinkage);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (!error)
1330Sstevel@tonic-gate 		(void) ddi_soft_state_init(&pcf8574soft_statep,
134*7656SSherry.Moore@Sun.COM 		    sizeof (struct pcf8574_unit), 1);
1350Sstevel@tonic-gate 	return (error);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate int
_fini(void)1390Sstevel@tonic-gate _fini(void)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	int error;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	error = mod_remove(&pcf8574_modlinkage);
1440Sstevel@tonic-gate 	if (!error)
1450Sstevel@tonic-gate 		ddi_soft_state_fini(&pcf8574soft_statep);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	return (error);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1510Sstevel@tonic-gate _info(struct modinfo *modinfop)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	return (mod_info(&pcf8574_modlinkage, modinfop));
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static int
pcf8574_open(dev_t * devp,int flags,int otyp,cred_t * credp)1570Sstevel@tonic-gate pcf8574_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
1600Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
1610Sstevel@tonic-gate 	int instance;
1620Sstevel@tonic-gate 	int error = 0;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Opening the PCF8574 device\n"));
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	instance = getminor(*devp);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (instance < 0) {
1690Sstevel@tonic-gate 		return (ENXIO);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
173*7656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8574soft_statep, instance);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (unitp == NULL) {
1760Sstevel@tonic-gate 		return (ENXIO);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
1800Sstevel@tonic-gate 		return (EINVAL);
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (flags & FEXCL) {
1860Sstevel@tonic-gate 		if (unitp->pcf8574_oflag != 0) {
1870Sstevel@tonic-gate 			error = EBUSY;
1880Sstevel@tonic-gate 		} else {
1890Sstevel@tonic-gate 			unitp->pcf8574_oflag = FEXCL;
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	} else {
1920Sstevel@tonic-gate 		if (unitp->pcf8574_oflag == FEXCL) {
1930Sstevel@tonic-gate 			error = EBUSY;
1940Sstevel@tonic-gate 		} else {
1950Sstevel@tonic-gate 			unitp->pcf8574_oflag = FOPEN;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	return (error);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate static int
pcf8574_close(dev_t dev,int flags,int otyp,cred_t * credp)2050Sstevel@tonic-gate pcf8574_close(dev_t dev, int flags, int otyp, cred_t *credp)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
2080Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
2090Sstevel@tonic-gate 	int instance;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	instance = getminor(dev);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (instance < 0) {
2140Sstevel@tonic-gate 		return (ENXIO);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
217*7656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8574soft_statep, instance);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (unitp == NULL) {
2200Sstevel@tonic-gate 		return (ENXIO);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	unitp->pcf8574_oflag = 0;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
2280Sstevel@tonic-gate 	return (DDI_SUCCESS);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static int
pcf8574_get(struct pcf8574_unit * unitp,uchar_t * byte)2320Sstevel@tonic-gate pcf8574_get(struct pcf8574_unit *unitp, uchar_t *byte) {
2330Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2340Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Entered the pcf8574_get routine\n"));
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pcf8574_hdl, &i2c_tran_pointer,
2390Sstevel@tonic-gate 					0, 1, I2C_SLEEP);
2400Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2410Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in pcf8574_get "
2420Sstevel@tonic-gate 				"i2c_tran_pointer not allocated\n",
2430Sstevel@tonic-gate 				unitp->pcf8574_name));
2440Sstevel@tonic-gate 		return (ENOMEM);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_RD;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	err = i2c_transfer(unitp->pcf8574_hdl, i2c_tran_pointer);
2500Sstevel@tonic-gate 	if (err) {
2510Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the i2c_transfer routine\n",
2520Sstevel@tonic-gate 				unitp->pcf8574_name));
2530Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
2540Sstevel@tonic-gate 		return (err);
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Back from a transfer value is %x\n",
2580Sstevel@tonic-gate 		i2c_tran_pointer->i2c_rbuf[0]));
2590Sstevel@tonic-gate 	*byte = i2c_tran_pointer->i2c_rbuf[0];
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
2620Sstevel@tonic-gate 	return (err);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static int
pcf8574_set(struct pcf8574_unit * unitp,uchar_t byte)2660Sstevel@tonic-gate pcf8574_set(struct pcf8574_unit *unitp, uchar_t byte) {
2670Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2680Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pcf8574_hdl, &i2c_tran_pointer,
2710Sstevel@tonic-gate 				1, 0, I2C_SLEEP);
2720Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2730Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in pcf8574_set "
2740Sstevel@tonic-gate 				"i2c_tran_pointer not allocated\n",
2750Sstevel@tonic-gate 				unitp->pcf8574_name));
2760Sstevel@tonic-gate 		return (ENOMEM);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR;
2800Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = byte;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: contains %x\n", unitp->pcf8574_name,
2830Sstevel@tonic-gate 			i2c_tran_pointer->i2c_wbuf[0]));
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	err = i2c_transfer(unitp->pcf8574_hdl, i2c_tran_pointer);
2860Sstevel@tonic-gate 	if (err) {
2870Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the pcf8574_set"
2880Sstevel@tonic-gate 				" i2c_transfer routine\n",
2890Sstevel@tonic-gate 				unitp->pcf8574_name));
2900Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
2910Sstevel@tonic-gate 		return (err);
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
2940Sstevel@tonic-gate 	return (err);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate static int
pcf8574_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2980Sstevel@tonic-gate pcf8574_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
2990Sstevel@tonic-gate 		cred_t *credp, int *rvalp)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
3020Sstevel@tonic-gate 	struct pcf8574_unit	*unitp;
3030Sstevel@tonic-gate 	int		instance;
3040Sstevel@tonic-gate 	int			err = 0;
3050Sstevel@tonic-gate 	i2c_bit_t		ioctl_bit;
3060Sstevel@tonic-gate 	i2c_port_t		ioctl_port;
3070Sstevel@tonic-gate 	uchar_t			byte;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (arg == NULL) {
3100Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "PCF8574: ioctl: arg passed in to ioctl "
311*7656SSherry.Moore@Sun.COM 		    "= NULL\n"));
3120Sstevel@tonic-gate 		err = EINVAL;
3130Sstevel@tonic-gate 		return (err);
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	instance = getminor(dev);
3170Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
318*7656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8574soft_statep, instance);
3190Sstevel@tonic-gate 	if (unitp == NULL) {
3200Sstevel@tonic-gate 		cmn_err(CE_WARN, "PCF8574: ioctl: unitp not filled\n");
3210Sstevel@tonic-gate 		return (ENOMEM);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	switch (cmd) {
3270Sstevel@tonic-gate 	case I2C_GET_PORT:
3280Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
329*7656SSherry.Moore@Sun.COM 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3300Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
331*7656SSherry.Moore@Sun.COM 			    " ddi_copyin routine\n",
332*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3330Sstevel@tonic-gate 			err = EFAULT;
3340Sstevel@tonic-gate 			break;
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
3380Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3390Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
340*7656SSherry.Moore@Sun.COM 			    " pcf8574_get routine\n",
341*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3420Sstevel@tonic-gate 			break;
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		ioctl_port.value = byte;
3460Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_port, (caddr_t)arg,
347*7656SSherry.Moore@Sun.COM 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3480Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_PORT "
349*7656SSherry.Moore@Sun.COM 			    "ddi_copyout routine\n",
350*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3510Sstevel@tonic-gate 			err = EFAULT;
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: contains %x\n", unitp->pcf8574_name,
355*7656SSherry.Moore@Sun.COM 		    byte));
3560Sstevel@tonic-gate 		break;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	case I2C_SET_PORT:
3590Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
360*7656SSherry.Moore@Sun.COM 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
3610Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
362*7656SSherry.Moore@Sun.COM 			    "ddi_cpoyin routine\n",
363*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3640Sstevel@tonic-gate 			err = EFAULT;
3650Sstevel@tonic-gate 			break;
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		err = pcf8574_set(unitp, ioctl_port.value);
3690Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3700Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
371*7656SSherry.Moore@Sun.COM 			    " pcf8574_set routine\n",
372*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3730Sstevel@tonic-gate 			break;
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 		break;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	case I2C_GET_BIT:
3780Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
379*7656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
3800Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
381*7656SSherry.Moore@Sun.COM 			    " ddi_copyin routine\n",
382*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3830Sstevel@tonic-gate 			err = EFAULT;
3840Sstevel@tonic-gate 			break;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 
387930Smathue 		if (ioctl_bit.bit_num > 7) {
3880Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: In I2C_GET_BIT bit num"
389*7656SSherry.Moore@Sun.COM 			    " was not between 0 and 7\n",
390*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3910Sstevel@tonic-gate 			err = EIO;
3920Sstevel@tonic-gate 			break;
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
3960Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3970Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
398*7656SSherry.Moore@Sun.COM 			    " pcf8574_get routine\n",
399*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4000Sstevel@tonic-gate 			break;
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x\n",
404*7656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, byte));
4050Sstevel@tonic-gate 		ioctl_bit.bit_value = (boolean_t)PCF8574_BIT_READ_MASK(byte,
406*7656SSherry.Moore@Sun.COM 		    ioctl_bit.bit_num);
4070Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte now contains %x\n",
408*7656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, byte));
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_bit, (caddr_t)arg,
411*7656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4120Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_BIT"
413*7656SSherry.Moore@Sun.COM 			    " ddi_copyout routine\n",
414*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4150Sstevel@tonic-gate 			err = EFAULT;
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 		break;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	case I2C_SET_BIT:
4200Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
421*7656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4220Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_BIT"
423*7656SSherry.Moore@Sun.COM 			    " ddi_copyin routine\n",
424*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4250Sstevel@tonic-gate 			err = EFAULT;
4260Sstevel@tonic-gate 			break;
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 
429930Smathue 		if (ioctl_bit.bit_num > 7) {
4300Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: I2C_SET_BIT: bit_num sent"
431*7656SSherry.Moore@Sun.COM 			    " in was not between 0 and 7",
432*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4330Sstevel@tonic-gate 			err = EIO;
4340Sstevel@tonic-gate 			break;
4350Sstevel@tonic-gate 		}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
4380Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4390Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
440*7656SSherry.Moore@Sun.COM 			    " pcf8574_get routine\n",
441*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4420Sstevel@tonic-gate 			break;
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x\n",
446*7656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, byte));
4470Sstevel@tonic-gate 		byte = PCF8574_BIT_WRITE_MASK(byte, ioctl_bit.bit_num,
448*7656SSherry.Moore@Sun.COM 		    ioctl_bit.bit_value);
4490Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte after shifting is %x\n",
450*7656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, byte));
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		err = pcf8574_set(unitp, byte);
4530Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4540Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
455*7656SSherry.Moore@Sun.COM 			    " pcf8574_set routine\n",
456*7656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4570Sstevel@tonic-gate 			break;
4580Sstevel@tonic-gate 		}
4590Sstevel@tonic-gate 		break;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	default:
4620Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n",
463*7656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, cmd));
4640Sstevel@tonic-gate 		err = EINVAL;
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
4680Sstevel@tonic-gate 	return (err);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate static int
pcf8574_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4720Sstevel@tonic-gate pcf8574_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	switch (cmd) {
4750Sstevel@tonic-gate 	case DDI_ATTACH:
4760Sstevel@tonic-gate 		return (pcf8574_do_attach(dip));
4770Sstevel@tonic-gate 	case DDI_RESUME:
4780Sstevel@tonic-gate 		return (pcf8574_do_resume());
4790Sstevel@tonic-gate 	default:
4800Sstevel@tonic-gate 		return (DDI_FAILURE);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate static int
pcf8574_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4850Sstevel@tonic-gate pcf8574_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	switch (cmd) {
4880Sstevel@tonic-gate 	case DDI_DETACH:
4890Sstevel@tonic-gate 		return (pcf8574_do_detach(dip));
4900Sstevel@tonic-gate 	case DDI_SUSPEND:
4910Sstevel@tonic-gate 		return (pcf8574_do_suspend());
4920Sstevel@tonic-gate 	default:
4930Sstevel@tonic-gate 		return (DDI_FAILURE);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate static int
pcf8574_do_attach(dev_info_t * dip)4980Sstevel@tonic-gate pcf8574_do_attach(dev_info_t *dip)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
5010Sstevel@tonic-gate 	int instance, err;
5020Sstevel@tonic-gate 	uint_t len;
5030Sstevel@tonic-gate 	int *regs;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pcf8574soft_statep, instance) != 0) {
5080Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
509*7656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
5100Sstevel@tonic-gate 		return (DDI_FAILURE);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8574soft_statep, instance);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (unitp == NULL) {
5160Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
517*7656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
5180Sstevel@tonic-gate 		return (ENOMEM);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	(void) snprintf(unitp->pcf8574_name, sizeof (unitp->pcf8574_name),
522*7656SSherry.Moore@Sun.COM 	    "%s%d", ddi_node_name(dip), instance);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "pcf8574", S_IFCHR, instance,
526*7656SSherry.Moore@Sun.COM 	    "ddi_i2c:ioexp", NULL) == DDI_FAILURE) {
5270Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
528*7656SSherry.Moore@Sun.COM 		    "%s\n", unitp->pcf8574_name, "pcf8574");
5290Sstevel@tonic-gate 		ddi_soft_state_free(pcf8574soft_statep, instance);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 		return (DDI_FAILURE);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->pcf8574_hdl) != I2C_SUCCESS) {
5350Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
5360Sstevel@tonic-gate 		ddi_soft_state_free(pcf8574soft_statep, instance);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		return (DDI_FAILURE);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
542*7656SSherry.Moore@Sun.COM 	    DDI_PROP_DONTPASS,
543*7656SSherry.Moore@Sun.COM 	    "reg", (int **)&regs, &len);
5440Sstevel@tonic-gate 	if (err != DDI_PROP_SUCCESS) {
5450Sstevel@tonic-gate 		return (DDI_FAILURE);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * regs[0] contains the bus number and regs[1] contains the device
5500Sstevel@tonic-gate 	 * address of the i2c device. 0x7c is the device address of the
5510Sstevel@tonic-gate 	 * i2c device from which the key switch position is read.
5520Sstevel@tonic-gate 	 */
5530Sstevel@tonic-gate 	if (regs[0] == 0 && regs[1] == 0x7c) {
5540Sstevel@tonic-gate 		abort_seq_handler = littleneck_abort_seq_handler;
5550Sstevel@tonic-gate 		keypoll_timeout_hz =
556*7656SSherry.Moore@Sun.COM 		    drv_usectohz(LNECK_KEY_POLL_INTVL * MICROSEC);
5570Sstevel@tonic-gate 		littleneck_ks_poll(unitp);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	ddi_prop_free(regs);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	mutex_init(&unitp->pcf8574_mutex, NULL, MUTEX_DRIVER, NULL);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	return (DDI_SUCCESS);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate static int
pcf8574_do_resume()5680Sstevel@tonic-gate pcf8574_do_resume()
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	return (ret);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate static int
pcf8574_do_suspend()5760Sstevel@tonic-gate pcf8574_do_suspend()
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	return (ret);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate static int
pcf8574_do_detach(dev_info_t * dip)5840Sstevel@tonic-gate pcf8574_do_detach(dev_info_t *dip)
5850Sstevel@tonic-gate {
5860Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
5870Sstevel@tonic-gate 	int instance;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8574soft_statep, instance);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if (unitp == NULL) {
5940Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
595*7656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
5960Sstevel@tonic-gate 		return (ENOMEM);
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	(void) untimeout(keypoll_timeout_id);
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	i2c_client_unregister(unitp->pcf8574_hdl);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	mutex_destroy(&unitp->pcf8574_mutex);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	ddi_soft_state_free(pcf8574soft_statep, instance);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	return (DDI_SUCCESS);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate static void
littleneck_ks_poll(void * arg)6140Sstevel@tonic-gate littleneck_ks_poll(void *arg)
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate 	struct pcf8574_unit *unitp = (struct pcf8574_unit *)arg;
6170Sstevel@tonic-gate 	uint8_t byte;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if (pcf8574_get(unitp, &byte) != I2C_SUCCESS) {
6220Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in littleneck_ks_poll"
623*7656SSherry.Moore@Sun.COM 		    " pcf8574_get routine\n", unitp->pcf8574_name));
6240Sstevel@tonic-gate 		mutex_exit(&unitp->pcf8574_mutex);
6250Sstevel@tonic-gate 		return;
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * 5th bit in the byte is the key LOCKED position
6300Sstevel@tonic-gate 	 */
6310Sstevel@tonic-gate 	key_locked_bit = (boolean_t)PCF8574_BIT_READ_MASK(byte,
632*7656SSherry.Moore@Sun.COM 	    LNECK_KEY_POLL_BIT);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	keypoll_timeout_id = (timeout(littleneck_ks_poll,
635*7656SSherry.Moore@Sun.COM 	    (caddr_t)unitp, keypoll_timeout_hz));
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate static void
littleneck_abort_seq_handler(char * msg)6410Sstevel@tonic-gate littleneck_abort_seq_handler(char *msg)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	if (key_locked_bit == 0)
6450Sstevel@tonic-gate 		cmn_err(CE_CONT, "KEY in LOCKED position, "
646*7656SSherry.Moore@Sun.COM 		    "ignoring debug enter sequence\n");
6470Sstevel@tonic-gate 	else  {
6480Sstevel@tonic-gate 		D1CMN_ERR((CE_CONT, "debug enter sequence \n"));
6490Sstevel@tonic-gate 		debug_enter(msg);
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate }
652