Home | History | Annotate | Line # | Download | only in i2c
au8522.c revision 1.2.2.1
      1  1.2.2.1    jruoho /* $NetBSD: au8522.c,v 1.2.2.1 2011/06/06 09:07:49 jruoho Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill /*
     30      1.1  jmcneill  * Auvitek AU8522
     31      1.1  jmcneill  */
     32      1.1  jmcneill 
     33      1.1  jmcneill #include <sys/cdefs.h>
     34  1.2.2.1    jruoho __KERNEL_RCSID(0, "$NetBSD: au8522.c,v 1.2.2.1 2011/06/06 09:07:49 jruoho Exp $");
     35      1.1  jmcneill 
     36      1.1  jmcneill #include <sys/param.h>
     37      1.1  jmcneill #include <sys/systm.h>
     38      1.1  jmcneill #include <sys/device.h>
     39      1.1  jmcneill #include <sys/conf.h>
     40      1.1  jmcneill #include <sys/bus.h>
     41      1.1  jmcneill #include <sys/kmem.h>
     42      1.1  jmcneill #include <sys/module.h>
     43      1.1  jmcneill 
     44      1.1  jmcneill #include <dev/i2c/i2cvar.h>
     45      1.1  jmcneill 
     46      1.1  jmcneill #include <dev/i2c/au8522reg.h>
     47      1.1  jmcneill #include <dev/i2c/au8522var.h>
     48      1.1  jmcneill 
     49      1.1  jmcneill static int	au8522_reset(struct au8522 *);
     50      1.1  jmcneill static int	au8522_read_1(struct au8522 *, uint16_t, uint8_t *);
     51      1.1  jmcneill static int	au8522_write_1(struct au8522 *, uint16_t, uint8_t);
     52      1.1  jmcneill static int	au8522_set_vinput(struct au8522 *, au8522_vinput_t);
     53      1.1  jmcneill static int	au8522_set_ainput(struct au8522 *, au8522_ainput_t);
     54      1.1  jmcneill static void	au8522_set_common(struct au8522 *, au8522_vinput_t);
     55      1.1  jmcneill 
     56      1.1  jmcneill static int
     57      1.1  jmcneill au8522_reset(struct au8522 *au)
     58      1.1  jmcneill {
     59      1.1  jmcneill 	return au8522_write_1(au, 0xa4, 1 << 5);
     60      1.1  jmcneill }
     61      1.1  jmcneill 
     62      1.1  jmcneill static int
     63      1.1  jmcneill au8522_read_1(struct au8522 *au, uint16_t reg, uint8_t *val)
     64      1.1  jmcneill {
     65      1.1  jmcneill 	uint8_t cmd[2];
     66      1.1  jmcneill 	int error;
     67      1.1  jmcneill 
     68      1.1  jmcneill 	cmd[0] = (reg >> 8) | 0x40;
     69      1.1  jmcneill 	cmd[1] = reg & 0xff;
     70      1.1  jmcneill 	error = iic_exec(au->i2c, I2C_OP_WRITE, au->i2c_addr,
     71      1.1  jmcneill 	    cmd, sizeof(cmd), NULL, 0, 0);
     72      1.1  jmcneill 	if (error)
     73      1.1  jmcneill 		return error;
     74      1.1  jmcneill 	return iic_exec(au->i2c, I2C_OP_READ, au->i2c_addr,
     75      1.1  jmcneill 		    NULL, 0, val, sizeof(*val), 0);
     76      1.1  jmcneill }
     77      1.1  jmcneill 
     78      1.1  jmcneill static int
     79      1.1  jmcneill au8522_write_1(struct au8522 *au, uint16_t reg, uint8_t val)
     80      1.1  jmcneill {
     81      1.1  jmcneill 	uint8_t data[3];
     82      1.1  jmcneill 
     83      1.1  jmcneill 	data[0] = (reg >> 8) | 0x80;
     84      1.1  jmcneill 	data[1] = reg & 0xff;
     85      1.1  jmcneill 	data[2] = val;
     86      1.1  jmcneill 	return iic_exec(au->i2c, I2C_OP_WRITE, au->i2c_addr,
     87      1.1  jmcneill 	    data, sizeof(data), NULL, 0, 0);
     88      1.1  jmcneill }
     89      1.1  jmcneill 
     90      1.1  jmcneill static int
     91      1.1  jmcneill au8522_set_vinput(struct au8522 *au, au8522_vinput_t vi)
     92      1.1  jmcneill {
     93      1.1  jmcneill 	switch (vi) {
     94      1.1  jmcneill 	case AU8522_VINPUT_CVBS:
     95      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_MODCLKCTL, AU8522_MODCLKCTL_CVBS);
     96      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_PGACTL, 0x00);
     97      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_CLAMPCTL, 0x0e);
     98      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_PGACTL, 0x10);
     99      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_INPUTCTL,
    100      1.1  jmcneill 		    AU8522_INPUTCTL_CVBS_CH1);
    101      1.1  jmcneill 
    102      1.1  jmcneill 		au8522_set_common(au, vi);
    103      1.1  jmcneill 
    104      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
    105      1.1  jmcneill 		    AU8522_SYSMODCTL0_CVBS);
    106      1.1  jmcneill 		break;
    107      1.1  jmcneill 	case AU8522_VINPUT_SVIDEO:
    108      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_MODCLKCTL,
    109      1.1  jmcneill 		    AU8522_MODCLKCTL_SVIDEO);
    110      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_INPUTCTL,
    111      1.1  jmcneill 		    AU8522_INPUTCTL_SVIDEO_CH13);
    112      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_CLAMPCTL, 0x00);
    113      1.1  jmcneill 
    114      1.1  jmcneill 		au8522_set_common(au, vi);
    115      1.1  jmcneill 
    116      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
    117      1.1  jmcneill 		    AU8522_SYSMODCTL0_CVBS);
    118      1.1  jmcneill 
    119      1.1  jmcneill 		break;
    120      1.1  jmcneill 	case AU8522_VINPUT_CVBS_TUNER:
    121      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_MODCLKCTL, AU8522_MODCLKCTL_CVBS);
    122      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_PGACTL, 0x00);
    123      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_CLAMPCTL, 0x0e);
    124      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_PGACTL, 0x10);
    125      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_INPUTCTL,
    126      1.1  jmcneill 		    AU8522_INPUTCTL_CVBS_CH4_SIF);
    127      1.1  jmcneill 
    128      1.1  jmcneill 		au8522_set_common(au, vi);
    129      1.1  jmcneill 
    130      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
    131      1.1  jmcneill 		    AU8522_SYSMODCTL0_CVBS);
    132      1.1  jmcneill 
    133      1.1  jmcneill 		break;
    134      1.1  jmcneill 	default:
    135      1.1  jmcneill 		return EINVAL;
    136      1.1  jmcneill 	}
    137      1.1  jmcneill 
    138      1.1  jmcneill 	return 0;
    139      1.1  jmcneill }
    140      1.1  jmcneill 
    141      1.1  jmcneill static void
    142      1.1  jmcneill au8522_set_common(struct au8522 *au, au8522_vinput_t vi)
    143      1.1  jmcneill {
    144      1.1  jmcneill 	au8522_write_1(au, AU8522_REG_INTMASK, 0x00);
    145      1.1  jmcneill 	au8522_write_1(au, AU8522_REG_VIDEOMODE, vi == AU8522_VINPUT_SVIDEO ?
    146      1.1  jmcneill 	    AU8522_VIDEOMODE_SVIDEO : AU8522_VIDEOMODE_CVBS);
    147      1.1  jmcneill 	au8522_write_1(au, AU8522_REG_TV_PGA, AU8522_TV_PGA_CVBS);
    148      1.1  jmcneill }
    149      1.1  jmcneill 
    150      1.1  jmcneill static int
    151      1.1  jmcneill au8522_set_ainput(struct au8522 *au, au8522_ainput_t ai)
    152      1.1  jmcneill {
    153      1.1  jmcneill 	/* mute during mode change */
    154      1.1  jmcneill 	au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x00);
    155      1.1  jmcneill 	au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x00);
    156      1.1  jmcneill 	au8522_write_1(au, AU8522_REG_AUDIO_VOL, 0x00);
    157      1.1  jmcneill 
    158      1.1  jmcneill 	switch (ai) {
    159      1.1  jmcneill 	case AU8522_AINPUT_SIF:
    160      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
    161      1.1  jmcneill 		    AU8522_SYSMODCTL0_CVBS);
    162      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_MODE, 0x82);
    163      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL1,
    164      1.1  jmcneill 		    AU8522_SYSMODCTL1_I2S);
    165      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_FREQ, 0x03);
    166      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_I2S_CTL2, 0xc2);
    167      1.1  jmcneill 		/* unmute */
    168      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x7f);
    169      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x7f);
    170      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_VOL, 0xff);
    171      1.1  jmcneill 		break;
    172      1.1  jmcneill 	case AU8522_AINPUT_NONE:
    173      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_USBEN, 0x00);
    174      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x7f);
    175      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x7f);
    176      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_MODE, 0x40);
    177      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL1,
    178      1.1  jmcneill 		    AU8522_SYSMODCTL1_SVIDEO);
    179      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_AUDIO_FREQ, 0x03);
    180      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_I2S_CTL2, 0x02);
    181      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
    182      1.1  jmcneill 		    AU8522_SYSMODCTL0_CVBS);
    183      1.1  jmcneill 		break;
    184      1.1  jmcneill 	default:
    185      1.1  jmcneill 		return EINVAL;
    186      1.1  jmcneill 	}
    187      1.1  jmcneill 	return 0;
    188      1.1  jmcneill }
    189      1.1  jmcneill 
    190      1.1  jmcneill struct au8522 *
    191      1.1  jmcneill au8522_open(device_t parent, i2c_tag_t i2c, i2c_addr_t addr)
    192      1.1  jmcneill {
    193      1.1  jmcneill 	struct au8522 *au;
    194      1.1  jmcneill 
    195      1.1  jmcneill 	au = kmem_alloc(sizeof(*au), KM_SLEEP);
    196      1.1  jmcneill 	if (au == NULL)
    197      1.1  jmcneill 		return NULL;
    198      1.1  jmcneill 	au->parent = parent;
    199      1.1  jmcneill 	au->i2c = i2c;
    200      1.1  jmcneill 	au->i2c_addr = addr;
    201      1.1  jmcneill 
    202      1.1  jmcneill 	if (au8522_reset(au))
    203      1.1  jmcneill 		goto failed;
    204      1.1  jmcneill 	if (au8522_write_1(au, AU8522_REG_TUNERCTL, AU8522_TUNERCTL_EN))
    205      1.1  jmcneill 		goto failed;
    206      1.1  jmcneill 
    207      1.1  jmcneill 	return au;
    208      1.1  jmcneill 
    209      1.1  jmcneill failed:
    210      1.1  jmcneill 	kmem_free(au, sizeof(*au));
    211      1.1  jmcneill 	return NULL;
    212      1.1  jmcneill }
    213      1.1  jmcneill 
    214      1.1  jmcneill void
    215      1.1  jmcneill au8522_close(struct au8522 *au)
    216      1.1  jmcneill {
    217      1.1  jmcneill 	kmem_free(au, sizeof(*au));
    218      1.1  jmcneill }
    219      1.1  jmcneill 
    220      1.1  jmcneill void
    221      1.1  jmcneill au8522_enable(struct au8522 *au, bool enable)
    222      1.1  jmcneill {
    223      1.1  jmcneill 	if (enable) {
    224      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
    225      1.1  jmcneill 		    AU8522_SYSMODCTL0_RESET);
    226      1.1  jmcneill 		delay(1000);
    227      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
    228      1.1  jmcneill 		    AU8522_SYSMODCTL0_CVBS);
    229      1.1  jmcneill 	} else {
    230      1.1  jmcneill 		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
    231      1.1  jmcneill 		    AU8522_SYSMODCTL0_DISABLE);
    232      1.1  jmcneill 	}
    233      1.1  jmcneill }
    234      1.1  jmcneill 
    235      1.1  jmcneill void
    236      1.1  jmcneill au8522_set_input(struct au8522 *au, au8522_vinput_t vi, au8522_ainput_t ai)
    237      1.1  jmcneill {
    238      1.1  jmcneill 	au8522_reset(au);
    239      1.1  jmcneill 
    240      1.1  jmcneill 	if (vi != AU8522_VINPUT_UNCONF)
    241      1.1  jmcneill 		au8522_set_vinput(au, vi);
    242      1.1  jmcneill 	if (ai != AU8522_AINPUT_UNCONF)
    243      1.1  jmcneill 		au8522_set_ainput(au, ai);
    244      1.1  jmcneill }
    245      1.1  jmcneill 
    246      1.1  jmcneill int
    247      1.1  jmcneill au8522_get_signal(struct au8522 *au)
    248      1.1  jmcneill {
    249      1.1  jmcneill 	uint8_t status;
    250      1.1  jmcneill 
    251      1.1  jmcneill 	if (au8522_read_1(au, AU8522_REG_STATUS, &status))
    252      1.1  jmcneill 		return 0;
    253      1.1  jmcneill 
    254      1.1  jmcneill #ifdef AU8522_DEBUG
    255      1.1  jmcneill 	printf("au8522: status=0x%02x\n", status);
    256      1.1  jmcneill #endif
    257      1.1  jmcneill 	return (status & AU8522_STATUS_LOCK) == AU8522_STATUS_LOCK ? 1 : 0;
    258      1.1  jmcneill }
    259      1.1  jmcneill 
    260  1.2.2.1    jruoho void
    261  1.2.2.1    jruoho au8522_set_audio(struct au8522 *au, bool onoff)
    262  1.2.2.1    jruoho {
    263  1.2.2.1    jruoho 	if (onoff) {
    264  1.2.2.1    jruoho 		au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x7f);
    265  1.2.2.1    jruoho 		au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x7f);
    266  1.2.2.1    jruoho 		au8522_write_1(au, AU8522_REG_AUDIO_VOL, 0xff);
    267  1.2.2.1    jruoho 	} else {
    268  1.2.2.1    jruoho 		au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x00);
    269  1.2.2.1    jruoho 		au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x00);
    270  1.2.2.1    jruoho 		au8522_write_1(au, AU8522_REG_AUDIO_VOL, 0x00);
    271  1.2.2.1    jruoho 	}
    272  1.2.2.1    jruoho }
    273  1.2.2.1    jruoho 
    274      1.1  jmcneill MODULE(MODULE_CLASS_DRIVER, au8522, NULL);
    275      1.1  jmcneill 
    276      1.1  jmcneill static int
    277      1.1  jmcneill au8522_modcmd(modcmd_t cmd, void *opaque)
    278      1.1  jmcneill {
    279      1.1  jmcneill 	switch (cmd) {
    280      1.1  jmcneill 	case MODULE_CMD_INIT:
    281      1.1  jmcneill 		return 0;
    282      1.1  jmcneill 	case MODULE_CMD_FINI:
    283      1.1  jmcneill 		return 0;
    284      1.1  jmcneill 	default:
    285      1.1  jmcneill 		return ENOTTY;
    286      1.1  jmcneill 	}
    287      1.1  jmcneill }
    288