Module Name:    src
Committed By:   rkujawa
Date:           Fri Jul 27 12:02:26 UTC 2018

Modified Files:
        src/distrib/sets/lists/man: mi
        src/doc: CHANGES
        src/share/man/man4: Makefile
        src/sys/dev: DEVNAMES
        src/sys/dev/i2c: files.i2c
Added Files:
        src/share/man/man4: tea5767radio.4
        src/sys/dev/i2c: tea5767.c tea5767reg.h

Log Message:
Add TEA5767 FM radio driver. From Karuna Grewal.


To generate a diff of this commit:
cvs rdiff -u -r1.1601 -r1.1602 src/distrib/sets/lists/man/mi
cvs rdiff -u -r1.2414 -r1.2415 src/doc/CHANGES
cvs rdiff -u -r1.658 -r1.659 src/share/man/man4/Makefile
cvs rdiff -u -r0 -r1.1 src/share/man/man4/tea5767radio.4
cvs rdiff -u -r1.311 -r1.312 src/sys/dev/DEVNAMES
cvs rdiff -u -r1.93 -r1.94 src/sys/dev/i2c/files.i2c
cvs rdiff -u -r0 -r1.1 src/sys/dev/i2c/tea5767.c src/sys/dev/i2c/tea5767reg.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/man/mi
diff -u src/distrib/sets/lists/man/mi:1.1601 src/distrib/sets/lists/man/mi:1.1602
--- src/distrib/sets/lists/man/mi:1.1601	Tue Jul 24 15:29:05 2018
+++ src/distrib/sets/lists/man/mi	Fri Jul 27 12:02:26 2018
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1601 2018/07/24 15:29:05 christos Exp $
+# $NetBSD: mi,v 1.1602 2018/07/27 12:02:26 rkujawa Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -1803,6 +1803,7 @@
 ./usr/share/man/cat4/tcu.0			man-sys-catman		.cat
 ./usr/share/man/cat4/tdfxdrm.0			man-sys-catman		.cat
 ./usr/share/man/cat4/tdvfb.0			man-sys-catman		.cat
+./usr/share/man/cat4/tea5767radio.0		man-sys-catman		.cat
 ./usr/share/man/cat4/termios.0			man-sys-catman		.cat
 ./usr/share/man/cat4/tfb.0			man-sys-catman		.cat
 ./usr/share/man/cat4/thinkpad.0			man-sys-catman		.cat
@@ -4875,6 +4876,7 @@
 ./usr/share/man/html4/tcu.html			man-sys-htmlman		html
 ./usr/share/man/html4/tdfxdrm.html		man-sys-htmlman		html
 ./usr/share/man/html4/tdvfb.html		man-sys-htmlman		html
+./usr/share/man/html4/tea5767radio.html		man-sys-htmlman		html
 ./usr/share/man/html4/termios.html		man-sys-htmlman		html
 ./usr/share/man/html4/tfb.html			man-sys-htmlman		html
 ./usr/share/man/html4/thinkpad.html		man-sys-htmlman		html
@@ -7867,6 +7869,7 @@
 ./usr/share/man/man4/tcu.4			man-sys-man		.man
 ./usr/share/man/man4/tdfxdrm.4			man-sys-man		.man
 ./usr/share/man/man4/tdvfb.4			man-sys-man		.man
+./usr/share/man/man4/tea5767radio.4		man-sys-man		.man
 ./usr/share/man/man4/termios.4			man-sys-man		.man
 ./usr/share/man/man4/tfb.4			man-sys-man		.man
 ./usr/share/man/man4/thinkpad.4			man-sys-man		.man

Index: src/doc/CHANGES
diff -u src/doc/CHANGES:1.2414 src/doc/CHANGES:1.2415
--- src/doc/CHANGES:1.2414	Tue Jul 24 07:59:46 2018
+++ src/doc/CHANGES	Fri Jul 27 12:02:25 2018
@@ -1,4 +1,4 @@
-# LIST OF CHANGES FROM LAST RELEASE:			<$Revision: 1.2414 $>
+# LIST OF CHANGES FROM LAST RELEASE:			<$Revision: 1.2415 $>
 #
 #
 # [Note: This file does not mention every change made to the NetBSD source tree.
