2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* Generic i2c interface for ALSA
|
|
|
|
*
|
|
|
|
* (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
|
2007-10-15 01:50:19 -06:00
|
|
|
* Modified for the ALSA driver by Jaroslav Kysela <perex@perex.cz>
|
2005-04-16 16:20:36 -06:00
|
|
|
*
|
|
|
|
* 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 program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <sound/core.h>
|
|
|
|
#include <sound/i2c.h>
|
|
|
|
|
2007-10-15 01:50:19 -06:00
|
|
|
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
|
2005-04-16 16:20:36 -06:00
|
|
|
MODULE_DESCRIPTION("Generic i2c interface for ALSA");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
|
|
|
|
unsigned char *bytes, int count);
|
|
|
|
static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
|
|
|
|
unsigned char *bytes, int count);
|
|
|
|
static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus,
|
|
|
|
unsigned short addr);
|
|
|
|
|
|
|
|
static struct snd_i2c_ops snd_i2c_bit_ops = {
|
2005-04-16 16:20:36 -06:00
|
|
|
.sendbytes = snd_i2c_bit_sendbytes,
|
|
|
|
.readbytes = snd_i2c_bit_readbytes,
|
|
|
|
.probeaddr = snd_i2c_bit_probeaddr,
|
|
|
|
};
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bus_free(struct snd_i2c_bus *bus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2005-11-17 06:17:19 -07:00
|
|
|
struct snd_i2c_bus *slave;
|
|
|
|
struct snd_i2c_device *device;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-08-08 09:12:47 -06:00
|
|
|
if (snd_BUG_ON(!bus))
|
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
while (!list_empty(&bus->devices)) {
|
|
|
|
device = snd_i2c_device(bus->devices.next);
|
|
|
|
snd_i2c_device_free(device);
|
|
|
|
}
|
|
|
|
if (bus->master)
|
|
|
|
list_del(&bus->buses);
|
|
|
|
else {
|
|
|
|
while (!list_empty(&bus->buses)) {
|
|
|
|
slave = snd_i2c_slave_bus(bus->buses.next);
|
|
|
|
snd_device_free(bus->card, slave);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bus->private_free)
|
|
|
|
bus->private_free(bus);
|
|
|
|
kfree(bus);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bus_dev_free(struct snd_device *device)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2005-11-17 06:17:19 -07:00
|
|
|
struct snd_i2c_bus *bus = device->device_data;
|
2005-04-16 16:20:36 -06:00
|
|
|
return snd_i2c_bus_free(bus);
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
int snd_i2c_bus_create(struct snd_card *card, const char *name,
|
|
|
|
struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2005-11-17 06:17:19 -07:00
|
|
|
struct snd_i2c_bus *bus;
|
2005-04-16 16:20:36 -06:00
|
|
|
int err;
|
2005-11-17 06:17:19 -07:00
|
|
|
static struct snd_device_ops ops = {
|
2005-04-16 16:20:36 -06:00
|
|
|
.dev_free = snd_i2c_bus_dev_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
*ri2c = NULL;
|
[ALSA] Replace with kzalloc() - others
Documentation,SA11xx UDA1341 driver,Generic drivers,MPU401 UART,OPL3
OPL4,Digigram VX core,I2C cs8427,I2C lib core,I2C tea6330t,L3 drivers
AK4114 receiver,AK4117 receiver,PDAudioCF driver,PPC PMAC driver
SPARC AMD7930 driver,SPARC cs4231 driver,Synth,Common EMU synth
USB generic driver,USB USX2Y
Replace kcalloc(1,..) with kzalloc().
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2005-09-09 06:22:34 -06:00
|
|
|
bus = kzalloc(sizeof(*bus), GFP_KERNEL);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (bus == NULL)
|
|
|
|
return -ENOMEM;
|
2006-01-16 08:31:42 -07:00
|
|
|
mutex_init(&bus->lock_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
INIT_LIST_HEAD(&bus->devices);
|
|
|
|
INIT_LIST_HEAD(&bus->buses);
|
|
|
|
bus->card = card;
|
|
|
|
bus->ops = &snd_i2c_bit_ops;
|
|
|
|
if (master) {
|
|
|
|
list_add_tail(&bus->buses, &master->buses);
|
|
|
|
bus->master = master;
|
|
|
|
}
|
|
|
|
strlcpy(bus->name, name, sizeof(bus->name));
|
|
|
|
if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops)) < 0) {
|
|
|
|
snd_i2c_bus_free(bus);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
*ri2c = bus;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-28 07:13:40 -06:00
|
|
|
EXPORT_SYMBOL(snd_i2c_bus_create);
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
|
|
|
|
unsigned char addr, struct snd_i2c_device **rdevice)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2005-11-17 06:17:19 -07:00
|
|
|
struct snd_i2c_device *device;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
*rdevice = NULL;
|
2008-08-08 09:12:47 -06:00
|
|
|
if (snd_BUG_ON(!bus))
|
|
|
|
return -EINVAL;
|
[ALSA] Replace with kzalloc() - others
Documentation,SA11xx UDA1341 driver,Generic drivers,MPU401 UART,OPL3
OPL4,Digigram VX core,I2C cs8427,I2C lib core,I2C tea6330t,L3 drivers
AK4114 receiver,AK4117 receiver,PDAudioCF driver,PPC PMAC driver
SPARC AMD7930 driver,SPARC cs4231 driver,Synth,Common EMU synth
USB generic driver,USB USX2Y
Replace kcalloc(1,..) with kzalloc().
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2005-09-09 06:22:34 -06:00
|
|
|
device = kzalloc(sizeof(*device), GFP_KERNEL);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (device == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
device->addr = addr;
|
|
|
|
strlcpy(device->name, name, sizeof(device->name));
|
|
|
|
list_add_tail(&device->list, &bus->devices);
|
|
|
|
device->bus = bus;
|
|
|
|
*rdevice = device;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-28 07:13:40 -06:00
|
|
|
EXPORT_SYMBOL(snd_i2c_device_create);
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
int snd_i2c_device_free(struct snd_i2c_device *device)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
if (device->bus)
|
|
|
|
list_del(&device->list);
|
|
|
|
if (device->private_free)
|
|
|
|
device->private_free(device);
|
|
|
|
kfree(device);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-28 07:13:40 -06:00
|
|
|
EXPORT_SYMBOL(snd_i2c_device_free);
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
return device->bus->ops->sendbytes(device, bytes, count);
|
|
|
|
}
|
|
|
|
|
2006-04-28 07:13:40 -06:00
|
|
|
EXPORT_SYMBOL(snd_i2c_sendbytes);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
return device->bus->ops->readbytes(device, bytes, count);
|
|
|
|
}
|
|
|
|
|
2006-04-28 07:13:40 -06:00
|
|
|
EXPORT_SYMBOL(snd_i2c_readbytes);
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
return bus->ops->probeaddr(bus, addr);
|
|
|
|
}
|
|
|
|
|
2006-04-28 07:13:40 -06:00
|
|
|
EXPORT_SYMBOL(snd_i2c_probeaddr);
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* bit-operations
|
|
|
|
*/
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus *bus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
if (bus->hw_ops.bit->start)
|
|
|
|
bus->hw_ops.bit->start(bus);
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static inline void snd_i2c_bit_hw_stop(struct snd_i2c_bus *bus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
if (bus->hw_ops.bit->stop)
|
|
|
|
bus->hw_ops.bit->stop(bus);
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static void snd_i2c_bit_direction(struct snd_i2c_bus *bus, int clock, int data)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
if (bus->hw_ops.bit->direction)
|
|
|
|
bus->hw_ops.bit->direction(bus, clock, data);
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static void snd_i2c_bit_set(struct snd_i2c_bus *bus, int clock, int data)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
bus->hw_ops.bit->setlines(bus, clock, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_clock(struct snd_i2c_bus *bus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
if (bus->hw_ops.bit->getclock)
|
|
|
|
return bus->hw_ops.bit->getclock(bus);
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_data(struct snd_i2c_bus *bus, int ack)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
return bus->hw_ops.bit->getdata(bus, ack);
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static void snd_i2c_bit_start(struct snd_i2c_bus *bus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
snd_i2c_bit_hw_start(bus);
|
|
|
|
snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
|
|
|
|
snd_i2c_bit_set(bus, 1, 1);
|
|
|
|
snd_i2c_bit_set(bus, 1, 0);
|
|
|
|
snd_i2c_bit_set(bus, 0, 0);
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static void snd_i2c_bit_stop(struct snd_i2c_bus *bus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
snd_i2c_bit_set(bus, 0, 0);
|
|
|
|
snd_i2c_bit_set(bus, 1, 0);
|
|
|
|
snd_i2c_bit_set(bus, 1, 1);
|
|
|
|
snd_i2c_bit_hw_stop(bus);
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static void snd_i2c_bit_send(struct snd_i2c_bus *bus, int data)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
snd_i2c_bit_set(bus, 0, data);
|
|
|
|
snd_i2c_bit_set(bus, 1, data);
|
|
|
|
snd_i2c_bit_set(bus, 0, data);
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_ack(struct snd_i2c_bus *bus)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int ack;
|
|
|
|
|
|
|
|
snd_i2c_bit_set(bus, 0, 1);
|
|
|
|
snd_i2c_bit_set(bus, 1, 1);
|
|
|
|
snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
|
|
|
|
ack = snd_i2c_bit_data(bus, 1);
|
|
|
|
snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
|
|
|
|
snd_i2c_bit_set(bus, 0, 1);
|
|
|
|
return ack ? -EIO : 0;
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_sendbyte(struct snd_i2c_bus *bus, unsigned char data)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int i, err;
|
|
|
|
|
|
|
|
for (i = 7; i >= 0; i--)
|
|
|
|
snd_i2c_bit_send(bus, !!(data & (1 << i)));
|
|
|
|
if ((err = snd_i2c_bit_ack(bus)) < 0)
|
|
|
|
return err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char data = 0;
|
|
|
|
|
|
|
|
snd_i2c_bit_set(bus, 0, 1);
|
|
|
|
snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
|
|
|
|
for (i = 7; i >= 0; i--) {
|
|
|
|
snd_i2c_bit_set(bus, 1, 1);
|
|
|
|
if (snd_i2c_bit_data(bus, 0))
|
|
|
|
data |= (1 << i);
|
|
|
|
snd_i2c_bit_set(bus, 0, 1);
|
|
|
|
}
|
|
|
|
snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
|
|
|
|
snd_i2c_bit_send(bus, !!last);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
|
|
|
|
unsigned char *bytes, int count)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2005-11-17 06:17:19 -07:00
|
|
|
struct snd_i2c_bus *bus = device->bus;
|
2005-04-16 16:20:36 -06:00
|
|
|
int err, res = 0;
|
|
|
|
|
|
|
|
if (device->flags & SND_I2C_DEVICE_ADDRTEN)
|
|
|
|
return -EIO; /* not yet implemented */
|
|
|
|
snd_i2c_bit_start(bus);
|
|
|
|
if ((err = snd_i2c_bit_sendbyte(bus, device->addr << 1)) < 0) {
|
|
|
|
snd_i2c_bit_hw_stop(bus);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
while (count-- > 0) {
|
|
|
|
if ((err = snd_i2c_bit_sendbyte(bus, *bytes++)) < 0) {
|
|
|
|
snd_i2c_bit_hw_stop(bus);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
snd_i2c_bit_stop(bus);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
|
|
|
|
unsigned char *bytes, int count)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2005-11-17 06:17:19 -07:00
|
|
|
struct snd_i2c_bus *bus = device->bus;
|
2005-04-16 16:20:36 -06:00
|
|
|
int err, res = 0;
|
|
|
|
|
|
|
|
if (device->flags & SND_I2C_DEVICE_ADDRTEN)
|
|
|
|
return -EIO; /* not yet implemented */
|
|
|
|
snd_i2c_bit_start(bus);
|
|
|
|
if ((err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1)) < 0) {
|
|
|
|
snd_i2c_bit_hw_stop(bus);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
while (count-- > 0) {
|
|
|
|
if ((err = snd_i2c_bit_readbyte(bus, count == 0)) < 0) {
|
|
|
|
snd_i2c_bit_hw_stop(bus);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
*bytes++ = (unsigned char)err;
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
snd_i2c_bit_stop(bus);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-11-17 06:17:19 -07:00
|
|
|
static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (addr & 0x8000) /* 10-bit address */
|
|
|
|
return -EIO; /* not yet implemented */
|
|
|
|
if (addr & 0x7f80) /* invalid address */
|
|
|
|
return -EINVAL;
|
|
|
|
snd_i2c_bit_start(bus);
|
|
|
|
err = snd_i2c_bit_sendbyte(bus, addr << 1);
|
|
|
|
snd_i2c_bit_stop(bus);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int __init alsa_i2c_init(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit alsa_i2c_exit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(alsa_i2c_init)
|
|
|
|
module_exit(alsa_i2c_exit)
|