blob: 6f0162ae9d689cf96ab6a5c1059f853aa16099cb [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __SOUND_I2C_H
3#define __SOUND_I2C_H
4
5/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#define SND_I2C_DEVICE_ADDRTEN (1<<0) /* 10-bit I2C address */
9
Takashi Iwai97f02e02005-11-17 14:17:19 +010010struct snd_i2c_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 struct list_head list;
Takashi Iwai97f02e02005-11-17 14:17:19 +010012 struct snd_i2c_bus *bus; /* I2C bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 char name[32]; /* some useful device name */
14 unsigned short flags; /* device flags */
15 unsigned short addr; /* device address (might be 10-bit) */
16 unsigned long private_value;
17 void *private_data;
Takashi Iwai97f02e02005-11-17 14:17:19 +010018 void (*private_free)(struct snd_i2c_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019};
20
Takashi Iwai97f02e02005-11-17 14:17:19 +010021#define snd_i2c_device(n) list_entry(n, struct snd_i2c_device, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Takashi Iwai97f02e02005-11-17 14:17:19 +010023struct snd_i2c_bit_ops {
24 void (*start)(struct snd_i2c_bus *bus); /* transfer start */
25 void (*stop)(struct snd_i2c_bus *bus); /* transfer stop */
26 void (*direction)(struct snd_i2c_bus *bus, int clock, int data); /* set line direction (0 = write, 1 = read) */
27 void (*setlines)(struct snd_i2c_bus *bus, int clock, int data);
28 int (*getclock)(struct snd_i2c_bus *bus);
29 int (*getdata)(struct snd_i2c_bus *bus, int ack);
30};
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Takashi Iwai97f02e02005-11-17 14:17:19 +010032struct snd_i2c_ops {
33 int (*sendbytes)(struct snd_i2c_device *device, unsigned char *bytes, int count);
34 int (*readbytes)(struct snd_i2c_device *device, unsigned char *bytes, int count);
35 int (*probeaddr)(struct snd_i2c_bus *bus, unsigned short addr);
36};
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai97f02e02005-11-17 14:17:19 +010038struct snd_i2c_bus {
39 struct snd_card *card; /* card which I2C belongs to */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 char name[32]; /* some useful label */
41
Ingo Molnaref9f0a42006-01-16 16:31:42 +010042 struct mutex lock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Takashi Iwai97f02e02005-11-17 14:17:19 +010044 struct snd_i2c_bus *master; /* master bus when SCK/SCL is shared */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct list_head buses; /* master: slave buses sharing SCK/SCL, slave: link list */
46
47 struct list_head devices; /* attached devices to this bus */
48
49 union {
Takashi Iwai97f02e02005-11-17 14:17:19 +010050 struct snd_i2c_bit_ops *bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 void *ops;
52 } hw_ops; /* lowlevel operations */
Julia Lawall5df29bc2015-11-29 18:25:24 +010053 const struct snd_i2c_ops *ops; /* midlevel operations */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 unsigned long private_value;
56 void *private_data;
Takashi Iwai97f02e02005-11-17 14:17:19 +010057 void (*private_free)(struct snd_i2c_bus *bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
Takashi Iwai97f02e02005-11-17 14:17:19 +010060#define snd_i2c_slave_bus(n) list_entry(n, struct snd_i2c_bus, buses)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Takashi Iwai97f02e02005-11-17 14:17:19 +010062int snd_i2c_bus_create(struct snd_card *card, const char *name,
63 struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c);
64int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
65 unsigned char addr, struct snd_i2c_device **rdevice);
66int snd_i2c_device_free(struct snd_i2c_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Takashi Iwai97f02e02005-11-17 14:17:19 +010068static inline void snd_i2c_lock(struct snd_i2c_bus *bus)
69{
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (bus->master)
Ingo Molnaref9f0a42006-01-16 16:31:42 +010071 mutex_lock(&bus->master->lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 else
Ingo Molnaref9f0a42006-01-16 16:31:42 +010073 mutex_lock(&bus->lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
Takashi Iwai97f02e02005-11-17 14:17:19 +010075
76static inline void snd_i2c_unlock(struct snd_i2c_bus *bus)
77{
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (bus->master)
Ingo Molnaref9f0a42006-01-16 16:31:42 +010079 mutex_unlock(&bus->master->lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 else
Ingo Molnaref9f0a42006-01-16 16:31:42 +010081 mutex_unlock(&bus->lock_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Takashi Iwai97f02e02005-11-17 14:17:19 +010084int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count);
85int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count);
86int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#endif /* __SOUND_I2C_H */