xref: /onnv-gate/usr/src/uts/sun4u/io/i2c/clients/ssc050.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
53402Smb158278  * Common Development and Distribution License (the "License").
63402Smb158278  * 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/sunddi.h>
310Sstevel@tonic-gate #include <sys/conf.h>		/* req. by dev_ops flags MTSAFE etc. */
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/file.h>
340Sstevel@tonic-gate #include <sys/note.h>
350Sstevel@tonic-gate #include <sys/i2c/clients/i2c_client.h>
360Sstevel@tonic-gate #include <sys/i2c/clients/ssc050.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	SSC050_NUM_PORTS		5
390Sstevel@tonic-gate #define	SSC050_DATADIRECTION_REG(port)	(0x10 | (port))
400Sstevel@tonic-gate #define	SSC050_COUNT_REG(port)		(0x32 | ((port) << 2))
410Sstevel@tonic-gate #define	SSC050_GP_REG(port)		(port)
420Sstevel@tonic-gate #define	SSC050_BIT_REG(port, bit)	(SSC050_PORT_BIT_REG(port) | (bit))
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	SSC050_FAN_SPEED(div, count)	(1200000 / ((count) * (1<<(div))))
450Sstevel@tonic-gate #define	SSC050_FAN_CONTROL_ENABLE	0x80
460Sstevel@tonic-gate #define	SSC050_FAN_CONTROL_DIVISOR	0x03
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	SSC050_DATADIRECTION_BIT	0x02
490Sstevel@tonic-gate 
500Sstevel@tonic-gate struct ssc050_unit {
510Sstevel@tonic-gate 	kmutex_t		mutex;
520Sstevel@tonic-gate 	int			oflag;
530Sstevel@tonic-gate 	i2c_client_hdl_t	hdl;
540Sstevel@tonic-gate 	char			name[12];
550Sstevel@tonic-gate };
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #ifdef DEBUG
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static int ssc050debug = 0;
600Sstevel@tonic-gate #define	D1CMN_ERR(ARGS) if (ssc050debug & 0x01) cmn_err ARGS;
610Sstevel@tonic-gate #define	D2CMN_ERR(ARGS) if (ssc050debug & 0x02) cmn_err ARGS;
620Sstevel@tonic-gate #define	D3CMN_ERR(ARGS) if (ssc050debug & 0x04) cmn_err ARGS;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #else
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	D1CMN_ERR(ARGS)
670Sstevel@tonic-gate #define	D2CMN_ERR(ARGS)
680Sstevel@tonic-gate #define	D3CMN_ERR(ARGS)
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static void *ssc050soft_statep;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static int ssc050_do_attach(dev_info_t *);
750Sstevel@tonic-gate static int ssc050_do_detach(dev_info_t *);
760Sstevel@tonic-gate static int ssc050_set(struct ssc050_unit *, int, uchar_t);
770Sstevel@tonic-gate static int ssc050_get(struct ssc050_unit *, int, uchar_t *, int);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * cb ops
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate static int ssc050_open(dev_t *, int, int, cred_t *);
830Sstevel@tonic-gate static int ssc050_close(dev_t, int, int, cred_t *);
840Sstevel@tonic-gate static int ssc050_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static struct cb_ops ssc050_cbops = {
880Sstevel@tonic-gate 	ssc050_open,			/* open  */
890Sstevel@tonic-gate 	ssc050_close,			/* close */
900Sstevel@tonic-gate 	nodev,				/* strategy */
910Sstevel@tonic-gate 	nodev,				/* print */
920Sstevel@tonic-gate 	nodev,				/* dump */
930Sstevel@tonic-gate 	nodev,				/* read */
940Sstevel@tonic-gate 	nodev,				/* write */
950Sstevel@tonic-gate 	ssc050_ioctl,			/* ioctl */
960Sstevel@tonic-gate 	nodev,				/* devmap */
970Sstevel@tonic-gate 	nodev,				/* mmap */
980Sstevel@tonic-gate 	nodev,				/* segmap */
990Sstevel@tonic-gate 	nochpoll,			/* poll */
1000Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1010Sstevel@tonic-gate 	NULL,				/* streamtab */
1020Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
1030Sstevel@tonic-gate 	CB_REV,				/* rev */
1040Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1050Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * dev ops
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate static int ssc050_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1120Sstevel@tonic-gate 		void **result);
1130Sstevel@tonic-gate static int ssc050_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
1140Sstevel@tonic-gate static int ssc050_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static struct dev_ops ssc050_ops = {
1170Sstevel@tonic-gate 	DEVO_REV,
1180Sstevel@tonic-gate 	0,
1190Sstevel@tonic-gate 	ssc050_info,
1200Sstevel@tonic-gate 	nulldev,
1210Sstevel@tonic-gate 	nulldev,
1220Sstevel@tonic-gate 	ssc050_attach,
1230Sstevel@tonic-gate 	ssc050_detach,
1240Sstevel@tonic-gate 	nodev,
1250Sstevel@tonic-gate 	&ssc050_cbops,
1267656SSherry.Moore@Sun.COM 	NULL,
1277656SSherry.Moore@Sun.COM 	NULL,
1287656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1290Sstevel@tonic-gate };
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static struct modldrv ssc050_modldrv = {
1340Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
135*7696SRichard.Bean@Sun.COM 	"SSC050 i2c device driver",
1360Sstevel@tonic-gate 	&ssc050_ops
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static struct modlinkage ssc050_modlinkage = {
1400Sstevel@tonic-gate 	MODREV_1,
1410Sstevel@tonic-gate 	&ssc050_modldrv,
1420Sstevel@tonic-gate 	0
1430Sstevel@tonic-gate };
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate int
_init(void)1470Sstevel@tonic-gate _init(void)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	int error;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	error = mod_install(&ssc050_modlinkage);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (!error)
1540Sstevel@tonic-gate 		(void) ddi_soft_state_init(&ssc050soft_statep,
1557656SSherry.Moore@Sun.COM 		    sizeof (struct ssc050_unit), 1);
1560Sstevel@tonic-gate 	return (error);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate int
_fini(void)1600Sstevel@tonic-gate _fini(void)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	int error;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	error = mod_remove(&ssc050_modlinkage);
1650Sstevel@tonic-gate 	if (!error)
1660Sstevel@tonic-gate 		ddi_soft_state_fini(&ssc050soft_statep);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	return (error);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1720Sstevel@tonic-gate _info(struct modinfo *modinfop)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	return (mod_info(&ssc050_modlinkage, modinfop));
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static int
ssc050_open(dev_t * devp,int flags,int otyp,cred_t * credp)1780Sstevel@tonic-gate ssc050_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	struct ssc050_unit *unitp;
1830Sstevel@tonic-gate 	int instance;
1840Sstevel@tonic-gate 	int error = 0;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(*devp));
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (instance < 0) {
1890Sstevel@tonic-gate 		return (ENXIO);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	unitp = (struct ssc050_unit *)
1937656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(ssc050soft_statep, instance);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (unitp == NULL) {
1960Sstevel@tonic-gate 		return (ENXIO);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
2000Sstevel@tonic-gate 		return (EINVAL);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	mutex_enter(&unitp->mutex);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (flags & FEXCL) {
2060Sstevel@tonic-gate 		if (unitp->oflag != 0) {
2070Sstevel@tonic-gate 			error = EBUSY;
2080Sstevel@tonic-gate 		} else {
2090Sstevel@tonic-gate 			unitp->oflag = FEXCL;
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 	} else {
2120Sstevel@tonic-gate 		if (unitp->oflag == FEXCL) {
2130Sstevel@tonic-gate 			error = EBUSY;
2140Sstevel@tonic-gate 		} else {
2150Sstevel@tonic-gate 			unitp->oflag = FOPEN;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	mutex_exit(&unitp->mutex);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	return (error);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate static int
ssc050_close(dev_t dev,int flags,int otyp,cred_t * credp)2250Sstevel@tonic-gate ssc050_close(dev_t dev, int flags, int otyp, cred_t *credp)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	struct ssc050_unit *unitp;
2300Sstevel@tonic-gate 	int instance;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (instance < 0) {
2350Sstevel@tonic-gate 		return (ENXIO);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	unitp = (struct ssc050_unit *)
2397656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(ssc050soft_statep, instance);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (unitp == NULL) {
2420Sstevel@tonic-gate 		return (ENXIO);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	mutex_enter(&unitp->mutex);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	unitp->oflag = 0;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	mutex_exit(&unitp->mutex);
2500Sstevel@tonic-gate 	return (DDI_SUCCESS);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static int
ssc050_get(struct ssc050_unit * unitp,int reg,uchar_t * byte,int flags)2540Sstevel@tonic-gate ssc050_get(struct ssc050_unit *unitp, int reg, uchar_t *byte, int flags)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2570Sstevel@tonic-gate 	int			err;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->hdl, &i2c_tran_pointer,
2607656SSherry.Moore@Sun.COM 	    1, 1, flags);
2610Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2620Sstevel@tonic-gate 		return (ENOMEM);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR_RD;
2660Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
2670Sstevel@tonic-gate 	err = i2c_transfer(unitp->hdl, i2c_tran_pointer);
2680Sstevel@tonic-gate 	if (err) {
2690Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: ssc050_get failed reg=%x",
2707656SSherry.Moore@Sun.COM 		    unitp->name, reg));
2710Sstevel@tonic-gate 	} else {
2720Sstevel@tonic-gate 		*byte = i2c_tran_pointer->i2c_rbuf[0];
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	i2c_transfer_free(unitp->hdl, i2c_tran_pointer);
2760Sstevel@tonic-gate 	return (err);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate static int
ssc050_set(struct ssc050_unit * unitp,int reg,uchar_t byte)2800Sstevel@tonic-gate ssc050_set(struct ssc050_unit *unitp, int reg, uchar_t byte)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2830Sstevel@tonic-gate 	int			err;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->hdl, &i2c_tran_pointer,
2867656SSherry.Moore@Sun.COM 	    2, 0, I2C_SLEEP);
2870Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2880Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in ssc050_set "
2897656SSherry.Moore@Sun.COM 		    "i2c_tran_pointer not allocated", unitp->name));
2900Sstevel@tonic-gate 		return (ENOMEM);
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR;
2940Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
2950Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[1] = byte;
2960Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: set reg %x to %x", unitp->name, reg, byte));
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	err = i2c_transfer(unitp->hdl, i2c_tran_pointer);
2990Sstevel@tonic-gate 	if (err) {
3000Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the ssc050_set"
3017656SSherry.Moore@Sun.COM 		    " i2c_transfer routine", unitp->name));
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	i2c_transfer_free(unitp->hdl, i2c_tran_pointer);
3040Sstevel@tonic-gate 	return (err);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate static int
ssc050_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)3080Sstevel@tonic-gate ssc050_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
3090Sstevel@tonic-gate 		int *rvalp)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	struct ssc050_unit	*unitp;
3140Sstevel@tonic-gate 	int			err = 0;
3150Sstevel@tonic-gate 	i2c_bit_t		ioctl_bit;
3160Sstevel@tonic-gate 	i2c_port_t		ioctl_port;
3170Sstevel@tonic-gate 	i2c_reg_t		ioctl_reg;
3180Sstevel@tonic-gate 	int port = MINOR_TO_PORT(getminor(dev));
3190Sstevel@tonic-gate 	int instance = MINOR_TO_INST(getminor(dev));
3200Sstevel@tonic-gate 	uchar_t 		reg, val8;
3210Sstevel@tonic-gate 	uchar_t 		control;
3220Sstevel@tonic-gate 	uchar_t			fan_count;
3230Sstevel@tonic-gate 	int			divisor;
3240Sstevel@tonic-gate 	int32_t			fan_speed;
3250Sstevel@tonic-gate 	uint8_t			inverted_mask;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (arg == NULL) {
3280Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "SSC050: ioctl: arg passed in to ioctl "
3297656SSherry.Moore@Sun.COM 		    "= NULL"));
3300Sstevel@tonic-gate 		return (EINVAL);
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	unitp = (struct ssc050_unit *)
3337656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(ssc050soft_statep, instance);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (unitp == NULL) {
3360Sstevel@tonic-gate 		return (ENXIO);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	mutex_enter(&unitp->mutex);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	D3CMN_ERR((CE_NOTE, "%s: ioctl: port = %d", unitp->name, port));
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	switch (cmd) {
3440Sstevel@tonic-gate 	case I2C_GET_PORT:
3450Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
3467656SSherry.Moore@Sun.COM 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3470Sstevel@tonic-gate 			err = EFAULT;
3480Sstevel@tonic-gate 			break;
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		if (ioctl_port.direction == DIR_INPUT) {
3520Sstevel@tonic-gate 			reg = SSC050_DATADIRECTION_REG(port);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			err = ssc050_get(unitp, reg, &val8, I2C_SLEEP);
3550Sstevel@tonic-gate 			if (err != I2C_SUCCESS) {
3560Sstevel@tonic-gate 				break;
3570Sstevel@tonic-gate 			}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 			if (val8 != ioctl_port.dir_mask) {
3600Sstevel@tonic-gate 				D2CMN_ERR((CE_NOTE, "GET_PORT sleeping! "
3617656SSherry.Moore@Sun.COM 				    "wanted %x, had %x",
3627656SSherry.Moore@Sun.COM 				    ioctl_port.dir_mask, val8));
3630Sstevel@tonic-gate 				err = ssc050_set(unitp, reg,
3640Sstevel@tonic-gate 				    ioctl_port.dir_mask);
3650Sstevel@tonic-gate 				if (err != I2C_SUCCESS) {
3660Sstevel@tonic-gate 					break;
3670Sstevel@tonic-gate 				}
3680Sstevel@tonic-gate 				delay(10);
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		err = ssc050_get(unitp, port, &val8, I2C_SLEEP);
3730Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3740Sstevel@tonic-gate 			break;
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		ioctl_port.value = val8;
3770Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_port, (caddr_t)arg,
3787656SSherry.Moore@Sun.COM 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3790Sstevel@tonic-gate 			err = EFAULT;
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 		break;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	case I2C_SET_PORT:
3840Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
3857656SSherry.Moore@Sun.COM 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3860Sstevel@tonic-gate 			err = EFAULT;
3870Sstevel@tonic-gate 			break;
3880Sstevel@tonic-gate 		}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		reg = SSC050_DATADIRECTION_REG(port);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 		err = ssc050_get(unitp, reg, &val8, I2C_SLEEP);
3930Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3940Sstevel@tonic-gate 			break;
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: ioctl: Data Direction Register "
3987656SSherry.Moore@Sun.COM 		    "contains %x", unitp->name, val8));
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		inverted_mask = ioctl_port.dir_mask ^ 0xff;
4010Sstevel@tonic-gate 		val8 = val8 & inverted_mask;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: ioctl: Data Direction Register "
4047656SSherry.Moore@Sun.COM 		    "NOW contains %x", unitp->name, val8));
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		err = ssc050_set(unitp, reg, val8);
4070Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4080Sstevel@tonic-gate 			break;
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		err = ssc050_get(unitp, port, &val8, I2C_SLEEP);
4120Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4130Sstevel@tonic-gate 			break;
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: ioctl: GP Register "
4177656SSherry.Moore@Sun.COM 		    "contains %x", unitp->name, val8));
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		val8 = val8 & inverted_mask;
4200Sstevel@tonic-gate 		val8 = val8 | ioctl_port.value;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: ioctl: GP Register "
4237656SSherry.Moore@Sun.COM 		    "NOW contains %x", unitp->name, val8));
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		err = ssc050_set(unitp, SSC050_GP_REG(port), val8);
4260Sstevel@tonic-gate 		break;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	case I2C_GET_FAN_SPEED:
4290Sstevel@tonic-gate 		err = ssc050_get(unitp, SSC050_FAN_CONTROL_REG(port),
4300Sstevel@tonic-gate 		    &control, I2C_SLEEP);
4310Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4320Sstevel@tonic-gate 			break;
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: port %d: control = %x", unitp->name,
4367656SSherry.Moore@Sun.COM 		    port, control));
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 		if (!(control & SSC050_FAN_CONTROL_ENABLE)) {
4390Sstevel@tonic-gate 			err = EIO;
4400Sstevel@tonic-gate 			break;
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 		err = ssc050_get(unitp, SSC050_COUNT_REG(port), &fan_count,
4440Sstevel@tonic-gate 		    I2C_SLEEP);
4450Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4460Sstevel@tonic-gate 			break;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 		if (fan_count == 0) {
4500Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_FAN_SPEED "
4517656SSherry.Moore@Sun.COM 			    "i2c_rbuf = 0", unitp->name));
4520Sstevel@tonic-gate 			err = EIO;
4530Sstevel@tonic-gate 			break;
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 		if (fan_count == 0xff) {
4560Sstevel@tonic-gate 			fan_speed = 0;
4570Sstevel@tonic-gate 			if (ddi_copyout((caddr_t)&fan_speed, (caddr_t)arg,
4587656SSherry.Moore@Sun.COM 			    sizeof (int32_t), mode) != DDI_SUCCESS) {
4590Sstevel@tonic-gate 				err = EFAULT;
4600Sstevel@tonic-gate 				break;
4610Sstevel@tonic-gate 			}
4620Sstevel@tonic-gate 			break;
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		divisor = control & SSC050_FAN_CONTROL_DIVISOR;
4660Sstevel@tonic-gate 		fan_speed = SSC050_FAN_SPEED(divisor, fan_count);
4670Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&fan_speed, (caddr_t)arg,
4687656SSherry.Moore@Sun.COM 		    sizeof (int32_t), mode) != DDI_SUCCESS) {
4690Sstevel@tonic-gate 			err = EFAULT;
4700Sstevel@tonic-gate 		}
4710Sstevel@tonic-gate 		break;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	case I2C_GET_BIT:
4740Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
4757656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4760Sstevel@tonic-gate 			err = EFAULT;
4770Sstevel@tonic-gate 			break;
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 
480946Smathue 		if (ioctl_bit.bit_num > 7) {
4810Sstevel@tonic-gate 			err = EINVAL;
4820Sstevel@tonic-gate 			break;
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		reg = (uchar_t)SSC050_BIT_REG(port, ioctl_bit.bit_num);
4860Sstevel@tonic-gate 		D3CMN_ERR((CE_NOTE, "%s: reg = %x", unitp->name, reg));
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		if (ioctl_bit.direction == DIR_INPUT) {
4890Sstevel@tonic-gate 			err = ssc050_get(unitp, reg, &val8, I2C_SLEEP);
4900Sstevel@tonic-gate 			if (err != I2C_SUCCESS) {
4910Sstevel@tonic-gate 				break;
4920Sstevel@tonic-gate 			}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 			if (!(val8 & SSC050_DATADIRECTION_BIT)) {
4950Sstevel@tonic-gate 				D2CMN_ERR((CE_NOTE, "GET_PORT sleeping! "
4967656SSherry.Moore@Sun.COM 				    "wanted %x, had %x",
4977656SSherry.Moore@Sun.COM 				    val8 | SSC050_DATADIRECTION_BIT,
4987656SSherry.Moore@Sun.COM 				    val8));
4990Sstevel@tonic-gate 				err = ssc050_set(unitp, reg,
5000Sstevel@tonic-gate 				    val8 | SSC050_DATADIRECTION_BIT);
5017656SSherry.Moore@Sun.COM 					if (err != I2C_SUCCESS) {
5027656SSherry.Moore@Sun.COM 						break;
5037656SSherry.Moore@Sun.COM 					}
5047656SSherry.Moore@Sun.COM 					delay(10);
5050Sstevel@tonic-gate 			}
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 		err = ssc050_get(unitp, reg, &val8, I2C_SLEEP);
5090Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
5100Sstevel@tonic-gate 			break;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 		D3CMN_ERR((CE_NOTE, "byte back from device = %x", val8));
5130Sstevel@tonic-gate 		val8 = val8 & 0x01;
5140Sstevel@tonic-gate 		ioctl_bit.bit_value = (boolean_t)val8;
5150Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_bit, (caddr_t)arg,
5167656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
5170Sstevel@tonic-gate 			err = EFAULT;
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 		break;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	case I2C_SET_BIT:
5220Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
5237656SSherry.Moore@Sun.COM 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
5240Sstevel@tonic-gate 			err = EFAULT;
5250Sstevel@tonic-gate 			break;
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 
528946Smathue 		if (ioctl_bit.bit_num > 7) {
5290Sstevel@tonic-gate 			err = EINVAL;
5300Sstevel@tonic-gate 			break;
5310Sstevel@tonic-gate 		}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		reg = (uchar_t)SSC050_BIT_REG(port, ioctl_bit.bit_num);
5340Sstevel@tonic-gate 		D3CMN_ERR((CE_NOTE, "%s: reg = %x", unitp->name, reg));
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		val8 = (uchar_t)ioctl_bit.bit_value;
5370Sstevel@tonic-gate 		err = ssc050_set(unitp, reg, val8);
5380Sstevel@tonic-gate 		break;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	case I2C_GET_REG:
5410Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg,
5427656SSherry.Moore@Sun.COM 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
5430Sstevel@tonic-gate 			err = EFAULT;
5440Sstevel@tonic-gate 			break;
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 		err = ssc050_get(unitp, ioctl_reg.reg_num, &val8,
5477656SSherry.Moore@Sun.COM 		    I2C_SLEEP);
5480Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
5490Sstevel@tonic-gate 			break;
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 		ioctl_reg.reg_value = val8;
5530Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_reg, (caddr_t)arg,
5547656SSherry.Moore@Sun.COM 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
5550Sstevel@tonic-gate 			err = EFAULT;
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 		break;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	case I2C_SET_REG:
5600Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg,
5617656SSherry.Moore@Sun.COM 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
5620Sstevel@tonic-gate 			err = EFAULT;
5630Sstevel@tonic-gate 			break;
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 		err = ssc050_set(unitp, ioctl_reg.reg_num,
5667656SSherry.Moore@Sun.COM 		    ioctl_reg.reg_value);
5670Sstevel@tonic-gate 		break;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	default:
5700Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x",
5717656SSherry.Moore@Sun.COM 		    unitp->name, cmd));
5720Sstevel@tonic-gate 		err = EINVAL;
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	mutex_exit(&unitp->mutex);
5760Sstevel@tonic-gate 	return (err);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate /* ARGSUSED */
5800Sstevel@tonic-gate static int
ssc050_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)5810Sstevel@tonic-gate ssc050_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	dev_t	dev;
5840Sstevel@tonic-gate 	int	instance;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (infocmd == DDI_INFO_DEVT2INSTANCE) {
5870Sstevel@tonic-gate 		dev = (dev_t)arg;
5880Sstevel@tonic-gate 		instance = MINOR_TO_INST(getminor(dev));
5890Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
5900Sstevel@tonic-gate 		return (DDI_SUCCESS);
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 	return (DDI_FAILURE);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate static int
ssc050_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)5960Sstevel@tonic-gate ssc050_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	switch (cmd) {
5990Sstevel@tonic-gate 	case DDI_ATTACH:
6000Sstevel@tonic-gate 		return (ssc050_do_attach(dip));
6010Sstevel@tonic-gate 	case DDI_RESUME:
6020Sstevel@tonic-gate 		return (DDI_SUCCESS);
6030Sstevel@tonic-gate 	default:
6040Sstevel@tonic-gate 		return (DDI_FAILURE);
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate static int
ssc050_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)6090Sstevel@tonic-gate ssc050_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	switch (cmd) {
6120Sstevel@tonic-gate 	case DDI_DETACH:
6130Sstevel@tonic-gate 		return (ssc050_do_detach(dip));
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	case DDI_SUSPEND:
6160Sstevel@tonic-gate 		return (DDI_SUCCESS);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	default:
6190Sstevel@tonic-gate 		return (DDI_FAILURE);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate static int
ssc050_do_attach(dev_info_t * dip)6240Sstevel@tonic-gate ssc050_do_attach(dev_info_t *dip)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	struct ssc050_unit	*unitp;
6270Sstevel@tonic-gate 	int			instance;
6280Sstevel@tonic-gate 	char			name[MAXNAMELEN];
6290Sstevel@tonic-gate 	minor_t			minor_number;
6300Sstevel@tonic-gate 	int			i;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(ssc050soft_statep, instance) != 0) {
6350Sstevel@tonic-gate 		return (DDI_FAILURE);
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ssc050soft_statep, instance);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	(void) snprintf(unitp->name, sizeof (unitp->name),
6417656SSherry.Moore@Sun.COM 	    "%s%d", ddi_node_name(dip), instance);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	for (i = 0; i < SSC050_NUM_PORTS; i++) {
6440Sstevel@tonic-gate 		(void) sprintf(name, "port_%d", i);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 		minor_number = INST_TO_MINOR(instance) |
6477656SSherry.Moore@Sun.COM 		    PORT_TO_MINOR(I2C_PORT(i));
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
6507656SSherry.Moore@Sun.COM 		"ddi_i2c:ioexp", NULL) == DDI_FAILURE) {
6510Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: failed to create node for %s",
6520Sstevel@tonic-gate 			    unitp->name, name);
6530Sstevel@tonic-gate 			ddi_soft_state_free(ssc050soft_statep, instance);
6540Sstevel@tonic-gate 			return (DDI_FAILURE);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->hdl) != I2C_SUCCESS) {
6590Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
6600Sstevel@tonic-gate 		ddi_soft_state_free(ssc050soft_statep, instance);
6610Sstevel@tonic-gate 		return (DDI_FAILURE);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	mutex_init(&unitp->mutex, NULL, MUTEX_DRIVER, NULL);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	return (DDI_SUCCESS);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate static int
ssc050_do_detach(dev_info_t * dip)6700Sstevel@tonic-gate ssc050_do_detach(dev_info_t *dip)
6710Sstevel@tonic-gate {
6720Sstevel@tonic-gate 	struct ssc050_unit *unitp;
6730Sstevel@tonic-gate 	int instance;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
6760Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ssc050soft_statep, instance);
6770Sstevel@tonic-gate 	i2c_client_unregister(unitp->hdl);
6780Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
6790Sstevel@tonic-gate 	mutex_destroy(&unitp->mutex);
6800Sstevel@tonic-gate 	ddi_soft_state_free(ssc050soft_statep, instance);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	return (DDI_SUCCESS);
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate int
ssc050_get_port_bit(dev_info_t * dip,int port,int bit,uchar_t * rval,int flags)6860Sstevel@tonic-gate ssc050_get_port_bit(dev_info_t *dip, int port, int bit, uchar_t *rval,
6870Sstevel@tonic-gate 			int flags)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate 	struct ssc050_unit	*unitp;
6900Sstevel@tonic-gate 	int			instance;
6910Sstevel@tonic-gate 	int			reg = (uchar_t)SSC050_BIT_REG(port, bit);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (rval == NULL || dip == NULL)
6940Sstevel@tonic-gate 		return (EINVAL);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
6970Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ssc050soft_statep, instance);
6983402Smb158278 	if (unitp == NULL) {
6993402Smb158278 		return (ENXIO);
7003402Smb158278 	}
7010Sstevel@tonic-gate 	return (ssc050_get(unitp, reg, rval, flags));
7020Sstevel@tonic-gate }
703