comparison target-utils/libbase/abbdrv.c @ 991:5cff3579814c

target-utils: libbase factored out of libcommon The library dependency order is now strictly unidirectional
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Wed, 30 Dec 2015 20:48:12 +0000
parents target-utils/libcommon/abbdrv.c@6661e5bc0712
children
comparison
equal deleted inserted replaced
990:2a867e5768e9 991:5cff3579814c
1 /* Driver for Analog Baseband Circuit (TWL3025) */
2 /* lifted from OsmocomBB and ported to FreeCalypso target-utils environment */
3
4 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24 #include "types.h"
25 #include "abbdefs.h"
26
27 /* TWL3025 */
28 #define REG_PAGE(n) ((n) >> 7)
29 #define REG_ADDR(n) ((n) & 0x1f)
30
31 #define TWL3025_DEV_IDX 0 /* On the SPI bus */
32 #define TWL3025_TSP_DEV_IDX 0 /* On the TSP bus */
33
34 int abb_state_initdone, abb_state_page;
35
36 void
37 abb_reg_write(reg, data)
38 {
39 u16 tx;
40
41 if (reg != PAGEREG && REG_PAGE(reg) != abb_state_page)
42 abb_select_page(REG_PAGE(reg));
43
44 tx = ((data & 0x3ff) << 6) | (REG_ADDR(reg) << 1);
45
46 spi_xfer(TWL3025_DEV_IDX, 16, &tx, 0);
47 }
48
49 u16
50 abb_reg_read(reg)
51 {
52 u16 tx, rx;
53
54 if (REG_PAGE(reg) != abb_state_page)
55 abb_select_page(REG_PAGE(reg));
56
57 tx = (REG_ADDR(reg) << 1) | 1;
58
59 /* A read cycle contains two SPI transfers */
60 spi_xfer(TWL3025_DEV_IDX, 16, &tx, &rx);
61 osmo_delay_ms(1);
62 spi_xfer(TWL3025_DEV_IDX, 16, &tx, &rx);
63
64 rx >>= 6;
65
66 return rx;
67 }
68
69 /* Switch the register page of the TWL3025 */
70 abb_select_page(page)
71 {
72 if (page == 0)
73 abb_reg_write(PAGEREG, 1 << 0);
74 else
75 abb_reg_write(PAGEREG, 1 << 1);
76 abb_state_page = page;
77 return(0);
78 }
79
80 abb_init()
81 {
82 if (abb_state_initdone)
83 return(0);
84 spi_init();
85 abb_select_page(0);
86 /* CLK13M enable */
87 abb_reg_write(TOGBR2, TOGBR2_ACTS);
88 osmo_delay_ms(1);
89 /* for whatever reason we need to do this twice */
90 abb_reg_write(TOGBR2, TOGBR2_ACTS);
91 osmo_delay_ms(1);
92 abb_state_initdone = 1;
93 return(1);
94 }
95
96 void
97 abb_power_off()
98 {
99 abb_init();
100 serial_flush();
101 abb_reg_write(VRPCDEV, 0x01);
102 }