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
This commit is contained in:
Amaury Pouly 2017-11-03 20:14:01 +01:00
parent ee2eb13b74
commit 0fe7b8becf
10 changed files with 279 additions and 18 deletions

View file

@ -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 */

View file

@ -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__ */

View file

@ -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;
}