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.
23f47a9c50Smathue  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/stat.h>		/* ddi_create_minor_node S_IFCHR */
277c478bd9Sstevel@tonic-gate #include <sys/modctl.h>		/* for modldrv */
287c478bd9Sstevel@tonic-gate #include <sys/open.h>		/* for open params.	 */
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>		/* req. by dev_ops flags MTSAFE etc. */
337c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/file.h>
357c478bd9Sstevel@tonic-gate #include <sys/note.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/pcf8591.h>
387c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/pcf8591_impl.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static void *pcf8591soft_statep;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static uint8_t ipmode = PCF8591_4SINGLE;
437c478bd9Sstevel@tonic-gate static int32_t current_value = 0;
447c478bd9Sstevel@tonic-gate static int current_set_flag = 0;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static int pcf8591_do_attach(dev_info_t *);
477c478bd9Sstevel@tonic-gate static int pcf8591_do_detach(dev_info_t *);
487c478bd9Sstevel@tonic-gate static int pcf8591_do_resume(void);
497c478bd9Sstevel@tonic-gate static int pcf8591_do_suspend(void);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * cb ops (only need ioctl)
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate static int pcf8591_open(dev_t *, int, int, cred_t *);
557c478bd9Sstevel@tonic-gate static int pcf8591_close(dev_t, int, int, cred_t *);
567c478bd9Sstevel@tonic-gate static int pcf8591_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static struct cb_ops pcf8591_cbops = {
597c478bd9Sstevel@tonic-gate 	pcf8591_open,			/* open  */
607c478bd9Sstevel@tonic-gate 	pcf8591_close,			/* close */
617c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
627c478bd9Sstevel@tonic-gate 	nodev,				/* print */
637c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
647c478bd9Sstevel@tonic-gate 	nodev,				/* read */
657c478bd9Sstevel@tonic-gate 	nodev,				/* write */
667c478bd9Sstevel@tonic-gate 	pcf8591_ioctl,			/* ioctl */
677c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
687c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
697c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
707c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
717c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
727c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
737c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
747c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
757c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
767c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * dev ops
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate static int pcf8591_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
837c478bd9Sstevel@tonic-gate 		void **result);
847c478bd9Sstevel@tonic-gate static int pcf8591_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
857c478bd9Sstevel@tonic-gate static int pcf8591_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static struct dev_ops pcf8591_ops = {
887c478bd9Sstevel@tonic-gate 	DEVO_REV,
897c478bd9Sstevel@tonic-gate 	0,
907c478bd9Sstevel@tonic-gate 	pcf8591_info,
917c478bd9Sstevel@tonic-gate 	nulldev,
927c478bd9Sstevel@tonic-gate 	nulldev,
937c478bd9Sstevel@tonic-gate 	pcf8591_attach,
947c478bd9Sstevel@tonic-gate 	pcf8591_detach,
957c478bd9Sstevel@tonic-gate 	nodev,
967c478bd9Sstevel@tonic-gate 	&pcf8591_cbops,
9719397407SSherry Moore 	NULL,
9819397407SSherry Moore 	NULL,
9919397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static struct modldrv pcf8591_modldrv = {
1057c478bd9Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
10688294e09SRichard Bean 	"PCF8591 i2c device driver",
1077c478bd9Sstevel@tonic-gate 	&pcf8591_ops
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static struct modlinkage pcf8591_modlinkage = {
1117c478bd9Sstevel@tonic-gate 	MODREV_1,
1127c478bd9Sstevel@tonic-gate 	&pcf8591_modldrv,
1137c478bd9Sstevel@tonic-gate 	0
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
_init(void)1187c478bd9Sstevel@tonic-gate _init(void)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int error;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	error = mod_install(&pcf8591_modlinkage);
1237c478bd9Sstevel@tonic-gate 	if (!error)
1247c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_init(&pcf8591soft_statep,
12519397407SSherry Moore 		    sizeof (struct pcf8591_unit), 1);
1267c478bd9Sstevel@tonic-gate 	return (error);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate int
_fini(void)1307c478bd9Sstevel@tonic-gate _fini(void)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	int error;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	error = mod_remove(&pcf8591_modlinkage);
1357c478bd9Sstevel@tonic-gate 	if (!error)
1367c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&pcf8591soft_statep);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	return (error);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1427c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	return (mod_info(&pcf8591_modlinkage, modinfop));
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static int
pcf8591_open(dev_t * devp,int flags,int otyp,cred_t * credp)1487c478bd9Sstevel@tonic-gate pcf8591_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	struct pcf8591_unit *unitp;
1537c478bd9Sstevel@tonic-gate 	int instance;
1547c478bd9Sstevel@tonic-gate 	int error = 0;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(*devp));
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (instance < 0) {
1597c478bd9Sstevel@tonic-gate 		return (ENXIO);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	unitp = (struct pcf8591_unit *)
16319397407SSherry Moore 	    ddi_get_soft_state(pcf8591soft_statep, instance);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
1667c478bd9Sstevel@tonic-gate 		return (ENXIO);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
1707c478bd9Sstevel@tonic-gate 		return (EINVAL);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8591_mutex);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
1767c478bd9Sstevel@tonic-gate 		if (unitp->pcf8591_oflag != 0) {
1777c478bd9Sstevel@tonic-gate 			error = EBUSY;
1787c478bd9Sstevel@tonic-gate 		} else {
1797c478bd9Sstevel@tonic-gate 			unitp->pcf8591_oflag = FEXCL;
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 	} else {
1827c478bd9Sstevel@tonic-gate 		if (unitp->pcf8591_oflag == FEXCL) {
1837c478bd9Sstevel@tonic-gate 			error = EBUSY;
1847c478bd9Sstevel@tonic-gate 		} else {
1857c478bd9Sstevel@tonic-gate 			unitp->pcf8591_oflag = FOPEN;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8591_mutex);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	return (error);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static int
pcf8591_close(dev_t dev,int flags,int otyp,cred_t * credp)1957c478bd9Sstevel@tonic-gate pcf8591_close(dev_t dev, int flags, int otyp, cred_t *credp)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	struct pcf8591_unit *unitp;
2007c478bd9Sstevel@tonic-gate 	int instance;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	instance = MINOR_TO_INST(getminor(dev));
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (instance < 0) {
2057c478bd9Sstevel@tonic-gate 		return (ENXIO);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	unitp = (struct pcf8591_unit *)
20919397407SSherry Moore 	    ddi_get_soft_state(pcf8591soft_statep, instance);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
2127c478bd9Sstevel@tonic-gate 		return (ENXIO);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8591_mutex);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	unitp->pcf8591_oflag = 0;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8591_mutex);
2207c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static int
pcf8591_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2247c478bd9Sstevel@tonic-gate pcf8591_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
225*a4ee5977SToomas Soome     cred_t *credp, int *rvalp)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	struct pcf8591_unit	*unitp;
2307c478bd9Sstevel@tonic-gate 	int			err = 0;
2317c478bd9Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2327c478bd9Sstevel@tonic-gate 	int port = MINOR_TO_PORT(getminor(dev));
2337c478bd9Sstevel@tonic-gate 	int instance = MINOR_TO_INST(getminor(dev));
2347c478bd9Sstevel@tonic-gate 	int autoincr = 0;
2357c478bd9Sstevel@tonic-gate 	uchar_t control, reg;
2367c478bd9Sstevel@tonic-gate 	int32_t value;
2377c478bd9Sstevel@tonic-gate 	uint8_t uvalue;
2387c478bd9Sstevel@tonic-gate 
239*a4ee5977SToomas Soome 	if (arg == (intptr_t)NULL) {
2407c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "PCF8591: ioctl: arg passed in to ioctl "
24119397407SSherry Moore 		    "= NULL\n"));
2427c478bd9Sstevel@tonic-gate 		err = EINVAL;
2437c478bd9Sstevel@tonic-gate 		return (err);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	unitp = (struct pcf8591_unit *)
24619397407SSherry Moore 	    ddi_get_soft_state(pcf8591soft_statep, instance);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
2497c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "PCF8591: ioctl: unitp not filled\n");
2507c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8591_mutex);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: ioctl: port = %d  instance = %d\n",
25619397407SSherry Moore 	    unitp->pcf8591_name, port, instance));
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	switch (cmd) {
2597c478bd9Sstevel@tonic-gate 	case I2C_GET_INPUT:
2607c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(unitp->pcf8591_hdl, &i2c_tran_pointer,
26119397407SSherry Moore 		    1, 2, I2C_SLEEP);
2627c478bd9Sstevel@tonic-gate 		if (i2c_tran_pointer == NULL) {
2637c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_INPUT "
26419397407SSherry Moore 			    "i2c_tran_pointer not allocated\n",
26519397407SSherry Moore 			    unitp->pcf8591_name));
2667c478bd9Sstevel@tonic-gate 			err = ENOMEM;
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		reg = (uchar_t)port;
2707c478bd9Sstevel@tonic-gate 		if ((reg == 0x02) && (ipmode == PCF8591_2DIFF)) {
2717c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_INPUT "
27219397407SSherry Moore 			    "cannot use port 2 when ipmode is "
27319397407SSherry Moore 			    "0x03\n", unitp->pcf8591_name));
2747c478bd9Sstevel@tonic-gate 			err = EIO;
2757c478bd9Sstevel@tonic-gate 			i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		if ((reg == 0x03) && (ipmode != PCF8591_4SINGLE)) {
2807c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_INPUT "
28119397407SSherry Moore 			    "cannot use port 3 when ipmode is not "
28219397407SSherry Moore 			    "0x00\n", unitp->pcf8591_name));
2837c478bd9Sstevel@tonic-gate 			err = EIO;
2847c478bd9Sstevel@tonic-gate 			i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 		control = ((0 << PCF8591_ANALOG_OUTPUT_SHIFT) |
28819397407SSherry Moore 		    (ipmode << PCF8591_ANALOG_INPUT_SHIFT) |
28919397407SSherry Moore 		    (autoincr << PCF8591_AUTOINCR_SHIFT) | reg);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_flags = I2C_WR_RD;
2927c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[0] = control;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		err = i2c_transfer(unitp->pcf8591_hdl, i2c_tran_pointer);
2957c478bd9Sstevel@tonic-gate 		if (err) {
2967c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_INPUT"
29719397407SSherry Moore 			    " i2c_transfer routine\n",
29819397407SSherry Moore 			    unitp->pcf8591_name));
2997c478bd9Sstevel@tonic-gate 			i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_rbuf[0] = i2c_tran_pointer->i2c_rbuf[1];
3037c478bd9Sstevel@tonic-gate 		value = i2c_tran_pointer->i2c_rbuf[0];
3047c478bd9Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: Back from transfer result is %x\n",
30519397407SSherry Moore 		    unitp->pcf8591_name, value));
3067c478bd9Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&value,
30719397407SSherry Moore 		    (caddr_t)arg,
30819397407SSherry Moore 		    sizeof (int32_t), mode) != DDI_SUCCESS) {
3097c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed I2C_GET_INPUT"
31019397407SSherry Moore 			    " ddi_copyout routine\n",
31119397407SSherry Moore 			    unitp->pcf8591_name));
3127c478bd9Sstevel@tonic-gate 			err = EFAULT;
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
3157c478bd9Sstevel@tonic-gate 		break;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	case I2C_SET_OUTPUT:
3187c478bd9Sstevel@tonic-gate 		reg = (uchar_t)port;
3197c478bd9Sstevel@tonic-gate 		if (ipmode != PCF8591_4SINGLE) {
3207c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_OUTPUT "
32119397407SSherry Moore 			    "cannot set output when ipmode is not "
32219397407SSherry Moore 			    "0x00\n", unitp->pcf8591_name));
3237c478bd9Sstevel@tonic-gate 			err = EIO;
3247c478bd9Sstevel@tonic-gate 			break;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(unitp->pcf8591_hdl, &i2c_tran_pointer,
32819397407SSherry Moore 		    2, 0, I2C_SLEEP);
3297c478bd9Sstevel@tonic-gate 		if (i2c_tran_pointer == NULL) {
3307c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in "
33119397407SSherry Moore 			    "I2C_SET_OUTPUT "
33219397407SSherry Moore 			    "i2c_tran_pointer not allocated\n",
33319397407SSherry Moore 			    unitp->pcf8591_name));
3347c478bd9Sstevel@tonic-gate 			err = ENOMEM;
3357c478bd9Sstevel@tonic-gate 			break;
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&value,
33819397407SSherry Moore 		    sizeof (int32_t), mode) != DDI_SUCCESS) {
3397c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_OUTPUT"
34019397407SSherry Moore 			    " ddi_copyout routine\n",
34119397407SSherry Moore 			    unitp->pcf8591_name));
3427c478bd9Sstevel@tonic-gate 			err = EFAULT;
3437c478bd9Sstevel@tonic-gate 			i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
3447c478bd9Sstevel@tonic-gate 			break;
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 		control = ((1 << PCF8591_ANALOG_OUTPUT_SHIFT) |
34719397407SSherry Moore 		    (0 << PCF8591_ANALOG_INPUT_SHIFT) |
34819397407SSherry Moore 		    (autoincr << PCF8591_AUTOINCR_SHIFT) | reg);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_flags = I2C_WR;
3517c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[0] = control;
3527c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[1] = (uchar_t)value;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		err = i2c_transfer(unitp->pcf8591_hdl, i2c_tran_pointer);
3557c478bd9Sstevel@tonic-gate 		if (!err) {
3567c478bd9Sstevel@tonic-gate 			current_value = value;
3577c478bd9Sstevel@tonic-gate 			current_set_flag = 1;
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8591_hdl, i2c_tran_pointer);
3607c478bd9Sstevel@tonic-gate 		break;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	case I2C_GET_OUTPUT:
3637c478bd9Sstevel@tonic-gate 		if (current_set_flag == 0) {
3647c478bd9Sstevel@tonic-gate 			err = EIO;
3657c478bd9Sstevel@tonic-gate 			break;
3667c478bd9Sstevel@tonic-gate 		} else {
367f47a9c50Smathue 			if (ddi_copyout((caddr_t)(uintptr_t)current_value,
368f47a9c50Smathue 			    (caddr_t)arg, sizeof (int32_t), mode)
369f47a9c50Smathue 			    != DDI_SUCCESS) {
3707c478bd9Sstevel@tonic-gate 				D2CMN_ERR((CE_WARN, "%s: Failed in "
37119397407SSherry Moore 				    "I2C_GET_OUTPUT ddi_copyout routine\n",
37219397407SSherry Moore 				    unitp->pcf8591_name));
3737c478bd9Sstevel@tonic-gate 				err = EFAULT;
3747c478bd9Sstevel@tonic-gate 				break;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 		break;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	case PCF8591_SET_IPMODE:
3807c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&uvalue,
38119397407SSherry Moore 		    sizeof (uint_t), mode) != DDI_SUCCESS) {
3827c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in PCF8591_SET_IPMODE"
38319397407SSherry Moore 			    " ddi_copyout routine\n",
38419397407SSherry Moore 			    unitp->pcf8591_name));
3857c478bd9Sstevel@tonic-gate 			err = EFAULT;
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		if (uvalue > 0x03) {
3907c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in PCF8591_SET_IPMODE"
39119397407SSherry Moore 			    " value is not a valid mode\n",
39219397407SSherry Moore 			    unitp->pcf8591_name));
3937c478bd9Sstevel@tonic-gate 			err = EIO;
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		ipmode = uvalue;
3987c478bd9Sstevel@tonic-gate 		break;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	default:
4017c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n",
40219397407SSherry Moore 		    unitp->pcf8591_name, cmd));
4037c478bd9Sstevel@tonic-gate 		err = EINVAL;
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8591_mutex);
4077c478bd9Sstevel@tonic-gate 	return (err);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /* ARGSUSED */
4127c478bd9Sstevel@tonic-gate static int
pcf8591_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)4137c478bd9Sstevel@tonic-gate pcf8591_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	dev_t	dev;
4167c478bd9Sstevel@tonic-gate 	int	instance;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (infocmd == DDI_INFO_DEVT2INSTANCE) {
4197c478bd9Sstevel@tonic-gate 		dev = (dev_t)arg;
4207c478bd9Sstevel@tonic-gate 		instance = MINOR_TO_INST(getminor(dev));
4217c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
4227c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate static int
pcf8591_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4287c478bd9Sstevel@tonic-gate pcf8591_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	switch (cmd) {
4317c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4327c478bd9Sstevel@tonic-gate 		return (pcf8591_do_attach(dip));
4337c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
4347c478bd9Sstevel@tonic-gate 		return (pcf8591_do_resume());
4357c478bd9Sstevel@tonic-gate 	default:
4367c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate static int
pcf8591_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4417c478bd9Sstevel@tonic-gate pcf8591_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	switch (cmd) {
4447c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4457c478bd9Sstevel@tonic-gate 		return (pcf8591_do_detach(dip));
4467c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
4477c478bd9Sstevel@tonic-gate 		return (pcf8591_do_suspend());
4487c478bd9Sstevel@tonic-gate 	default:
4497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate static int
pcf8591_do_attach(dev_info_t * dip)4547c478bd9Sstevel@tonic-gate pcf8591_do_attach(dev_info_t *dip)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	struct pcf8591_unit *unitp;
4577c478bd9Sstevel@tonic-gate 	int instance;
4587c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN];
4597c478bd9Sstevel@tonic-gate 	minor_t minor_number;
4607c478bd9Sstevel@tonic-gate 	int i;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pcf8591soft_statep, instance) != 0) {
4657c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
46619397407SSherry Moore 		    ddi_get_name(dip), instance);
4677c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8591soft_statep, instance);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
4737c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
47419397407SSherry Moore 		    ddi_get_name(dip), instance);
4757c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	(void) snprintf(unitp->pcf8591_name, sizeof (unitp->pcf8591_name),
47919397407SSherry Moore 	    "%s%d", ddi_node_name(dip), instance);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
4827c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "port_%d", i);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		minor_number = INST_TO_MINOR(instance) |
48519397407SSherry Moore 		    PORT_TO_MINOR(I2C_PORT(i));
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
488*a4ee5977SToomas Soome 		    "ddi_i2c:adio", 0) == DDI_FAILURE) {
4897c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
49019397407SSherry Moore 			    "%s\n", unitp->pcf8591_name, name);
4917c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(pcf8591soft_statep, instance);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->pcf8591_hdl) != I2C_SUCCESS) {
4987c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
4997c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(pcf8591soft_statep, instance);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	mutex_init(&unitp->pcf8591_mutex, NULL, MUTEX_DRIVER, NULL);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate static int
pcf8591_do_resume()5107c478bd9Sstevel@tonic-gate pcf8591_do_resume()
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	return (ret);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate static int
pcf8591_do_suspend()5187c478bd9Sstevel@tonic-gate pcf8591_do_suspend()
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	return (ret);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate static int
pcf8591_do_detach(dev_info_t * dip)5267c478bd9Sstevel@tonic-gate pcf8591_do_detach(dev_info_t *dip)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	struct pcf8591_unit *unitp;
5297c478bd9Sstevel@tonic-gate 	int instance;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8591soft_statep, instance);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
5367c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
53719397407SSherry Moore 		    ddi_get_name(dip), instance);
5387c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	i2c_client_unregister(unitp->pcf8591_hdl);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	mutex_destroy(&unitp->pcf8591_mutex);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(pcf8591soft_statep, instance);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5507c478bd9Sstevel@tonic-gate }
551