xref: /titanic_41/usr/src/uts/sun4u/io/i2c/clients/tda8444.c (revision 193974072f41a843678abf5f61979c748687e66b)
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
5*19397407SSherry Moore  * Common Development and Distribution License (the "License").
6*19397407SSherry 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 /*
22*19397407SSherry 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/tda8444_impl.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * cb ops
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate static int tda8444_open(dev_t *, int, int, cred_t *);
477c478bd9Sstevel@tonic-gate static int tda8444_close(dev_t, int, int, cred_t *);
487c478bd9Sstevel@tonic-gate static int tda8444_read(dev_t dev, struct uio *uiop, cred_t *cred_p);
497c478bd9Sstevel@tonic-gate static int tda8444_write(dev_t dev, struct uio *uiop, cred_t *cred_p);
507c478bd9Sstevel@tonic-gate static int tda8444_io(dev_t dev, struct uio *uiop, int rw);
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * dev ops
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate static int tda8444_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
557c478bd9Sstevel@tonic-gate 		void **result);
567c478bd9Sstevel@tonic-gate static int tda8444_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
577c478bd9Sstevel@tonic-gate static int tda8444_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static struct cb_ops tda8444_cbops = {
607c478bd9Sstevel@tonic-gate 	tda8444_open,			/* open */
617c478bd9Sstevel@tonic-gate 	tda8444_close,			/* close */
627c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
637c478bd9Sstevel@tonic-gate 	nodev,				/* print */
647c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
657c478bd9Sstevel@tonic-gate 	tda8444_read,			/* read */
667c478bd9Sstevel@tonic-gate 	tda8444_write,			/* write */
677c478bd9Sstevel@tonic-gate 	nodev,				/* ioctl */
687c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
697c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
707c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
717c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
727c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
737c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
747c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
757c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
767c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
777c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static struct dev_ops tda8444_ops = {
817c478bd9Sstevel@tonic-gate 	DEVO_REV,
827c478bd9Sstevel@tonic-gate 	0,
837c478bd9Sstevel@tonic-gate 	tda8444_info,
847c478bd9Sstevel@tonic-gate 	nulldev,
857c478bd9Sstevel@tonic-gate 	nulldev,
867c478bd9Sstevel@tonic-gate 	tda8444_attach,
877c478bd9Sstevel@tonic-gate 	tda8444_detach,
887c478bd9Sstevel@tonic-gate 	nodev,
897c478bd9Sstevel@tonic-gate 	&tda8444_cbops,
90*19397407SSherry Moore 	NULL,
91*19397407SSherry Moore 	NULL,
92*19397407SSherry Moore 	ddi_quiesce_not_supported,	/* devo_quiesce */
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static struct modldrv tda8444_modldrv = {
967c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* type of module - driver */
97*19397407SSherry Moore 	"tda8444 device driver",
987c478bd9Sstevel@tonic-gate 	&tda8444_ops,
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static struct modlinkage tda8444_modlinkage = {
1027c478bd9Sstevel@tonic-gate 	MODREV_1,
1037c478bd9Sstevel@tonic-gate 	&tda8444_modldrv,
1047c478bd9Sstevel@tonic-gate 	0
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static void *tda8444_soft_statep;
1087c478bd9Sstevel@tonic-gate static int tda8444_debug = 0;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate int
_init(void)1117c478bd9Sstevel@tonic-gate _init(void)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	int    error;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	error = mod_install(&tda8444_modlinkage);
1167c478bd9Sstevel@tonic-gate 	if (error == 0) {
1177c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_init(&tda8444_soft_statep,
1187c478bd9Sstevel@tonic-gate 		    sizeof (struct tda8444_unit), TDA8444_MAX_DACS);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	return (error);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate int
_fini(void)1257c478bd9Sstevel@tonic-gate _fini(void)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	int    error;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	error = mod_remove(&tda8444_modlinkage);
1307c478bd9Sstevel@tonic-gate 	if (error == 0) {
1317c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&tda8444_soft_statep);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return (error);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1387c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	return (mod_info(&tda8444_modlinkage, modinfop));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /* ARGSUSED */
1447c478bd9Sstevel@tonic-gate static int
tda8444_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1457c478bd9Sstevel@tonic-gate tda8444_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	dev_t	dev;
1487c478bd9Sstevel@tonic-gate 	int	instance;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (infocmd == DDI_INFO_DEVT2INSTANCE) {
1517c478bd9Sstevel@tonic-gate 		dev = (dev_t)arg;
1527c478bd9Sstevel@tonic-gate 		instance = TDA8444_MINOR_TO_DEVINST(dev);
1537c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
1547c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static int
tda8444_do_resume(dev_info_t * dip)1607c478bd9Sstevel@tonic-gate tda8444_do_resume(dev_info_t *dip)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
1637c478bd9Sstevel@tonic-gate 	struct tda8444_unit *unitp;
1647c478bd9Sstevel@tonic-gate 	int channel;
1657c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	unitp = (struct tda8444_unit *)
1687c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(tda8444_soft_statep, instance);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		return (ENXIO);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	for (channel = 0; channel < TDA8444_CHANS; channel++) {
1767c478bd9Sstevel@tonic-gate 		unitp->tda8444_transfer->i2c_wbuf[0] = TDA8444_REGBASE |
1777c478bd9Sstevel@tonic-gate 		    channel;
1787c478bd9Sstevel@tonic-gate 		unitp->tda8444_transfer->i2c_wbuf[1] =
1797c478bd9Sstevel@tonic-gate 		    unitp->tda8444_output[channel];
1807c478bd9Sstevel@tonic-gate 		DPRINTF(RESUME, ("tda8444_resume: setting channel %d to %d",
1817c478bd9Sstevel@tonic-gate 		    channel, unitp->tda8444_output[channel]));
1827c478bd9Sstevel@tonic-gate 		if (i2c_transfer(unitp->tda8444_hdl,
1837c478bd9Sstevel@tonic-gate 		    unitp->tda8444_transfer) != I2C_SUCCESS) {
1847c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->tda8444_mutex);
1897c478bd9Sstevel@tonic-gate 	unitp->tda8444_flags = 0;
1907c478bd9Sstevel@tonic-gate 	cv_signal(&unitp->tda8444_cv);
1917c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->tda8444_mutex);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (ret);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static int
tda8444_do_attach(dev_info_t * dip)1977c478bd9Sstevel@tonic-gate tda8444_do_attach(dev_info_t *dip)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	struct tda8444_unit *unitp;
2007c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN];
2017c478bd9Sstevel@tonic-gate 	int instance;
2027c478bd9Sstevel@tonic-gate 	minor_t minor;
2037c478bd9Sstevel@tonic-gate 	int i;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(tda8444_soft_statep, instance) != 0) {
2087c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d failed to zalloc softstate",
2097c478bd9Sstevel@tonic-gate 		    ddi_get_name(dip), instance);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(tda8444_soft_statep, instance);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
2177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) snprintf(unitp->tda8444_name, sizeof (unitp->tda8444_name),
2217c478bd9Sstevel@tonic-gate 	    "%s%d", ddi_driver_name(dip), instance);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	for (i = 0; i < TDA8444_CHANS; i++) {
2247c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%d", i);
2257c478bd9Sstevel@tonic-gate 		minor = TDA8444_CHANNEL_TO_MINOR(i) |
2267c478bd9Sstevel@tonic-gate 		    TDA8444_DEVINST_TO_MINOR(instance);
2277c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor,
2287c478bd9Sstevel@tonic-gate 		    TDA8444_NODE_TYPE, NULL) == DDI_FAILURE) {
2297c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s ddi_create_minor_node failed",
2307c478bd9Sstevel@tonic-gate 			    unitp->tda8444_name);
2317c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(tda8444_soft_statep, instance);
2327c478bd9Sstevel@tonic-gate 			ddi_remove_minor_node(dip, NULL);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 		unitp->tda8444_output[i] = TDA8444_UNKNOWN_OUT;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * preallocate a single buffer for all writes
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	if (i2c_transfer_alloc(unitp->tda8444_hdl, &unitp->tda8444_transfer,
2437c478bd9Sstevel@tonic-gate 	    2, 0, I2C_SLEEP) != I2C_SUCCESS) {
2447c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i2c_transfer_alloc failed");
2457c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2467c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(tda8444_soft_statep, instance);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	unitp->tda8444_transfer->i2c_flags = I2C_WR;
2517c478bd9Sstevel@tonic-gate 	unitp->tda8444_transfer->i2c_version = I2C_XFER_REV;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->tda8444_hdl) != I2C_SUCCESS) {
2547c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2557c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i2c_client_register failed");
2567c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(tda8444_soft_statep, instance);
2577c478bd9Sstevel@tonic-gate 		i2c_transfer_free(unitp->tda8444_hdl, unitp->tda8444_transfer);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	mutex_init(&unitp->tda8444_mutex, NULL, MUTEX_DRIVER, NULL);
2637c478bd9Sstevel@tonic-gate 	cv_init(&unitp->tda8444_cv, NULL, CV_DRIVER, NULL);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static int
tda8444_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2697c478bd9Sstevel@tonic-gate tda8444_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	switch (cmd) {
2727c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		return (tda8444_do_attach(dip));
2757c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		return (tda8444_do_resume(dip));
2787c478bd9Sstevel@tonic-gate 	default:
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static int
tda8444_do_detach(dev_info_t * dip)2857c478bd9Sstevel@tonic-gate tda8444_do_detach(dev_info_t *dip)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	struct tda8444_unit *unitp;
2887c478bd9Sstevel@tonic-gate 	int instance;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2917c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(tda8444_soft_statep, instance);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	i2c_transfer_free(unitp->tda8444_hdl, unitp->tda8444_transfer);
2947c478bd9Sstevel@tonic-gate 	i2c_client_unregister(unitp->tda8444_hdl);
2957c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2967c478bd9Sstevel@tonic-gate 	mutex_destroy(&unitp->tda8444_mutex);
2977c478bd9Sstevel@tonic-gate 	cv_destroy(&unitp->tda8444_cv);
2987c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(tda8444_soft_statep, instance);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static int
tda8444_do_suspend(dev_info_t * dip)3047c478bd9Sstevel@tonic-gate tda8444_do_suspend(dev_info_t *dip)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	struct tda8444_unit *unitp;
3077c478bd9Sstevel@tonic-gate 	int instance;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3107c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(tda8444_soft_statep, instance);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * Set the busy flag so that future transactions block
3147c478bd9Sstevel@tonic-gate 	 * until resume.
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->tda8444_mutex);
3177c478bd9Sstevel@tonic-gate 	while (unitp->tda8444_flags == TDA8444_BUSY) {
3187c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&unitp->tda8444_cv,
3197c478bd9Sstevel@tonic-gate 		    &unitp->tda8444_mutex) <= 0) {
3207c478bd9Sstevel@tonic-gate 			mutex_exit(&unitp->tda8444_mutex);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 	unitp->tda8444_flags = TDA8444_SUSPENDED;
3267c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->tda8444_mutex);
3277c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate static int
tda8444_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3317c478bd9Sstevel@tonic-gate tda8444_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	switch (cmd) {
3347c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		return (tda8444_do_detach(dip));
3377c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		return (tda8444_do_suspend(dip));
3407c478bd9Sstevel@tonic-gate 	default:
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate static int
tda8444_open(dev_t * devp,int flags,int otyp,cred_t * credp)3477c478bd9Sstevel@tonic-gate tda8444_open(dev_t *devp, int flags, int otyp, cred_t *credp)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
3507c478bd9Sstevel@tonic-gate 	struct tda8444_unit *unitp;
3517c478bd9Sstevel@tonic-gate 	int err = 0;
3527c478bd9Sstevel@tonic-gate 	int instance = TDA8444_MINOR_TO_DEVINST(*devp);
3537c478bd9Sstevel@tonic-gate 	int channel = TDA8444_MINOR_TO_CHANNEL(*devp);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (instance < 0) {
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		return (ENXIO);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	unitp = (struct tda8444_unit *)
3617c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(tda8444_soft_statep, instance);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		return (ENXIO);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		return (EINVAL);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->tda8444_mutex);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
3767c478bd9Sstevel@tonic-gate 		if (unitp->tda8444_oflag[channel] != 0) {
3777c478bd9Sstevel@tonic-gate 			err = EBUSY;
3787c478bd9Sstevel@tonic-gate 		} else {
3797c478bd9Sstevel@tonic-gate 			unitp->tda8444_oflag[channel] = FEXCL;
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 	} else {
3827c478bd9Sstevel@tonic-gate 		if (unitp->tda8444_oflag[channel] == FEXCL) {
3837c478bd9Sstevel@tonic-gate 			err = EBUSY;
3847c478bd9Sstevel@tonic-gate 		} else {
385f47a9c50Smathue 			unitp->tda8444_oflag[channel] = (uint16_t)FOPEN;
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->tda8444_mutex);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	return (err);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static int
tda8444_close(dev_t dev,int flags,int otyp,cred_t * credp)3957c478bd9Sstevel@tonic-gate tda8444_close(dev_t dev, int flags, int otyp, cred_t *credp)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
3987c478bd9Sstevel@tonic-gate 	struct tda8444_unit *unitp;
3997c478bd9Sstevel@tonic-gate 	int instance = TDA8444_MINOR_TO_DEVINST(dev);
4007c478bd9Sstevel@tonic-gate 	int channel = TDA8444_MINOR_TO_CHANNEL(dev);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (instance < 0) {
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		return (ENXIO);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	unitp = (struct tda8444_unit *)
4087c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(tda8444_soft_statep, instance);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		return (ENXIO);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->tda8444_mutex);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	unitp->tda8444_oflag[channel] = 0;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->tda8444_mutex);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate static int
tda8444_read(dev_t dev,struct uio * uiop,cred_t * cred_p)4257c478bd9Sstevel@tonic-gate tda8444_read(dev_t dev, struct uio *uiop, cred_t *cred_p)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(cred_p))
4287c478bd9Sstevel@tonic-gate 	return (tda8444_io(dev, uiop, B_READ));
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate static int
tda8444_write(dev_t dev,struct uio * uiop,cred_t * cred_p)4327c478bd9Sstevel@tonic-gate tda8444_write(dev_t dev, struct uio *uiop, cred_t *cred_p)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(cred_p))
4357c478bd9Sstevel@tonic-gate 	return (tda8444_io(dev, uiop, B_WRITE));
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate static int
tda8444_io(dev_t dev,struct uio * uiop,int rw)4397c478bd9Sstevel@tonic-gate tda8444_io(dev_t dev, struct uio *uiop, int rw)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	struct tda8444_unit *unitp;
4427c478bd9Sstevel@tonic-gate 	int instance = TDA8444_MINOR_TO_DEVINST(getminor(dev));
4437c478bd9Sstevel@tonic-gate 	int channel = TDA8444_MINOR_TO_CHANNEL(getminor(dev));
4447c478bd9Sstevel@tonic-gate 	int ret = 0;
4457c478bd9Sstevel@tonic-gate 	size_t len = uiop->uio_resid;
4467c478bd9Sstevel@tonic-gate 	int8_t out_value;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (instance < 0) {
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		return (ENXIO);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (len == 0) {
4547c478bd9Sstevel@tonic-gate 		return (0);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	unitp = (struct tda8444_unit *)
4587c478bd9Sstevel@tonic-gate 	    ddi_get_soft_state(tda8444_soft_statep, instance);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		return (ENXIO);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (rw == B_READ) {
4667c478bd9Sstevel@tonic-gate 		if (unitp->tda8444_output[channel] != TDA8444_UNKNOWN_OUT) {
4677c478bd9Sstevel@tonic-gate 			return (uiomove(&unitp->tda8444_output[channel], 1,
4687c478bd9Sstevel@tonic-gate 			    UIO_READ, uiop));
4697c478bd9Sstevel@tonic-gate 		} else {
4707c478bd9Sstevel@tonic-gate 			return (EIO);
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	/*
4757c478bd9Sstevel@tonic-gate 	 * rw == B_WRITE.  Make sure each write to a device is single
4767c478bd9Sstevel@tonic-gate 	 * threaded since we pre-allocate a single write buffer.  This is not a
4777c478bd9Sstevel@tonic-gate 	 * bottleneck since concurrent writes would serialize at the
4787c478bd9Sstevel@tonic-gate 	 * transport level anyway.
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->tda8444_mutex);
4817c478bd9Sstevel@tonic-gate 	if (unitp->tda8444_flags == TDA8444_SUSPENDED) {
4827c478bd9Sstevel@tonic-gate 		mutex_exit(&unitp->tda8444_mutex);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		return (EAGAIN);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	while (unitp->tda8444_flags == TDA8444_BUSY) {
4887c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&unitp->tda8444_cv,
4897c478bd9Sstevel@tonic-gate 		    &unitp->tda8444_mutex) <= 0) {
4907c478bd9Sstevel@tonic-gate 			mutex_exit(&unitp->tda8444_mutex);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 			return (EINTR);
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	unitp->tda8444_flags = TDA8444_BUSY;
4967c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->tda8444_mutex);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	unitp->tda8444_transfer->i2c_wbuf[0] = (TDA8444_REGBASE | channel);
4997c478bd9Sstevel@tonic-gate 	if ((ret = uiomove(&out_value, sizeof (out_value), UIO_WRITE,
5007c478bd9Sstevel@tonic-gate 	    uiop)) == 0) {
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		/*
5037c478bd9Sstevel@tonic-gate 		 * Check bounds
5047c478bd9Sstevel@tonic-gate 		 */
5057c478bd9Sstevel@tonic-gate 		if ((out_value > TDA8444_MAX_OUT) ||
5067c478bd9Sstevel@tonic-gate 		    (out_value < TDA8444_MIN_OUT)) {
5077c478bd9Sstevel@tonic-gate 			ret = EINVAL;
5087c478bd9Sstevel@tonic-gate 		} else {
5097c478bd9Sstevel@tonic-gate 			unitp->tda8444_transfer->i2c_wbuf[1] =
5107c478bd9Sstevel@tonic-gate 			    (uchar_t)out_value;
5117c478bd9Sstevel@tonic-gate 			DPRINTF(IO, ("setting channel %d to %d", channel,
5127c478bd9Sstevel@tonic-gate 			    unitp->tda8444_transfer->i2c_wbuf[1]));
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 			if (i2c_transfer(unitp->tda8444_hdl,
5157c478bd9Sstevel@tonic-gate 			    unitp->tda8444_transfer) != I2C_SUCCESS) {
5167c478bd9Sstevel@tonic-gate 				ret = EIO;
5177c478bd9Sstevel@tonic-gate 			} else {
5187c478bd9Sstevel@tonic-gate 				unitp->tda8444_output[channel] = out_value;
5197c478bd9Sstevel@tonic-gate 			}
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 	} else {
5227c478bd9Sstevel@tonic-gate 		ret = EFAULT;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->tda8444_mutex);
5267c478bd9Sstevel@tonic-gate 	unitp->tda8444_flags = 0;
5277c478bd9Sstevel@tonic-gate 	cv_signal(&unitp->tda8444_cv);
5287c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->tda8444_mutex);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	return (ret);
5317c478bd9Sstevel@tonic-gate }
532