lm_i2c.c revision 1.6 1 1.6 jdolecek /* $NetBSD: lm_i2c.c,v 1.6 2020/06/24 19:11:49 jdolecek 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.6 jdolecek __KERNEL_RCSID(0, "$NetBSD: lm_i2c.c,v 1.6 2020/06/24 19:11:49 jdolecek 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.6 jdolecek #include <sys/kmem.h>
41 1.1 pgoyette
42 1.1 pgoyette #include <dev/i2c/i2cvar.h>
43 1.1 pgoyette
44 1.1 pgoyette #include <dev/sysmon/sysmonvar.h>
45 1.1 pgoyette
46 1.1 pgoyette #include <dev/ic/nslm7xvar.h>
47 1.1 pgoyette
48 1.1 pgoyette int lm_i2c_match(device_t, cfdata_t, void *);
49 1.1 pgoyette void lm_i2c_attach(device_t, device_t, void *);
50 1.1 pgoyette int lm_i2c_detach(device_t, int);
51 1.1 pgoyette
52 1.1 pgoyette uint8_t lm_i2c_readreg(struct lm_softc *, int);
53 1.3 msaitoh void lm_i2c_writereg(struct lm_softc *, int, uint8_t);
54 1.1 pgoyette
55 1.1 pgoyette struct lm_i2c_softc {
56 1.1 pgoyette struct lm_softc sc_lmsc;
57 1.1 pgoyette i2c_tag_t sc_tag;
58 1.1 pgoyette i2c_addr_t sc_addr;
59 1.1 pgoyette };
60 1.1 pgoyette
61 1.1 pgoyette CFATTACH_DECL_NEW(lm_iic, sizeof(struct lm_i2c_softc),
62 1.1 pgoyette lm_i2c_match, lm_i2c_attach, lm_i2c_detach, NULL);
63 1.1 pgoyette
64 1.1 pgoyette int
65 1.1 pgoyette lm_i2c_match(device_t parent, cfdata_t match, void *aux)
66 1.1 pgoyette {
67 1.1 pgoyette struct i2c_attach_args *ia = aux;
68 1.1 pgoyette int rv = 0;
69 1.6 jdolecek struct lm_i2c_softc *sc;
70 1.1 pgoyette
71 1.1 pgoyette /* Must supply an address */
72 1.1 pgoyette if (ia->ia_addr < 1)
73 1.1 pgoyette return 0;
74 1.1 pgoyette
75 1.5 thorpej /* XXXJRT filter addresses //at all// please? */
76 1.5 thorpej
77 1.1 pgoyette /* Bus independent probe */
78 1.6 jdolecek sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
79 1.6 jdolecek sc->sc_lmsc.lm_writereg = lm_i2c_writereg;
80 1.6 jdolecek sc->sc_lmsc.lm_readreg = lm_i2c_readreg;
81 1.6 jdolecek sc->sc_tag = ia->ia_tag;
82 1.6 jdolecek sc->sc_addr = ia->ia_addr;
83 1.6 jdolecek rv = lm_match(&sc->sc_lmsc);
84 1.6 jdolecek kmem_free(sc, sizeof(*sc));
85 1.1 pgoyette
86 1.5 thorpej return rv ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
87 1.1 pgoyette }
88 1.1 pgoyette
89 1.1 pgoyette
90 1.1 pgoyette void
91 1.1 pgoyette lm_i2c_attach(device_t parent, device_t self, void *aux)
92 1.1 pgoyette {
93 1.1 pgoyette struct lm_i2c_softc *sc = device_private(self);
94 1.1 pgoyette struct i2c_attach_args *ia = aux;
95 1.1 pgoyette
96 1.1 pgoyette sc->sc_tag = ia->ia_tag;
97 1.1 pgoyette sc->sc_addr = ia->ia_addr;
98 1.1 pgoyette
99 1.1 pgoyette /* Bus-independent attachment */
100 1.1 pgoyette sc->sc_lmsc.sc_dev = self;
101 1.1 pgoyette sc->sc_lmsc.lm_writereg = lm_i2c_writereg;
102 1.1 pgoyette sc->sc_lmsc.lm_readreg = lm_i2c_readreg;
103 1.1 pgoyette
104 1.1 pgoyette lm_attach(&sc->sc_lmsc);
105 1.1 pgoyette }
106 1.1 pgoyette
107 1.1 pgoyette int
108 1.1 pgoyette lm_i2c_detach(device_t self, int flags)
109 1.1 pgoyette {
110 1.1 pgoyette struct lm_i2c_softc *sc = device_private(self);
111 1.1 pgoyette
112 1.1 pgoyette lm_detach(&sc->sc_lmsc);
113 1.1 pgoyette return 0;
114 1.1 pgoyette }
115 1.1 pgoyette
116 1.1 pgoyette uint8_t
117 1.1 pgoyette lm_i2c_readreg(struct lm_softc *lmsc, int reg)
118 1.1 pgoyette {
119 1.1 pgoyette struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
120 1.1 pgoyette uint8_t cmd, data;
121 1.1 pgoyette
122 1.1 pgoyette iic_acquire_bus(sc->sc_tag, 0);
123 1.1 pgoyette
124 1.1 pgoyette cmd = reg;
125 1.1 pgoyette iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
126 1.1 pgoyette sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
127 1.1 pgoyette
128 1.1 pgoyette iic_release_bus(sc->sc_tag, 0);
129 1.1 pgoyette
130 1.1 pgoyette return data;
131 1.1 pgoyette }
132 1.1 pgoyette
133 1.1 pgoyette
134 1.1 pgoyette void
135 1.3 msaitoh lm_i2c_writereg(struct lm_softc *lmsc, int reg, uint8_t val)
136 1.1 pgoyette {
137 1.1 pgoyette struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
138 1.1 pgoyette uint8_t cmd, data;
139 1.1 pgoyette
140 1.1 pgoyette iic_acquire_bus(sc->sc_tag, 0);
141 1.1 pgoyette
142 1.1 pgoyette cmd = reg;
143 1.1 pgoyette data = val;
144 1.1 pgoyette iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
145 1.1 pgoyette sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
146 1.1 pgoyette
147 1.1 pgoyette iic_release_bus(sc->sc_tag, 0);
148 1.1 pgoyette }
149