⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tda7313.c

📁 iic_at24c03_lc72131_redio用于FM
💻 C
字号:

 /* Driver for the TDA7313 and PT2313L audio processor chips
 *
 * Copyright (c) 2003 Mark McClelland <mark@alpha.dyndns.org>
 *
 * 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. No warranty of any kind is expressed or implied.
 */

#define DRIVER_VERSION "1.00"

#include <linux/version.h>

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/videodev.h>
#include <linux/i2c.h>

#include "id.h"
#include "audiochip.h"

/* For temporary OV511 I2C ID */
#include "ovsensor.h"

#define I2C_TDA7313	0x88

/* Addresses to scan */
static unsigned short normal_i2c[] = {I2C_TDA7313 >> 1, I2C_CLIENT_END};
static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
I2C_CLIENT_INSMOD;

MODULE_DESCRIPTION("Driver for the TDA7313 and PT2313L audio processor chips");
MODULE_AUTHOR("Mark McClelland <mark@alpha.dyndns.org>");
MODULE_LICENSE("GPL");

struct tda7313 {
	struct i2c_client c;
	unsigned char volume; /* Range: 0 - 0x3f */
};

static struct i2c_driver driver;
static struct i2c_client client_template;

/* ----------------------------------------------------------------------- */

static int
tda7313_write(struct i2c_client *c, unsigned char val)
{
	int rc;

	rc = i2c_smbus_write_byte(c, val);
	if (rc < 0)
		printk(KERN_ERR "tda7313: I2C write error\n");
	return rc;
}

static int
tda7313_set_volume(struct i2c_client *c, unsigned char val)
{
	struct tda7313 *t = c->data;

	tda7313_write(c, (~val)&0x3f);
	t->volume = val;
	return 0;
}

static void
tda7313_set_defaults(struct i2c_client *c)
{
	tda7313_set_volume(c, 0);

	/* Disable attenuation */
	tda7313_write(c, 0x80);
	tda7313_write(c, 0xa0);
	tda7313_write(c, 0xc0);
	tda7313_write(c, 0xe0);

	/* Select input 1, loudness enabled (+0dB) */
	tda7313_write(c, 0x40);

	/* Flat EQ */
	tda7313_write(c, 0x6f);
	tda7313_write(c, 0x7f);
}

/* ----------------------------------------------------------------------- */

static int
tda7313_attach(struct i2c_adapter *adap, int addr,
               unsigned short flags, int kind)
{
	struct tda7313 *t;
	struct i2c_client *client;

	t = kmalloc(sizeof(*t), GFP_KERNEL);
	if (!t)
		return -ENOMEM;
	memset(t, 0, sizeof(*t));

	client = &t->c;
	memcpy(client, &client_template, sizeof *client);
	client->adapter = adap;
	client->addr = addr;
	client->data = t;

	tda7313_set_defaults(client);

	MOD_INC_USE_COUNT;
	i2c_attach_client(client);
	return 0;
}

static int
tda7313_detach(struct i2c_client *client)
{
	struct tda7313 *t = client->data;

	i2c_detach_client(client);
	kfree(t);
	MOD_DEC_USE_COUNT;
	return 0;
}

static int
tda7313_probe(struct i2c_adapter *adap)
{
	switch (adap->id) {
	case (I2C_ALGO_SMBUS | I2C_HW_SMBUS_OV511):
		printk(KERN_INFO "tda7313: probing %s i2c adapter [id=0x%06x]\n",
		       adap->name, adap->id);
		return i2c_probe(adap, &addr_data, tda7313_attach);
	default:
		printk(KERN_INFO "tda7313: ignoring adapter: %s [0x%06x]\n",
		       adap->name, adap->id);
		return 0;
	}
}

static int
tda7313_command(struct i2c_client *c, unsigned int cmd, void *arg)
{
	struct tda7313 *t = c->data;

	switch (cmd) {
	case VIDIOCGAUDIO:
	{
		struct video_audio *va = arg;

		printk(KERN_DEBUG "tda7313: VIDIOCGAUDIO\n");

		va->flags |= VIDEO_AUDIO_VOLUME;
		va->volume = t->volume*(65535/0x3f);
		va->balance = 32768;
		va->bass = 0x8;
		va->treble = 0x8;
		va->mode |= VIDEO_SOUND_MONO;

		return 0;
	}
	case VIDIOCSAUDIO:
	{
		struct video_audio *va = arg;

		printk(KERN_DEBUG "tda7313: VIDIOCSAUDIO\n");

		tda7313_set_volume(c, va->volume/(65535/0x3f));

		return 0;
	}
	default:
		return -ENOIOCTLCMD;
	}
}

/* ----------------------------------------------------------------------- */

static struct i2c_driver driver = {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
	.owner           = THIS_MODULE,
#endif
	.name            = "i2c tda7313 driver",
	.id              = I2C_DRIVERID_TDA7313,
	.flags           = I2C_DF_NOTIFY,
	.attach_adapter  = tda7313_probe,
	.detach_client   = tda7313_detach,
	.command         = tda7313_command,
};

static struct i2c_client client_template = {
	.name    = "tda7313",
	.id      = -1,
	.driver  = &driver,
};

static int __init
tda7313_init(void)
{
	return i2c_add_driver(&driver);
}

static void __exit
tda7313_exit(void)
{
	i2c_del_driver(&driver);
}

module_init(tda7313_init);
module_exit(tda7313_exit);

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -