17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
519397407SSherry Moore  * Common Development and Distribution License (the "License").
619397407SSherry Moore  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2219397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/stat.h>
287c478bd9Sstevel@tonic-gate #include <sys/file.h>
297c478bd9Sstevel@tonic-gate #include <sys/uio.h>
307c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
317c478bd9Sstevel@tonic-gate #include <sys/open.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/conf.h>
387c478bd9Sstevel@tonic-gate #include <sys/mode.h>
397c478bd9Sstevel@tonic-gate #include <sys/note.h>
407c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/i2c_gpio.h>
417c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/pca9556_impl.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * The PCA9556 is a gpio chip with 8 I/O ports.  The ports may be controlled by
457c478bd9Sstevel@tonic-gate  * an 8 bit input  port register, 8 bit output port register, 8 bit polarity
467c478bd9Sstevel@tonic-gate  * inversion register and an 8 bit configuration register.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * The input port register is a read only port and writes to this register
497c478bd9Sstevel@tonic-gate  * will have no effect regardless of whether the pin is an input or output.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * The output port register reflects the outgoing logic levels of the pins
527c478bd9Sstevel@tonic-gate  * defined as outputs by the configuration register.  Bit values in this
537c478bd9Sstevel@tonic-gate  * register have no effect on pins defined as inputs.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * The polarity register enables polarity inversion of pins defined as inputs by
567c478bd9Sstevel@tonic-gate  * the configuration register.  A set bit inverts the corresponding port's
577c478bd9Sstevel@tonic-gate  * polarity.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * The configuration register configures the directions of the I/O pins.  If a
607c478bd9Sstevel@tonic-gate  * bit is set the corresponding port is enabled as an input and if cleared,
617c478bd9Sstevel@tonic-gate  * as an output.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * The commands supported in the ioctl routine are:
647c478bd9Sstevel@tonic-gate  * GPIO_GET_INPUT	-- Read bits in the input port register.
657c478bd9Sstevel@tonic-gate  * GPIO_GET_OUTPUT	-- Read bits in the output port register.
667c478bd9Sstevel@tonic-gate  * GPIO_SET_OUPUT	-- Modify bits in the output port register.
677c478bd9Sstevel@tonic-gate  * GPIO_GET_POLARITY    -- Read bits in the polarity register.
687c478bd9Sstevel@tonic-gate  * GPIO_SET_POLARITY    -- Modify bits in the polarity register.
697c478bd9Sstevel@tonic-gate  * GPIO_GET_CONFIG	-- Read bits in the configuration register.
707c478bd9Sstevel@tonic-gate  * GPIO_SET_CONFIG	-- Modify bits in the configuration register.
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  * A pointer to the i2c_gpio_t data structure is sent as the third argument
737c478bd9Sstevel@tonic-gate  * in the ioctl call.  The reg_mask member identifies the bits that the user
747c478bd9Sstevel@tonic-gate  * wants to read or modify and reg_val has the actual value of the
757c478bd9Sstevel@tonic-gate  * corresponding bits set in reg_mask.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * To read a whole register the user has to set all the  bits in reg_mask
787c478bd9Sstevel@tonic-gate  * and the values will be copied into reg_val.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * In addition the pca9555 device has been added to this driver.  It is similar
817c478bd9Sstevel@tonic-gate  * to the pca9556 except that it has 2 8 bit I/O ports.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * cb ops
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate static int pca9556_open(dev_t *, int, int, cred_t *);
887c478bd9Sstevel@tonic-gate static int pca9556_close(dev_t, int, int, cred_t *);
897c478bd9Sstevel@tonic-gate static int pca9556_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * dev ops
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate static int pca9556_s_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
957c478bd9Sstevel@tonic-gate static int pca9556_s_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
967c478bd9Sstevel@tonic-gate static int pca9556_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static struct cb_ops pca9556_cb_ops = {
997c478bd9Sstevel@tonic-gate 	pca9556_open,			/* open */
1007c478bd9Sstevel@tonic-gate 	pca9556_close,			/* close */
1017c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
1027c478bd9Sstevel@tonic-gate 	nodev,				/* print */
1037c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
1047c478bd9Sstevel@tonic-gate 	nodev,				/* read */
1057c478bd9Sstevel@tonic-gate 	nodev,				/* write */
1067c478bd9Sstevel@tonic-gate 	pca9556_ioctl,			/* ioctl */
1077c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
1087c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
1097c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
1107c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
1117c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1127c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
1137c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static struct dev_ops pca9556_dev_ops = {
1177c478bd9Sstevel@tonic-gate 	DEVO_REV,
1187c478bd9Sstevel@tonic-gate 	0,
1197c478bd9Sstevel@tonic-gate 	pca9556_info,
1207c478bd9Sstevel@tonic-gate 	nulldev,
1217c478bd9Sstevel@tonic-gate 	nulldev,
1227c478bd9Sstevel@tonic-gate 	pca9556_s_attach,
1237c478bd9Sstevel@tonic-gate 	pca9556_s_detach,
1247c478bd9Sstevel@tonic-gate 	nodev,
1257c478bd9Sstevel@tonic-gate 	&pca9556_cb_ops,
12619397407SSherry Moore 	NULL,
12719397407SSherry Moore 	NULL,
12819397407SSherry Moore 	ddi_quiesce_not_supported,	/* devo_quiesce */
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static struct modldrv pca9556_modldrv = {
1327c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* type of module - driver */
13319397407SSherry Moore 	"pca9556 device driver",
1347c478bd9Sstevel@tonic-gate 	&pca9556_dev_ops,
1357c478bd9Sstevel@tonic-gate };
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static struct modlinkage pca9556_modlinkage = {
1387c478bd9Sstevel@tonic-gate 	MODREV_1,
1397c478bd9Sstevel@tonic-gate 	&pca9556_modldrv,
1407c478bd9Sstevel@tonic-gate 	0
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static void *pca9556_soft_statep;
1447c478bd9Sstevel@tonic-gate int pca9556_debug;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate int
_init(void)1477c478bd9Sstevel@tonic-gate _init(void)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	int    err;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	err = mod_install(&pca9556_modlinkage);
1527c478bd9Sstevel@tonic-gate 	if (err == 0) {
1537c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_init(&pca9556_soft_statep,
1547c478bd9Sstevel@tonic-gate 		    sizeof (pca9556_unit_t), PCA9556_MAX_SIZE);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	return (err);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
_fini(void)1607c478bd9Sstevel@tonic-gate _fini(void)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int    err;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	err = mod_remove(&pca9556_modlinkage);
1657c478bd9Sstevel@tonic-gate 	if (err == 0) {
1667c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&pca9556_soft_statep);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	return (err);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1727c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	return (mod_info(&pca9556_modlinkage, modinfop));
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static int
pca9556_resume(dev_info_t * dip)1787c478bd9Sstevel@tonic-gate pca9556_resume(dev_info_t *dip)
1797c478bd9Sstevel@tonic-gate {
180*fa4e72b0SToomas Soome 	int		instance = ddi_get_instance(dip);
1817c478bd9Sstevel@tonic-gate 	pca9556_unit_t	*pcap;
182*fa4e72b0SToomas Soome 	int		err = DDI_SUCCESS;
1837c478bd9Sstevel@tonic-gate 	int		reg_offset, num_of_ports;
1847c478bd9Sstevel@tonic-gate 	int		i, j;
1857c478bd9Sstevel@tonic-gate 	uint8_t		reg, reg_num = 0;
186afd7fd7bSosaeed 	extern int	do_polled_io;
187afd7fd7bSosaeed 	int		saved_pio;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	pcap = (pca9556_unit_t *)
1907c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(pca9556_soft_statep, instance);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (pcap == NULL)
1937c478bd9Sstevel@tonic-gate 		return (ENXIO);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * Restore registers to status existing before cpr
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_flags = I2C_WR;
1997c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_wlen = 2;
2007c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_rlen = 0;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (pcap->pca9555_device) {
2037c478bd9Sstevel@tonic-gate 		reg_offset = 2;
2047c478bd9Sstevel@tonic-gate 		num_of_ports = PCA9555_NUM_PORTS;
2057c478bd9Sstevel@tonic-gate 	} else {
2067c478bd9Sstevel@tonic-gate 		reg_offset = 1;
2077c478bd9Sstevel@tonic-gate 		num_of_ports = PCA9556_NUM_PORTS;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
210afd7fd7bSosaeed 	/*
211afd7fd7bSosaeed 	 * Since the parent node that handles interrupts may have already
212afd7fd7bSosaeed 	 * been suspended, perform the following i2c transfers in poll-mode.
213afd7fd7bSosaeed 	 */
214afd7fd7bSosaeed 	saved_pio = do_polled_io;
215afd7fd7bSosaeed 	do_polled_io = 1;
216afd7fd7bSosaeed 
2177c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_of_ports; i++) {
2187c478bd9Sstevel@tonic-gate 		if (pcap->pca9555_device)
2197c478bd9Sstevel@tonic-gate 			reg = PCA9555_OUTPUT_REG;
2207c478bd9Sstevel@tonic-gate 		else
2217c478bd9Sstevel@tonic-gate 			reg = PCA9556_OUTPUT_REG;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		for (j = 0; j < PCA9556_NUM_REG; j++) {
2247c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] = reg + i;
2257c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[1] =
2267c478bd9Sstevel@tonic-gate 			    pcap->pca9556_cpr_state[reg_num++];
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 			if (i2c_transfer(pcap->pca9556_hdl,
22919397407SSherry Moore 			    pcap->pca9556_transfer) != DDI_SUCCESS) {
2307c478bd9Sstevel@tonic-gate 				err = EIO;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 				goto done;
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 			reg = reg + reg_offset;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate done:
240afd7fd7bSosaeed 	do_polled_io = saved_pio;
2417c478bd9Sstevel@tonic-gate 	if (err != DDI_SUCCESS) {
2427c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s Unable to restore registers",
2437c478bd9Sstevel@tonic-gate 		    pcap->pca9556_name);
2447c478bd9Sstevel@tonic-gate 	}
245afd7fd7bSosaeed 
246afd7fd7bSosaeed 	/*
247afd7fd7bSosaeed 	 * Clear busy flag so that transactions may continue
248afd7fd7bSosaeed 	 */
2497c478bd9Sstevel@tonic-gate 	mutex_enter(&pcap->pca9556_mutex);
2507c478bd9Sstevel@tonic-gate 	pcap->pca9556_flags = pcap->pca9556_flags & ~PCA9556_BUSYFLAG;
2517c478bd9Sstevel@tonic-gate 	cv_broadcast(&pcap->pca9556_cv);
2527c478bd9Sstevel@tonic-gate 	mutex_exit(&pcap->pca9556_mutex);
2537c478bd9Sstevel@tonic-gate 	return (err);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static void
pca9556_detach(dev_info_t * dip)2577c478bd9Sstevel@tonic-gate pca9556_detach(dev_info_t *dip)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	pca9556_unit_t *pcap;
260*fa4e72b0SToomas Soome 	int		instance = ddi_get_instance(dip);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	pcap = ddi_get_soft_state(pca9556_soft_statep, instance);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if ((pcap->pca9556_flags & PCA9556_REGFLAG) == PCA9556_REGFLAG) {
2657c478bd9Sstevel@tonic-gate 		i2c_client_unregister(pcap->pca9556_hdl);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	if ((pcap->pca9556_flags & PCA9556_TBUFFLAG) == PCA9556_TBUFFLAG) {
2687c478bd9Sstevel@tonic-gate 		i2c_transfer_free(pcap->pca9556_hdl, pcap->pca9556_transfer);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	if ((pcap->pca9556_flags & PCA9556_MINORFLAG) == PCA9556_MINORFLAG) {
2717c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	cv_destroy(&pcap->pca9556_cv);
2747c478bd9Sstevel@tonic-gate 	mutex_destroy(&pcap->pca9556_mutex);
2757c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(pca9556_soft_statep, instance);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate static int
pca9556_attach(dev_info_t * dip)2807c478bd9Sstevel@tonic-gate pca9556_attach(dev_info_t *dip)
2817c478bd9Sstevel@tonic-gate {
282*fa4e72b0SToomas Soome 	pca9556_unit_t		*pcap;
283*fa4e72b0SToomas Soome 	int			instance = ddi_get_instance(dip);
2847c478bd9Sstevel@tonic-gate 	char			name[MAXNAMELEN];
2857c478bd9Sstevel@tonic-gate 	char *device_name;
286*fa4e72b0SToomas Soome 	minor_t			minor;
2877c478bd9Sstevel@tonic-gate 	int			i, num_ports;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pca9556_soft_statep, instance) != 0) {
2907c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d failed to zalloc softstate",
2917c478bd9Sstevel@tonic-gate 		    ddi_get_name(dip), instance);
2927c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	pcap = ddi_get_soft_state(pca9556_soft_statep, instance);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (pcap == NULL)
2987c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	mutex_init(&pcap->pca9556_mutex, NULL, MUTEX_DRIVER, NULL);
3017c478bd9Sstevel@tonic-gate 	cv_init(&pcap->pca9556_cv, NULL, CV_DRIVER, NULL);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	(void) snprintf(pcap->pca9556_name, sizeof (pcap->pca9556_name),
3047c478bd9Sstevel@tonic-gate 	    "%s_%d", ddi_driver_name(dip), instance);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	device_name = ddi_get_name(dip);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (strcmp(device_name, "i2c-pca9555") == 0) {
3097c478bd9Sstevel@tonic-gate 		num_ports = PCA9555_NUM_PORTS;
3107c478bd9Sstevel@tonic-gate 		pcap->pca9555_device = B_TRUE;
3117c478bd9Sstevel@tonic-gate 	} else {
3127c478bd9Sstevel@tonic-gate 		num_ports = PCA9556_NUM_PORTS;
3137c478bd9Sstevel@tonic-gate 		pcap->pca9555_device = B_FALSE;
3147c478bd9Sstevel@tonic-gate 		minor = INST_TO_MINOR(instance);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_ports; i++) {
3187c478bd9Sstevel@tonic-gate 		if (!(pcap->pca9555_device)) {
3197c478bd9Sstevel@tonic-gate 			(void) snprintf(pcap->pca9556_name,
3207c478bd9Sstevel@tonic-gate 			    sizeof (pcap->pca9556_name), "%s_%d",
3217c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), instance);
3227c478bd9Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%s",
3237c478bd9Sstevel@tonic-gate 			    pcap->pca9556_name);
3247c478bd9Sstevel@tonic-gate 		} else {
3257c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "port_%d", i);
3267c478bd9Sstevel@tonic-gate 			minor = INST_TO_MINOR(instance) |
3277c478bd9Sstevel@tonic-gate 			    PORT_TO_MINOR(I2C_PORT(i));
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor,
331*fa4e72b0SToomas Soome 		    PCA9556_NODE_TYPE, 0) == DDI_FAILURE) {
3327c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: failed to create node for %s",
3337c478bd9Sstevel@tonic-gate 			    pcap->pca9556_name, name);
3347c478bd9Sstevel@tonic-gate 			pca9556_detach(dip);
3357c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 	pcap->pca9556_flags |= PCA9556_MINORFLAG;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * Add a zero-length attribute to tell the world we support
3427c478bd9Sstevel@tonic-gate 	 * kernel ioctls (for layered drivers)
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
3457c478bd9Sstevel@tonic-gate 	    DDI_KERNEL_IOCTL, NULL, 0);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * preallocate a single buffer for all reads and writes
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	if (i2c_transfer_alloc(pcap->pca9556_hdl, &pcap->pca9556_transfer,
3527c478bd9Sstevel@tonic-gate 	    2, 2, I2C_SLEEP) != I2C_SUCCESS) {
3537c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s i2c_transfer_alloc failed",
3547c478bd9Sstevel@tonic-gate 		    pcap->pca9556_name);
3557c478bd9Sstevel@tonic-gate 		pca9556_detach(dip);
3567c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	pcap->pca9556_flags |= PCA9556_TBUFFLAG;
3597c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_version = I2C_XFER_REV;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &pcap->pca9556_hdl) != I2C_SUCCESS) {
3627c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
3637c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s i2c_client_register failed",
3647c478bd9Sstevel@tonic-gate 		    pcap->pca9556_name);
3657c478bd9Sstevel@tonic-gate 		pca9556_detach(dip);
3667c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	pcap->pca9556_flags |= PCA9556_REGFLAG;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * Store the dip for future dip.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	pcap->pca9556_dip = dip;
3747c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate static int
pca9556_info(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3797c478bd9Sstevel@tonic-gate pca9556_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	pca9556_unit_t	*pcap;
3847c478bd9Sstevel@tonic-gate 	int		instance = MINOR_TO_INST(getminor((dev_t)arg));
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	switch (cmd) {
3877c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3887c478bd9Sstevel@tonic-gate 		pcap = ddi_get_soft_state(pca9556_soft_statep, instance);
3897c478bd9Sstevel@tonic-gate 		if (pcap == NULL)
3907c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3917c478bd9Sstevel@tonic-gate 		*result = (void *)pcap->pca9556_dip;
3927c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3937c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
394f47a9c50Smathue 		*result = (void *)(uintptr_t)instance;
3957c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3967c478bd9Sstevel@tonic-gate 	default:
3977c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static int
pca9556_s_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4027c478bd9Sstevel@tonic-gate pca9556_s_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	switch (cmd) {
4057c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4067c478bd9Sstevel@tonic-gate 		return (pca9556_attach(dip));
4077c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
4087c478bd9Sstevel@tonic-gate 		return (pca9556_resume(dip));
4097c478bd9Sstevel@tonic-gate 	default:
4107c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate static int
pca9556_suspend(dev_info_t * dip)4157c478bd9Sstevel@tonic-gate pca9556_suspend(dev_info_t *dip)
4167c478bd9Sstevel@tonic-gate {
417*fa4e72b0SToomas Soome 	pca9556_unit_t	*pcap;
418*fa4e72b0SToomas Soome 	int		instance = ddi_get_instance(dip);
4197c478bd9Sstevel@tonic-gate 	int		err = DDI_SUCCESS;
4207c478bd9Sstevel@tonic-gate 	int		reg_offset, num_of_ports;
4217c478bd9Sstevel@tonic-gate 	int		i, j;
4227c478bd9Sstevel@tonic-gate 	uint8_t		reg, reg_num = 0;
423afd7fd7bSosaeed 	extern int	do_polled_io;
424afd7fd7bSosaeed 	int		saved_pio;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	pcap = ddi_get_soft_state(pca9556_soft_statep, instance);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	mutex_enter(&pcap->pca9556_mutex);
4297c478bd9Sstevel@tonic-gate 	while ((pcap->pca9556_flags & PCA9556_BUSYFLAG) == PCA9556_BUSYFLAG) {
4307c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&pcap->pca9556_cv,
4317c478bd9Sstevel@tonic-gate 		    &pcap->pca9556_mutex) <= 0) {
4327c478bd9Sstevel@tonic-gate 			mutex_exit(&pcap->pca9556_mutex);
4337c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	pcap->pca9556_flags |= PCA9556_BUSYFLAG;
4377c478bd9Sstevel@tonic-gate 	mutex_exit(&pcap->pca9556_mutex);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/*
4407c478bd9Sstevel@tonic-gate 	 * A pca9555 devices command registers are offset by 2 and it has 2
4417c478bd9Sstevel@tonic-gate 	 * ports to save. A pca9556 devices command registers are offset by 1
4427c478bd9Sstevel@tonic-gate 	 * while it only has one "port"
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	if (pcap->pca9555_device) {
4457c478bd9Sstevel@tonic-gate 		reg_offset = 2;
4467c478bd9Sstevel@tonic-gate 		num_of_ports = PCA9555_NUM_PORTS;
4477c478bd9Sstevel@tonic-gate 	} else {
4487c478bd9Sstevel@tonic-gate 		reg_offset = 1;
4497c478bd9Sstevel@tonic-gate 		num_of_ports = PCA9556_NUM_PORTS;
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Save the state of the registers
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_flags = I2C_WR_RD;
4557c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_wlen = 1;
4567c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_rlen = 1;
4577c478bd9Sstevel@tonic-gate 
458afd7fd7bSosaeed 	/*
459afd7fd7bSosaeed 	 * Since the parent node that handles interrupts may have not been
460afd7fd7bSosaeed 	 * resumed yet, perform the following i2c transfers in poll-mode.
461afd7fd7bSosaeed 	 */
462afd7fd7bSosaeed 	saved_pio = do_polled_io;
463afd7fd7bSosaeed 	do_polled_io = 1;
464afd7fd7bSosaeed 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * The following for loop will run through once for a pca9556 device
4677c478bd9Sstevel@tonic-gate 	 * and twice for a pca9555 device. i will represent the port number
4687c478bd9Sstevel@tonic-gate 	 * for the pca9555.
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_of_ports; i++) {
4717c478bd9Sstevel@tonic-gate 		/*
4727c478bd9Sstevel@tonic-gate 		 * We set the first Register here so it can be reset if we
4737c478bd9Sstevel@tonic-gate 		 * loop through (pca9555 device).
4747c478bd9Sstevel@tonic-gate 		 */
4757c478bd9Sstevel@tonic-gate 		if (pcap->pca9555_device)
4767c478bd9Sstevel@tonic-gate 			reg = PCA9555_OUTPUT_REG;
4777c478bd9Sstevel@tonic-gate 		else
4787c478bd9Sstevel@tonic-gate 			reg = PCA9556_OUTPUT_REG;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		/* We run through this loop 3 times. Once for each register */
4817c478bd9Sstevel@tonic-gate 		for (j = 0; j < PCA9556_NUM_REG; j++) {
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 			/*
4847c478bd9Sstevel@tonic-gate 			 * We add the port number (0 for pca9556, 0 or 1 for
4857c478bd9Sstevel@tonic-gate 			 * a pca9555) to the register.
4867c478bd9Sstevel@tonic-gate 			 */
4877c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] = reg + i;
4887c478bd9Sstevel@tonic-gate 			if (i2c_transfer(pcap->pca9556_hdl,
48919397407SSherry Moore 			    pcap->pca9556_transfer) != DDI_SUCCESS) {
4907c478bd9Sstevel@tonic-gate 				err = EIO;
4917c478bd9Sstevel@tonic-gate 				goto done;
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 			pcap->pca9556_cpr_state[reg_num++] =
4957c478bd9Sstevel@tonic-gate 			    pcap->pca9556_transfer->i2c_rbuf[0];
4967c478bd9Sstevel@tonic-gate 			/*
4977c478bd9Sstevel@tonic-gate 			 * The register is then added to the offset and saved
4987c478bd9Sstevel@tonic-gate 			 * to go and read the next command register.
4997c478bd9Sstevel@tonic-gate 			 */
5007c478bd9Sstevel@tonic-gate 			reg = reg + reg_offset;
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate done:
505afd7fd7bSosaeed 	do_polled_io = saved_pio;
5067c478bd9Sstevel@tonic-gate 	if (err != DDI_SUCCESS) {
5077c478bd9Sstevel@tonic-gate 		mutex_enter(&pcap->pca9556_mutex);
5087c478bd9Sstevel@tonic-gate 		pcap->pca9556_flags = pcap->pca9556_flags & ~PCA9556_BUSYFLAG;
5097c478bd9Sstevel@tonic-gate 		cv_broadcast(&pcap->pca9556_cv);
5107c478bd9Sstevel@tonic-gate 		mutex_exit(&pcap->pca9556_mutex);
5117c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s Suspend failed, unable to save registers",
5127c478bd9Sstevel@tonic-gate 		    pcap->pca9556_name);
5137c478bd9Sstevel@tonic-gate 		return (err);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate static int
pca9556_s_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)5197c478bd9Sstevel@tonic-gate pca9556_s_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	switch (cmd) {
5227c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
5237c478bd9Sstevel@tonic-gate 		pca9556_detach(dip);
5247c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5257c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
5267c478bd9Sstevel@tonic-gate 		return (pca9556_suspend(dip));
5277c478bd9Sstevel@tonic-gate 	default:
5287c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate static int
pca9556_open(dev_t * devp,int flags,int otyp,cred_t * credp)5337c478bd9Sstevel@tonic-gate pca9556_open(dev_t *devp, int flags, int otyp, cred_t *credp)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	int			instance;
5367c478bd9Sstevel@tonic-gate 	pca9556_unit_t		*pcap;
5377c478bd9Sstevel@tonic-gate 	int			err = EBUSY;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/*
5407c478bd9Sstevel@tonic-gate 	 * Make sure the open is for the right file type
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
5437c478bd9Sstevel@tonic-gate 		return (EINVAL);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(*devp));
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	pcap = (pca9556_unit_t *)
5487c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(pca9556_soft_statep, instance);
5497c478bd9Sstevel@tonic-gate 	if (pcap == NULL)
5507c478bd9Sstevel@tonic-gate 		return (ENXIO);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* must be privileged to access this device */
5537c478bd9Sstevel@tonic-gate 	if (drv_priv(credp) != 0)
5547c478bd9Sstevel@tonic-gate 		return (EPERM);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	/*
5577c478bd9Sstevel@tonic-gate 	 * Enforce exclusive access if required
5587c478bd9Sstevel@tonic-gate 	 */
5597c478bd9Sstevel@tonic-gate 	mutex_enter(&pcap->pca9556_mutex);
5607c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
5617c478bd9Sstevel@tonic-gate 		if (pcap->pca9556_oflag == 0) {
5627c478bd9Sstevel@tonic-gate 			pcap->pca9556_oflag = FEXCL;
5637c478bd9Sstevel@tonic-gate 			err = DDI_SUCCESS;
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 	} else if (pcap->pca9556_oflag != FEXCL) {
566f47a9c50Smathue 		pcap->pca9556_oflag = (uint16_t)FOPEN;
5677c478bd9Sstevel@tonic-gate 		err = DDI_SUCCESS;
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 	mutex_exit(&pcap->pca9556_mutex);
5707c478bd9Sstevel@tonic-gate 	return (err);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate static int
pca9556_close(dev_t dev,int flags,int otyp,cred_t * credp)5747c478bd9Sstevel@tonic-gate pca9556_close(dev_t dev, int flags, int otyp, cred_t *credp)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	int		instance;
577*fa4e72b0SToomas Soome 	pca9556_unit_t	*pcap;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, credp))
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/*
5827c478bd9Sstevel@tonic-gate 	 * Make sure the close is for the right file type
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
5857c478bd9Sstevel@tonic-gate 		return (EINVAL);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	pcap = (pca9556_unit_t *)
5907c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(pca9556_soft_statep, instance);
5917c478bd9Sstevel@tonic-gate 	if (pcap == NULL)
5927c478bd9Sstevel@tonic-gate 		return (ENXIO);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	mutex_enter(&pcap->pca9556_mutex);
5957c478bd9Sstevel@tonic-gate 	pcap->pca9556_oflag = 0;
5967c478bd9Sstevel@tonic-gate 	mutex_exit(&pcap->pca9556_mutex);
5977c478bd9Sstevel@tonic-gate 	return (0);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate static int
pca9556_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)6017c478bd9Sstevel@tonic-gate pca9556_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
602*fa4e72b0SToomas Soome     int *rvalp)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	pca9556_unit_t		*pcap;
6057c478bd9Sstevel@tonic-gate 	int			err = 0;
6067c478bd9Sstevel@tonic-gate 	int			instance = MINOR_TO_INST(getminor(dev));
6077c478bd9Sstevel@tonic-gate 	int			port;
6087c478bd9Sstevel@tonic-gate 	i2c_gpio_t		g_buf;
6097c478bd9Sstevel@tonic-gate 	uchar_t			temp;
6107c478bd9Sstevel@tonic-gate 	boolean_t		write_io = B_FALSE;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	pcap = (pca9556_unit_t *)
6157c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(pca9556_soft_statep, instance);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (pcap->pca9555_device) {
6187c478bd9Sstevel@tonic-gate 		port =  MINOR_TO_PORT(getminor(dev));
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	if (pca9556_debug) {
6217c478bd9Sstevel@tonic-gate 		prom_printf("pca9556_ioctl: instance=%d\n", instance);
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/*
6257c478bd9Sstevel@tonic-gate 	 * We serialize here and  block any pending transacations.
6267c478bd9Sstevel@tonic-gate 	 */
6277c478bd9Sstevel@tonic-gate 	mutex_enter(&pcap->pca9556_mutex);
6287c478bd9Sstevel@tonic-gate 	while ((pcap->pca9556_flags & PCA9556_BUSYFLAG) == PCA9556_BUSYFLAG) {
6297c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&pcap->pca9556_cv,
6307c478bd9Sstevel@tonic-gate 		    &pcap->pca9556_mutex) <= 0) {
6317c478bd9Sstevel@tonic-gate 			mutex_exit(&pcap->pca9556_mutex);
6327c478bd9Sstevel@tonic-gate 			return (EINTR);
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	pcap->pca9556_flags |= PCA9556_BUSYFLAG;
6367c478bd9Sstevel@tonic-gate 	mutex_exit(&pcap->pca9556_mutex);
6377c478bd9Sstevel@tonic-gate 	if (ddi_copyin((caddr_t)arg, &g_buf,
6387c478bd9Sstevel@tonic-gate 	    sizeof (i2c_gpio_t), mode) != DDI_SUCCESS) {
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		err = EFAULT;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		goto cleanup;
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_flags = I2C_WR_RD;
6457c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_wlen = 1;
6467c478bd9Sstevel@tonic-gate 	pcap->pca9556_transfer->i2c_rlen = 1;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * Evaluate which register is to be read or modified
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	switch (cmd) {
6537c478bd9Sstevel@tonic-gate 	case GPIO_GET_INPUT:
6547c478bd9Sstevel@tonic-gate 		if (pcap->pca9555_device)
6557c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] =
6567c478bd9Sstevel@tonic-gate 			    PCA9555_INPUT_REG + port;
6577c478bd9Sstevel@tonic-gate 		else
6587c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] =
6597c478bd9Sstevel@tonic-gate 			    PCA9556_INPUT_REG;
6607c478bd9Sstevel@tonic-gate 		break;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	case GPIO_SET_OUTPUT:
6637c478bd9Sstevel@tonic-gate 		write_io = B_TRUE;
6647c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	case GPIO_GET_OUTPUT:
6677c478bd9Sstevel@tonic-gate 		if (pcap->pca9555_device)
6687c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] =
6697c478bd9Sstevel@tonic-gate 			    PCA9555_OUTPUT_REG + port;
6707c478bd9Sstevel@tonic-gate 		else
6717c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] =
6727c478bd9Sstevel@tonic-gate 			    PCA9556_OUTPUT_REG;
6737c478bd9Sstevel@tonic-gate 		break;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	case GPIO_SET_POLARITY:
6767c478bd9Sstevel@tonic-gate 		write_io = B_TRUE;
6777c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	case GPIO_GET_POLARITY:
6807c478bd9Sstevel@tonic-gate 		if (pcap->pca9555_device)
6817c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] =
6827c478bd9Sstevel@tonic-gate 			    PCA9555_POLARITY_REG + port;
6837c478bd9Sstevel@tonic-gate 		else
6847c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] =
6857c478bd9Sstevel@tonic-gate 			    PCA9556_POLARITY_REG;
6867c478bd9Sstevel@tonic-gate 		break;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	case GPIO_SET_CONFIG:
6897c478bd9Sstevel@tonic-gate 		write_io = B_TRUE;
6907c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	case GPIO_GET_CONFIG:
6937c478bd9Sstevel@tonic-gate 		if (pcap->pca9555_device)
6947c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] =
6957c478bd9Sstevel@tonic-gate 			    PCA9555_CONFIG_REG + port;
6967c478bd9Sstevel@tonic-gate 		else
6977c478bd9Sstevel@tonic-gate 			pcap->pca9556_transfer->i2c_wbuf[0] =
6987c478bd9Sstevel@tonic-gate 			    PCA9556_CONFIG_REG;
6997c478bd9Sstevel@tonic-gate 		break;
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * Read the required register
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 	if (i2c_transfer(pcap->pca9556_hdl, pcap->pca9556_transfer)
7067c478bd9Sstevel@tonic-gate 	    != I2C_SUCCESS) {
7077c478bd9Sstevel@tonic-gate 		err = EIO;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		goto cleanup;
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 	/*
7127c478bd9Sstevel@tonic-gate 	 * Evaluate whether the register is to be read or modified
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	if (!write_io) {
7157c478bd9Sstevel@tonic-gate 		g_buf.reg_val = g_buf.reg_mask &
7167c478bd9Sstevel@tonic-gate 		    pcap->pca9556_transfer->i2c_rbuf[0];
7177c478bd9Sstevel@tonic-gate 		err = ddi_copyout(&g_buf, (caddr_t)arg,
7187c478bd9Sstevel@tonic-gate 		    sizeof (i2c_gpio_t), mode);
7197c478bd9Sstevel@tonic-gate 	} else {
7207c478bd9Sstevel@tonic-gate 		pcap->pca9556_transfer->i2c_flags = I2C_WR;
7217c478bd9Sstevel@tonic-gate 		pcap->pca9556_transfer->i2c_wlen = 2;
7227c478bd9Sstevel@tonic-gate 		pcap->pca9556_transfer->i2c_rlen = 0;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		/*
7257c478bd9Sstevel@tonic-gate 		 * Modify register without overwriting existing contents
7267c478bd9Sstevel@tonic-gate 		 */
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		temp = pcap->pca9556_transfer->i2c_rbuf[0] & (~g_buf.reg_mask);
7297c478bd9Sstevel@tonic-gate 		pcap->pca9556_transfer->i2c_wbuf[1] = temp|
7307c478bd9Sstevel@tonic-gate 		    (g_buf.reg_val & g_buf.reg_mask);
7317c478bd9Sstevel@tonic-gate 		if (i2c_transfer(pcap->pca9556_hdl, pcap->pca9556_transfer)
7327c478bd9Sstevel@tonic-gate 		    != I2C_SUCCESS) {
7337c478bd9Sstevel@tonic-gate 				err = EIO;
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate cleanup:
7387c478bd9Sstevel@tonic-gate 	mutex_enter(&pcap->pca9556_mutex);
7397c478bd9Sstevel@tonic-gate 	pcap->pca9556_flags  = pcap->pca9556_flags & ~PCA9556_BUSYFLAG;
7407c478bd9Sstevel@tonic-gate 	cv_signal(&pcap->pca9556_cv);
7417c478bd9Sstevel@tonic-gate 	mutex_exit(&pcap->pca9556_mutex);
7427c478bd9Sstevel@tonic-gate 	return (err);
7437c478bd9Sstevel@tonic-gate 	}
744