xref: /onnv-gate/usr/src/uts/sun4u/io/i2c/clients/ics951601.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/stat.h>
280Sstevel@tonic-gate #include <sys/file.h>
290Sstevel@tonic-gate #include <sys/uio.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/open.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/mode.h>
390Sstevel@tonic-gate #include <sys/note.h>
400Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h>
410Sstevel@tonic-gate #include <sys/i2c/clients/ics951601.h>
420Sstevel@tonic-gate #include <sys/i2c/clients/ics951601_impl.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * This is the client driver for the ics951601 device which is a general
460Sstevel@tonic-gate  * purpose clock generator.  Setting a clock to 1 enables the clock while
470Sstevel@tonic-gate  * setting it to 0 disables it.  All clocks are enabled by default by
480Sstevel@tonic-gate  * the driver.  The user can read a clock, enable it or disable it.
490Sstevel@tonic-gate  * The command sent as an ioctl argument should be the bitwise OR of the
500Sstevel@tonic-gate  * clock number and the action upon it.  The supported clock numbers and
510Sstevel@tonic-gate  * actions are defined in ics951601.h.  A pointer to an integer is sent
520Sstevel@tonic-gate  * as the third  ioctl argument.  If the clock is to be read the value of the
530Sstevel@tonic-gate  * clock is copied into it and if it is to be modified the integer referred
540Sstevel@tonic-gate  * to should have the appropriate value of either 1 or 0.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * cb ops
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate static int ics951601_open(dev_t *, int, int, cred_t *);
610Sstevel@tonic-gate static int ics951601_close(dev_t, int, int, cred_t *);
620Sstevel@tonic-gate static int ics951601_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * dev ops
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate static int ics951601_s_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
680Sstevel@tonic-gate static int ics951601_s_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
690Sstevel@tonic-gate static int ics951601_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static struct cb_ops ics951601_cb_ops = {
720Sstevel@tonic-gate 	ics951601_open,			/* open */
730Sstevel@tonic-gate 	ics951601_close,		/* close */
740Sstevel@tonic-gate 	nodev,				/* strategy */
750Sstevel@tonic-gate 	nodev,				/* print */
760Sstevel@tonic-gate 	nodev,				/* dump */
770Sstevel@tonic-gate 	nodev,				/* read */
780Sstevel@tonic-gate 	nodev,				/* write */
790Sstevel@tonic-gate 	ics951601_ioctl,		/* ioctl */
800Sstevel@tonic-gate 	nodev,				/* devmap */
810Sstevel@tonic-gate 	nodev,				/* mmap */
820Sstevel@tonic-gate 	nodev,				/* segmap */
830Sstevel@tonic-gate 	nochpoll,			/* poll */
840Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
850Sstevel@tonic-gate 	NULL,				/* streamtab */
860Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct dev_ops ics951601_dev_ops = {
900Sstevel@tonic-gate 	DEVO_REV,
910Sstevel@tonic-gate 	0,
920Sstevel@tonic-gate 	ics951601_info,
930Sstevel@tonic-gate 	nulldev,
940Sstevel@tonic-gate 	nulldev,
950Sstevel@tonic-gate 	ics951601_s_attach,
960Sstevel@tonic-gate 	ics951601_s_detach,
970Sstevel@tonic-gate 	nodev,
980Sstevel@tonic-gate 	&ics951601_cb_ops,
99*7656SSherry.Moore@Sun.COM 	NULL,
100*7656SSherry.Moore@Sun.COM 	NULL,
101*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static struct modldrv ics951601_modldrv = {
1050Sstevel@tonic-gate 	&mod_driverops,		/* type of module - driver */
106*7656SSherry.Moore@Sun.COM 	"ics951601 device driver",
1070Sstevel@tonic-gate 	&ics951601_dev_ops,
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static struct modlinkage ics951601_modlinkage = {
1110Sstevel@tonic-gate 	MODREV_1,
1120Sstevel@tonic-gate 	&ics951601_modldrv,
1130Sstevel@tonic-gate 	0
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * Writes to the clock generator involve sending the dummy command code, the
1180Sstevel@tonic-gate  * dummy byte count followed by byte 0 through byte 5. The dummy command code
1190Sstevel@tonic-gate  * and the dummy byte count are ignored by the ICS clock but must be sent.
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * On reading from the clock generator, the controller will first receive a
1220Sstevel@tonic-gate  * byte count followed by byte 0 through byte 5.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * The array for initializing the internal registers at attach time.
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate static uchar_t init_clock_regs[8] = {
1290Sstevel@tonic-gate 	0x0,	/* Dummy command code */
1300Sstevel@tonic-gate 	0x7,	/* Dummy byte count */
1310Sstevel@tonic-gate 	0x0,	/* Initial value for functionality register */
1320Sstevel@tonic-gate 	0xff,	/* Initial value for PCI1A stop clocks register */
1330Sstevel@tonic-gate 	0xff,	/* Initial value for PCI2A stop clocks register */
1340Sstevel@tonic-gate 	0xff,	/* Initial value for PCI2B stop clocks register */
1350Sstevel@tonic-gate 	0xff,	/* Default value for reserved register */
1360Sstevel@tonic-gate 	0xef	/* Default value for latched input read back register */
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static void *ics951601_soft_statep;
1400Sstevel@tonic-gate int ics951601_debug = 0;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate int
_init(void)1430Sstevel@tonic-gate _init(void)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	int    err;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	err = mod_install(&ics951601_modlinkage);
1480Sstevel@tonic-gate 	if (err == 0) {
1490Sstevel@tonic-gate 		(void) ddi_soft_state_init(&ics951601_soft_statep,
1500Sstevel@tonic-gate 		    sizeof (ics951601_unit_t), 1);
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 	return (err);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate int
_fini(void)1560Sstevel@tonic-gate _fini(void)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	int    err;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	err = mod_remove(&ics951601_modlinkage);
1610Sstevel@tonic-gate 	if (err == 0) {
1620Sstevel@tonic-gate 		ddi_soft_state_fini(&ics951601_soft_statep);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	return (err);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1680Sstevel@tonic-gate _info(struct modinfo *modinfop)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	return (mod_info(&ics951601_modlinkage, modinfop));
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static int
ics951601_open(dev_t * devp,int flags,int otyp,cred_t * credp)1740Sstevel@tonic-gate ics951601_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	int			instance;
1770Sstevel@tonic-gate 	ics951601_unit_t	*icsp;
1780Sstevel@tonic-gate 	int			err = EBUSY;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * Make sure the open is for the right file type
1820Sstevel@tonic-gate 	 */
1830Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
1840Sstevel@tonic-gate 		return (EINVAL);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	instance = getminor(*devp);
1880Sstevel@tonic-gate 	if (instance < 0) {
1890Sstevel@tonic-gate 		return (ENXIO);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 	icsp = (ics951601_unit_t *)ddi_get_soft_state(ics951601_soft_statep,
1920Sstevel@tonic-gate 	    instance);
1930Sstevel@tonic-gate 	if (icsp == NULL) {
1940Sstevel@tonic-gate 		return (ENXIO);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* must be privileged to access this device */
1980Sstevel@tonic-gate 	if (drv_priv(credp) != 0) {
1990Sstevel@tonic-gate 		return (EPERM);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * Enforce exclusive access if required
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
2060Sstevel@tonic-gate 	if (flags & FEXCL) {
2070Sstevel@tonic-gate 		if (icsp->ics951601_oflag == 0) {
2080Sstevel@tonic-gate 			icsp->ics951601_oflag = FEXCL;
2090Sstevel@tonic-gate 			err = DDI_SUCCESS;
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 	} else if (icsp->ics951601_oflag != FEXCL) {
212946Smathue 		icsp->ics951601_oflag = (uint16_t)FOPEN;
2130Sstevel@tonic-gate 		err = DDI_SUCCESS;
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
2160Sstevel@tonic-gate 	return (err);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static int
ics951601_close(dev_t dev,int flags,int otyp,cred_t * credp)2200Sstevel@tonic-gate ics951601_close(dev_t dev, int flags, int otyp, cred_t *credp)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, credp))
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	int		instance;
2250Sstevel@tonic-gate 	ics951601_unit_t 	*icsp;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/*
2280Sstevel@tonic-gate 	 * 	Make sure the close is for the right file type
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
2310Sstevel@tonic-gate 		return (EINVAL);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	instance = getminor(dev);
2350Sstevel@tonic-gate 	icsp = (ics951601_unit_t *)ddi_get_soft_state(ics951601_soft_statep,
2360Sstevel@tonic-gate 	    instance);
2370Sstevel@tonic-gate 	if (icsp == NULL) {
2380Sstevel@tonic-gate 		return (ENXIO);
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
2420Sstevel@tonic-gate 	icsp->ics951601_oflag = 0;
2430Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
2440Sstevel@tonic-gate 	return (DDI_SUCCESS);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static int
ics951601_attach(dev_info_t * dip)2480Sstevel@tonic-gate ics951601_attach(dev_info_t *dip)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	ics951601_unit_t 	*icsp;
2510Sstevel@tonic-gate 	int 			instance = ddi_get_instance(dip);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(ics951601_soft_statep, instance) != 0) {
2540Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d failed to zalloc softstate",
2550Sstevel@tonic-gate 		    ddi_get_name(dip), instance);
2560Sstevel@tonic-gate 		return (DDI_FAILURE);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	icsp = ddi_get_soft_state(ics951601_soft_statep, instance);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (icsp == NULL) {
2620Sstevel@tonic-gate 		return (DDI_FAILURE);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	mutex_init(&icsp->ics951601_mutex, NULL, MUTEX_DRIVER, NULL);
2660Sstevel@tonic-gate 	cv_init(&icsp->ics951601_cv, NULL, CV_DRIVER, NULL);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	(void) snprintf(icsp->ics951601_name, sizeof (icsp->ics951601_name),
2690Sstevel@tonic-gate 	    "%s_%d", ddi_driver_name(dip), instance);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, icsp->ics951601_name, S_IFCHR,
2730Sstevel@tonic-gate 	    instance, ICS951601_NODE_TYPE, NULL) == DDI_FAILURE) {
2740Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed",
2750Sstevel@tonic-gate 		    icsp->ics951601_name);
2760Sstevel@tonic-gate 		goto ATTACH_ERR;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * preallocate a single buffer for all reads and writes
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	if (i2c_transfer_alloc(icsp->ics951601_hdl, &icsp->ics951601_transfer,
2830Sstevel@tonic-gate 	    0, 0, I2C_SLEEP) != I2C_SUCCESS) {
2840Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s i2c_transfer_alloc failed",
2850Sstevel@tonic-gate 		    icsp->ics951601_name);
2860Sstevel@tonic-gate 		goto CREATE_NODE_ERR;
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	icsp->ics951601_cpr_state[0] = 0x0;
2890Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_version = I2C_XFER_REV;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (i2c_client_register(dip, &icsp->ics951601_hdl) != I2C_SUCCESS) {
2920Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s i2c_client_register failed",
2930Sstevel@tonic-gate 		    icsp->ics951601_name);
2940Sstevel@tonic-gate 		goto ALLOC_ERR;
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/* Enable all clocks */
2980Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_WR;
2990Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = ICS951601_I2C_WRITE_TRANS_SIZE;
3000Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = 0;
3010Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wbuf = init_clock_regs;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
3040Sstevel@tonic-gate 	    != I2C_SUCCESS) {
3050Sstevel@tonic-gate 		goto REG_ERR;
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/*
3090Sstevel@tonic-gate 	 * Store the dip for future use
3100Sstevel@tonic-gate 	 */
3110Sstevel@tonic-gate 	icsp->ics951601_dip = dip;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	return (DDI_SUCCESS);
3140Sstevel@tonic-gate REG_ERR:
3150Sstevel@tonic-gate 	i2c_client_unregister(icsp->ics951601_hdl);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate ALLOC_ERR:
3180Sstevel@tonic-gate 	i2c_transfer_free(icsp->ics951601_hdl, icsp->ics951601_transfer);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate CREATE_NODE_ERR:
3210Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate ATTACH_ERR:
3240Sstevel@tonic-gate 	cv_destroy(&icsp->ics951601_cv);
3250Sstevel@tonic-gate 	mutex_destroy(&icsp->ics951601_mutex);
3260Sstevel@tonic-gate 	ddi_soft_state_free(ics951601_soft_statep, instance);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	return (DDI_FAILURE);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static void
ics951601_detach(dev_info_t * dip)3330Sstevel@tonic-gate ics951601_detach(dev_info_t *dip)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	ics951601_unit_t *icsp;
3360Sstevel@tonic-gate 	int 		instance;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3390Sstevel@tonic-gate 	icsp = ddi_get_soft_state(ics951601_soft_statep, instance);
3400Sstevel@tonic-gate 	cv_destroy(&icsp->ics951601_cv);
3410Sstevel@tonic-gate 	mutex_destroy(&icsp->ics951601_mutex);
3420Sstevel@tonic-gate 	i2c_client_unregister(icsp->ics951601_hdl);
3430Sstevel@tonic-gate 	i2c_transfer_free(icsp->ics951601_hdl, icsp->ics951601_transfer);
3440Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3450Sstevel@tonic-gate 	ddi_soft_state_free(ics951601_soft_statep, instance);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate static int
ics951601_info(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3490Sstevel@tonic-gate ics951601_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	ics951601_unit_t	*icsp;
3540Sstevel@tonic-gate 	int			instance = getminor((dev_t)arg);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	switch (cmd) {
3570Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3580Sstevel@tonic-gate 		icsp = ddi_get_soft_state(ics951601_soft_statep, instance);
3590Sstevel@tonic-gate 		if (icsp == NULL) {
3600Sstevel@tonic-gate 			return (DDI_FAILURE);
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 		*result = (void *)icsp->ics951601_dip;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		return (DDI_SUCCESS);
3650Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
366946Smathue 		*result = (void *)(uintptr_t)instance;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		return (DDI_SUCCESS);
3690Sstevel@tonic-gate 	default:
3700Sstevel@tonic-gate 		return (DDI_FAILURE);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate static int
ics951601_suspend(dev_info_t * dip)3750Sstevel@tonic-gate ics951601_suspend(dev_info_t *dip)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	ics951601_unit_t 	*icsp;
3780Sstevel@tonic-gate 	int		instance = ddi_get_instance(dip);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	icsp = ddi_get_soft_state(ics951601_soft_statep, instance);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * Set the busy flag so that future transactions block
3840Sstevel@tonic-gate 	 * until resume.
3850Sstevel@tonic-gate 	 */
3860Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
3870Sstevel@tonic-gate 	while ((icsp->ics951601_flags & ICS951601_BUSYFLAG) ==
3880Sstevel@tonic-gate 	    ICS951601_BUSYFLAG) {
3890Sstevel@tonic-gate 		if (cv_wait_sig(&icsp->ics951601_cv,
3900Sstevel@tonic-gate 		    &icsp->ics951601_mutex) <= 0) {
3910Sstevel@tonic-gate 			mutex_exit(&icsp->ics951601_mutex);
3920Sstevel@tonic-gate 			return (DDI_FAILURE);
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 	icsp->ics951601_flags |= ICS951601_BUSYFLAG;
3960Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_RD;
3990Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = 0;
4000Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = ICS951601_I2C_READ_TRANS_SIZE;
4010Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rbuf = icsp->ics951601_cpr_state + 1;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
4040Sstevel@tonic-gate 	    != I2C_SUCCESS) {
4050Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s Suspend failed, unable to save registers",
4060Sstevel@tonic-gate 		    icsp->ics951601_name);
4070Sstevel@tonic-gate 		return (EIO);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 	return (DDI_SUCCESS);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate static int
ics951601_resume(dev_info_t * dip)4140Sstevel@tonic-gate ics951601_resume(dev_info_t *dip)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	int 		instance = ddi_get_instance(dip);
4170Sstevel@tonic-gate 	ics951601_unit_t	*icsp;
4180Sstevel@tonic-gate 	int 		err = DDI_SUCCESS;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	icsp = (ics951601_unit_t *)
4210Sstevel@tonic-gate 	    ddi_get_soft_state(ics951601_soft_statep, instance);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (icsp == NULL) {
4240Sstevel@tonic-gate 		return (ENXIO);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Restore registers to status existing before cpr
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_WR;
4310Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = 0;
4320Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = ICS951601_I2C_WRITE_TRANS_SIZE;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wbuf = icsp->ics951601_cpr_state;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
4370Sstevel@tonic-gate 	    != I2C_SUCCESS) {
4380Sstevel@tonic-gate 		err = EIO;
4390Sstevel@tonic-gate 		cmn_err(CE_WARN, " %s Unable to restore registers",
4400Sstevel@tonic-gate 		    icsp->ics951601_name);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * Clear busy flag so that transactions may continue
4450Sstevel@tonic-gate 	 */
4460Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
4470Sstevel@tonic-gate 	icsp->ics951601_flags = icsp->ics951601_flags & ~ICS951601_BUSYFLAG;
4480Sstevel@tonic-gate 	cv_signal(&icsp->ics951601_cv);
4490Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
4500Sstevel@tonic-gate 	return (err);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate static int
ics951601_s_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4540Sstevel@tonic-gate ics951601_s_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	switch (cmd) {
4570Sstevel@tonic-gate 	case DDI_ATTACH:
4580Sstevel@tonic-gate 		return (ics951601_attach(dip));
4590Sstevel@tonic-gate 	case DDI_RESUME:
4600Sstevel@tonic-gate 		return (ics951601_resume(dip));
4610Sstevel@tonic-gate 	default:
4620Sstevel@tonic-gate 		return (DDI_FAILURE);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate static int
ics951601_s_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4670Sstevel@tonic-gate ics951601_s_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	switch (cmd) {
4700Sstevel@tonic-gate 	case DDI_DETACH:
4710Sstevel@tonic-gate 		ics951601_detach(dip);
4720Sstevel@tonic-gate 		return (DDI_SUCCESS);
4730Sstevel@tonic-gate 	case DDI_SUSPEND:
4740Sstevel@tonic-gate 		return (ics951601_suspend(dip));
4750Sstevel@tonic-gate 	default:
4760Sstevel@tonic-gate 		return (DDI_FAILURE);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate static int
ics951601_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)4810Sstevel@tonic-gate ics951601_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
4820Sstevel@tonic-gate 	int *rvalp)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	ics951601_unit_t		*icsp;
4870Sstevel@tonic-gate 	int			err = 0;
4880Sstevel@tonic-gate 	uchar_t			temp_arr[ICS951601_I2C_WRITE_TRANS_SIZE];
4890Sstevel@tonic-gate 	int			reg_no;
4900Sstevel@tonic-gate 	uint8_t			clock_bit;
4910Sstevel@tonic-gate 	int			ics_temp;
4920Sstevel@tonic-gate 	int			instance = getminor(dev);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	temp_arr[0] = 0x0;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	icsp = (ics951601_unit_t *)
4970Sstevel@tonic-gate 	    ddi_get_soft_state(ics951601_soft_statep, instance);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (ics951601_debug) {
5000Sstevel@tonic-gate 		printf("ics951601_ioctl: instance=%d\n", instance);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/*
5040Sstevel@tonic-gate 	 * We serialize here and  block if there are pending transacations .
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
5070Sstevel@tonic-gate 	while ((icsp->ics951601_flags & ICS951601_BUSYFLAG) ==
5080Sstevel@tonic-gate 	    ICS951601_BUSYFLAG) {
5090Sstevel@tonic-gate 		if (cv_wait_sig(&icsp->ics951601_cv,
5100Sstevel@tonic-gate 		    &icsp->ics951601_mutex) <= 0) {
5110Sstevel@tonic-gate 			mutex_exit(&icsp->ics951601_mutex);
5120Sstevel@tonic-gate 			return (EINTR);
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 	icsp->ics951601_flags |= ICS951601_BUSYFLAG;
5160Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	reg_no	= ICS951601_CMD_TO_CLOCKREG(cmd);
5190Sstevel@tonic-gate 	clock_bit = ICS951601_CMD_TO_CLOCKBIT(cmd);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_RD;
5220Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = 0;
5230Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = ICS951601_I2C_READ_TRANS_SIZE;
5240Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rbuf = temp_arr + 1;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
5270Sstevel@tonic-gate 	    != I2C_SUCCESS) {
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		err = DDI_FAILURE;
5300Sstevel@tonic-gate 		goto cleanup;
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 	switch (ICS951601_CMD_TO_ACTION(cmd)) {
5330Sstevel@tonic-gate 	case ICS951601_READ_CLOCK:
5340Sstevel@tonic-gate 		temp_arr[reg_no] &= clock_bit;
5350Sstevel@tonic-gate 		ics_temp = temp_arr[reg_no] ? ICS951601_CLOCK_SET:
5360Sstevel@tonic-gate 		    ICS951601_CLOCK_CLEAR;
5370Sstevel@tonic-gate 		err = ddi_copyout((caddr_t)&ics_temp, (caddr_t)arg,
5380Sstevel@tonic-gate 		    sizeof (int), mode);
5390Sstevel@tonic-gate 		goto cleanup;
5400Sstevel@tonic-gate 	case ICS951601_MODIFY_CLOCK:
5410Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ics_temp,
5420Sstevel@tonic-gate 		    sizeof (int), mode) != DDI_SUCCESS) {
5430Sstevel@tonic-gate 			err = EIO;
5440Sstevel@tonic-gate 			goto cleanup;
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 		if (ics_temp == ICS951601_CLOCK_SET) {
5470Sstevel@tonic-gate 			temp_arr[reg_no] |= clock_bit;
5480Sstevel@tonic-gate 		} else if (ics_temp == ICS951601_CLOCK_CLEAR) {
5490Sstevel@tonic-gate 			temp_arr[reg_no] &= ~clock_bit;
5500Sstevel@tonic-gate 		} else {
5510Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s Clock can only be set to 1 or 0",
5520Sstevel@tonic-gate 			    icsp->ics951601_name);
5530Sstevel@tonic-gate 			err = EINVAL;
5540Sstevel@tonic-gate 			goto cleanup;
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 		break;
5570Sstevel@tonic-gate 	default:
5580Sstevel@tonic-gate 		err = EINVAL;
5590Sstevel@tonic-gate 		goto cleanup;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_WR;
5630Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = ICS951601_I2C_WRITE_TRANS_SIZE;
5640Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = 0;
5650Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wbuf = temp_arr;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
5680Sstevel@tonic-gate 	    != I2C_SUCCESS) {
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		err = DDI_FAILURE;
5710Sstevel@tonic-gate 		goto cleanup;
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate cleanup:
5740Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
5750Sstevel@tonic-gate 	icsp->ics951601_flags  = icsp->ics951601_flags & ~ICS951601_BUSYFLAG;
5760Sstevel@tonic-gate 	cv_signal(&icsp->ics951601_cv);
5770Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
5780Sstevel@tonic-gate 	return (err);
5790Sstevel@tonic-gate }
580