xref: /onnv-gate/usr/src/uts/sun4u/io/i2c/clients/pcf8591.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/pcf8591.h>
380Sstevel@tonic-gate #include <sys/i2c/clients/pcf8591_impl.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static void *pcf8591soft_statep;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static uint8_t ipmode = PCF8591_4SINGLE;
430Sstevel@tonic-gate static int32_t current_value = 0;
440Sstevel@tonic-gate static int current_set_flag = 0;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static int pcf8591_do_attach(dev_info_t *);
470Sstevel@tonic-gate static int pcf8591_do_detach(dev_info_t *);
480Sstevel@tonic-gate static int pcf8591_do_resume(void);
490Sstevel@tonic-gate static int pcf8591_do_suspend(void);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * cb ops (only need ioctl)
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate static int pcf8591_open(dev_t *, int, int, cred_t *);
550Sstevel@tonic-gate static int pcf8591_close(dev_t, int, int, cred_t *);
560Sstevel@tonic-gate static int pcf8591_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static struct cb_ops pcf8591_cbops = {
590Sstevel@tonic-gate 	pcf8591_open,			/* open  */
600Sstevel@tonic-gate 	pcf8591_close,			/* close */
610Sstevel@tonic-gate 	nodev,				/* strategy */
620Sstevel@tonic-gate 	nodev,				/* print */
630Sstevel@tonic-gate 	nodev,				/* dump */
640Sstevel@tonic-gate 	nodev,				/* read */
650Sstevel@tonic-gate 	nodev,				/* write */
660Sstevel@tonic-gate 	pcf8591_ioctl,			/* ioctl */
670Sstevel@tonic-gate 	nodev,				/* devmap */
680Sstevel@tonic-gate 	nodev,				/* mmap */
690Sstevel@tonic-gate 	nodev,				/* segmap */
700Sstevel@tonic-gate 	nochpoll,			/* poll */
710Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
720Sstevel@tonic-gate 	NULL,				/* streamtab */
730Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
740Sstevel@tonic-gate 	CB_REV,				/* rev */
750Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
760Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
770Sstevel@tonic-gate };
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * dev ops
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate static int pcf8591_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
830Sstevel@tonic-gate 		void **result);
840Sstevel@tonic-gate static int pcf8591_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
850Sstevel@tonic-gate static int pcf8591_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static struct dev_ops pcf8591_ops = {
880Sstevel@tonic-gate 	DEVO_REV,
890Sstevel@tonic-gate 	0,
900Sstevel@tonic-gate 	pcf8591_info,
910Sstevel@tonic-gate 	nulldev,
920Sstevel@tonic-gate 	nulldev,
930Sstevel@tonic-gate 	pcf8591_attach,
940Sstevel@tonic-gate 	pcf8591_detach,
950Sstevel@tonic-gate 	nodev,
960Sstevel@tonic-gate 	&pcf8591_cbops,
977656SSherry.Moore@Sun.COM 	NULL,
987656SSherry.Moore@Sun.COM 	NULL,
997656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static struct modldrv pcf8591_modldrv = {
1050Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
106*7696SRichard.Bean@Sun.COM 	"PCF8591 i2c device driver",
1070Sstevel@tonic-gate 	&pcf8591_ops
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static struct modlinkage pcf8591_modlinkage = {
1110Sstevel@tonic-gate 	MODREV_1,
1120Sstevel@tonic-gate 	&pcf8591_modldrv,
1130Sstevel@tonic-gate 	0
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate int
_init(void)1180Sstevel@tonic-gate _init(void)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	int error;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	error = mod_install(&pcf8591_modlinkage);
1230Sstevel@tonic-gate 	if (!error)
1240Sstevel@tonic-gate 		(void) ddi_soft_state_init(&pcf8591soft_statep,
1257656SSherry.Moore@Sun.COM 		    sizeof (struct pcf8591_unit), 1);
1260Sstevel@tonic-gate 	return (error);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate int
_fini(void)1300Sstevel@tonic-gate _fini(void)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	int error;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	error = mod_remove(&pcf8591_modlinkage);
1350Sstevel@tonic-gate 	if (!error)
1360Sstevel@tonic-gate 		ddi_soft_state_fini(&pcf8591soft_statep);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	return (error);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1420Sstevel@tonic-gate _info(struct modinfo *modinfop)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	return (mod_info(&pcf8591_modlinkage, modinfop));
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static int
pcf8591_open(dev_t * devp,int flags,int otyp,cred_t * credp)1480Sstevel@tonic-gate pcf8591_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	struct pcf8591_unit *unitp;
1530Sstevel@tonic-gate 	int instance;
1540Sstevel@tonic-gate 	int error = 0;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(*devp));
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (instance < 0) {
1590Sstevel@tonic-gate 		return (ENXIO);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	unitp = (struct pcf8591_unit *)
1637656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8591soft_statep, instance);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (unitp == NULL) {
1660Sstevel@tonic-gate 		return (ENXIO);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
1700Sstevel@tonic-gate 		return (EINVAL);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8591_mutex);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (flags & FEXCL) {
1760Sstevel@tonic-gate 		if (unitp->pcf8591_oflag != 0) {
1770Sstevel@tonic-gate 			error = EBUSY;
1780Sstevel@tonic-gate 		} else {
1790Sstevel@tonic-gate 			unitp->pcf8591_oflag = FEXCL;
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 	} else {
1820Sstevel@tonic-gate 		if (unitp->pcf8591_oflag == FEXCL) {
1830Sstevel@tonic-gate 			error = EBUSY;
1840Sstevel@tonic-gate 		} else {
1850Sstevel@tonic-gate 			unitp->pcf8591_oflag = FOPEN;
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8591_mutex);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return (error);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int
pcf8591_close(dev_t dev,int flags,int otyp,cred_t * credp)1950Sstevel@tonic-gate pcf8591_close(dev_t dev, int flags, int otyp, cred_t *credp)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	struct pcf8591_unit *unitp;
2000Sstevel@tonic-gate 	int instance;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	if (instance < 0) {
2050Sstevel@tonic-gate 		return (ENXIO);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	unitp = (struct pcf8591_unit *)
2097656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8591soft_statep, instance);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (unitp == NULL) {
2120Sstevel@tonic-gate 		return (ENXIO);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8591_mutex);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	unitp->pcf8591_oflag = 0;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8591_mutex);
2200Sstevel@tonic-gate 	return (DDI_SUCCESS);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static int
pcf8591_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2240Sstevel@tonic-gate pcf8591_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
2250Sstevel@tonic-gate 		cred_t *credp, int *rvalp)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	struct pcf8591_unit	*unitp;
2300Sstevel@tonic-gate 	int			err = 0;
2310Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2320Sstevel@tonic-gate 	int port = MINOR_TO_PORT(getminor(dev));
2330Sstevel@tonic-gate 	int instance = MINOR_TO_INST(getminor(dev));
2340Sstevel@tonic-gate 	int autoincr = 0;
2350Sstevel@tonic-gate 	uchar_t control, reg;
2360Sstevel@tonic-gate 	int32_t value;
2370Sstevel@tonic-gate 	uint8_t uvalue;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (arg == NULL) {
2400Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "PCF8591: ioctl: arg passed in to ioctl "
2417656SSherry.Moore@Sun.COM 		    "= NULL\n"));
2420Sstevel@tonic-gate 		err = EINVAL;
2430Sstevel@tonic-gate 		return (err);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	unitp = (struct pcf8591_unit *)
2467656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pcf8591soft_statep, instance);
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (unitp == NULL) {
2490Sstevel@tonic-gate 		cmn_err(CE_WARN, "PCF8591: ioctl: unitp not filled\n");
2500Sstevel@tonic-gate 		return (ENOMEM);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8591_mutex);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: ioctl: port = %d  instance = %d\n",
2567656SSherry.Moore@Sun.COM 	    unitp->pcf8591_name, port, instance));
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	switch (cmd) {
2590Sstevel@tonic-gate 	case I2C_GET_INPUT:
2600Sstevel@tonic-gate 		(void) i2c_transfer_alloc(unitp->pcf8591_hdl, &i2c_tran_pointer,
2617656SSherry.Moore@Sun.COM 		    1, 2, I2C_SLEEP);
2620Sstevel@tonic-gate 		if (i2c_tran_pointer == NULL) {
2630Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_INPUT "
2647656SSherry.Moore@Sun.COM 			    "i2c_tran_pointer not allocated\n",
2657656SSherry.Moore@Sun.COM 			    unitp->pcf8591_name));
2660Sstevel@tonic-gate 			err = ENOMEM;
2670Sstevel@tonic-gate 			break;
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 		reg = (uchar_t)port;
2700Sstevel@tonic-gate 		if ((reg == 0x02) && (ipmode == PCF8591_2DIFF)) {
2710Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_INPUT "
2727656SSherry.Moore@Sun.COM 			    "cannot use port 2 when ipmode is "
2737656SSherry.Moore@Sun.COM 			    "0x03\n", unitp->pcf8591_name));
2740Sstevel@tonic-gate 			err = EIO;
2750Sstevel@tonic-gate 			i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
2760Sstevel@tonic-gate 			break;
2770Sstevel@tonic-gate 		}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		if ((reg == 0x03) && (ipmode != PCF8591_4SINGLE)) {
2800Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_INPUT "
2817656SSherry.Moore@Sun.COM 			    "cannot use port 3 when ipmode is not "
2827656SSherry.Moore@Sun.COM 			    "0x00\n", unitp->pcf8591_name));
2830Sstevel@tonic-gate 			err = EIO;
2840Sstevel@tonic-gate 			i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
2850Sstevel@tonic-gate 			break;
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 		control = ((0 << PCF8591_ANALOG_OUTPUT_SHIFT) |
2887656SSherry.Moore@Sun.COM 		    (ipmode << PCF8591_ANALOG_INPUT_SHIFT) |
2897656SSherry.Moore@Sun.COM 		    (autoincr << PCF8591_AUTOINCR_SHIFT) | reg);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 		i2c_tran_pointer->i2c_flags = I2C_WR_RD;
2920Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[0] = control;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		err = i2c_transfer(unitp->pcf8591_hdl, i2c_tran_pointer);
2950Sstevel@tonic-gate 		if (err) {
2960Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_INPUT"
2977656SSherry.Moore@Sun.COM 			    " i2c_transfer routine\n",
2987656SSherry.Moore@Sun.COM 			    unitp->pcf8591_name));
2990Sstevel@tonic-gate 			i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
3000Sstevel@tonic-gate 			break;
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 		i2c_tran_pointer->i2c_rbuf[0] = i2c_tran_pointer->i2c_rbuf[1];
3030Sstevel@tonic-gate 		value = i2c_tran_pointer->i2c_rbuf[0];
3040Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: Back from transfer result is %x\n",
3057656SSherry.Moore@Sun.COM 		    unitp->pcf8591_name, value));
3060Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&value,
3077656SSherry.Moore@Sun.COM 		    (caddr_t)arg,
3087656SSherry.Moore@Sun.COM 		    sizeof (int32_t), mode) != DDI_SUCCESS) {
3090Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed I2C_GET_INPUT"
3107656SSherry.Moore@Sun.COM 			    " ddi_copyout routine\n",
3117656SSherry.Moore@Sun.COM 			    unitp->pcf8591_name));
3120Sstevel@tonic-gate 			err = EFAULT;
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
3150Sstevel@tonic-gate 		break;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	case I2C_SET_OUTPUT:
3180Sstevel@tonic-gate 		reg = (uchar_t)port;
3190Sstevel@tonic-gate 		if (ipmode != PCF8591_4SINGLE) {
3200Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_OUTPUT "
3217656SSherry.Moore@Sun.COM 			    "cannot set output when ipmode is not "
3227656SSherry.Moore@Sun.COM 			    "0x00\n", unitp->pcf8591_name));
3230Sstevel@tonic-gate 			err = EIO;
3240Sstevel@tonic-gate 			break;
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		(void) i2c_transfer_alloc(unitp->pcf8591_hdl, &i2c_tran_pointer,
3287656SSherry.Moore@Sun.COM 		    2, 0, I2C_SLEEP);
3290Sstevel@tonic-gate 		if (i2c_tran_pointer == NULL) {
3300Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in "
3317656SSherry.Moore@Sun.COM 			    "I2C_SET_OUTPUT "
3327656SSherry.Moore@Sun.COM 			    "i2c_tran_pointer not allocated\n",
3337656SSherry.Moore@Sun.COM 			    unitp->pcf8591_name));
3340Sstevel@tonic-gate 			err = ENOMEM;
3350Sstevel@tonic-gate 			break;
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&value,
3387656SSherry.Moore@Sun.COM 		    sizeof (int32_t), mode) != DDI_SUCCESS) {
3390Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_OUTPUT"
3407656SSherry.Moore@Sun.COM 			    " ddi_copyout routine\n",
3417656SSherry.Moore@Sun.COM 			    unitp->pcf8591_name));
3420Sstevel@tonic-gate 			err = EFAULT;
3430Sstevel@tonic-gate 			i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
3440Sstevel@tonic-gate 			break;
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 		control = ((1 << PCF8591_ANALOG_OUTPUT_SHIFT) |
3477656SSherry.Moore@Sun.COM 		    (0 << PCF8591_ANALOG_INPUT_SHIFT) |
3487656SSherry.Moore@Sun.COM 		    (autoincr << PCF8591_AUTOINCR_SHIFT) | reg);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		i2c_tran_pointer->i2c_flags = I2C_WR;
3510Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[0] = control;
3520Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[1] = (uchar_t)value;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		err = i2c_transfer(unitp->pcf8591_hdl, i2c_tran_pointer);
3550Sstevel@tonic-gate 		if (!err) {
3560Sstevel@tonic-gate 			current_value = value;
3570Sstevel@tonic-gate 			current_set_flag = 1;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
3600Sstevel@tonic-gate 		break;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	case I2C_GET_OUTPUT:
3630Sstevel@tonic-gate 		if (current_set_flag == 0) {
3640Sstevel@tonic-gate 			err = EIO;
3650Sstevel@tonic-gate 			break;
3660Sstevel@tonic-gate 		} else {
367946Smathue 			if (ddi_copyout((caddr_t)(uintptr_t)current_value,
368946Smathue 			    (caddr_t)arg, sizeof (int32_t), mode)
369946Smathue 			    != DDI_SUCCESS) {
3700Sstevel@tonic-gate 				D2CMN_ERR((CE_WARN, "%s: Failed in "
3717656SSherry.Moore@Sun.COM 				    "I2C_GET_OUTPUT ddi_copyout routine\n",
3727656SSherry.Moore@Sun.COM 				    unitp->pcf8591_name));
3730Sstevel@tonic-gate 				err = EFAULT;
3740Sstevel@tonic-gate 				break;
3750Sstevel@tonic-gate 			}
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 		break;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	case PCF8591_SET_IPMODE:
3800Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&uvalue,
3817656SSherry.Moore@Sun.COM 		    sizeof (uint_t), mode) != DDI_SUCCESS) {
3820Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in PCF8591_SET_IPMODE"
3837656SSherry.Moore@Sun.COM 			    " ddi_copyout routine\n",
3847656SSherry.Moore@Sun.COM 			    unitp->pcf8591_name));
3850Sstevel@tonic-gate 			err = EFAULT;
3860Sstevel@tonic-gate 			break;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		if (uvalue > 0x03) {
3900Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in PCF8591_SET_IPMODE"
3917656SSherry.Moore@Sun.COM 			    " value is not a valid mode\n",
3927656SSherry.Moore@Sun.COM 			    unitp->pcf8591_name));
3930Sstevel@tonic-gate 			err = EIO;
3940Sstevel@tonic-gate 			break;
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		ipmode = uvalue;
3980Sstevel@tonic-gate 		break;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	default:
4010Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n",
4027656SSherry.Moore@Sun.COM 		    unitp->pcf8591_name, cmd));
4030Sstevel@tonic-gate 		err = EINVAL;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8591_mutex);
4070Sstevel@tonic-gate 	return (err);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /* ARGSUSED */
4120Sstevel@tonic-gate static int
pcf8591_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)4130Sstevel@tonic-gate pcf8591_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	dev_t	dev;
4160Sstevel@tonic-gate 	int	instance;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (infocmd == DDI_INFO_DEVT2INSTANCE) {
4190Sstevel@tonic-gate 		dev = (dev_t)arg;
4200Sstevel@tonic-gate 		instance = MINOR_TO_INST(getminor(dev));
4210Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
4220Sstevel@tonic-gate 		return (DDI_SUCCESS);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	return (DDI_FAILURE);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate static int
pcf8591_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4280Sstevel@tonic-gate pcf8591_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	switch (cmd) {
4310Sstevel@tonic-gate 	case DDI_ATTACH:
4320Sstevel@tonic-gate 		return (pcf8591_do_attach(dip));
4330Sstevel@tonic-gate 	case DDI_RESUME:
4340Sstevel@tonic-gate 		return (pcf8591_do_resume());
4350Sstevel@tonic-gate 	default:
4360Sstevel@tonic-gate 		return (DDI_FAILURE);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate static int
pcf8591_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4410Sstevel@tonic-gate pcf8591_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	switch (cmd) {
4440Sstevel@tonic-gate 	case DDI_DETACH:
4450Sstevel@tonic-gate 		return (pcf8591_do_detach(dip));
4460Sstevel@tonic-gate 	case DDI_SUSPEND:
4470Sstevel@tonic-gate 		return (pcf8591_do_suspend());
4480Sstevel@tonic-gate 	default:
4490Sstevel@tonic-gate 		return (DDI_FAILURE);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate static int
pcf8591_do_attach(dev_info_t * dip)4540Sstevel@tonic-gate pcf8591_do_attach(dev_info_t *dip)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	struct pcf8591_unit *unitp;
4570Sstevel@tonic-gate 	int instance;
4580Sstevel@tonic-gate 	char name[MAXNAMELEN];
4590Sstevel@tonic-gate 	minor_t minor_number;
4600Sstevel@tonic-gate 	int i;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pcf8591soft_statep, instance) != 0) {
4650Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
4667656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
4670Sstevel@tonic-gate 		return (DDI_FAILURE);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8591soft_statep, instance);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (unitp == NULL) {
4730Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
4747656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
4750Sstevel@tonic-gate 		return (ENOMEM);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	(void) snprintf(unitp->pcf8591_name, sizeof (unitp->pcf8591_name),
4797656SSherry.Moore@Sun.COM 	    "%s%d", ddi_node_name(dip), instance);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
4820Sstevel@tonic-gate 		(void) sprintf(name, "port_%d", i);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		minor_number = INST_TO_MINOR(instance) |
4857656SSherry.Moore@Sun.COM 		    PORT_TO_MINOR(I2C_PORT(i));
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
4887656SSherry.Moore@Sun.COM 		    "ddi_i2c:adio", NULL) == DDI_FAILURE) {
4890Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
4907656SSherry.Moore@Sun.COM 			    "%s\n", unitp->pcf8591_name, name);
4910Sstevel@tonic-gate 			ddi_soft_state_free(pcf8591soft_statep, instance);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 			return (DDI_FAILURE);
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->pcf8591_hdl) != I2C_SUCCESS) {
4980Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
4990Sstevel@tonic-gate 		ddi_soft_state_free(pcf8591soft_statep, instance);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		return (DDI_FAILURE);
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	mutex_init(&unitp->pcf8591_mutex, NULL, MUTEX_DRIVER, NULL);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	return (DDI_SUCCESS);
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate static int
pcf8591_do_resume()5100Sstevel@tonic-gate pcf8591_do_resume()
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	return (ret);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate static int
pcf8591_do_suspend()5180Sstevel@tonic-gate pcf8591_do_suspend()
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	return (ret);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate static int
pcf8591_do_detach(dev_info_t * dip)5260Sstevel@tonic-gate pcf8591_do_detach(dev_info_t *dip)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	struct pcf8591_unit *unitp;
5290Sstevel@tonic-gate 	int instance;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8591soft_statep, instance);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if (unitp == NULL) {
5360Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
5377656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
5380Sstevel@tonic-gate 		return (ENOMEM);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	i2c_client_unregister(unitp->pcf8591_hdl);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	mutex_destroy(&unitp->pcf8591_mutex);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	ddi_soft_state_free(pcf8591soft_statep, instance);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	return (DDI_SUCCESS);
5500Sstevel@tonic-gate }
551