xref: /onnv-gate/usr/src/uts/sun4u/io/i2c/clients/pcf8574.c (revision 7696:21f5c73c0c15)
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
57656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
67656SSherry.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 /*
227656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23946Smathue  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/stat.h>		/* ddi_create_minor_node S_IFCHR */
270Sstevel@tonic-gate #include <sys/modctl.h>		/* for modldrv */
280Sstevel@tonic-gate #include <sys/open.h>		/* for open params.	 */
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/conf.h>		/* req. by dev_ops flags MTSAFE etc. */
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/file.h>
350Sstevel@tonic-gate #include <sys/note.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/i2c/clients/pcf8574_impl.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static void *pcf8574soft_statep;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static int pcf8574_do_attach(dev_info_t *);
420Sstevel@tonic-gate static int pcf8574_do_detach(dev_info_t *);
430Sstevel@tonic-gate static int pcf8574_do_resume(void);
440Sstevel@tonic-gate static int pcf8574_do_suspend(void);
450Sstevel@tonic-gate static int pcf8574_get(struct pcf8574_unit *, uchar_t *);
460Sstevel@tonic-gate static int pcf8574_set(struct pcf8574_unit *, uchar_t);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * cb ops (only need ioctl)
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate static int pcf8574_open(dev_t *, int, int, cred_t *);
520Sstevel@tonic-gate static int pcf8574_close(dev_t, int, int, cred_t *);
530Sstevel@tonic-gate static int pcf8574_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static struct cb_ops pcf8574_cbops = {
560Sstevel@tonic-gate 	pcf8574_open,			/* open  */
570Sstevel@tonic-gate 	pcf8574_close,			/* close */
580Sstevel@tonic-gate 	nodev,				/* strategy */
590Sstevel@tonic-gate 	nodev,				/* print */
600Sstevel@tonic-gate 	nodev,				/* dump */
610Sstevel@tonic-gate 	nodev,				/* read */
620Sstevel@tonic-gate 	nodev,				/* write */
630Sstevel@tonic-gate 	pcf8574_ioctl,			/* ioctl */
640Sstevel@tonic-gate 	nodev,				/* devmap */
650Sstevel@tonic-gate 	nodev,				/* mmap */
660Sstevel@tonic-gate 	nodev,				/* segmap */
670Sstevel@tonic-gate 	nochpoll,			/* poll */
680Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
690Sstevel@tonic-gate 	NULL,				/* streamtab */
700Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
710Sstevel@tonic-gate 	CB_REV,				/* rev */
720Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
730Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
740Sstevel@tonic-gate };
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * dev ops
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate static int pcf8574_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
800Sstevel@tonic-gate static int pcf8574_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static struct dev_ops pcf8574_ops = {
830Sstevel@tonic-gate 	DEVO_REV,
840Sstevel@tonic-gate 	0,
850Sstevel@tonic-gate 	ddi_getinfo_1to1,
860Sstevel@tonic-gate 	nulldev,
870Sstevel@tonic-gate 	nulldev,
880Sstevel@tonic-gate 	pcf8574_attach,
890Sstevel@tonic-gate 	pcf8574_detach,
900Sstevel@tonic-gate 	nodev,
910Sstevel@tonic-gate 	&pcf8574_cbops,
927656SSherry.Moore@Sun.COM 	NULL,			/* bus_ops */
937656SSherry.Moore@Sun.COM 	NULL,			/* power */
947656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
950Sstevel@tonic-gate };
960Sstevel@tonic-gate 
970Sstevel@tonic-gate extern struct mod_ops mod_driverops;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static struct modldrv pcf8574_modldrv = {
1000Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
101*7696SRichard.Bean@Sun.COM 	"PCF8574 i2c device driver",
1020Sstevel@tonic-gate 	&pcf8574_ops
1030Sstevel@tonic-gate };
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static struct modlinkage pcf8574_modlinkage = {
1060Sstevel@tonic-gate 	MODREV_1,
1070Sstevel@tonic-gate 	&pcf8574_modldrv,
1080Sstevel@tonic-gate 	0
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate int
_init(void)1130Sstevel@tonic-gate _init(void)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	int error;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	error = mod_install(&pcf8574_modlinkage);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (!error)
1200Sstevel@tonic-gate 		(void) ddi_soft_state_init(&pcf8574soft_statep,
1217656SSherry.Moore@Sun.COM 		    sizeof (struct pcf8574_unit), 1);
1220Sstevel@tonic-gate 	return (error);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int
_fini(void)1260Sstevel@tonic-gate _fini(void)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	int error;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	error = mod_remove(&pcf8574_modlinkage);
1310Sstevel@tonic-gate 	if (!error)
1320Sstevel@tonic-gate 		ddi_soft_state_fini(&pcf8574soft_statep);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	return (error);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1380Sstevel@tonic-gate _info(struct modinfo *modinfop)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	return (mod_info(&pcf8574_modlinkage, modinfop));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static int
pcf8574_open(dev_t * devp,int flags,int otyp,cred_t * credp)1440Sstevel@tonic-gate pcf8574_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
1490Sstevel@tonic-gate 	int instance;
1500Sstevel@tonic-gate 	int error = 0;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Opening the PCF8574 device\n"));
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	instance = getminor(*devp);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (instance < 0) {
1570Sstevel@tonic-gate 		return (ENXIO);
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
1617656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8574soft_statep, instance);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (unitp == NULL) {
1640Sstevel@tonic-gate 		return (ENXIO);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
1680Sstevel@tonic-gate 		return (EINVAL);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	if (flags & FEXCL) {
1740Sstevel@tonic-gate 		if (unitp->pcf8574_oflag != 0) {
1750Sstevel@tonic-gate 			error = EBUSY;
1760Sstevel@tonic-gate 		} else {
1770Sstevel@tonic-gate 			unitp->pcf8574_oflag = FEXCL;
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 	} else {
1800Sstevel@tonic-gate 		if (unitp->pcf8574_oflag == FEXCL) {
1810Sstevel@tonic-gate 			error = EBUSY;
1820Sstevel@tonic-gate 		} else {
1830Sstevel@tonic-gate 			unitp->pcf8574_oflag = FOPEN;
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	return (error);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate static int
pcf8574_close(dev_t dev,int flags,int otyp,cred_t * credp)1930Sstevel@tonic-gate pcf8574_close(dev_t dev, int flags, int otyp, cred_t *credp)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
1980Sstevel@tonic-gate 	int instance;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	instance = getminor(dev);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (instance < 0) {
2030Sstevel@tonic-gate 		return (ENXIO);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
2067656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8574soft_statep, instance);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (unitp == NULL) {
2090Sstevel@tonic-gate 		return (ENXIO);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	unitp->pcf8574_oflag = 0;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
2170Sstevel@tonic-gate 	return (DDI_SUCCESS);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate static int
pcf8574_get(struct pcf8574_unit * unitp,uchar_t * byte)2210Sstevel@tonic-gate pcf8574_get(struct pcf8574_unit *unitp, uchar_t *byte) {
2220Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2230Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Entered the pcf8574_get routine\n"));
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pcf8574_hdl, &i2c_tran_pointer,
2280Sstevel@tonic-gate 					0, 1, I2C_SLEEP);
2290Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2300Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in pcf8574_get "
2310Sstevel@tonic-gate 				"i2c_tran_pointer not allocated\n",
2320Sstevel@tonic-gate 				unitp->pcf8574_name));
2330Sstevel@tonic-gate 		return (ENOMEM);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_RD;
2370Sstevel@tonic-gate 	err = i2c_transfer(unitp->pcf8574_hdl, i2c_tran_pointer);
2380Sstevel@tonic-gate 	if (err) {
2390Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the i2c_transfer routine\n",
2400Sstevel@tonic-gate 				unitp->pcf8574_name));
2410Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
2420Sstevel@tonic-gate 		return (err);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Back from a transfer value is %x\n",
2460Sstevel@tonic-gate 		i2c_tran_pointer->i2c_rbuf[0]));
2470Sstevel@tonic-gate 	*byte = i2c_tran_pointer->i2c_rbuf[0];
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
2500Sstevel@tonic-gate 	return (err);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static int
pcf8574_set(struct pcf8574_unit * unitp,uchar_t byte)2540Sstevel@tonic-gate pcf8574_set(struct pcf8574_unit *unitp, uchar_t byte) {
2550Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2560Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pcf8574_hdl, &i2c_tran_pointer,
2590Sstevel@tonic-gate 				1, 0, I2C_SLEEP);
2600Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2610Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in pcf8574_set "
2620Sstevel@tonic-gate 				"i2c_tran_pointer not allocated\n",
2630Sstevel@tonic-gate 				unitp->pcf8574_name));
2640Sstevel@tonic-gate 		return (ENOMEM);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR;
2680Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = byte;
2690Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: contains %x\n", unitp->pcf8574_name,
2700Sstevel@tonic-gate 			i2c_tran_pointer->i2c_wbuf[0]));
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	err = i2c_transfer(unitp->pcf8574_hdl, i2c_tran_pointer);
2730Sstevel@tonic-gate 	if (err) {
2740Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the pcf8574_set"
2750Sstevel@tonic-gate 				" i2c_transfer routine\n",
2760Sstevel@tonic-gate 				unitp->pcf8574_name));
2770Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
2780Sstevel@tonic-gate 		return (err);
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 	i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
2810Sstevel@tonic-gate 	return (err);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate static int
pcf8574_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2850Sstevel@tonic-gate pcf8574_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
2860Sstevel@tonic-gate 		cred_t *credp, int *rvalp)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	struct pcf8574_unit	*unitp;
2910Sstevel@tonic-gate 	int		instance;
2920Sstevel@tonic-gate 	int			err = 0;
2930Sstevel@tonic-gate 	i2c_bit_t		ioctl_bit;
2940Sstevel@tonic-gate 	i2c_port_t		ioctl_port;
2950Sstevel@tonic-gate 	uchar_t			byte;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (arg == NULL) {
2980Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "PCF8574: ioctl: arg passed in to ioctl "
2997656SSherry.Moore@Sun.COM 		    "= NULL\n"));
3000Sstevel@tonic-gate 		err = EINVAL;
3010Sstevel@tonic-gate 		return (err);
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	instance = getminor(dev);
3050Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
3067656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8574soft_statep, instance);
3070Sstevel@tonic-gate 	if (unitp == NULL) {
3080Sstevel@tonic-gate 		cmn_err(CE_WARN, "PCF8574: ioctl: unitp not filled\n");
3090Sstevel@tonic-gate 		return (ENOMEM);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	switch (cmd) {
3150Sstevel@tonic-gate 	case I2C_GET_PORT:
3160Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
3177656SSherry.Moore@Sun.COM 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3180Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
3197656SSherry.Moore@Sun.COM 			    " ddi_copyin routine\n",
3207656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3210Sstevel@tonic-gate 			err = EFAULT;
3220Sstevel@tonic-gate 			break;
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
3260Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3270Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
3287656SSherry.Moore@Sun.COM 			    " pcf8574_get routine\n",
3297656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3300Sstevel@tonic-gate 			break;
3310Sstevel@tonic-gate 		}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		ioctl_port.value = byte;
3340Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_port, (caddr_t)arg,
3357656SSherry.Moore@Sun.COM 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3360Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_PORT "
3377656SSherry.Moore@Sun.COM 			    "ddi_copyout routine\n",
3387656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3390Sstevel@tonic-gate 			err = EFAULT;
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: contains %x\n", unitp->pcf8574_name,
3437656SSherry.Moore@Sun.COM 		    byte));
3440Sstevel@tonic-gate 		break;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	case I2C_SET_PORT:
3470Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
3487656SSherry.Moore@Sun.COM 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
3490Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
3507656SSherry.Moore@Sun.COM 			    "ddi_cpoyin routine\n",
3517656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3520Sstevel@tonic-gate 			err = EFAULT;
3530Sstevel@tonic-gate 			break;
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 		err = pcf8574_set(unitp, ioctl_port.value);
3570Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3580Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
3597656SSherry.Moore@Sun.COM 			    " pcf8574_set routine\n",
3607656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3610Sstevel@tonic-gate 			break;
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 		break;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	case I2C_GET_BIT:
3660Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
3677656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
3680Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
3697656SSherry.Moore@Sun.COM 			    " ddi_copyin routine\n",
3707656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3710Sstevel@tonic-gate 			err = EFAULT;
3720Sstevel@tonic-gate 			break;
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 
375946Smathue 		if (ioctl_bit.bit_num > 7) {
3760Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: In I2C_GET_BIT bit num"
3777656SSherry.Moore@Sun.COM 			    " was not between 0 and 7\n",
3787656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3790Sstevel@tonic-gate 			err = EIO;
3800Sstevel@tonic-gate 			break;
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
3840Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3850Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
3867656SSherry.Moore@Sun.COM 			    " pcf8574_get routine\n",
3877656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
3880Sstevel@tonic-gate 			break;
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x\n",
3927656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, byte));
3930Sstevel@tonic-gate 		ioctl_bit.bit_value = (boolean_t)PCF8574_BIT_READ_MASK(byte,
3947656SSherry.Moore@Sun.COM 		    ioctl_bit.bit_num);
3950Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte now contains %x\n",
3967656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, byte));
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_bit, (caddr_t)arg,
3997656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4000Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_BIT"
4017656SSherry.Moore@Sun.COM 			    " ddi_copyout routine\n",
4027656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4030Sstevel@tonic-gate 			err = EFAULT;
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 		break;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	case I2C_SET_BIT:
4080Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
4097656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4100Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_BIT"
4117656SSherry.Moore@Sun.COM 			    " ddi_copyin routine\n",
4127656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4130Sstevel@tonic-gate 			err = EFAULT;
4140Sstevel@tonic-gate 			break;
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 
417946Smathue 		if (ioctl_bit.bit_num > 7) {
4180Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: I2C_SET_BIT: bit_num sent"
4197656SSherry.Moore@Sun.COM 			    " in was not between 0 and 7",
4207656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4210Sstevel@tonic-gate 			err = EIO;
4220Sstevel@tonic-gate 			break;
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
4260Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4270Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
4287656SSherry.Moore@Sun.COM 			    " pcf8574_get routine\n",
4297656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4300Sstevel@tonic-gate 			break;
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x\n",
4347656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, byte));
4350Sstevel@tonic-gate 		byte = PCF8574_BIT_WRITE_MASK(byte, ioctl_bit.bit_num,
4367656SSherry.Moore@Sun.COM 		    ioctl_bit.bit_value);
4370Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte after shifting is %x\n",
4387656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, byte));
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 		err = pcf8574_set(unitp, byte);
4410Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4420Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
4437656SSherry.Moore@Sun.COM 			    " pcf8574_set routine\n",
4447656SSherry.Moore@Sun.COM 			    unitp->pcf8574_name));
4450Sstevel@tonic-gate 			break;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 		break;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	default:
4500Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n",
4517656SSherry.Moore@Sun.COM 		    unitp->pcf8574_name, cmd));
4520Sstevel@tonic-gate 		err = EINVAL;
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
4560Sstevel@tonic-gate 	return (err);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate static int
pcf8574_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4600Sstevel@tonic-gate pcf8574_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	switch (cmd) {
4630Sstevel@tonic-gate 	case DDI_ATTACH:
4640Sstevel@tonic-gate 		return (pcf8574_do_attach(dip));
4650Sstevel@tonic-gate 	case DDI_RESUME:
4660Sstevel@tonic-gate 		return (pcf8574_do_resume());
4670Sstevel@tonic-gate 	default:
4680Sstevel@tonic-gate 		return (DDI_FAILURE);
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate static int
pcf8574_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4730Sstevel@tonic-gate pcf8574_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	switch (cmd) {
4760Sstevel@tonic-gate 	case DDI_DETACH:
4770Sstevel@tonic-gate 		return (pcf8574_do_detach(dip));
4780Sstevel@tonic-gate 	case DDI_SUSPEND:
4790Sstevel@tonic-gate 		return (pcf8574_do_suspend());
4800Sstevel@tonic-gate 	default:
4810Sstevel@tonic-gate 		return (DDI_FAILURE);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate static int
pcf8574_do_attach(dev_info_t * dip)4860Sstevel@tonic-gate pcf8574_do_attach(dev_info_t *dip)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
4890Sstevel@tonic-gate 	int instance;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pcf8574soft_statep, instance) != 0) {
4940Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
4957656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
4960Sstevel@tonic-gate 		return (DDI_FAILURE);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8574soft_statep, instance);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	if (unitp == NULL) {
5020Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
5037656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
5040Sstevel@tonic-gate 		return (ENOMEM);
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	(void) snprintf(unitp->pcf8574_name, sizeof (unitp->pcf8574_name),
5087656SSherry.Moore@Sun.COM 	    "%s%d", ddi_node_name(dip), instance);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "pcf8574", S_IFCHR, instance,
5127656SSherry.Moore@Sun.COM 	    "ddi_i2c:ioexp", NULL) == DDI_FAILURE) {
5130Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
5147656SSherry.Moore@Sun.COM 		    "%s\n", unitp->pcf8574_name, "pcf8574");
5150Sstevel@tonic-gate 		ddi_soft_state_free(pcf8574soft_statep, instance);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		return (DDI_FAILURE);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->pcf8574_hdl) != I2C_SUCCESS) {
5210Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
5220Sstevel@tonic-gate 		ddi_soft_state_free(pcf8574soft_statep, instance);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		return (DDI_FAILURE);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	mutex_init(&unitp->pcf8574_mutex, NULL, MUTEX_DRIVER, NULL);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	return (DDI_SUCCESS);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate static int
pcf8574_do_resume()5330Sstevel@tonic-gate pcf8574_do_resume()
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	return (ret);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate static int
pcf8574_do_suspend()5410Sstevel@tonic-gate pcf8574_do_suspend()
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	return (ret);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate static int
pcf8574_do_detach(dev_info_t * dip)5490Sstevel@tonic-gate pcf8574_do_detach(dev_info_t *dip)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
5520Sstevel@tonic-gate 	int instance;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8574soft_statep, instance);
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (unitp == NULL) {
5590Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
5607656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
5610Sstevel@tonic-gate 		return (ENOMEM);
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	i2c_client_unregister(unitp->pcf8574_hdl);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	mutex_destroy(&unitp->pcf8574_mutex);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	ddi_soft_state_free(pcf8574soft_statep, instance);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	return (DDI_SUCCESS);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate }
575