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/misc/i2c_svc.h>
417c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/ics951601.h>
427c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/ics951601_impl.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * This is the client driver for the ics951601 device which is a general
467c478bd9Sstevel@tonic-gate  * purpose clock generator.  Setting a clock to 1 enables the clock while
477c478bd9Sstevel@tonic-gate  * setting it to 0 disables it.  All clocks are enabled by default by
487c478bd9Sstevel@tonic-gate  * the driver.  The user can read a clock, enable it or disable it.
497c478bd9Sstevel@tonic-gate  * The command sent as an ioctl argument should be the bitwise OR of the
507c478bd9Sstevel@tonic-gate  * clock number and the action upon it.  The supported clock numbers and
517c478bd9Sstevel@tonic-gate  * actions are defined in ics951601.h.  A pointer to an integer is sent
527c478bd9Sstevel@tonic-gate  * as the third  ioctl argument.  If the clock is to be read the value of the
537c478bd9Sstevel@tonic-gate  * clock is copied into it and if it is to be modified the integer referred
547c478bd9Sstevel@tonic-gate  * to should have the appropriate value of either 1 or 0.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * cb ops
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate static int ics951601_open(dev_t *, int, int, cred_t *);
617c478bd9Sstevel@tonic-gate static int ics951601_close(dev_t, int, int, cred_t *);
627c478bd9Sstevel@tonic-gate static int ics951601_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * dev ops
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate static int ics951601_s_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
687c478bd9Sstevel@tonic-gate static int ics951601_s_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
697c478bd9Sstevel@tonic-gate static int ics951601_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static struct cb_ops ics951601_cb_ops = {
727c478bd9Sstevel@tonic-gate 	ics951601_open,			/* open */
737c478bd9Sstevel@tonic-gate 	ics951601_close,		/* close */
747c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
757c478bd9Sstevel@tonic-gate 	nodev,				/* print */
767c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
777c478bd9Sstevel@tonic-gate 	nodev,				/* read */
787c478bd9Sstevel@tonic-gate 	nodev,				/* write */
797c478bd9Sstevel@tonic-gate 	ics951601_ioctl,		/* ioctl */
807c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
817c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
827c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
837c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
847c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
857c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
867c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static struct dev_ops ics951601_dev_ops = {
907c478bd9Sstevel@tonic-gate 	DEVO_REV,
917c478bd9Sstevel@tonic-gate 	0,
927c478bd9Sstevel@tonic-gate 	ics951601_info,
937c478bd9Sstevel@tonic-gate 	nulldev,
947c478bd9Sstevel@tonic-gate 	nulldev,
957c478bd9Sstevel@tonic-gate 	ics951601_s_attach,
967c478bd9Sstevel@tonic-gate 	ics951601_s_detach,
977c478bd9Sstevel@tonic-gate 	nodev,
987c478bd9Sstevel@tonic-gate 	&ics951601_cb_ops,
9919397407SSherry Moore 	NULL,
10019397407SSherry Moore 	NULL,
10119397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1027c478bd9Sstevel@tonic-gate };
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static struct modldrv ics951601_modldrv = {
1057c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* type of module - driver */
10619397407SSherry Moore 	"ics951601 device driver",
1077c478bd9Sstevel@tonic-gate 	&ics951601_dev_ops,
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static struct modlinkage ics951601_modlinkage = {
1117c478bd9Sstevel@tonic-gate 	MODREV_1,
1127c478bd9Sstevel@tonic-gate 	&ics951601_modldrv,
1137c478bd9Sstevel@tonic-gate 	0
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Writes to the clock generator involve sending the dummy command code, the
1187c478bd9Sstevel@tonic-gate  * dummy byte count followed by byte 0 through byte 5. The dummy command code
1197c478bd9Sstevel@tonic-gate  * and the dummy byte count are ignored by the ICS clock but must be sent.
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  * On reading from the clock generator, the controller will first receive a
1227c478bd9Sstevel@tonic-gate  * byte count followed by byte 0 through byte 5.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * The array for initializing the internal registers at attach time.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate static uchar_t init_clock_regs[8] = {
1297c478bd9Sstevel@tonic-gate 	0x0,	/* Dummy command code */
1307c478bd9Sstevel@tonic-gate 	0x7,	/* Dummy byte count */
1317c478bd9Sstevel@tonic-gate 	0x0,	/* Initial value for functionality register */
1327c478bd9Sstevel@tonic-gate 	0xff,	/* Initial value for PCI1A stop clocks register */
1337c478bd9Sstevel@tonic-gate 	0xff,	/* Initial value for PCI2A stop clocks register */
1347c478bd9Sstevel@tonic-gate 	0xff,	/* Initial value for PCI2B stop clocks register */
1357c478bd9Sstevel@tonic-gate 	0xff,	/* Default value for reserved register */
1367c478bd9Sstevel@tonic-gate 	0xef	/* Default value for latched input read back register */
1377c478bd9Sstevel@tonic-gate };
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static void *ics951601_soft_statep;
1407c478bd9Sstevel@tonic-gate int ics951601_debug = 0;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate int
_init(void)1437c478bd9Sstevel@tonic-gate _init(void)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	int    err;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	err = mod_install(&ics951601_modlinkage);
1487c478bd9Sstevel@tonic-gate 	if (err == 0) {
1497c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_init(&ics951601_soft_statep,
1507c478bd9Sstevel@tonic-gate 		    sizeof (ics951601_unit_t), 1);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	return (err);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate int
_fini(void)1567c478bd9Sstevel@tonic-gate _fini(void)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	int    err;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	err = mod_remove(&ics951601_modlinkage);
1617c478bd9Sstevel@tonic-gate 	if (err == 0) {
1627c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&ics951601_soft_statep);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	return (err);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1687c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	return (mod_info(&ics951601_modlinkage, modinfop));
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static int
ics951601_open(dev_t * devp,int flags,int otyp,cred_t * credp)1747c478bd9Sstevel@tonic-gate ics951601_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	int			instance;
1777c478bd9Sstevel@tonic-gate 	ics951601_unit_t	*icsp;
1787c478bd9Sstevel@tonic-gate 	int			err = EBUSY;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * Make sure the open is for the right file type
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
1847c478bd9Sstevel@tonic-gate 		return (EINVAL);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	instance = getminor(*devp);
1887c478bd9Sstevel@tonic-gate 	if (instance < 0) {
1897c478bd9Sstevel@tonic-gate 		return (ENXIO);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	icsp = (ics951601_unit_t *)ddi_get_soft_state(ics951601_soft_statep,
1927c478bd9Sstevel@tonic-gate 	    instance);
1937c478bd9Sstevel@tonic-gate 	if (icsp == NULL) {
1947c478bd9Sstevel@tonic-gate 		return (ENXIO);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* must be privileged to access this device */
1987c478bd9Sstevel@tonic-gate 	if (drv_priv(credp) != 0) {
1997c478bd9Sstevel@tonic-gate 		return (EPERM);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * Enforce exclusive access if required
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
2067c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
2077c478bd9Sstevel@tonic-gate 		if (icsp->ics951601_oflag == 0) {
2087c478bd9Sstevel@tonic-gate 			icsp->ics951601_oflag = FEXCL;
2097c478bd9Sstevel@tonic-gate 			err = DDI_SUCCESS;
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	} else if (icsp->ics951601_oflag != FEXCL) {
212f47a9c50Smathue 		icsp->ics951601_oflag = (uint16_t)FOPEN;
2137c478bd9Sstevel@tonic-gate 		err = DDI_SUCCESS;
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
2167c478bd9Sstevel@tonic-gate 	return (err);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static int
ics951601_close(dev_t dev,int flags,int otyp,cred_t * credp)2207c478bd9Sstevel@tonic-gate ics951601_close(dev_t dev, int flags, int otyp, cred_t *credp)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, credp))
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	int		instance;
225*5be38ebbSToomas Soome 	ics951601_unit_t	*icsp;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
228*5be38ebbSToomas Soome 	 *	Make sure the close is for the right file type
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
2317c478bd9Sstevel@tonic-gate 		return (EINVAL);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
2357c478bd9Sstevel@tonic-gate 	icsp = (ics951601_unit_t *)ddi_get_soft_state(ics951601_soft_statep,
2367c478bd9Sstevel@tonic-gate 	    instance);
2377c478bd9Sstevel@tonic-gate 	if (icsp == NULL) {
2387c478bd9Sstevel@tonic-gate 		return (ENXIO);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
2427c478bd9Sstevel@tonic-gate 	icsp->ics951601_oflag = 0;
2437c478bd9Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
2447c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static int
ics951601_attach(dev_info_t * dip)2487c478bd9Sstevel@tonic-gate ics951601_attach(dev_info_t *dip)
2497c478bd9Sstevel@tonic-gate {
250*5be38ebbSToomas Soome 	ics951601_unit_t	*icsp;
251*5be38ebbSToomas Soome 	int			instance = ddi_get_instance(dip);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(ics951601_soft_statep, instance) != 0) {
2547c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d failed to zalloc softstate",
2557c478bd9Sstevel@tonic-gate 		    ddi_get_name(dip), instance);
2567c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	icsp = ddi_get_soft_state(ics951601_soft_statep, instance);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (icsp == NULL) {
2627c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	mutex_init(&icsp->ics951601_mutex, NULL, MUTEX_DRIVER, NULL);
2667c478bd9Sstevel@tonic-gate 	cv_init(&icsp->ics951601_cv, NULL, CV_DRIVER, NULL);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	(void) snprintf(icsp->ics951601_name, sizeof (icsp->ics951601_name),
2697c478bd9Sstevel@tonic-gate 	    "%s_%d", ddi_driver_name(dip), instance);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, icsp->ics951601_name, S_IFCHR,
273*5be38ebbSToomas Soome 	    instance, ICS951601_NODE_TYPE, 0) == DDI_FAILURE) {
2747c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed",
2757c478bd9Sstevel@tonic-gate 		    icsp->ics951601_name);
2767c478bd9Sstevel@tonic-gate 		goto ATTACH_ERR;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * preallocate a single buffer for all reads and writes
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	if (i2c_transfer_alloc(icsp->ics951601_hdl, &icsp->ics951601_transfer,
2837c478bd9Sstevel@tonic-gate 	    0, 0, I2C_SLEEP) != I2C_SUCCESS) {
2847c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s i2c_transfer_alloc failed",
2857c478bd9Sstevel@tonic-gate 		    icsp->ics951601_name);
2867c478bd9Sstevel@tonic-gate 		goto CREATE_NODE_ERR;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	icsp->ics951601_cpr_state[0] = 0x0;
2897c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_version = I2C_XFER_REV;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &icsp->ics951601_hdl) != I2C_SUCCESS) {
2927c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s i2c_client_register failed",
2937c478bd9Sstevel@tonic-gate 		    icsp->ics951601_name);
2947c478bd9Sstevel@tonic-gate 		goto ALLOC_ERR;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* Enable all clocks */
2987c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_WR;
2997c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = ICS951601_I2C_WRITE_TRANS_SIZE;
3007c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = 0;
3017c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wbuf = init_clock_regs;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
3047c478bd9Sstevel@tonic-gate 	    != I2C_SUCCESS) {
3057c478bd9Sstevel@tonic-gate 		goto REG_ERR;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * Store the dip for future use
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	icsp->ics951601_dip = dip;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3147c478bd9Sstevel@tonic-gate REG_ERR:
3157c478bd9Sstevel@tonic-gate 	i2c_client_unregister(icsp->ics951601_hdl);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate ALLOC_ERR:
3187c478bd9Sstevel@tonic-gate 	i2c_transfer_free(icsp->ics951601_hdl, icsp->ics951601_transfer);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate CREATE_NODE_ERR:
3217c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate ATTACH_ERR:
3247c478bd9Sstevel@tonic-gate 	cv_destroy(&icsp->ics951601_cv);
3257c478bd9Sstevel@tonic-gate 	mutex_destroy(&icsp->ics951601_mutex);
3267c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(ics951601_soft_statep, instance);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate static void
ics951601_detach(dev_info_t * dip)3337c478bd9Sstevel@tonic-gate ics951601_detach(dev_info_t *dip)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	ics951601_unit_t *icsp;
336*5be38ebbSToomas Soome 	int		instance;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3397c478bd9Sstevel@tonic-gate 	icsp = ddi_get_soft_state(ics951601_soft_statep, instance);
3407c478bd9Sstevel@tonic-gate 	cv_destroy(&icsp->ics951601_cv);
3417c478bd9Sstevel@tonic-gate 	mutex_destroy(&icsp->ics951601_mutex);
3427c478bd9Sstevel@tonic-gate 	i2c_client_unregister(icsp->ics951601_hdl);
3437c478bd9Sstevel@tonic-gate 	i2c_transfer_free(icsp->ics951601_hdl, icsp->ics951601_transfer);
3447c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3457c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(ics951601_soft_statep, instance);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate static int
ics951601_info(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3497c478bd9Sstevel@tonic-gate ics951601_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	ics951601_unit_t	*icsp;
3547c478bd9Sstevel@tonic-gate 	int			instance = getminor((dev_t)arg);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	switch (cmd) {
3577c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3587c478bd9Sstevel@tonic-gate 		icsp = ddi_get_soft_state(ics951601_soft_statep, instance);
3597c478bd9Sstevel@tonic-gate 		if (icsp == NULL) {
3607c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		*result = (void *)icsp->ics951601_dip;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3657c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
366f47a9c50Smathue 		*result = (void *)(uintptr_t)instance;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3697c478bd9Sstevel@tonic-gate 	default:
3707c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static int
ics951601_suspend(dev_info_t * dip)3757c478bd9Sstevel@tonic-gate ics951601_suspend(dev_info_t *dip)
3767c478bd9Sstevel@tonic-gate {
377*5be38ebbSToomas Soome 	ics951601_unit_t	*icsp;
3787c478bd9Sstevel@tonic-gate 	int		instance = ddi_get_instance(dip);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	icsp = ddi_get_soft_state(ics951601_soft_statep, instance);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Set the busy flag so that future transactions block
3847c478bd9Sstevel@tonic-gate 	 * until resume.
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
3877c478bd9Sstevel@tonic-gate 	while ((icsp->ics951601_flags & ICS951601_BUSYFLAG) ==
3887c478bd9Sstevel@tonic-gate 	    ICS951601_BUSYFLAG) {
3897c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&icsp->ics951601_cv,
3907c478bd9Sstevel@tonic-gate 		    &icsp->ics951601_mutex) <= 0) {
3917c478bd9Sstevel@tonic-gate 			mutex_exit(&icsp->ics951601_mutex);
3927c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 	icsp->ics951601_flags |= ICS951601_BUSYFLAG;
3967c478bd9Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_RD;
3997c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = 0;
4007c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = ICS951601_I2C_READ_TRANS_SIZE;
4017c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rbuf = icsp->ics951601_cpr_state + 1;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
4047c478bd9Sstevel@tonic-gate 	    != I2C_SUCCESS) {
4057c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s Suspend failed, unable to save registers",
4067c478bd9Sstevel@tonic-gate 		    icsp->ics951601_name);
4077c478bd9Sstevel@tonic-gate 		return (EIO);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate static int
ics951601_resume(dev_info_t * dip)4147c478bd9Sstevel@tonic-gate ics951601_resume(dev_info_t *dip)
4157c478bd9Sstevel@tonic-gate {
416*5be38ebbSToomas Soome 	int		instance = ddi_get_instance(dip);
4177c478bd9Sstevel@tonic-gate 	ics951601_unit_t	*icsp;
418*5be38ebbSToomas Soome 	int		err = DDI_SUCCESS;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	icsp = (ics951601_unit_t *)
4217c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(ics951601_soft_statep, instance);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (icsp == NULL) {
4247c478bd9Sstevel@tonic-gate 		return (ENXIO);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
4287c478bd9Sstevel@tonic-gate 	 * Restore registers to status existing before cpr
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_WR;
4317c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = 0;
4327c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = ICS951601_I2C_WRITE_TRANS_SIZE;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wbuf = icsp->ics951601_cpr_state;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
4377c478bd9Sstevel@tonic-gate 	    != I2C_SUCCESS) {
4387c478bd9Sstevel@tonic-gate 		err = EIO;
4397c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, " %s Unable to restore registers",
4407c478bd9Sstevel@tonic-gate 		    icsp->ics951601_name);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/*
4447c478bd9Sstevel@tonic-gate 	 * Clear busy flag so that transactions may continue
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
4477c478bd9Sstevel@tonic-gate 	icsp->ics951601_flags = icsp->ics951601_flags & ~ICS951601_BUSYFLAG;
4487c478bd9Sstevel@tonic-gate 	cv_signal(&icsp->ics951601_cv);
4497c478bd9Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
4507c478bd9Sstevel@tonic-gate 	return (err);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate static int
ics951601_s_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4547c478bd9Sstevel@tonic-gate ics951601_s_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	switch (cmd) {
4577c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4587c478bd9Sstevel@tonic-gate 		return (ics951601_attach(dip));
4597c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
4607c478bd9Sstevel@tonic-gate 		return (ics951601_resume(dip));
4617c478bd9Sstevel@tonic-gate 	default:
4627c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate static int
ics951601_s_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4677c478bd9Sstevel@tonic-gate ics951601_s_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	switch (cmd) {
4707c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4717c478bd9Sstevel@tonic-gate 		ics951601_detach(dip);
4727c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4737c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
4747c478bd9Sstevel@tonic-gate 		return (ics951601_suspend(dip));
4757c478bd9Sstevel@tonic-gate 	default:
4767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static int
ics951601_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)4817c478bd9Sstevel@tonic-gate ics951601_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
482*5be38ebbSToomas Soome     int *rvalp)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	ics951601_unit_t		*icsp;
4877c478bd9Sstevel@tonic-gate 	int			err = 0;
4887c478bd9Sstevel@tonic-gate 	uchar_t			temp_arr[ICS951601_I2C_WRITE_TRANS_SIZE];
4897c478bd9Sstevel@tonic-gate 	int			reg_no;
4907c478bd9Sstevel@tonic-gate 	uint8_t			clock_bit;
4917c478bd9Sstevel@tonic-gate 	int			ics_temp;
4927c478bd9Sstevel@tonic-gate 	int			instance = getminor(dev);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	temp_arr[0] = 0x0;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	icsp = (ics951601_unit_t *)
4977c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(ics951601_soft_statep, instance);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if (ics951601_debug) {
5007c478bd9Sstevel@tonic-gate 		printf("ics951601_ioctl: instance=%d\n", instance);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * We serialize here and  block if there are pending transacations .
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
5077c478bd9Sstevel@tonic-gate 	while ((icsp->ics951601_flags & ICS951601_BUSYFLAG) ==
5087c478bd9Sstevel@tonic-gate 	    ICS951601_BUSYFLAG) {
5097c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&icsp->ics951601_cv,
5107c478bd9Sstevel@tonic-gate 		    &icsp->ics951601_mutex) <= 0) {
5117c478bd9Sstevel@tonic-gate 			mutex_exit(&icsp->ics951601_mutex);
5127c478bd9Sstevel@tonic-gate 			return (EINTR);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	icsp->ics951601_flags |= ICS951601_BUSYFLAG;
5167c478bd9Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	reg_no	= ICS951601_CMD_TO_CLOCKREG(cmd);
5197c478bd9Sstevel@tonic-gate 	clock_bit = ICS951601_CMD_TO_CLOCKBIT(cmd);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_RD;
5227c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = 0;
5237c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = ICS951601_I2C_READ_TRANS_SIZE;
5247c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rbuf = temp_arr + 1;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
5277c478bd9Sstevel@tonic-gate 	    != I2C_SUCCESS) {
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		err = DDI_FAILURE;
5307c478bd9Sstevel@tonic-gate 		goto cleanup;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 	switch (ICS951601_CMD_TO_ACTION(cmd)) {
5337c478bd9Sstevel@tonic-gate 	case ICS951601_READ_CLOCK:
5347c478bd9Sstevel@tonic-gate 		temp_arr[reg_no] &= clock_bit;
5357c478bd9Sstevel@tonic-gate 		ics_temp = temp_arr[reg_no] ? ICS951601_CLOCK_SET:
5367c478bd9Sstevel@tonic-gate 		    ICS951601_CLOCK_CLEAR;
5377c478bd9Sstevel@tonic-gate 		err = ddi_copyout((caddr_t)&ics_temp, (caddr_t)arg,
5387c478bd9Sstevel@tonic-gate 		    sizeof (int), mode);
5397c478bd9Sstevel@tonic-gate 		goto cleanup;
5407c478bd9Sstevel@tonic-gate 	case ICS951601_MODIFY_CLOCK:
5417c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ics_temp,
5427c478bd9Sstevel@tonic-gate 		    sizeof (int), mode) != DDI_SUCCESS) {
5437c478bd9Sstevel@tonic-gate 			err = EIO;
5447c478bd9Sstevel@tonic-gate 			goto cleanup;
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 		if (ics_temp == ICS951601_CLOCK_SET) {
5477c478bd9Sstevel@tonic-gate 			temp_arr[reg_no] |= clock_bit;
5487c478bd9Sstevel@tonic-gate 		} else if (ics_temp == ICS951601_CLOCK_CLEAR) {
5497c478bd9Sstevel@tonic-gate 			temp_arr[reg_no] &= ~clock_bit;
5507c478bd9Sstevel@tonic-gate 		} else {
5517c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s Clock can only be set to 1 or 0",
5527c478bd9Sstevel@tonic-gate 			    icsp->ics951601_name);
5537c478bd9Sstevel@tonic-gate 			err = EINVAL;
5547c478bd9Sstevel@tonic-gate 			goto cleanup;
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 		break;
5577c478bd9Sstevel@tonic-gate 	default:
5587c478bd9Sstevel@tonic-gate 		err = EINVAL;
5597c478bd9Sstevel@tonic-gate 		goto cleanup;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_flags = I2C_WR;
5637c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wlen = ICS951601_I2C_WRITE_TRANS_SIZE;
5647c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_rlen = 0;
5657c478bd9Sstevel@tonic-gate 	icsp->ics951601_transfer->i2c_wbuf = temp_arr;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (i2c_transfer(icsp->ics951601_hdl, icsp->ics951601_transfer)
5687c478bd9Sstevel@tonic-gate 	    != I2C_SUCCESS) {
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		err = DDI_FAILURE;
5717c478bd9Sstevel@tonic-gate 		goto cleanup;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate cleanup:
5747c478bd9Sstevel@tonic-gate 	mutex_enter(&icsp->ics951601_mutex);
5757c478bd9Sstevel@tonic-gate 	icsp->ics951601_flags  = icsp->ics951601_flags & ~ICS951601_BUSYFLAG;
5767c478bd9Sstevel@tonic-gate 	cv_signal(&icsp->ics951601_cv);
5777c478bd9Sstevel@tonic-gate 	mutex_exit(&icsp->ics951601_mutex);
5787c478bd9Sstevel@tonic-gate 	return (err);
5797c478bd9Sstevel@tonic-gate }
580