@@ -181,3 +181,6 @@ Changes from NetBSD 8.0 to NetBSD 9.0:
 	arm: Add support for ARMv7 performance event monitoring with tprof(4).
 		[jmcneill 20180715]
 	dhcpcd: Import 7.0.7. [roy 20180724]
+	tea5767radio(4): Add Philips/NXP TEA5767 I2C-controlled stereo FM radio
+		driver. [rkujawa 20180727]
+

Index: src/share/man/man4/Makefile
diff -u src/share/man/man4/Makefile:1.658 src/share/man/man4/Makefile:1.659
--- src/share/man/man4/Makefile:1.658	Sun Jul 15 05:16:40 2018
+++ src/share/man/man4/Makefile	Fri Jul 27 12:02:26 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.658 2018/07/15 05:16:40 maxv Exp $
+#	$NetBSD: Makefile,v 1.659 2018/07/27 12:02:26 rkujawa Exp $
 #	@(#)Makefile	8.1 (Berkeley) 6/18/93
 
 MAN=	aac.4 ac97.4 acardide.4 aceride.4 acphy.4 \
@@ -61,8 +61,8 @@ MAN=	aac.4 ac97.4 acardide.4 aceride.4 a
 	sm.4 smsh.4 sn.4 sony.4 spc.4 speaker.4 spif.4 sqphy.4 ss.4 \
 	st.4 ste.4 stge.4 sti.4 stpcide.4 sv.4 strip.4 \
 	svwsata.4 swsensor.4 swwdog.4 sysmon.4 \
-	tap.4 tc.4 tcds.4 tcp.4 tcu.4 tdvfb.4 termios.4 tfb.4 thinkpad.4 \
-	ti.4 tl.4 tlp.4 tlphy.4 tpm.4 tprof.4 tr.4 tra.4 \
+	tap.4 tc.4 tcds.4 tcp.4 tcu.4 tdvfb.4 tea5767radio.4 termios.4 tfb.4
+	thinkpad.4 ti.4 tl.4 tlp.4 tlphy.4 tpm.4 tprof.4 tr.4 tra.4 \
 	trm.4 tsllux.4 tty.4 tun.4 tqphy.4 twa.4 twe.4 txp.4 \
 	uark.4 ubsec.4 udp.4 uep.4 ug.4 uha.4 uk.4 ukphy.4 unix.4 userconf.4 \
 	vald.4 valz.4 veriexec.4 vga.4 vge.4 viaide.4 video.4 vioif.4 viomb.4 \

Index: src/sys/dev/DEVNAMES
diff -u src/sys/dev/DEVNAMES:1.311 src/sys/dev/DEVNAMES:1.312
--- src/sys/dev/DEVNAMES:1.311	Tue Jun  5 08:03:29 2018
+++ src/sys/dev/DEVNAMES	Fri Jul 27 12:02:26 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: DEVNAMES,v 1.311 2018/06/05 08:03:29 hkenken Exp $
+#	$NetBSD: DEVNAMES,v 1.312 2018/07/27 12:02:26 rkujawa Exp $
 #
 # This file contains all used device names and defined attributes in
 # alphabetical order. New devices added to the system somewhere should first
@@ -1325,6 +1325,7 @@ tcx			sparc64
 tdvfb			MI
 te			vax
 tea5757			MI
+tea5767radio		MI
 tfb			MI
 tga			MI
 ti			MI

Index: src/sys/dev/i2c/files.i2c
diff -u src/sys/dev/i2c/files.i2c:1.93 src/sys/dev/i2c/files.i2c:1.94
--- src/sys/dev/i2c/files.i2c:1.93	Tue Jun 26 06:34:55 2018
+++ src/sys/dev/i2c/files.i2c	Fri Jul 27 12:02:26 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.i2c,v 1.93 2018/06/26 06:34:55 thorpej Exp $
+#	$NetBSD: files.i2c,v 1.94 2018/07/27 12:02:26 rkujawa Exp $
 
 obsolete defflag	opt_i2cbus.h		I2C_SCAN
 define	i2cbus { }
