162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci#ifndef __SOUND_I2C_H
362306a36Sopenharmony_ci#define __SOUND_I2C_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci/*
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#define SND_I2C_DEVICE_ADDRTEN	(1<<0)	/* 10-bit I2C address */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cistruct snd_i2c_device {
1162306a36Sopenharmony_ci	struct list_head list;
1262306a36Sopenharmony_ci	struct snd_i2c_bus *bus;	/* I2C bus */
1362306a36Sopenharmony_ci	char name[32];		/* some useful device name */
1462306a36Sopenharmony_ci	unsigned short flags;	/* device flags */
1562306a36Sopenharmony_ci	unsigned short addr;	/* device address (might be 10-bit) */
1662306a36Sopenharmony_ci	unsigned long private_value;
1762306a36Sopenharmony_ci	void *private_data;
1862306a36Sopenharmony_ci	void (*private_free)(struct snd_i2c_device *device);
1962306a36Sopenharmony_ci};
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define snd_i2c_device(n) list_entry(n, struct snd_i2c_device, list)
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cistruct snd_i2c_bit_ops {
2462306a36Sopenharmony_ci	void (*start)(struct snd_i2c_bus *bus);	/* transfer start */
2562306a36Sopenharmony_ci	void (*stop)(struct snd_i2c_bus *bus);	/* transfer stop */
2662306a36Sopenharmony_ci	void (*direction)(struct snd_i2c_bus *bus, int clock, int data);  /* set line direction (0 = write, 1 = read) */
2762306a36Sopenharmony_ci	void (*setlines)(struct snd_i2c_bus *bus, int clock, int data);
2862306a36Sopenharmony_ci	int (*getclock)(struct snd_i2c_bus *bus);
2962306a36Sopenharmony_ci	int (*getdata)(struct snd_i2c_bus *bus, int ack);
3062306a36Sopenharmony_ci};
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistruct snd_i2c_ops {
3362306a36Sopenharmony_ci	int (*sendbytes)(struct snd_i2c_device *device, unsigned char *bytes, int count);
3462306a36Sopenharmony_ci	int (*readbytes)(struct snd_i2c_device *device, unsigned char *bytes, int count);
3562306a36Sopenharmony_ci	int (*probeaddr)(struct snd_i2c_bus *bus, unsigned short addr);
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistruct snd_i2c_bus {
3962306a36Sopenharmony_ci	struct snd_card *card;	/* card which I2C belongs to */
4062306a36Sopenharmony_ci	char name[32];		/* some useful label */
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	struct mutex lock_mutex;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	struct snd_i2c_bus *master;	/* master bus when SCK/SCL is shared */
4562306a36Sopenharmony_ci	struct list_head buses;	/* master: slave buses sharing SCK/SCL, slave: link list */
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	struct list_head devices; /* attached devices to this bus */
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	union {
5062306a36Sopenharmony_ci		struct snd_i2c_bit_ops *bit;
5162306a36Sopenharmony_ci		void *ops;
5262306a36Sopenharmony_ci	} hw_ops;		/* lowlevel operations */
5362306a36Sopenharmony_ci	const struct snd_i2c_ops *ops;	/* midlevel operations */
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	unsigned long private_value;
5662306a36Sopenharmony_ci	void *private_data;
5762306a36Sopenharmony_ci	void (*private_free)(struct snd_i2c_bus *bus);
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#define snd_i2c_slave_bus(n) list_entry(n, struct snd_i2c_bus, buses)
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciint snd_i2c_bus_create(struct snd_card *card, const char *name,
6362306a36Sopenharmony_ci		       struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c);
6462306a36Sopenharmony_ciint snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
6562306a36Sopenharmony_ci			  unsigned char addr, struct snd_i2c_device **rdevice);
6662306a36Sopenharmony_ciint snd_i2c_device_free(struct snd_i2c_device *device);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic inline void snd_i2c_lock(struct snd_i2c_bus *bus)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	if (bus->master)
7162306a36Sopenharmony_ci		mutex_lock(&bus->master->lock_mutex);
7262306a36Sopenharmony_ci	else
7362306a36Sopenharmony_ci		mutex_lock(&bus->lock_mutex);
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistatic inline void snd_i2c_unlock(struct snd_i2c_bus *bus)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	if (bus->master)
7962306a36Sopenharmony_ci		mutex_unlock(&bus->master->lock_mutex);
8062306a36Sopenharmony_ci	else
8162306a36Sopenharmony_ci		mutex_unlock(&bus->lock_mutex);
8262306a36Sopenharmony_ci}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciint snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count);
8562306a36Sopenharmony_ciint snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count);
8662306a36Sopenharmony_ciint snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#endif /* __SOUND_I2C_H */
89