ac100.c revision 1.1.2.2 1 1.1.2.2 skrll /* $NetBSD: ac100.c,v 1.1.2.2 2015/04/06 15:18:09 skrll Exp $ */
2 1.1.2.2 skrll
3 1.1.2.2 skrll /*-
4 1.1.2.2 skrll * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1.2.2 skrll * All rights reserved.
6 1.1.2.2 skrll *
7 1.1.2.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 skrll * modification, are permitted provided that the following conditions
9 1.1.2.2 skrll * are met:
10 1.1.2.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 skrll * documentation and/or other materials provided with the distribution.
15 1.1.2.2 skrll *
16 1.1.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 skrll * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 skrll */
28 1.1.2.2 skrll
29 1.1.2.2 skrll #include <sys/cdefs.h>
30 1.1.2.2 skrll __KERNEL_RCSID(0, "$NetBSD: ac100.c,v 1.1.2.2 2015/04/06 15:18:09 skrll Exp $");
31 1.1.2.2 skrll
32 1.1.2.2 skrll #include <sys/param.h>
33 1.1.2.2 skrll #include <sys/systm.h>
34 1.1.2.2 skrll #include <sys/kernel.h>
35 1.1.2.2 skrll #include <sys/device.h>
36 1.1.2.2 skrll #include <sys/conf.h>
37 1.1.2.2 skrll #include <sys/bus.h>
38 1.1.2.2 skrll #include <sys/kmem.h>
39 1.1.2.2 skrll
40 1.1.2.2 skrll #include <dev/clock_subr.h>
41 1.1.2.2 skrll
42 1.1.2.2 skrll #include <dev/i2c/i2cvar.h>
43 1.1.2.2 skrll
44 1.1.2.2 skrll #define AC100_CHIP_AUDIO_RST_REG 0x00
45 1.1.2.2 skrll #define AC100_PLL_CTRL1_REG 0x02
46 1.1.2.2 skrll #define AC100_PLL_CTRL2_REG 0x03
47 1.1.2.2 skrll #define AC100_SYSCLK_CTRL_REG 0x04
48 1.1.2.2 skrll #define AC100_MOD_RST_CTRL_REG 0x05
49 1.1.2.2 skrll #define AC100_ADDA_SR_CTRL_REG 0x06
50 1.1.2.2 skrll #define AC100_I2S1_LCK_CTRL_REG 0x10
51 1.1.2.2 skrll #define AC100_I2S1_SDIN_CTRL_REG 0x11
52 1.1.2.2 skrll #define AC100_I2S1_SDOUT_CTRL_REG 0x12
53 1.1.2.2 skrll #define AC100_I2S1_DIG_MIXER_REG 0x13
54 1.1.2.2 skrll #define AC100_I2S1_VOL_CTRL1_REG 0x14
55 1.1.2.2 skrll #define AC100_I2S1_VOL_CTRL2_REG 0x15
56 1.1.2.2 skrll #define AC100_I2S1_VOL_CTRL3_REG 0x16
57 1.1.2.2 skrll #define AC100_I2S1_VOL_CTRL4_REG 0x17
58 1.1.2.2 skrll #define AC100_I2S1_MXR_GAIN_REG 0x18
59 1.1.2.2 skrll #define AC100_I2S2_CLK_CTRL_REG 0x20
60 1.1.2.2 skrll #define AC100_I2S2_SDIN_CTRL_REG 0x21
61 1.1.2.2 skrll #define AC100_I2S2_SDOUT_CTRL_REG 0x22
62 1.1.2.2 skrll #define AC100_I2S2_DIG_MIXER_REG 0x23
63 1.1.2.2 skrll #define AC100_I2S2_VOL_CTRL1_REG 0x24
64 1.1.2.2 skrll #define AC100_I2S2_VOL_CTRL2_REG 0x26
65 1.1.2.2 skrll #define AC100_I2S2_MXR_GAIN_REG 0x28
66 1.1.2.2 skrll #define AC100_I2S3_CLK_CTRL_REG 0x30
67 1.1.2.2 skrll #define AC100_I2S3_SDIN_CTRL_REG 0x31
68 1.1.2.2 skrll #define AC100_I2S3_SDOUT_CTRL_REG 0x32
69 1.1.2.2 skrll #define AC100_I2S3_SGP_CTRL_REG 0x33
70 1.1.2.2 skrll #define AC100_ADC_DIG_CTRL_REG 0x40
71 1.1.2.2 skrll
72 1.1.2.2 skrll #define AC100_RTC_RESET_REG 0xc6
73 1.1.2.2 skrll #define AC100_RTC_CTRL_REG 0xc7
74 1.1.2.2 skrll #define AC100_RTC_SEC_REG 0xc8
75 1.1.2.2 skrll #define AC100_RTC_MIN_REG 0xc9
76 1.1.2.2 skrll #define AC100_RTC_HOU_REG 0xca
77 1.1.2.2 skrll #define AC100_RTC_WEE_REG 0xcb
78 1.1.2.2 skrll #define AC100_RTC_DAY_REG 0xcc
79 1.1.2.2 skrll #define AC100_RTC_MON_REG 0xcd
80 1.1.2.2 skrll #define AC100_RTC_YEA_REG 0xce
81 1.1.2.2 skrll #define AC100_RTC_UPD_TRIG_REG 0xcf
82 1.1.2.2 skrll
83 1.1.2.2 skrll #define AC100_RTC_GP_REG(n) (0xe0 + (n))
84 1.1.2.2 skrll
85 1.1.2.2 skrll #define AC100_RTC_CTRL_12H_24H_MODE __BIT(0)
86 1.1.2.2 skrll
87 1.1.2.2 skrll #define AC100_RTC_UPD_TRIG_WRITE __BIT(15)
88 1.1.2.2 skrll
89 1.1.2.2 skrll struct ac100_softc {
90 1.1.2.2 skrll device_t sc_dev;
91 1.1.2.2 skrll i2c_tag_t sc_i2c;
92 1.1.2.2 skrll i2c_addr_t sc_addr;
93 1.1.2.2 skrll
94 1.1.2.2 skrll struct todr_chip_handle sc_todr;
95 1.1.2.2 skrll };
96 1.1.2.2 skrll
97 1.1.2.2 skrll static int ac100_match(device_t, cfdata_t, void *);
98 1.1.2.2 skrll static void ac100_attach(device_t, device_t, void *);
99 1.1.2.2 skrll
100 1.1.2.2 skrll static int ac100_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
101 1.1.2.2 skrll static int ac100_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
102 1.1.2.2 skrll
103 1.1.2.2 skrll static int ac100_read(struct ac100_softc *, uint8_t, uint16_t *);
104 1.1.2.2 skrll static int ac100_write(struct ac100_softc *, uint8_t, uint16_t);
105 1.1.2.2 skrll
106 1.1.2.2 skrll CFATTACH_DECL_NEW(ac100ic, sizeof(struct ac100_softc),
107 1.1.2.2 skrll ac100_match, ac100_attach, NULL, NULL);
108 1.1.2.2 skrll
109 1.1.2.2 skrll static int
110 1.1.2.2 skrll ac100_match(device_t parent, cfdata_t match, void *aux)
111 1.1.2.2 skrll {
112 1.1.2.2 skrll return 1;
113 1.1.2.2 skrll }
114 1.1.2.2 skrll
115 1.1.2.2 skrll static void
116 1.1.2.2 skrll ac100_attach(device_t parent, device_t self, void *aux)
117 1.1.2.2 skrll {
118 1.1.2.2 skrll struct ac100_softc *sc = device_private(self);
119 1.1.2.2 skrll struct i2c_attach_args *ia = aux;
120 1.1.2.2 skrll
121 1.1.2.2 skrll sc->sc_dev = self;
122 1.1.2.2 skrll sc->sc_i2c = ia->ia_tag;
123 1.1.2.2 skrll sc->sc_addr = ia->ia_addr;
124 1.1.2.2 skrll
125 1.1.2.2 skrll aprint_naive("\n");
126 1.1.2.2 skrll aprint_normal(": CODEC/RTC\n");
127 1.1.2.2 skrll
128 1.1.2.2 skrll iic_acquire_bus(sc->sc_i2c, 0);
129 1.1.2.2 skrll ac100_write(sc, AC100_RTC_CTRL_REG, AC100_RTC_CTRL_12H_24H_MODE);
130 1.1.2.2 skrll iic_release_bus(sc->sc_i2c, 0);
131 1.1.2.2 skrll
132 1.1.2.2 skrll sc->sc_todr.todr_gettime_ymdhms = ac100_rtc_gettime;
133 1.1.2.2 skrll sc->sc_todr.todr_settime_ymdhms = ac100_rtc_settime;
134 1.1.2.2 skrll sc->sc_todr.cookie = sc;
135 1.1.2.2 skrll todr_attach(&sc->sc_todr);
136 1.1.2.2 skrll }
137 1.1.2.2 skrll
138 1.1.2.2 skrll static int
139 1.1.2.2 skrll ac100_read(struct ac100_softc *sc, uint8_t reg, uint16_t *val)
140 1.1.2.2 skrll {
141 1.1.2.2 skrll return iic_smbus_read_word(sc->sc_i2c, sc->sc_addr, reg, val,
142 1.1.2.2 skrll cold ? I2C_F_POLL : 0);
143 1.1.2.2 skrll }
144 1.1.2.2 skrll
145 1.1.2.2 skrll static int
146 1.1.2.2 skrll ac100_write(struct ac100_softc *sc, uint8_t reg, uint16_t val)
147 1.1.2.2 skrll {
148 1.1.2.2 skrll return iic_smbus_write_word(sc->sc_i2c, sc->sc_addr, reg, val,
149 1.1.2.2 skrll cold ? I2C_F_POLL : 0);
150 1.1.2.2 skrll }
151 1.1.2.2 skrll
152 1.1.2.2 skrll static int
153 1.1.2.2 skrll ac100_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
154 1.1.2.2 skrll {
155 1.1.2.2 skrll struct ac100_softc *sc = tch->cookie;
156 1.1.2.2 skrll uint16_t sec, min, hou, wee, day, mon, yea;
157 1.1.2.2 skrll
158 1.1.2.2 skrll iic_acquire_bus(sc->sc_i2c, 0);
159 1.1.2.2 skrll ac100_read(sc, AC100_RTC_SEC_REG, &sec);
160 1.1.2.2 skrll ac100_read(sc, AC100_RTC_MIN_REG, &min);
161 1.1.2.2 skrll ac100_read(sc, AC100_RTC_HOU_REG, &hou);
162 1.1.2.2 skrll ac100_read(sc, AC100_RTC_WEE_REG, &wee);
163 1.1.2.2 skrll ac100_read(sc, AC100_RTC_DAY_REG, &day);
164 1.1.2.2 skrll ac100_read(sc, AC100_RTC_MON_REG, &mon);
165 1.1.2.2 skrll ac100_read(sc, AC100_RTC_YEA_REG, &yea);
166 1.1.2.2 skrll iic_release_bus(sc->sc_i2c, 0);
167 1.1.2.2 skrll
168 1.1.2.2 skrll dt->dt_year = POSIX_BASE_YEAR + bcdtobin(yea & 0xff);
169 1.1.2.2 skrll dt->dt_mon = bcdtobin(mon & 0x1f);
170 1.1.2.2 skrll dt->dt_day = bcdtobin(day & 0x3f);
171 1.1.2.2 skrll dt->dt_wday = bcdtobin(wee & 0x7);
172 1.1.2.2 skrll dt->dt_hour = bcdtobin(hou & 0x3f);
173 1.1.2.2 skrll dt->dt_min = bcdtobin(min & 0x7f);
174 1.1.2.2 skrll dt->dt_sec = bcdtobin(sec & 0x7f);
175 1.1.2.2 skrll
176 1.1.2.2 skrll return 0;
177 1.1.2.2 skrll }
178 1.1.2.2 skrll
179 1.1.2.2 skrll static int
180 1.1.2.2 skrll ac100_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
181 1.1.2.2 skrll {
182 1.1.2.2 skrll struct ac100_softc *sc = tch->cookie;
183 1.1.2.2 skrll
184 1.1.2.2 skrll iic_acquire_bus(sc->sc_i2c, 0);
185 1.1.2.2 skrll ac100_write(sc, AC100_RTC_SEC_REG, bintobcd(dt->dt_sec) & 0x7f);
186 1.1.2.2 skrll ac100_write(sc, AC100_RTC_MIN_REG, bintobcd(dt->dt_min) & 0x7f);
187 1.1.2.2 skrll ac100_write(sc, AC100_RTC_HOU_REG, bintobcd(dt->dt_hour) & 0x3f);
188 1.1.2.2 skrll ac100_write(sc, AC100_RTC_WEE_REG, bintobcd(dt->dt_wday) & 0x7);
189 1.1.2.2 skrll ac100_write(sc, AC100_RTC_DAY_REG, bintobcd(dt->dt_day) & 0x3f);
190 1.1.2.2 skrll ac100_write(sc, AC100_RTC_MON_REG, bintobcd(dt->dt_mon) & 0x1f);
191 1.1.2.2 skrll ac100_write(sc, AC100_RTC_YEA_REG,
192 1.1.2.2 skrll bintobcd(dt->dt_year - POSIX_BASE_YEAR) & 0xff);
193 1.1.2.2 skrll ac100_write(sc, AC100_RTC_UPD_TRIG_REG, AC100_RTC_UPD_TRIG_WRITE);
194 1.1.2.2 skrll iic_release_bus(sc->sc_i2c, 0);
195 1.1.2.2 skrll
196 1.1.2.2 skrll return 0;
197 1.1.2.2 skrll }
198