@@ -339,3 +339,9 @@ file	dev/i2c/imt.c				imt
 device	tsllux: sysmon_envsys
 attach	tsllux at iic
 file	dev/i2c/tsl256x.c			tsllux
+
+# Philips/NXP TEA5767 
+device tea5767radio : radiodev
+attach tea5767radio at iic
+file dev/i2c/tea5767.c				tea5767radio
+

Added files:

Index: src/share/man/man4/tea5767radio.4
diff -u /dev/null src/share/man/man4/tea5767radio.4:1.1
--- /dev/null	Fri Jul 27 12:02:26 2018
+++ src/share/man/man4/tea5767radio.4	Fri Jul 27 12:02:26 2018
@@ -0,0 +1,77 @@
+.\"	$NetBSD: tea5767radio.4,v 1.1 2018/07/27 12:02:26 rkujawa Exp $
+.\"
+.\" Copyright (c) 2018 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Karuna Grewal.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd July 6, 2018
+.Dt TEA5767RADIO
+.Os
+.Sh NAME
+.Nm tea5767
+.Nd Philips/NXP TEA5767 FM Chip
+.Sh SYNOPSIS
+.Cd "tea5767radio* at iic? addr 0x60 flags 0x00"
+.Cd "radio* at tea5767radio?"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides support for the Philips/NXP TEA5767 FM stereo radio.
+.Pp
+The
+.Nm
+can tune in the range 87.5 \- 108.0 MHz, perform hardware signal search
+and supports mono/stereo toggling.
+.Pp
+The flags control the FM Band, XTAL and PLL values as follows:
+.Bl -tag -width Ds
+.It 0x01
+sets the FM Band to Japan.
+.It 0x02
+sets the PLL bit.
+.It 0x04
+sets the XTAL bit.
+.It 0x08
+force enables hardware search support.
+.Sh SEE ALSO
+.Xr iic 4 ,
+.Xr radio 4 ,
+.Xr radio 9 ,
+.Rs
+.%A TEA5767
+.%T Low-power FM stereo radio
+.%N Rev. 05
+.%D 26 January 2007
+.Re
+.Sh CAVEATS
+Many popular TEA5767 evaluation boards feature low quality 32.768kHz crystals.
+The inaccuracy of these crystals may often lead to malfunction of the hardware
+search funciton.
+Therefore, the 
+.Nm
+driver tries to detect the crystal quality during attachment. If the crystal
+of insufficient accuracy was detected, hardware search function is disabled.
+It can be forcefully re-enabled by setting the 0x08 flag.

