imxi2c.c revision 1.1.6.3 1 1.1.6.2 tls /* $NetBSD: imxi2c.c,v 1.1.6.3 2017/12/03 11:35:53 jdolecek Exp $ */
2 1.1.6.2 tls
3 1.1.6.2 tls /*
4 1.1.6.3 jdolecek * Copyright (c) 2012, 2015 Genetec Corporation. All rights reserved.
5 1.1.6.2 tls * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.1.6.2 tls *
7 1.1.6.2 tls * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 tls * modification, are permitted provided that the following conditions
9 1.1.6.2 tls * are met:
10 1.1.6.2 tls * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 tls * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 tls * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 tls * documentation and/or other materials provided with the distribution.
15 1.1.6.2 tls *
16 1.1.6.2 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.1.6.2 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.6.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.6.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.1.6.2 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.6.2 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.6.2 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.6.2 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.6.2 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.6.2 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.6.2 tls * SUCH DAMAGE.
27 1.1.6.2 tls */
28 1.1.6.2 tls
29 1.1.6.2 tls #include <sys/cdefs.h>
30 1.1.6.2 tls __KERNEL_RCSID(0, "$NetBSD: imxi2c.c,v 1.1.6.3 2017/12/03 11:35:53 jdolecek Exp $");
31 1.1.6.2 tls
32 1.1.6.2 tls #include "opt_imx.h"
33 1.1.6.2 tls
34 1.1.6.2 tls #include <sys/param.h>
35 1.1.6.2 tls #include <sys/bus.h>
36 1.1.6.2 tls #include <sys/device.h>
37 1.1.6.2 tls
38 1.1.6.3 jdolecek #include <dev/i2c/motoi2cvar.h>
39 1.1.6.3 jdolecek #include <dev/i2c/motoi2creg.h>
40 1.1.6.2 tls #include <arm/imx/imxi2cvar.h>
41 1.1.6.2 tls
42 1.1.6.2 tls struct clk_div {
43 1.1.6.2 tls uint8_t ic_val;
44 1.1.6.2 tls int div;
45 1.1.6.2 tls };
46 1.1.6.2 tls
47 1.1.6.3 jdolecek static const struct clk_div imxi2c_clk_div[] = {
48 1.1.6.2 tls {0x20, 22}, {0x21, 24}, {0x22, 26}, {0x23, 28},
49 1.1.6.2 tls {0x00, 30}, {0x01, 32}, {0x24, 32}, {0x02, 36},
50 1.1.6.2 tls {0x25, 36}, {0x26, 40}, {0x03, 42}, {0x27, 44},
51 1.1.6.2 tls {0x04, 48}, {0x28, 48}, {0x05, 52}, {0x29, 56},
52 1.1.6.2 tls {0x06, 60}, {0x2A, 64}, {0x07, 72}, {0x2B, 72},
53 1.1.6.2 tls {0x08, 80}, {0x2C, 80}, {0x09, 88}, {0x2D, 96},
54 1.1.6.2 tls {0x0A, 104}, {0x2E, 112}, {0x0B, 128}, {0x2F, 128},
55 1.1.6.2 tls {0x0C, 144}, {0x0D, 160}, {0x30, 160}, {0x0E, 192},
56 1.1.6.2 tls {0x31, 192}, {0x32, 224}, {0x0F, 240}, {0x33, 256},
57 1.1.6.2 tls {0x10, 288}, {0x11, 320}, {0x34, 320}, {0x12, 384},
58 1.1.6.2 tls {0x35, 384}, {0x36, 448}, {0x13, 480}, {0x37, 512},
59 1.1.6.2 tls {0x14, 576}, {0x15, 640}, {0x38, 640}, {0x16, 768},
60 1.1.6.2 tls {0x39, 768}, {0x3A, 896}, {0x17, 960}, {0x3B, 1024},
61 1.1.6.2 tls {0x18, 1152}, {0x19, 1280}, {0x3C, 1280}, {0x1A, 1536},
62 1.1.6.2 tls {0x3D, 1536}, {0x3E, 1792}, {0x1B, 1920}, {0x3F, 2048},
63 1.1.6.2 tls {0x1C, 2304}, {0x1D, 2560}, {0x1E, 3072}, {0x1F, 3840},
64 1.1.6.2 tls };
65 1.1.6.2 tls
66 1.1.6.2 tls CFATTACH_DECL_NEW(imxi2c, sizeof(struct imxi2c_softc),
67 1.1.6.2 tls imxi2c_match, imxi2c_attach, NULL, NULL);
68 1.1.6.2 tls
69 1.1.6.3 jdolecek static uint8_t
70 1.1.6.3 jdolecek imxi2c_iord1(struct motoi2c_softc *sc, bus_size_t off)
71 1.1.6.3 jdolecek {
72 1.1.6.3 jdolecek if (off < I2CDFSRR)
73 1.1.6.3 jdolecek return bus_space_read_2(sc->sc_iot, sc->sc_ioh, off) & 0xff;
74 1.1.6.3 jdolecek else
75 1.1.6.3 jdolecek return 0;
76 1.1.6.3 jdolecek }
77 1.1.6.3 jdolecek
78 1.1.6.3 jdolecek static void
79 1.1.6.3 jdolecek imxi2c_iowr1(struct motoi2c_softc *sc, bus_size_t off, uint8_t data)
80 1.1.6.3 jdolecek {
81 1.1.6.3 jdolecek if (off < I2CDFSRR)
82 1.1.6.3 jdolecek bus_space_write_2(sc->sc_iot, sc->sc_ioh, off, data);
83 1.1.6.3 jdolecek }
84 1.1.6.3 jdolecek
85 1.1.6.2 tls int
86 1.1.6.2 tls imxi2c_attach_common(device_t parent, device_t self,
87 1.1.6.2 tls bus_space_tag_t iot, paddr_t iobase, size_t size, int intr, int flags)
88 1.1.6.2 tls {
89 1.1.6.2 tls struct imxi2c_softc *sc = device_private(self);
90 1.1.6.3 jdolecek struct motoi2c_softc *msc = &sc->sc_motoi2c;
91 1.1.6.2 tls int error;
92 1.1.6.2 tls
93 1.1.6.3 jdolecek aprint_naive("\n");
94 1.1.6.3 jdolecek aprint_normal("\n");
95 1.1.6.2 tls
96 1.1.6.2 tls sc->sc_dev = self;
97 1.1.6.3 jdolecek msc->sc_iot = iot;
98 1.1.6.3 jdolecek error = bus_space_map(msc->sc_iot, iobase, size, 0, &msc->sc_ioh);
99 1.1.6.2 tls if (error) {
100 1.1.6.2 tls aprint_error_dev(sc->sc_dev,
101 1.1.6.2 tls "failed to map registers (errno=%d)\n", error);
102 1.1.6.2 tls return 1;
103 1.1.6.2 tls }
104 1.1.6.2 tls
105 1.1.6.3 jdolecek sc->sc_motoi2c_settings.i2c_adr = MOTOI2C_ADR_DEFAULT;
106 1.1.6.3 jdolecek sc->sc_motoi2c_settings.i2c_dfsrr = MOTOI2C_DFSRR_DEFAULT;
107 1.1.6.3 jdolecek msc->sc_iord = imxi2c_iord1;
108 1.1.6.3 jdolecek msc->sc_iowr = imxi2c_iowr1;
109 1.1.6.2 tls
110 1.1.6.3 jdolecek motoi2c_attach_common(self, msc, &sc->sc_motoi2c_settings);
111 1.1.6.2 tls
112 1.1.6.2 tls return 0;
113 1.1.6.2 tls }
114 1.1.6.2 tls
115 1.1.6.2 tls int
116 1.1.6.2 tls imxi2c_set_freq(device_t self, long freq, int speed)
117 1.1.6.2 tls {
118 1.1.6.2 tls struct imxi2c_softc *sc = device_private(self);
119 1.1.6.2 tls bool found = false;
120 1.1.6.2 tls int index;
121 1.1.6.2 tls
122 1.1.6.3 jdolecek for (index = 0; index < __arraycount(imxi2c_clk_div); index++) {
123 1.1.6.3 jdolecek if (freq / imxi2c_clk_div[index].div < speed) {
124 1.1.6.2 tls found = true;
125 1.1.6.2 tls break;
126 1.1.6.2 tls }
127 1.1.6.2 tls }
128 1.1.6.2 tls
129 1.1.6.2 tls if (found == false)
130 1.1.6.3 jdolecek sc->sc_motoi2c_settings.i2c_fdr = 0x1f;
131 1.1.6.2 tls else
132 1.1.6.3 jdolecek sc->sc_motoi2c_settings.i2c_fdr = imxi2c_clk_div[index].ic_val;
133 1.1.6.2 tls
134 1.1.6.2 tls return 0;
135 1.1.6.2 tls }
136