lm_i2c.c revision 1.1 1 /* $NetBSD: lm_i2c.c,v 1.1 2008/10/12 13:17:28 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Squier.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: lm_i2c.c,v 1.1 2008/10/12 13:17:28 pgoyette Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40
41 #include <dev/i2c/i2cvar.h>
42
43 #include <dev/sysmon/sysmonvar.h>
44
45 #include <dev/ic/nslm7xvar.h>
46
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);
50
51 uint8_t lm_i2c_readreg(struct lm_softc *, int);
52 void lm_i2c_writereg(struct lm_softc *, int, int);
53
54 struct lm_i2c_softc {
55 struct lm_softc sc_lmsc;
56 i2c_tag_t sc_tag;
57 i2c_addr_t sc_addr;
58 };
59
60 CFATTACH_DECL_NEW(lm_iic, sizeof(struct lm_i2c_softc),
61 lm_i2c_match, lm_i2c_attach, lm_i2c_detach, NULL);
62
63 int
64 lm_i2c_match(device_t parent, cfdata_t match, void *aux)
65 {
66 struct i2c_attach_args *ia = aux;
67 int rv = 0;
68 struct lm_i2c_softc sc;
69
70 /* Must supply an address */
71 if (ia->ia_addr < 1)
72 return 0;
73
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);
80
81 return rv;
82 }
83
84
85 void
86 lm_i2c_attach(device_t parent, device_t self, void *aux)
87 {
88 struct lm_i2c_softc *sc = device_private(self);
89 struct i2c_attach_args *ia = aux;
90
91 sc->sc_tag = ia->ia_tag;
92 sc->sc_addr = ia->ia_addr;
93
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;
98
99 lm_attach(&sc->sc_lmsc);
100 }
101
102 int
103 lm_i2c_detach(device_t self, int flags)
104 {
105 struct lm_i2c_softc *sc = device_private(self);
106
107 lm_detach(&sc->sc_lmsc);
108 return 0;
109 }
110
111 uint8_t
112 lm_i2c_readreg(struct lm_softc *lmsc, int reg)
113 {
114 struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
115 uint8_t cmd, data;
116
117 iic_acquire_bus(sc->sc_tag, 0);
118
119 cmd = reg;
120 iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
121 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
122
123 iic_release_bus(sc->sc_tag, 0);
124
125 return data;
126 }
127
128
129 void
130 lm_i2c_writereg(struct lm_softc *lmsc, int reg, int val)
131 {
132 struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
133 uint8_t cmd, data;
134
135 iic_acquire_bus(sc->sc_tag, 0);
136
137 cmd = reg;
138 data = val;
139 iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
140 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
141
142 iic_release_bus(sc->sc_tag, 0);
143 }
144 /* $NetBSD: lm_i2c.c,v 1.1 2008/10/12 13:17:28 pgoyette Exp $ */
145
146 /*-
147 * Copyright (c) 2000 The NetBSD Foundation, Inc.
148 * All rights reserved.
149 *
150 * This code is derived from software contributed to The NetBSD Foundation
151 * by Bill Squier.
152 *
153 * Redistribution and use in source and binary forms, with or without
154 * modification, are permitted provided that the following conditions
155 * are met:
156 * 1. Redistributions of source code must retain the above copyright
157 * notice, this list of conditions and the following disclaimer.
158 * 2. Redistributions in binary form must reproduce the above copyright
159 * notice, this list of conditions and the following disclaimer in the
160 * documentation and/or other materials provided with the distribution.
161 *
162 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
163 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
164 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
165 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
166 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
167 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
168 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
169 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
170 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
171 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
172 * POSSIBILITY OF SUCH DAMAGE.
173 */
174
175 #include <sys/cdefs.h>
176 __KERNEL_RCSID(0, "$NetBSD: lm_i2c.c,v 1.1 2008/10/12 13:17:28 pgoyette Exp $");
177
178 #include <sys/param.h>
179 #include <sys/systm.h>
180 #include <sys/kernel.h>
181 #include <sys/device.h>
182 #include <sys/conf.h>
183
184 #include <dev/i2c/i2cvar.h>
185
186 #include <dev/sysmon/sysmonvar.h>
187
188 #include <dev/ic/nslm7xvar.h>
189
190 int lm_i2c_match(device_t, cfdata_t, void *);
191 void lm_i2c_attach(device_t, device_t, void *);
192 int lm_i2c_detach(device_t, int);
193
194 uint8_t lm_i2c_readreg(struct lm_softc *, int);
195 void lm_i2c_writereg(struct lm_softc *, int, int);
196
197 struct lm_i2c_softc {
198 struct lm_softc sc_lmsc;
199 i2c_tag_t sc_tag;
200 i2c_addr_t sc_addr;
201 };
202
203 CFATTACH_DECL_NEW(lm_iic, sizeof(struct lm_i2c_softc),
204 lm_i2c_match, lm_i2c_attach, lm_i2c_detach, NULL);
205
206 int
207 lm_i2c_match(device_t parent, cfdata_t match, void *aux)
208 {
209 struct i2c_attach_args *ia = aux;
210 int rv = 0;
211 struct lm_i2c_softc sc;
212
213 /* Must supply an address */
214 if (ia->ia_addr < 1)
215 return 0;
216
217 /* Bus independent probe */
218 sc.sc_lmsc.lm_writereg = lm_i2c_writereg;
219 sc.sc_lmsc.lm_readreg = lm_i2c_readreg;
220 sc.sc_tag = ia->ia_tag;
221 sc.sc_addr = ia->ia_addr;
222 rv = lm_probe(&sc.sc_lmsc);
223
224 return rv;
225 }
226
227
228 void
229 lm_i2c_attach(device_t parent, device_t self, void *aux)
230 {
231 struct lm_i2c_softc *sc = device_private(self);
232 struct i2c_attach_args *ia = aux;
233
234 sc->sc_tag = ia->ia_tag;
235 sc->sc_addr = ia->ia_addr;
236
237 /* Bus-independent attachment */
238 sc->sc_lmsc.sc_dev = self;
239 sc->sc_lmsc.lm_writereg = lm_i2c_writereg;
240 sc->sc_lmsc.lm_readreg = lm_i2c_readreg;
241
242 lm_attach(&sc->sc_lmsc);
243 }
244
245 int
246 lm_i2c_detach(device_t self, int flags)
247 {
248 struct lm_i2c_softc *sc = device_private(self);
249
250 lm_detach(&sc->sc_lmsc);
251 return 0;
252 }
253
254 uint8_t
255 lm_i2c_readreg(struct lm_softc *lmsc, int reg)
256 {
257 struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
258 uint8_t cmd, data;
259
260 iic_acquire_bus(sc->sc_tag, 0);
261
262 cmd = reg;
263 iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
264 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
265
266 iic_release_bus(sc->sc_tag, 0);
267
268 return data;
269 }
270
271
272 void
273 lm_i2c_writereg(struct lm_softc *lmsc, int reg, int val)
274 {
275 struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
276 uint8_t cmd, data;
277
278 iic_acquire_bus(sc->sc_tag, 0);
279
280 cmd = reg;
281 data = val;
282 iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
283 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
284
285 iic_release_bus(sc->sc_tag, 0);
286 }
287