xref: /onnv-gate/usr/src/uts/sun4u/io/i2c/clients/pcf8574.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <sys/stat.h>		/* ddi_create_minor_node S_IFCHR */
31*0Sstevel@tonic-gate #include <sys/modctl.h>		/* for modldrv */
32*0Sstevel@tonic-gate #include <sys/open.h>		/* for open params.	 */
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/kmem.h>
35*0Sstevel@tonic-gate #include <sys/sunddi.h>
36*0Sstevel@tonic-gate #include <sys/conf.h>		/* req. by dev_ops flags MTSAFE etc. */
37*0Sstevel@tonic-gate #include <sys/ddi.h>
38*0Sstevel@tonic-gate #include <sys/file.h>
39*0Sstevel@tonic-gate #include <sys/note.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <sys/i2c/clients/pcf8574_impl.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate static void *pcf8574soft_statep;
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate static int pcf8574_do_attach(dev_info_t *);
46*0Sstevel@tonic-gate static int pcf8574_do_detach(dev_info_t *);
47*0Sstevel@tonic-gate static int pcf8574_do_resume(void);
48*0Sstevel@tonic-gate static int pcf8574_do_suspend(void);
49*0Sstevel@tonic-gate static int pcf8574_get(struct pcf8574_unit *, uchar_t *);
50*0Sstevel@tonic-gate static int pcf8574_set(struct pcf8574_unit *, uchar_t);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * cb ops (only need ioctl)
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate static int pcf8574_open(dev_t *, int, int, cred_t *);
56*0Sstevel@tonic-gate static int pcf8574_close(dev_t, int, int, cred_t *);
57*0Sstevel@tonic-gate static int pcf8574_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static struct cb_ops pcf8574_cbops = {
60*0Sstevel@tonic-gate 	pcf8574_open,			/* open  */
61*0Sstevel@tonic-gate 	pcf8574_close,			/* close */
62*0Sstevel@tonic-gate 	nodev,				/* strategy */
63*0Sstevel@tonic-gate 	nodev,				/* print */
64*0Sstevel@tonic-gate 	nodev,				/* dump */
65*0Sstevel@tonic-gate 	nodev,				/* read */
66*0Sstevel@tonic-gate 	nodev,				/* write */
67*0Sstevel@tonic-gate 	pcf8574_ioctl,			/* ioctl */
68*0Sstevel@tonic-gate 	nodev,				/* devmap */
69*0Sstevel@tonic-gate 	nodev,				/* mmap */
70*0Sstevel@tonic-gate 	nodev,				/* segmap */
71*0Sstevel@tonic-gate 	nochpoll,			/* poll */
72*0Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
73*0Sstevel@tonic-gate 	NULL,				/* streamtab */
74*0Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
75*0Sstevel@tonic-gate 	CB_REV,				/* rev */
76*0Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
77*0Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
78*0Sstevel@tonic-gate };
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  * dev ops
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate static int pcf8574_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
84*0Sstevel@tonic-gate static int pcf8574_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static struct dev_ops pcf8574_ops = {
87*0Sstevel@tonic-gate 	DEVO_REV,
88*0Sstevel@tonic-gate 	0,
89*0Sstevel@tonic-gate 	ddi_getinfo_1to1,
90*0Sstevel@tonic-gate 	nulldev,
91*0Sstevel@tonic-gate 	nulldev,
92*0Sstevel@tonic-gate 	pcf8574_attach,
93*0Sstevel@tonic-gate 	pcf8574_detach,
94*0Sstevel@tonic-gate 	nodev,
95*0Sstevel@tonic-gate 	&pcf8574_cbops,
96*0Sstevel@tonic-gate 	NULL
97*0Sstevel@tonic-gate };
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate extern struct mod_ops mod_driverops;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate static struct modldrv pcf8574_modldrv = {
102*0Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
103*0Sstevel@tonic-gate 	"PCF8574 i2c device driver: v%I%",
104*0Sstevel@tonic-gate 	&pcf8574_ops
105*0Sstevel@tonic-gate };
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate static struct modlinkage pcf8574_modlinkage = {
108*0Sstevel@tonic-gate 	MODREV_1,
109*0Sstevel@tonic-gate 	&pcf8574_modldrv,
110*0Sstevel@tonic-gate 	0
111*0Sstevel@tonic-gate };
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate int
115*0Sstevel@tonic-gate _init(void)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	int error;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	error = mod_install(&pcf8574_modlinkage);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if (!error)
122*0Sstevel@tonic-gate 		(void) ddi_soft_state_init(&pcf8574soft_statep,
123*0Sstevel@tonic-gate 			sizeof (struct pcf8574_unit), 1);
124*0Sstevel@tonic-gate 	return (error);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate int
128*0Sstevel@tonic-gate _fini(void)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	int error;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	error = mod_remove(&pcf8574_modlinkage);
133*0Sstevel@tonic-gate 	if (!error)
134*0Sstevel@tonic-gate 		ddi_soft_state_fini(&pcf8574soft_statep);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	return (error);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate int
140*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	return (mod_info(&pcf8574_modlinkage, modinfop));
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate static int
146*0Sstevel@tonic-gate pcf8574_open(dev_t *devp, int flags, int otyp, cred_t *credp)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
151*0Sstevel@tonic-gate 	int instance;
152*0Sstevel@tonic-gate 	int error = 0;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Opening the PCF8574 device\n"));
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	instance = getminor(*devp);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	if (instance < 0) {
159*0Sstevel@tonic-gate 		return (ENXIO);
160*0Sstevel@tonic-gate 	}
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
163*0Sstevel@tonic-gate 		ddi_get_soft_state(pcf8574soft_statep, instance);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if (unitp == NULL) {
166*0Sstevel@tonic-gate 		return (ENXIO);
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
170*0Sstevel@tonic-gate 		return (EINVAL);
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (flags & FEXCL) {
176*0Sstevel@tonic-gate 		if (unitp->pcf8574_oflag != 0) {
177*0Sstevel@tonic-gate 			error = EBUSY;
178*0Sstevel@tonic-gate 		} else {
179*0Sstevel@tonic-gate 			unitp->pcf8574_oflag = FEXCL;
180*0Sstevel@tonic-gate 		}
181*0Sstevel@tonic-gate 	} else {
182*0Sstevel@tonic-gate 		if (unitp->pcf8574_oflag == FEXCL) {
183*0Sstevel@tonic-gate 			error = EBUSY;
184*0Sstevel@tonic-gate 		} else {
185*0Sstevel@tonic-gate 			unitp->pcf8574_oflag = FOPEN;
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate 	}
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	return (error);
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate static int
195*0Sstevel@tonic-gate pcf8574_close(dev_t dev, int flags, int otyp, cred_t *credp)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
200*0Sstevel@tonic-gate 	int instance;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	instance = getminor(dev);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	if (instance < 0) {
205*0Sstevel@tonic-gate 		return (ENXIO);
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
208*0Sstevel@tonic-gate 		ddi_get_soft_state(pcf8574soft_statep, instance);
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	if (unitp == NULL) {
211*0Sstevel@tonic-gate 		return (ENXIO);
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	unitp->pcf8574_oflag = 0;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
219*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate static int
223*0Sstevel@tonic-gate pcf8574_get(struct pcf8574_unit *unitp, uchar_t *byte) {
224*0Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
225*0Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Entered the pcf8574_get routine\n"));
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pcf8574_hdl, &i2c_tran_pointer,
230*0Sstevel@tonic-gate 					0, 1, I2C_SLEEP);
231*0Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
232*0Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in pcf8574_get "
233*0Sstevel@tonic-gate 				"i2c_tran_pointer not allocated\n",
234*0Sstevel@tonic-gate 				unitp->pcf8574_name));
235*0Sstevel@tonic-gate 		return (ENOMEM);
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_RD;
239*0Sstevel@tonic-gate 	err = i2c_transfer(unitp->pcf8574_hdl, i2c_tran_pointer);
240*0Sstevel@tonic-gate 	if (err) {
241*0Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the i2c_transfer routine\n",
242*0Sstevel@tonic-gate 				unitp->pcf8574_name));
243*0Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
244*0Sstevel@tonic-gate 		return (err);
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	D1CMN_ERR((CE_WARN, "Back from a transfer value is %x\n",
248*0Sstevel@tonic-gate 		i2c_tran_pointer->i2c_rbuf[0]));
249*0Sstevel@tonic-gate 	*byte = i2c_tran_pointer->i2c_rbuf[0];
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
252*0Sstevel@tonic-gate 	return (err);
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate static int
256*0Sstevel@tonic-gate pcf8574_set(struct pcf8574_unit *unitp, uchar_t byte) {
257*0Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
258*0Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pcf8574_hdl, &i2c_tran_pointer,
261*0Sstevel@tonic-gate 				1, 0, I2C_SLEEP);
262*0Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
263*0Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in pcf8574_set "
264*0Sstevel@tonic-gate 				"i2c_tran_pointer not allocated\n",
265*0Sstevel@tonic-gate 				unitp->pcf8574_name));
266*0Sstevel@tonic-gate 		return (ENOMEM);
267*0Sstevel@tonic-gate 	}
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR;
270*0Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = byte;
271*0Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: contains %x\n", unitp->pcf8574_name,
272*0Sstevel@tonic-gate 			i2c_tran_pointer->i2c_wbuf[0]));
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	err = i2c_transfer(unitp->pcf8574_hdl, i2c_tran_pointer);
275*0Sstevel@tonic-gate 	if (err) {
276*0Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the pcf8574_set"
277*0Sstevel@tonic-gate 				" i2c_transfer routine\n",
278*0Sstevel@tonic-gate 				unitp->pcf8574_name));
279*0Sstevel@tonic-gate 		i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
280*0Sstevel@tonic-gate 		return (err);
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 	i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer);
283*0Sstevel@tonic-gate 	return (err);
284*0Sstevel@tonic-gate }
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate static int
287*0Sstevel@tonic-gate pcf8574_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
288*0Sstevel@tonic-gate 		cred_t *credp, int *rvalp)
289*0Sstevel@tonic-gate {
290*0Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	struct pcf8574_unit	*unitp;
293*0Sstevel@tonic-gate 	int		instance;
294*0Sstevel@tonic-gate 	int			err = 0;
295*0Sstevel@tonic-gate 	i2c_bit_t		ioctl_bit;
296*0Sstevel@tonic-gate 	i2c_port_t		ioctl_port;
297*0Sstevel@tonic-gate 	uchar_t			byte;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	if (arg == NULL) {
300*0Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "PCF8574: ioctl: arg passed in to ioctl "
301*0Sstevel@tonic-gate 				"= NULL\n"));
302*0Sstevel@tonic-gate 		err = EINVAL;
303*0Sstevel@tonic-gate 		return (err);
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	instance = getminor(dev);
307*0Sstevel@tonic-gate 	unitp = (struct pcf8574_unit *)
308*0Sstevel@tonic-gate 		ddi_get_soft_state(pcf8574soft_statep, instance);
309*0Sstevel@tonic-gate 	if (unitp == NULL) {
310*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "PCF8574: ioctl: unitp not filled\n");
311*0Sstevel@tonic-gate 		return (ENOMEM);
312*0Sstevel@tonic-gate 	}
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	mutex_enter(&unitp->pcf8574_mutex);
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	switch (cmd) {
317*0Sstevel@tonic-gate 	case I2C_GET_PORT:
318*0Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
319*0Sstevel@tonic-gate 				sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
320*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
321*0Sstevel@tonic-gate 					" ddi_copyin routine\n",
322*0Sstevel@tonic-gate 					unitp->pcf8574_name));
323*0Sstevel@tonic-gate 			err = EFAULT;
324*0Sstevel@tonic-gate 			break;
325*0Sstevel@tonic-gate 		}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
328*0Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
329*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
330*0Sstevel@tonic-gate 					" pcf8574_get routine\n",
331*0Sstevel@tonic-gate 					unitp->pcf8574_name));
332*0Sstevel@tonic-gate 			break;
333*0Sstevel@tonic-gate 		}
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 		ioctl_port.value = byte;
336*0Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_port, (caddr_t)arg,
337*0Sstevel@tonic-gate 				sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
338*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_PORT "
339*0Sstevel@tonic-gate 					"ddi_copyout routine\n",
340*0Sstevel@tonic-gate 					unitp->pcf8574_name));
341*0Sstevel@tonic-gate 			err = EFAULT;
342*0Sstevel@tonic-gate 		}
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: contains %x\n", unitp->pcf8574_name,
345*0Sstevel@tonic-gate 			byte));
346*0Sstevel@tonic-gate 		break;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	case I2C_SET_PORT:
349*0Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
350*0Sstevel@tonic-gate 				sizeof (uint8_t), mode) != DDI_SUCCESS) {
351*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
352*0Sstevel@tonic-gate 					"ddi_cpoyin routine\n",
353*0Sstevel@tonic-gate 					unitp->pcf8574_name));
354*0Sstevel@tonic-gate 			err = EFAULT;
355*0Sstevel@tonic-gate 			break;
356*0Sstevel@tonic-gate 		}
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 		err = pcf8574_set(unitp, ioctl_port.value);
359*0Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
360*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
361*0Sstevel@tonic-gate 					" pcf8574_set routine\n",
362*0Sstevel@tonic-gate 					unitp->pcf8574_name));
363*0Sstevel@tonic-gate 			break;
364*0Sstevel@tonic-gate 		}
365*0Sstevel@tonic-gate 		break;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	case I2C_GET_BIT:
368*0Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
369*0Sstevel@tonic-gate 				sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
370*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
371*0Sstevel@tonic-gate 					" ddi_copyin routine\n",
372*0Sstevel@tonic-gate 					unitp->pcf8574_name));
373*0Sstevel@tonic-gate 			err = EFAULT;
374*0Sstevel@tonic-gate 			break;
375*0Sstevel@tonic-gate 		}
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 		if ((ioctl_bit.bit_num < 0) && (ioctl_bit.bit_num > 7)) {
378*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: In I2C_GET_BIT bit num"
379*0Sstevel@tonic-gate 					" was not between 0 and 7\n",
380*0Sstevel@tonic-gate 					unitp->pcf8574_name));
381*0Sstevel@tonic-gate 			err = EIO;
382*0Sstevel@tonic-gate 			break;
383*0Sstevel@tonic-gate 		}
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
386*0Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
387*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
388*0Sstevel@tonic-gate 					" pcf8574_get routine\n",
389*0Sstevel@tonic-gate 					unitp->pcf8574_name));
390*0Sstevel@tonic-gate 			break;
391*0Sstevel@tonic-gate 		}
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x\n",
394*0Sstevel@tonic-gate 			unitp->pcf8574_name, byte));
395*0Sstevel@tonic-gate 		ioctl_bit.bit_value = (boolean_t)PCF8574_BIT_READ_MASK(byte,
396*0Sstevel@tonic-gate 							ioctl_bit.bit_num);
397*0Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte now contains %x\n",
398*0Sstevel@tonic-gate 				unitp->pcf8574_name, byte));
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_bit, (caddr_t)arg,
401*0Sstevel@tonic-gate 				sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
402*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_BIT"
403*0Sstevel@tonic-gate 					" ddi_copyout routine\n",
404*0Sstevel@tonic-gate 					unitp->pcf8574_name));
405*0Sstevel@tonic-gate 			err = EFAULT;
406*0Sstevel@tonic-gate 		}
407*0Sstevel@tonic-gate 		break;
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	case I2C_SET_BIT:
410*0Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
411*0Sstevel@tonic-gate 				sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
412*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_BIT"
413*0Sstevel@tonic-gate 					" ddi_copyin routine\n",
414*0Sstevel@tonic-gate 					unitp->pcf8574_name));
415*0Sstevel@tonic-gate 			err = EFAULT;
416*0Sstevel@tonic-gate 			break;
417*0Sstevel@tonic-gate 		}
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 		if ((ioctl_bit.bit_num < 0) && (ioctl_bit.bit_num > 7)) {
420*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: I2C_SET_BIT: bit_num sent"
421*0Sstevel@tonic-gate 					" in was not between 0 and 7",
422*0Sstevel@tonic-gate 					unitp->pcf8574_name));
423*0Sstevel@tonic-gate 			err = EIO;
424*0Sstevel@tonic-gate 			break;
425*0Sstevel@tonic-gate 		}
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 		err = pcf8574_get(unitp, &byte);
428*0Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
429*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
430*0Sstevel@tonic-gate 					" pcf8574_get routine\n",
431*0Sstevel@tonic-gate 					unitp->pcf8574_name));
432*0Sstevel@tonic-gate 			break;
433*0Sstevel@tonic-gate 		}
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x\n",
436*0Sstevel@tonic-gate 			unitp->pcf8574_name, byte));
437*0Sstevel@tonic-gate 		byte = PCF8574_BIT_WRITE_MASK(byte, ioctl_bit.bit_num,
438*0Sstevel@tonic-gate 						ioctl_bit.bit_value);
439*0Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte after shifting is %x\n",
440*0Sstevel@tonic-gate 			unitp->pcf8574_name, byte));
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 		err = pcf8574_set(unitp, byte);
443*0Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
444*0Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
445*0Sstevel@tonic-gate 					" pcf8574_set routine\n",
446*0Sstevel@tonic-gate 					unitp->pcf8574_name));
447*0Sstevel@tonic-gate 			break;
448*0Sstevel@tonic-gate 		}
449*0Sstevel@tonic-gate 		break;
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	default:
452*0Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n",
453*0Sstevel@tonic-gate 				unitp->pcf8574_name, cmd));
454*0Sstevel@tonic-gate 		err = EINVAL;
455*0Sstevel@tonic-gate 	}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	mutex_exit(&unitp->pcf8574_mutex);
458*0Sstevel@tonic-gate 	return (err);
459*0Sstevel@tonic-gate }
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate static int
462*0Sstevel@tonic-gate pcf8574_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
463*0Sstevel@tonic-gate {
464*0Sstevel@tonic-gate 	switch (cmd) {
465*0Sstevel@tonic-gate 	case DDI_ATTACH:
466*0Sstevel@tonic-gate 		return (pcf8574_do_attach(dip));
467*0Sstevel@tonic-gate 	case DDI_RESUME:
468*0Sstevel@tonic-gate 		return (pcf8574_do_resume());
469*0Sstevel@tonic-gate 	default:
470*0Sstevel@tonic-gate 		return (DDI_FAILURE);
471*0Sstevel@tonic-gate 	}
472*0Sstevel@tonic-gate }
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate static int
475*0Sstevel@tonic-gate pcf8574_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
476*0Sstevel@tonic-gate {
477*0Sstevel@tonic-gate 	switch (cmd) {
478*0Sstevel@tonic-gate 	case DDI_DETACH:
479*0Sstevel@tonic-gate 		return (pcf8574_do_detach(dip));
480*0Sstevel@tonic-gate 	case DDI_SUSPEND:
481*0Sstevel@tonic-gate 		return (pcf8574_do_suspend());
482*0Sstevel@tonic-gate 	default:
483*0Sstevel@tonic-gate 		return (DDI_FAILURE);
484*0Sstevel@tonic-gate 	}
485*0Sstevel@tonic-gate }
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate static int
488*0Sstevel@tonic-gate pcf8574_do_attach(dev_info_t *dip)
489*0Sstevel@tonic-gate {
490*0Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
491*0Sstevel@tonic-gate 	int instance;
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pcf8574soft_statep, instance) != 0) {
496*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
497*0Sstevel@tonic-gate 			ddi_get_name(dip), instance);
498*0Sstevel@tonic-gate 		return (DDI_FAILURE);
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8574soft_statep, instance);
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 	if (unitp == NULL) {
504*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
505*0Sstevel@tonic-gate 			ddi_get_name(dip), instance);
506*0Sstevel@tonic-gate 		return (ENOMEM);
507*0Sstevel@tonic-gate 	}
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	(void) snprintf(unitp->pcf8574_name, sizeof (unitp->pcf8574_name),
510*0Sstevel@tonic-gate 			"%sd", ddi_node_name(dip), instance);
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "pcf8574", S_IFCHR, instance,
514*0Sstevel@tonic-gate 			"ddi_i2c:ioexp", NULL) == DDI_FAILURE) {
515*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
516*0Sstevel@tonic-gate 			"%s\n", unitp->pcf8574_name, "pcf8574");
517*0Sstevel@tonic-gate 		ddi_soft_state_free(pcf8574soft_statep, instance);
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 		return (DDI_FAILURE);
520*0Sstevel@tonic-gate 	}
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->pcf8574_hdl) != I2C_SUCCESS) {
523*0Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
524*0Sstevel@tonic-gate 		ddi_soft_state_free(pcf8574soft_statep, instance);
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 		return (DDI_FAILURE);
527*0Sstevel@tonic-gate 	}
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	mutex_init(&unitp->pcf8574_mutex, NULL, MUTEX_DRIVER, NULL);
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
532*0Sstevel@tonic-gate }
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate static int
535*0Sstevel@tonic-gate pcf8574_do_resume()
536*0Sstevel@tonic-gate {
537*0Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 	return (ret);
540*0Sstevel@tonic-gate }
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate static int
543*0Sstevel@tonic-gate pcf8574_do_suspend()
544*0Sstevel@tonic-gate {
545*0Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	return (ret);
548*0Sstevel@tonic-gate }
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate static int
551*0Sstevel@tonic-gate pcf8574_do_detach(dev_info_t *dip)
552*0Sstevel@tonic-gate {
553*0Sstevel@tonic-gate 	struct pcf8574_unit *unitp;
554*0Sstevel@tonic-gate 	int instance;
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pcf8574soft_statep, instance);
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	if (unitp == NULL) {
561*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
562*0Sstevel@tonic-gate 			ddi_get_name(dip), instance);
563*0Sstevel@tonic-gate 		return (ENOMEM);
564*0Sstevel@tonic-gate 	}
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	i2c_client_unregister(unitp->pcf8574_hdl);
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	mutex_destroy(&unitp->pcf8574_mutex);
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	ddi_soft_state_free(pcf8574soft_statep, instance);
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate }
577