Index: src/sys/dev/i2c/tea5767.c
diff -u /dev/null src/sys/dev/i2c/tea5767.c:1.1
--- /dev/null	Fri Jul 27 12:02:26 2018
+++ src/sys/dev/i2c/tea5767.c	Fri Jul 27 12:02:26 2018
@@ -0,0 +1,349 @@
+/*	$NetBSD: tea5767.c,v 1.1 2018/07/27 12:02:26 rkujawa Exp $	*/
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Karuna Grewal.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: tea5767.c,v 1.1 2018/07/27 12:02:26 rkujawa Exp $");
+
+#include <sys/proc.h>
+#include <sys/kernel.h>
+#include <sys/param.h>
+#include <sys/device.h>
+#include <sys/errno.h>
+#include <sys/systm.h>
+
+#include <dev/i2c/i2cvar.h>
+
+#include <sys/ioctl.h>
+#include <sys/radioio.h>
+#include <dev/radio_if.h>
+
+#include "tea5767reg.h"
+
+struct tea5767_tune {
+	int mute;
+	uint32_t freq;
+	int stereo;
+	bool is_pll_set;
+	bool is_xtal_set;
+	bool is_force_srch;
+	int fm_band;		/* Set = JAPAN. */
+	int lock;
+	int adc_stop_level;
+};
+
+struct tea5767_softc {
+	device_t sc_dev;
+	/* Tunable properties. */
+	struct tea5767_tune tune;
+	/* I2C bus controller. */
+	i2c_tag_t sc_i2c_tag;
+	/* Device addr on I2C. */
+	i2c_addr_t sc_addr;
+	/* Device capabilities. */
+	uint32_t caps;
+};
+
+static int tea5767_get_info(void *, struct radio_info *);
+static int tea5767_set_info(void *, struct radio_info *);
+static int tea5767_search(void *, int);
+
+static int tea5767_match(device_t, cfdata_t, void *);
+static void tea5767_attach(device_t, device_t, void *);
+
+static const struct radio_hw_if tea5767_hw_if = {
+	NULL,
+	NULL,
+	tea5767_get_info,
+	tea5767_set_info,
+	tea5767_search
+};
+
+static const uint32_t tea5767_common_caps =
+	RADIO_CAPS_DETECT_STEREO |
+	RADIO_CAPS_SET_MONO |
+	RADIO_CAPS_HW_SEARCH |
+	RADIO_CAPS_LOCK_SENSITIVITY;
+
+CFATTACH_DECL_NEW(tea5767radio, sizeof(struct tea5767_softc), tea5767_match,
+	tea5767_attach, NULL, NULL);
+
+static int
+tea5767_match(device_t parent, cfdata_t cf, void *aux)
+{
+	struct i2c_attach_args *ia = aux;
+
+	if (ia->ia_addr == TEA5767_ADDR)
+		return I2C_MATCH_ADDRESS_ONLY;
+
+	return 0;
+}
+
+static void
+tea5767_attach(device_t parent, device_t self, void *aux)
+{
+	struct tea5767_softc *sc = device_private(self);
+	int tea5767_flags = device_cfdata(self)->cf_flags;
+	struct i2c_attach_args *ia = aux;
+
+	aprint_normal(": Philips/NXP TEA5767 Radio\n");
+	aprint_naive(": FM radio\n");
+
+	sc->sc_dev = self;
+	sc->tune.mute = 0;
+	sc->tune.freq = MIN_FM_FREQ;
+	sc->tune.stereo = 1;
+	sc->tune.is_pll_set = false;
+	sc->tune.is_xtal_set = false;
+	sc->tune.is_force_srch = false;
+	sc->sc_i2c_tag = ia->ia_tag;
+	sc->sc_addr = ia->ia_addr;
+
+	if (tea5767_flags & TEA5767_PLL_FLAG)
+		sc->tune.is_pll_set = true;
+	if (tea5767_flags & TEA5767_XTAL_FLAG)
+		sc->tune.is_xtal_set = true;
+	if (tea5767_flags & TEA5767_JAPAN_FM_FLAG)
+		sc->tune.fm_band = 1;
+	if (tea5767_flags & TEA5767_FORCE_SRCH_FLAG)
+		sc->tune.is_force_srch = true;
+
+	sc->caps = tea5767_common_caps;
+
+	/* 
+	 * Check the quality of crystal and disable hardware search if
+	 * necessary.
+	 */
+	if (!tea5767_if_check(sc) && !(sc->tune.is_force_srch))
+		sc->caps &= ~RADIO_CAPS_HW_SEARCH;
+
+	radio_attach_mi(&tea5767_hw_if, sc, self);
+}
+
+static int
+tea5767_write(struct tea5767_softc *sc, uint8_t *reg)
+{
+	int exec_result;
+
+	if (iic_acquire_bus(sc->sc_i2c_tag, I2C_F_POLL)) {
+		device_printf(sc->sc_dev, "bus acquiration failed.\n");
+		return 1;
+	}
+
+	exec_result = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
+	    sc->sc_addr, NULL, 0, reg, 5, I2C_F_POLL);
+
+	if (exec_result) {
+		iic_release_bus(sc->sc_i2c_tag, I2C_F_POLL);
+		device_printf(sc->sc_dev, "write operation failed %d.\n",
+		    exec_result);
+		return 1;
+	}
+
+	iic_release_bus(sc->sc_i2c_tag, I2C_F_POLL);
+
+	return 0;
+}
+
+
+static int
+tea5767_read(struct tea5767_softc *sc, uint8_t *reg)
+{
+	int exec_result;
+
+	if (iic_acquire_bus(sc->sc_i2c_tag, I2C_F_POLL)) {
+		device_printf(sc->sc_dev, "bus acquiration failed.\n");
+		return 1;
+	}
+
+	iic_exec(sc->sc_i2c_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
+	    NULL, 0, reg, 5, I2C_F_POLL);
+
+	if (exec_result) {
+		iic_release_bus(sc->sc_i2c_tag, I2C_F_POLL);
+		device_printf(sc->sc_dev, "read operation failed.\n");
+		return 1;
+	}
+
+	iic_release_bus(sc->sc_i2c_tag, I2C_F_POLL);
+	return 0;
+}
+
+/*
+ * Calculate the PLL word.
+ * TODO: Extend this calculation to do high side injection as well
+ * (add fields to tea5767_tune).
+ */
+static void
+tea5767_freq_to_pll(struct tea5767_tune tune, uint8_t *buf)
+{
+	uint16_t pll_word = 0;
+
+	if (!tune.is_pll_set && !tune.is_xtal_set)
+		pll_word = 4 * (tune.freq  - 225) * 1000 / 50000;
+	else if (!tune.is_pll_set && tune.is_xtal_set) {
+		pll_word = 4 * (tune.freq - 225) * 1000 / 32768;
+		buf[3] = TEA5767_XTAL;
+	}
+	else {
+		pll_word = 4 * (tune.freq - 225) * 1000 / 50000;
+		buf[4] |= TEA5767_PLLREF;
+	}
+
+	buf[1] = pll_word & 0xff;
+	buf[0] = (pll_word>>8) & 0x3f;
+}
+
+static void
+tea5767_set_properties(struct tea5767_softc *sc, uint8_t *reg)
+{
+	memset(reg,0,5);
+
+	tea5767_freq_to_pll(sc->tune, reg);
+
+	if (sc->tune.mute) {
+		reg[0] |= TEA5767_MUTE;
+		reg[2] |=TEA5767_MUTE_R | TEA5767_MUTE_L;
+	}
+
+	reg[3] |= TEA5767_SNC;
+
+	if (sc->tune.stereo == 0)
+		reg[2] |= TEA5767_MONO;
+	if(sc->tune.fm_band)
+		reg[3] |= TEA5767_FM_BAND;
+}
+
+static bool
+tea5767_if_check(struct tea5767_soft *sc)
+{
+	uint8_t reg[5];
+
+	tea5767_set_properties(sc, reg);
+	tea5767_write(sc, reg);
+
+	memset(reg,0,5);
+	delay(1000);
+	tea5767_read(sc,reg);
+
+	if ((reg[2] & 0x7f) == 0x37)
+		return true;
+	return false;    
+}
+
+static void
+tea5767_pll_to_freq(struct tea5767_tune *tune, uint8_t *buf)
+{
+	int pll_word = buf[1] | (buf[0] & 0x3f) << 8;
+	if (!tune->is_pll_set && !tune->is_xtal_set)
+		tune->freq = pll_word * 50 / 4 + 225;
+	else if (!tune->is_pll_set && tune->is_xtal_set)
+		tune->freq = pll_word * 32768 / 4000 + 225;
+	else
+		tune->freq = pll_word * 50 / 4 + 225;
+}
+
+static int
+tea5767_get_info(void *v, struct radio_info *ri)
+{
+	struct tea5767_softc *sc = v;
+	uint8_t reg[5];
+	tea5767_read(sc, reg);
+
+	tea5767_pll_to_freq(&sc->tune, reg);
+	ri->mute = sc->tune.mute;
+	ri->stereo = sc->tune.stereo;
+	ri->freq = sc->tune.freq;
+	ri->lock = sc->tune.lock;
+
+	ri->caps = sc->caps;
+
+	return 0;
+}
+
+static int
+tea5767_set_info(void *v, struct radio_info *ri)
+{
+	struct tea5767_softc *sc = v;
+	int adc_conversion;
+	uint8_t reg[5];
+
+	sc->tune.mute = ri->mute;
+	sc->tune.stereo = ri->stereo;
+	sc->tune.freq = ri->freq;
+	sc->tune.lock = ri->lock;
+
+	adc_conversion = (ri->lock-60) * 3 / 30;
+
+	switch(adc_conversion) {
+	case 0: sc->tune.adc_stop_level = TEA5767_SSL_3;
+		break;
+	case 1: sc->tune.adc_stop_level = TEA5767_SSL_2;
+		break;
+	case 2: sc->tune.adc_stop_level = TEA5767_SSL_1;
+		break;
+	}
+
+	tea5767_set_properties(sc, reg);
+	tea5767_write(sc, reg);
+	return 0;
+}
+
+static int
+tea5767_search(void *v, int dir)
+{
+	struct tea5767_softc *sc = v;
+	uint8_t reg[5];
+
+	/* Increment frequency to search for the next frequency. */
+	sc->tune.freq = sc->tune.freq >= 107500 ? 87500 : sc->tune.freq + 100;
+	tea5767_set_properties(sc, reg);
+
+	/* Activate autonomous search. */
+	reg[0] |= TEA5767_SEARCH;
+
+	/*
+	 * If dir 1 => search up, if dir 0 => search down.
+	 */
+	if (dir)
+		reg[2] |= TEA5767_SUD;
+
+	reg[2] |= sc->tune.adc_stop_level; /* Stop level for search. */
+
+	tea5767_write(sc, reg);
+
+	memset(reg, 0, 5);
+
+	while (tea5767_read(sc, reg), !(reg[0] & TEA5767_READY_FLAG)) {
+		kpause("teasrch", true, hz/100, NULL);
+	}
+
+	return 0;
+}
+
Index: src/sys/dev/i2c/tea5767reg.h
diff -u /dev/null src/sys/dev/i2c/tea5767reg.h:1.1
--- /dev/null	Fri Jul 27 12:02:26 2018
+++ src/sys/dev/i2c/tea5767reg.h	Fri Jul 27 12:02:26 2018
@@ -0,0 +1,79 @@
+/*	$NetBSD: tea5767reg.h,v 1.1 2018/07/27 12:02:26 rkujawa Exp $	*/
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Karuna Grewal.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __TEA5767REG_H__
+#define __TEA5767REG_H__
+
+/* TEA5767 ADDR */
+#define TEA5767_ADDR                0x60
+
+/* 1st byte */
+#define TEA5767_MUTE                0x80    /* Set Mute */
+#define TEA5767_SEARCH              0x40    /* Activate search Mode */
+
+/* 3rd byte */
+#define TEA5767_SUD                 0x80    /* Search Up */
+#define TEA5767_SSL_1               0x60    /* ADC o/p = 10 */
+#define TEA5767_SSL_2               0x40    /* ADC o/p = 7 */
+#define TEA5767_SSL_3               0x20    /* ADC o/p = 5 */
+#define TEA5767_MONO                0x08    /* Force Mono */
+#define TEA5767_MUTE_R              0x04    /* Mute Right */
+#define TEA5767_MUTE_L              0x02    /* Mute Left */
+
+/* 4th byte */
+#define TEA5767_STANDBY             0x40
+#define TEA5767_FM_BAND             0x20    /* Set Japanese FM Band */
+#define TEA5767_XTAL                0x10
+#define TEA5767_SMUTE               0x08
+#define TEA5767_SNC                 0x02    /* Stereo Noise Cancelling */
+#define TEA5767_SEARCH_IND          0x01
+
+/* 5th byte */
+#define TEA5767_PLLREF              0x80    /* If enabled TEA5767_CLK_FREQ : 6.5MHZ*/
+
+/* Read Mode MASKS*/
+
+/* 1st byte */
+#define TEA5767_READY_FLAG          0x80
+#define TEA5767_BAND_LIMIT          0X40
+
+/* 3rd byte */
+#define TEA5767_STEREO              0x80
+#define TEA5767_IF_COUNTER          0x7f
+
+/* 4th byte */
+#define TEA5767_ADC_LEVEL           0xf0
+
+/* Kernel Config Maps */
+#define TEA5767_JAPAN_FM_FLAG       0x01
+#define TEA5767_PLL_FLAG            0x02
+#define TEA5767_XTAL_FLAG           0x04
+#define TEA5767_FORCE_SRCH_FLAG     0x08
+#endif

Reply via email to