1 /* $NetBSD: lm_i2c.c,v 1.1 2008/10/12 13:17:28 pgoyette Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: lm_i2c.c,v 1.1 2008/10/12 13:17:28 pgoyette Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
41 #include <dev/i2c/i2cvar.h>
43 #include <dev/sysmon/sysmonvar.h>
45 #include <dev/ic/nslm7xvar.h>
47 int lm_i2c_match(device_t
, cfdata_t
, void *);
48 void lm_i2c_attach(device_t
, device_t
, void *);
49 int lm_i2c_detach(device_t
, int);
51 uint8_t lm_i2c_readreg(struct lm_softc
*, int);
52 void lm_i2c_writereg(struct lm_softc
*, int, int);
55 struct lm_softc sc_lmsc
;
60 CFATTACH_DECL_NEW(lm_iic
, sizeof(struct lm_i2c_softc
),
61 lm_i2c_match
, lm_i2c_attach
, lm_i2c_detach
, NULL
);
64 lm_i2c_match(device_t parent
, cfdata_t match
, void *aux
)
66 struct i2c_attach_args
*ia
= aux
;
68 struct lm_i2c_softc sc
;
70 /* Must supply an address */
74 /* Bus independent probe */
75 sc
.sc_lmsc
.lm_writereg
= lm_i2c_writereg
;
76 sc
.sc_lmsc
.lm_readreg
= lm_i2c_readreg
;
77 sc
.sc_tag
= ia
->ia_tag
;
78 sc
.sc_addr
= ia
->ia_addr
;
79 rv
= lm_probe(&sc
.sc_lmsc
);
86 lm_i2c_attach(device_t parent
, device_t self
, void *aux
)
88 struct lm_i2c_softc
*sc
= device_private(self
);
89 struct i2c_attach_args
*ia
= aux
;
91 sc
->sc_tag
= ia
->ia_tag
;
92 sc
->sc_addr
= ia
->ia_addr
;
94 /* Bus-independent attachment */
95 sc
->sc_lmsc
.sc_dev
= self
;
96 sc
->sc_lmsc
.lm_writereg
= lm_i2c_writereg
;
97 sc
->sc_lmsc
.lm_readreg
= lm_i2c_readreg
;
99 lm_attach(&sc
->sc_lmsc
);
103 lm_i2c_detach(device_t self
, int flags
)
105 struct lm_i2c_softc
*sc
= device_private(self
);
107 lm_detach(&sc
->sc_lmsc
);
112 lm_i2c_readreg(struct lm_softc
*lmsc
, int reg
)
114 struct lm_i2c_softc
*sc
= (struct lm_i2c_softc
*)lmsc
;
117 iic_acquire_bus(sc
->sc_tag
, 0);
120 iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
,
121 sc
->sc_addr
, &cmd
, sizeof cmd
, &data
, sizeof data
, 0);
123 iic_release_bus(sc
->sc_tag
, 0);
130 lm_i2c_writereg(struct lm_softc
*lmsc
, int reg
, int val
)
132 struct lm_i2c_softc
*sc
= (struct lm_i2c_softc
*)lmsc
;
135 iic_acquire_bus(sc
->sc_tag
, 0);
139 iic_exec(sc
->sc_tag
, I2C_OP_WRITE_WITH_STOP
,
140 sc
->sc_addr
, &cmd
, sizeof cmd
, &data
, sizeof data
, 0);
142 iic_release_bus(sc
->sc_tag
, 0);