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