as3722.c revision 1.1 1 1.1 jmcneill /* $NetBSD: as3722.c,v 1.1 2015/11/11 12:35:22 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: as3722.c,v 1.1 2015/11/11 12:35:22 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/kernel.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/conf.h>
37 1.1 jmcneill #include <sys/bus.h>
38 1.1 jmcneill #include <sys/kmem.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/i2c/i2cvar.h>
41 1.1 jmcneill #include <dev/i2c/as3722.h>
42 1.1 jmcneill
43 1.1 jmcneill #define AS3722_RESET_CTRL_REG 0x36
44 1.1 jmcneill #define AS3722_RESET_CTRL_POWER_OFF __BIT(1)
45 1.1 jmcneill
46 1.1 jmcneill #define AS3722_ASIC_ID1_REG 0x90
47 1.1 jmcneill #define AS3722_ASIC_ID2_REG 0x91
48 1.1 jmcneill
49 1.1 jmcneill struct as3722_softc {
50 1.1 jmcneill device_t sc_dev;
51 1.1 jmcneill i2c_tag_t sc_i2c;
52 1.1 jmcneill i2c_addr_t sc_addr;
53 1.1 jmcneill };
54 1.1 jmcneill
55 1.1 jmcneill static int as3722_match(device_t, cfdata_t, void *);
56 1.1 jmcneill static void as3722_attach(device_t, device_t, void *);
57 1.1 jmcneill
58 1.1 jmcneill #if 0
59 1.1 jmcneill static int as3722_read(struct as3722_softc *, uint8_t, uint8_t *, int);
60 1.1 jmcneill #endif
61 1.1 jmcneill static int as3722_write(struct as3722_softc *, uint8_t, uint8_t, int);
62 1.1 jmcneill
63 1.1 jmcneill CFATTACH_DECL_NEW(as3722pmic, sizeof(struct as3722_softc),
64 1.1 jmcneill as3722_match, as3722_attach, NULL, NULL);
65 1.1 jmcneill
66 1.1 jmcneill static int
67 1.1 jmcneill as3722_match(device_t parent, cfdata_t match, void *aux)
68 1.1 jmcneill {
69 1.1 jmcneill struct i2c_attach_args *ia = aux;
70 1.1 jmcneill uint8_t reg, id1;
71 1.1 jmcneill int error;
72 1.1 jmcneill
73 1.1 jmcneill iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
74 1.1 jmcneill reg = AS3722_ASIC_ID1_REG;
75 1.1 jmcneill error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
76 1.1 jmcneill ®, 1, &id1, 1, I2C_F_POLL);
77 1.1 jmcneill iic_release_bus(ia->ia_tag, I2C_F_POLL);
78 1.1 jmcneill
79 1.1 jmcneill if (error == 0 && id1 == 0x0c)
80 1.1 jmcneill return 1;
81 1.1 jmcneill
82 1.1 jmcneill return 0;
83 1.1 jmcneill }
84 1.1 jmcneill
85 1.1 jmcneill static void
86 1.1 jmcneill as3722_attach(device_t parent, device_t self, void *aux)
87 1.1 jmcneill {
88 1.1 jmcneill struct as3722_softc * const sc = device_private(self);
89 1.1 jmcneill struct i2c_attach_args *ia = aux;
90 1.1 jmcneill
91 1.1 jmcneill sc->sc_dev = self;
92 1.1 jmcneill sc->sc_i2c = ia->ia_tag;
93 1.1 jmcneill sc->sc_addr = ia->ia_addr;
94 1.1 jmcneill
95 1.1 jmcneill aprint_naive("\n");
96 1.1 jmcneill aprint_normal(": AMS AS3822\n");
97 1.1 jmcneill }
98 1.1 jmcneill
99 1.1 jmcneill #if 0
100 1.1 jmcneill static int
101 1.1 jmcneill as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
102 1.1 jmcneill {
103 1.1 jmcneill return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
104 1.1 jmcneill ®, 1, val, 1, flags);
105 1.1 jmcneill }
106 1.1 jmcneill #endif
107 1.1 jmcneill
108 1.1 jmcneill static int
109 1.1 jmcneill as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
110 1.1 jmcneill {
111 1.1 jmcneill uint8_t buf[2] = { reg, val };
112 1.1 jmcneill return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
113 1.1 jmcneill NULL, 0, buf, 2, flags);
114 1.1 jmcneill }
115 1.1 jmcneill
116 1.1 jmcneill int
117 1.1 jmcneill as3722_poweroff(device_t dev)
118 1.1 jmcneill {
119 1.1 jmcneill struct as3722_softc * const sc = device_private(dev);
120 1.1 jmcneill int error;
121 1.1 jmcneill
122 1.1 jmcneill const int flags = I2C_F_POLL;
123 1.1 jmcneill
124 1.1 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
125 1.1 jmcneill error = as3722_write(sc, AS3722_RESET_CTRL_REG,
126 1.1 jmcneill AS3722_RESET_CTRL_POWER_OFF, flags);
127 1.1 jmcneill iic_release_bus(sc->sc_i2c, flags);
128 1.1 jmcneill
129 1.1 jmcneill return error;
130 1.1 jmcneill }
131