lm_i2c.c revision 1.5
1/*	$NetBSD: lm_i2c.c,v 1.5 2018/06/16 21:22:13 thorpej 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.5 2018/06/16 21:22:13 thorpej 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
47int 	lm_i2c_match(device_t, cfdata_t, void *);
48void 	lm_i2c_attach(device_t, device_t, void *);
49int 	lm_i2c_detach(device_t, int);
50
51uint8_t lm_i2c_readreg(struct lm_softc *, int);
52void 	lm_i2c_writereg(struct lm_softc *, int, uint8_t);
53
54struct lm_i2c_softc {
55	struct lm_softc sc_lmsc;
56	i2c_tag_t sc_tag;
57	i2c_addr_t sc_addr;
58};
59
60CFATTACH_DECL_NEW(lm_iic, sizeof(struct lm_i2c_softc),
61    lm_i2c_match, lm_i2c_attach, lm_i2c_detach, NULL);
62
63int
64lm_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	/* XXXJRT filter addresses //at all// please? */
75
76	/* Bus independent probe */
77	sc.sc_lmsc.lm_writereg = lm_i2c_writereg;
78	sc.sc_lmsc.lm_readreg = lm_i2c_readreg;
79	sc.sc_tag = ia->ia_tag;
80	sc.sc_addr = ia->ia_addr;
81	rv = lm_match(&sc.sc_lmsc);
82
83	return rv ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
84}
85
86
87void
88lm_i2c_attach(device_t parent, device_t self, void *aux)
89{
90	struct lm_i2c_softc *sc = device_private(self);
91	struct i2c_attach_args *ia = aux;
92
93	sc->sc_tag = ia->ia_tag;
94	sc->sc_addr = ia->ia_addr;
95
96	/* Bus-independent attachment */
97	sc->sc_lmsc.sc_dev = self;
98	sc->sc_lmsc.lm_writereg = lm_i2c_writereg;
99	sc->sc_lmsc.lm_readreg = lm_i2c_readreg;
100
101	lm_attach(&sc->sc_lmsc);
102}
103
104int
105lm_i2c_detach(device_t self, int flags)
106{
107	struct lm_i2c_softc *sc = device_private(self);
108
109	lm_detach(&sc->sc_lmsc);
110	return 0;
111}
112
113uint8_t
114lm_i2c_readreg(struct lm_softc *lmsc, int reg)
115{
116	struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
117	uint8_t cmd, data;
118
119	iic_acquire_bus(sc->sc_tag, 0);
120
121	cmd = reg;
122	iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
123		 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
124
125	iic_release_bus(sc->sc_tag, 0);
126
127	return data;
128}
129
130
131void
132lm_i2c_writereg(struct lm_softc *lmsc, int reg, uint8_t val)
133{
134	struct lm_i2c_softc *sc = (struct lm_i2c_softc *)lmsc;
135	uint8_t cmd, data;
136
137	iic_acquire_bus(sc->sc_tag, 0);
138
139	cmd = reg;
140	data = val;
141	iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
142		 sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0);
143
144	iic_release_bus(sc->sc_tag, 0);
145}
146