summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/sonynwz
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2017-11-03 20:14:01 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2017-11-06 21:37:06 +0100
commit0fe7b8becf26816eb303626addc8d34821e361f3 (patch)
treecb8fcd990d5aa3a55212b60e2a3d2282f8f92bff /firmware/target/hosted/sonynwz
parentee2eb13b7407841e9c0dbc76d18878b241d3e9f2 (diff)
downloadrockbox-0fe7b8becf26816eb303626addc8d34821e361f3.tar.gz
rockbox-0fe7b8becf26816eb303626addc8d34821e361f3.tar.bz2
rockbox-0fe7b8becf26816eb303626addc8d34821e361f3.zip
nwzlinux: add support for radio
None of the Sony up to A15 seem to support RDS (they use either Si4702 or Si4708), thus I did not add any code to support RDS. Change-Id: I64838993b9705b36b94665f8470c7a89c772c961
Diffstat (limited to 'firmware/target/hosted/sonynwz')
-rw-r--r--firmware/target/hosted/sonynwz/audio-nwz.c65
-rw-r--r--firmware/target/hosted/sonynwz/nwz_tuner.h61
-rw-r--r--firmware/target/hosted/sonynwz/radio-nwz.c110
3 files changed, 236 insertions, 0 deletions
diff --git a/firmware/target/hosted/sonynwz/audio-nwz.c b/firmware/target/hosted/sonynwz/audio-nwz.c
new file mode 100644
index 0000000000..d119ee736d
--- /dev/null
+++ b/firmware/target/hosted/sonynwz/audio-nwz.c
@@ -0,0 +1,65 @@
+/***************************************************************************
+ * __________ __ ___
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ *
+ * Copyright (C) 2017 Amaury Pouly
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include "config.h"
+#include "system.h"
+#include "cpu.h"
+#include "audio.h"
+#include "audiohw.h"
+#include "sound.h"
+#include "nwzlinux_codec.h"
+
+int audio_channels = 2;
+int audio_output_source = AUDIO_SRC_PLAYBACK;
+
+void audio_set_output_source(int source)
+{
+ if((unsigned)source >= AUDIO_NUM_SOURCES)
+ source = AUDIO_SRC_PLAYBACK;
+
+ audio_output_source = source;
+}
+
+void audio_input_mux(int source, unsigned flags)
+{
+ static int last_source = AUDIO_SRC_PLAYBACK;
+
+ (void)flags;
+
+ switch (source)
+ {
+ default: /* playback - no recording */
+ source = AUDIO_SRC_PLAYBACK;
+ case AUDIO_SRC_PLAYBACK:
+ audio_channels = 2;
+ if (source != last_source)
+ audiohw_set_playback_src(NWZ_PLAYBACK);
+ break;
+
+ case AUDIO_SRC_FMRADIO: /* recording and playback */
+ audio_channels = 2;
+ if (source == last_source)
+ break;
+
+ audiohw_set_playback_src(NWZ_RADIO);
+ break;
+ } /* end switch */
+
+ last_source = source;
+} /* audio_input_mux */
diff --git a/firmware/target/hosted/sonynwz/nwz_tuner.h b/firmware/target/hosted/sonynwz/nwz_tuner.h
new file mode 100644
index 0000000000..36b1b9634c
--- /dev/null
+++ b/firmware/target/hosted/sonynwz/nwz_tuner.h
@@ -0,0 +1,61 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2017 Amaury Pouly
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#ifndef __NWZ_TUNER_H__
+#define __NWZ_TUNER_H__
+
+#define NWZ_TUNER_DEV "/dev/radio0"
+/* we only describe the private ioctl, the rest is standard v4l2 */
+#define NWZ_TUNER_TYPE 'v'
+
+#define NWZ_TUNER_REG_COUNT 20
+
+/* there is something slightly fishy about this structure, the ioctl code says it must be of size
+ * 0x2C but the disassembled code looks like it uses a size of 0x24, probably Sony's code is
+ * massively broken */
+struct nwz_tuner_ioctl_t
+{
+ union
+ {
+ uint32_t regno; // for GET_REG
+ uint32_t rssi; // for GET_RSSI
+ uint32_t freq; // for SET_FREQ
+ struct
+ {
+ uint8_t regno;
+ uint8_t reserved;
+ uint16_t val;
+ }put; // for PUT_REG
+ };
+ uint16_t regs[NWZ_TUNER_REG_COUNT];
+} __attribute__((packed));
+
+/* these requests seem to be the only one supported by all generations */
+#define NWZ_TUNER_INIT_REG _IOWR(NWZ_TUNER_TYPE, 41, struct nwz_tuner_ioctl_t)
+#define NWZ_TUNER_PUT_REG _IOWR(NWZ_TUNER_TYPE, 42, struct nwz_tuner_ioctl_t)
+#define NWZ_TUNER_GET_REG _IOWR(NWZ_TUNER_TYPE, 43, struct nwz_tuner_ioctl_t)
+#define NWZ_TUNER_GET_REG_ALL _IOWR(NWZ_TUNER_TYPE, 44, struct nwz_tuner_ioctl_t)
+#define NWZ_TUNER_SET_FREQ _IOWR(NWZ_TUNER_TYPE, 45, struct nwz_tuner_ioctl_t)
+#define NWZ_TUNER_RESET _IOW(NWZ_TUNER_TYPE, 46, struct nwz_tuner_ioctl_t)
+#define NWZ_TUNER_GPIO2_ON _IOW(NWZ_TUNER_TYPE, 47, struct nwz_tuner_ioctl_t)
+#define NWZ_TUNER_GPIO2_OFF _IOW(NWZ_TUNER_TYPE, 48, struct nwz_tuner_ioctl_t)
+#define NWZ_TUNER_GET_RSSI _IOWR(NWZ_TUNER_TYPE, 49, struct nwz_tuner_ioctl_t)
+
+#endif /* __NWZ_TUNER_H__ */
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 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ *
+ * Copyright (C) 2017 Amaury Pouly
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include "stdint.h"
+#include "stdio.h"
+#include "string.h"
+#include "kernel.h"
+#include "rbendian.h"
+
+#include "nwz_tuner.h"
+#include "si4700.h"
+#include "power.h"
+
+static int radio_fd = -1;
+
+bool tuner_power(bool status)
+{
+ if(status != tuner_powered())
+ {
+ if(status)
+ {
+ radio_fd = open(NWZ_TUNER_DEV, 0);
+ if(radio_fd != -1)
+ {
+ /* Sony is braindead, opening the radio device mutes all audio (even from the DAC)
+ * until SET_FREQ is called with a valid frequency, choose 90MHz because it is valid
+ * in all bands */
+ struct nwz_tuner_ioctl_t req;
+ memset(&req, 0, sizeof(req)); // just to avoid garbage in
+ req.freq = 90000;
+ int ret = ioctl(radio_fd, NWZ_TUNER_SET_FREQ, &req);
+ if(ret != 0)
+ perror("tuner set_freq error");
+ }
+ else
+ perror("tuner open error");
+ }
+ else
+ {
+ close(radio_fd);
+ radio_fd = -1;
+ }
+ }
+ return tuner_powered();
+}
+
+bool tuner_powered(void)
+{
+ return radio_fd != -1;
+}
+
+/* one can adjust the following depending on the tuner, for now it is calibrated for the si470x
+ * which is crazy, because it starts reading at register 0xA and writing at 0x2. We need to
+ * "emulate" this behavior... Note that Si470x transmits in big-endian */
+#define READ_START_REG 0xA
+#define WRITE_START_REG 0x2
+#define REG_COUNT 16
+#define REG_SIZE 2
+#define REG_TYPE uint16_t
+#define REG_SWAP swap16
+
+int fmradio_i2c_write(unsigned char address, unsigned char* buf, int count)
+{
+ (void)address;
+ struct nwz_tuner_ioctl_t req;
+ memset(&req, 0, sizeof(req)); // just to avoid garbage in
+ for(int i = 0; i < count / REG_SIZE; i++)
+ {
+ req.put.regno = (WRITE_START_REG + i) % REG_COUNT;
+ req.put.val = REG_SWAP(*(REG_TYPE *)&buf[i * REG_SIZE]);
+ int ret = ioctl(radio_fd, NWZ_TUNER_PUT_REG, &req);
+ if(ret != 0)
+ return ret;
+ }
+ return 0;
+}
+
+int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
+{
+ (void)address;
+ struct nwz_tuner_ioctl_t req;
+ memset(&req, 0, sizeof(req)); // just to avoid garbage in
+ int ret = ioctl(radio_fd, NWZ_TUNER_GET_REG_ALL, &req);
+ if(ret != 0)
+ return ret;
+ for(int i = 0; i < count / REG_SIZE; i++)
+ *(REG_TYPE *)&buf[i * REG_SIZE] = REG_SWAP(req.regs[(READ_START_REG + i) % REG_COUNT]);
+ return 0;
+}