summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/sonynwz/radio-nwz.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/sonynwz/radio-nwz.c')
-rw-r--r--firmware/target/hosted/sonynwz/radio-nwz.c110
1 files changed, 110 insertions, 0 deletions
diff --git a/firmware/target/hosted/sonynwz/radio-nwz.c b/firmware/target/hosted/sonynwz/radio-nwz.c
new file mode 100644
index 0000000000..db4ffda465
--- /dev/null
+++ b/firmware/target/hosted/sonynwz/radio-nwz.c
@@ -0,0 +1,110 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2017 Amaury Pouly
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include <unistd.h>
22#include <fcntl.h>
23#include <sys/ioctl.h>
24#include <sys/stat.h>
25#include "stdint.h"
26#include "stdio.h"
27#include "string.h"
28#include "kernel.h"
29#include "rbendian.h"
30
31#include "nwz_tuner.h"
32#include "si4700.h"
33#include "power.h"
34
35static int radio_fd = -1;
36
37bool tuner_power(bool status)
38{
39 if(status != tuner_powered())
40 {
41 if(status)
42 {
43 radio_fd = open(NWZ_TUNER_DEV, 0);
44 if(radio_fd != -1)
45 {
46 /* Sony is braindead, opening the radio device mutes all audio (even from the DAC)
47 * until SET_FREQ is called with a valid frequency, choose 90MHz because it is valid
48 * in all bands */
49 struct nwz_tuner_ioctl_t req;
50 memset(&req, 0, sizeof(req)); // just to avoid garbage in
51 req.freq = 90000;
52 int ret = ioctl(radio_fd, NWZ_TUNER_SET_FREQ, &req);
53 if(ret != 0)
54 perror("tuner set_freq error");
55 }
56 else
57 perror("tuner open error");
58 }
59 else
60 {
61 close(radio_fd);
62 radio_fd = -1;
63 }
64 }
65 return tuner_powered();
66}
67
68bool tuner_powered(void)
69{
70 return radio_fd != -1;
71}
72
73/* one can adjust the following depending on the tuner, for now it is calibrated for the si470x
74 * which is crazy, because it starts reading at register 0xA and writing at 0x2. We need to
75 * "emulate" this behavior... Note that Si470x transmits in big-endian */
76#define READ_START_REG 0xA
77#define WRITE_START_REG 0x2
78#define REG_COUNT 16
79#define REG_SIZE 2
80#define REG_TYPE uint16_t
81#define REG_SWAP swap16
82
83int fmradio_i2c_write(unsigned char address, unsigned char* buf, int count)
84{
85 (void)address;
86 struct nwz_tuner_ioctl_t req;
87 memset(&req, 0, sizeof(req)); // just to avoid garbage in
88 for(int i = 0; i < count / REG_SIZE; i++)
89 {
90 req.put.regno = (WRITE_START_REG + i) % REG_COUNT;
91 req.put.val = REG_SWAP(*(REG_TYPE *)&buf[i * REG_SIZE]);
92 int ret = ioctl(radio_fd, NWZ_TUNER_PUT_REG, &req);
93 if(ret != 0)
94 return ret;
95 }
96 return 0;
97}
98
99int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
100{
101 (void)address;
102 struct nwz_tuner_ioctl_t req;
103 memset(&req, 0, sizeof(req)); // just to avoid garbage in
104 int ret = ioctl(radio_fd, NWZ_TUNER_GET_REG_ALL, &req);
105 if(ret != 0)
106 return ret;
107 for(int i = 0; i < count / REG_SIZE; i++)
108 *(REG_TYPE *)&buf[i * REG_SIZE] = REG_SWAP(req.regs[(READ_START_REG + i) % REG_COUNT]);
109 return 0;
110}