lm_i2c.c revision 1.2.2.2 1 1.2.2.2 haad /* $NetBSD: lm_i2c.c,v 1.2.2.2 2008/10/19 22:16:25 haad Exp $ */
2 1.2.2.2 haad
3 1.2.2.2 haad /*-
4 1.2.2.2 haad * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.2.2.2 haad * All rights reserved.
6 1.2.2.2 haad *
7 1.2.2.2 haad * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 haad * by Bill Squier.
9 1.2.2.2 haad *
10 1.2.2.2 haad * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 haad * modification, are permitted provided that the following conditions
12 1.2.2.2 haad * are met:
13 1.2.2.2 haad * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 haad * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 haad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 haad * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 haad * documentation and/or other materials provided with the distribution.
18 1.2.2.2 haad *
19 1.2.2.2 haad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 haad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 haad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 haad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 haad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 haad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 haad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 haad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 haad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 haad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 haad * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 haad */
31 1.2.2.2 haad
32 1.2.2.2 haad #include <sys/cdefs.h>
33 1.2.2.2 haad __KERNEL_RCSID(0, "$NetBSD: lm_i2c.c,v 1.2.2.2 2008/10/19 22:16:25 haad Exp $");
34 1.2.2.2 haad
35 1.2.2.2 haad #include <sys/param.h>
36 1.2.2.2 haad #include <sys/systm.h>
37 1.2.2.2 haad #include <sys/kernel.h>
38 1.2.2.2 haad #include <sys/device.h>
39 1.2.2.2 haad #include <sys/conf.h>
40 1.2.2.2 haad
41 1.2.2.2 haad #include <dev/i2c/i2cvar.h>
42 1.2.2.2 haad
43 1.2.2.2 haad #include <dev/sysmon/sysmonvar.h>
44 1.2.2.2 haad
45 1.2.2.2 haad #include <dev/ic/nslm7xvar.h>
46 1.2.2.2 haad
47 1.2.2.2 haad int lm_i2c_match(device_t, cfdata_t, void *);
48 1.2.2.2 haad void lm_i2c_attach(device_t, device_t, void *);
49 1.2.2.2 haad int lm_i2c_detach(device_t, int);
50 1.2.2.2 haad
51 1.2.2.2 haad uint8_t lm_i2c_readreg(struct lm_softc *, int);
52 1.2.2.2 haad void lm_i2c_writereg(struct lm_softc *, int, int);
53 1.2.2.2 haad
54 1.2.2.2 haad struct lm_i2c_softc {
55 1.2.2.2 haad struct lm_softc sc_lmsc;
56 1.2.2.2 haad i2c_tag_t sc_tag;
57 1.2.2.2 haad i2c_addr_t sc_addr;
58 1.2.2.2 haad };
59 1.2.2.2 haad
60 1.2.2.2 haad CFATTACH_DECL_NEW(lm_iic, sizeof(struct lm_i2c_softc),
61 1.2.2.2 haad lm_i2c_match, lm_i2c_attach, lm_i2c_detach, NULL);
62 1.2.2.2 haad
63 1.2.2.2 haad int
64 1.2.2.2 haad lm_i2c_match(device_t parent, cfdata_t match, void *aux)
65 1.2.2.2 haad {
66 1.2.2.2 haad struct i2c_attach_args *ia = aux;
67 1.2.2.2 haad int rv = 0;
68 1.2.2.2 haad struct lm_i2c_softc sc;
69 1.2.2.2 haad
70 1.2.2.2 haad /* Must supply an address */
71 1.2.2.2 haad if (ia->ia_addr < 1)
72 1.2.2.2 haad return 0;
73 1.2.2.2 haad
74 1.2.2.2 haad /* Bus independent probe */
75 1.2.2.2 haad sc.sc_lmsc.lm_writereg = lm_i2c_writereg;
76 1.2.2.2 haad sc.sc_lmsc.lm_readreg = lm_i2c_readreg;
77 1.2.2.2 haad sc.sc_tag = ia->ia_tag;
78 1.2.2.2 haad sc.sc_addr = ia->ia_addr;
79 1.2.2.2 haad rv = lm_probe(&sc.sc_lmsc);
80 1.2.2.2 haad
81 1.2.2.2 haad return rv;
82 1.2.2.2 haad }
83 1.2.2.2 haad
84 1.2.2.2 haad
85 1.2.2.2 haad void
86 1.2.2.2 haad lm_i2c_attach(device_t parent, device_t self, void *aux)
87 1.2.2.2 haad {
88 1.2.2.2 haad struct lm_i2c_softc *sc = device_private(self);
89 1.2.2.2 haad struct i2c_attach_args *ia = aux;
90 1.2.2.2 haad
91 1.2.2.2 haad sc->sc_tag = ia->ia_tag;
92 1.2.2.2 haad sc->sc_addr = ia->ia_addr;
93 1.2.2.2 haad
94 1.2.2.2 haad /* Bus-independent attachment */
95 1.2.2.2 haad sc->sc_lmsc.sc_dev = self;
96 1.2.2.2 haad sc->sc_lmsc.lm_writereg = lm_i2c_writereg;
97 1.2.2.2 haad sc->sc_lmsc.lm_readreg = lm_i2c_readreg;
98 1.2.2.2 haad
99 1.2.2.2 haad lm_attach(&sc->sc_lmsc);
100 1.2.2.2 haad }
101 1.2.2.2 haad
102 1.2.2.2 haad int
103 1.2.2.2 haad lm_i2c_detach(device_t self, int flags)
104 1.2.2.2 haad {
105 1.2.2.2 haad struct lm_i2c_softc *sc = device_private(self);
106 1.2.2.2 haad
107 1.2.2.2 haad lm_detach(&sc->sc_lmsc);
108 1.2.2.2 haad return 0;
109 1.2.2.2 haad }
110 1.2.2.2 haad
111 1.2.2.2 haad uint8_t
112 1.2.2.2 haad lm_i2c_readreg(struct lm_softc *lmsc, int reg)
113 1.2.2.2 haad {
114 1.2.2.2 haad struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
115 1.2.2.2 haad uint8_t cmd, data;
116 1.2.2.2 haad
117 1.2.2.2 haad iic_acquire_bus(sc->sc_tag, 0);
118 1.2.2.2 haad
119 1.2.2.2 haad cmd = reg;
120 1.2.2.2 haad iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
121 1.2.2.2 haad sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
122 1.2.2.2 haad
123 1.2.2.2 haad iic_release_bus(sc->sc_tag, 0);
124 1.2.2.2 haad
125 1.2.2.2 haad return data;
126 1.2.2.2 haad }
127 1.2.2.2 haad
128 1.2.2.2 haad
129 1.2.2.2 haad void
130 1.2.2.2 haad lm_i2c_writereg(struct lm_softc *lmsc, int reg, int val)
131 1.2.2.2 haad {
132 1.2.2.2 haad struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
133 1.2.2.2 haad uint8_t cmd, data;
134 1.2.2.2 haad
135 1.2.2.2 haad iic_acquire_bus(sc->sc_tag, 0);
136 1.2.2.2 haad
137 1.2.2.2 haad cmd = reg;
138 1.2.2.2 haad data = val;
139 1.2.2.2 haad iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
140 1.2.2.2 haad sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
141 1.2.2.2 haad
142 1.2.2.2 haad iic_release_bus(sc->sc_tag, 0);
143 1.2.2.2 haad }
144