summaryrefslogtreecommitdiff
path: root/firmware/target/arm/imx233/fmradio-imx233.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/imx233/fmradio-imx233.c')
-rw-r--r--firmware/target/arm/imx233/fmradio-imx233.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/firmware/target/arm/imx233/fmradio-imx233.c b/firmware/target/arm/imx233/fmradio-imx233.c
new file mode 100644
index 0000000000..28ac4a4893
--- /dev/null
+++ b/firmware/target/arm/imx233/fmradio-imx233.c
@@ -0,0 +1,162 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2013 by Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "fmradio-imx233.h"
22#include "fmradio-target.h"
23#include "pinctrl-imx233.h"
24#include "i2c.h"
25#include "generic_i2c.h"
26
27#ifndef IMX233_FMRADIO_I2C
28#error You must define IMX233_FMRADIO_I2C in fmradio-target.h
29#endif
30
31#ifndef IMX233_FMRADIO_POWER
32#error You must define IMX233_FMRADIO_POWER in fmradio-target.h
33#endif
34
35/** Hardware I2C */
36#if IMX233_FMRADIO_I2C == FMI_HW
37void fmradio_i2c_init(void)
38{
39}
40
41int fmradio_i2c_write(unsigned char address, const unsigned char* buf, int count)
42{
43 return i2c_write(address, buf, count);
44}
45
46int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
47{
48 return i2c_read(address, buf, count);
49}
50/** Software I2C */
51#elif IMX233_FMRADIO_I2C == FMI_SW
52#if !defined(FMI_SW_SDA_BANK) || !defined(FMI_SW_SDA_PIN) || \
53 !defined(FMI_SW_SCL_BANK) || !defined(FMI_SW_SCL_PIN)
54#error You must define FMI_SW_SDA_BANK, FMI_SW_SDA_PIN, FMI_SW_SCL_BANK and FMI_SW_SCL_PIN
55#endif
56static int fmradio_i2c_bus = -1;
57
58static void i2c_scl_dir(bool out)
59{
60 imx233_pinctrl_enable_gpio(FMI_SW_SCL_BANK, FMI_SW_SCL_PIN, out);
61}
62
63static void i2c_sda_dir(bool out)
64{
65 imx233_pinctrl_enable_gpio(FMI_SW_SDA_BANK, FMI_SW_SDA_PIN, out);
66}
67
68static void i2c_scl_out(bool high)
69{
70 imx233_pinctrl_set_gpio(FMI_SW_SCL_BANK, FMI_SW_SCL_PIN, high);
71}
72
73static void i2c_sda_out(bool high)
74{
75 imx233_pinctrl_set_gpio(FMI_SW_SDA_BANK, FMI_SW_SDA_PIN, high);
76}
77
78static bool i2c_scl_in(void)
79{
80 return imx233_pinctrl_get_gpio(FMI_SW_SCL_BANK, FMI_SW_SCL_PIN);
81}
82
83static bool i2c_sda_in(void)
84{
85 return imx233_pinctrl_get_gpio(FMI_SW_SDA_BANK, FMI_SW_SDA_PIN);
86}
87
88static void i2c_delay(int d)
89{
90 udelay(d);
91}
92
93struct i2c_interface fmradio_i2c =
94{
95 .scl_dir = i2c_scl_dir,
96 .sda_dir = i2c_sda_dir,
97 .scl_out = i2c_scl_out,
98 .sda_out = i2c_sda_out,
99 .scl_in = i2c_scl_in,
100 .sda_in = i2c_sda_in,
101 .delay = i2c_delay,
102 .delay_hd_sta = 4,
103 .delay_hd_dat = 5,
104 .delay_su_dat = 1,
105 .delay_su_sto = 4,
106 .delay_su_sta = 5,
107 .delay_thigh = 4
108};
109
110void fmradio_i2c_init(void)
111{
112 imx233_pinctrl_acquire(FMI_SW_SDA_BANK, FMI_SW_SDA_PIN, "fmradio_i2c_sda");
113 imx233_pinctrl_acquire(FMI_SW_SCL_BANK, FMI_SW_SCL_BANK, "fmradio_i2c_scl");
114 imx233_pinctrl_set_function(FMI_SW_SDA_BANK, FMI_SW_SDA_PIN, PINCTRL_FUNCTION_GPIO);
115 imx233_pinctrl_set_function(FMI_SW_SCL_BANK, FMI_SW_SCL_BANK, PINCTRL_FUNCTION_GPIO);
116 fmradio_i2c_bus = i2c_add_node(&fmradio_i2c);
117}
118
119int fmradio_i2c_write(unsigned char address, const unsigned char* buf, int count)
120{
121 return i2c_write_data(fmradio_i2c_bus, address, -1, buf, count);
122}
123
124int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
125{
126 return i2c_read_data(fmradio_i2c_bus, address, -1, buf, count);
127}
128#else
129#error Invalid value for IMX233_FMRADIO_I2C
130#endif
131
132static bool tuner_enable = false;
133
134bool tuner_power(bool enable)
135{
136 if(enable != tuner_enable)
137 {
138 tuner_enable = enable;
139#if IMX233_FMRADIO_POWER == FMP_GPIO
140 static bool init = false;
141 if(!init)
142 {
143 /* CE is B0P29 (active high) */
144 imx233_pinctrl_acquire(FMP_GPIO_BANK, FMP_GPIO_PIN, "tuner_power");
145 imx233_pinctrl_set_function(FMP_GPIO_BANK, FMP_GPIO_PIN, PINCTRL_FUNCTION_GPIO);
146 imx233_pinctrl_enable_gpio(FMP_GPIO_BANK, FMP_GPIO_PIN, true);
147 }
148 imx233_pinctrl_set_gpio(FMP_GPIO_BANK, FMP_GPIO_PIN, enable);
149 /* power up delay */
150 sleep(FMP_GPIO_DELAY);
151#elif IMX233_FMRADIO_POWER == FMP_NONE
152#else
153#error Invalid value for IMX233_FMRADIO_POWER
154#endif
155 }
156 return tuner_enable;
157}
158
159bool tuner_powered(void)
160{
161 return tuner_enable;
162}