18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci#ifndef __SOUND_I2C_H
38c2ecf20Sopenharmony_ci#define __SOUND_I2C_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/*
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define SND_I2C_DEVICE_ADDRTEN	(1<<0)	/* 10-bit I2C address */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistruct snd_i2c_device {
118c2ecf20Sopenharmony_ci	struct list_head list;
128c2ecf20Sopenharmony_ci	struct snd_i2c_bus *bus;	/* I2C bus */
138c2ecf20Sopenharmony_ci	char name[32];		/* some useful device name */
148c2ecf20Sopenharmony_ci	unsigned short flags;	/* device flags */
158c2ecf20Sopenharmony_ci	unsigned short addr;	/* device address (might be 10-bit) */
168c2ecf20Sopenharmony_ci	unsigned long private_value;
178c2ecf20Sopenharmony_ci	void *private_data;
188c2ecf20Sopenharmony_ci	void (*private_free)(struct snd_i2c_device *device);
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define snd_i2c_device(n) list_entry(n, struct snd_i2c_device, list)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct snd_i2c_bit_ops {
248c2ecf20Sopenharmony_ci	void (*start)(struct snd_i2c_bus *bus);	/* transfer start */
258c2ecf20Sopenharmony_ci	void (*stop)(struct snd_i2c_bus *bus);	/* transfer stop */
268c2ecf20Sopenharmony_ci	void (*direction)(struct snd_i2c_bus *bus, int clock, int data);  /* set line direction (0 = write, 1 = read) */
278c2ecf20Sopenharmony_ci	void (*setlines)(struct snd_i2c_bus *bus, int clock, int data);
288c2ecf20Sopenharmony_ci	int (*getclock)(struct snd_i2c_bus *bus);
298c2ecf20Sopenharmony_ci	int (*getdata)(struct snd_i2c_bus *bus, int ack);
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct snd_i2c_ops {
338c2ecf20Sopenharmony_ci	int (*sendbytes)(struct snd_i2c_device *device, unsigned char *bytes, int count);
348c2ecf20Sopenharmony_ci	int (*readbytes)(struct snd_i2c_device *device, unsigned char *bytes, int count);
358c2ecf20Sopenharmony_ci	int (*probeaddr)(struct snd_i2c_bus *bus, unsigned short addr);
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistruct snd_i2c_bus {
398c2ecf20Sopenharmony_ci	struct snd_card *card;	/* card which I2C belongs to */
408c2ecf20Sopenharmony_ci	char name[32];		/* some useful label */
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	struct mutex lock_mutex;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	struct snd_i2c_bus *master;	/* master bus when SCK/SCL is shared */
458c2ecf20Sopenharmony_ci	struct list_head buses;	/* master: slave buses sharing SCK/SCL, slave: link list */
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	struct list_head devices; /* attached devices to this bus */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	union {
508c2ecf20Sopenharmony_ci		struct snd_i2c_bit_ops *bit;
518c2ecf20Sopenharmony_ci		void *ops;
528c2ecf20Sopenharmony_ci	} hw_ops;		/* lowlevel operations */
538c2ecf20Sopenharmony_ci	const struct snd_i2c_ops *ops;	/* midlevel operations */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	unsigned long private_value;
568c2ecf20Sopenharmony_ci	void *private_data;
578c2ecf20Sopenharmony_ci	void (*private_free)(struct snd_i2c_bus *bus);
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define snd_i2c_slave_bus(n) list_entry(n, struct snd_i2c_bus, buses)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ciint snd_i2c_bus_create(struct snd_card *card, const char *name,
638c2ecf20Sopenharmony_ci		       struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c);
648c2ecf20Sopenharmony_ciint snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
658c2ecf20Sopenharmony_ci			  unsigned char addr, struct snd_i2c_device **rdevice);
668c2ecf20Sopenharmony_ciint snd_i2c_device_free(struct snd_i2c_device *device);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic inline void snd_i2c_lock(struct snd_i2c_bus *bus)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	if (bus->master)
718c2ecf20Sopenharmony_ci		mutex_lock(&bus->master->lock_mutex);
728c2ecf20Sopenharmony_ci	else
738c2ecf20Sopenharmony_ci		mutex_lock(&bus->lock_mutex);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic inline void snd_i2c_unlock(struct snd_i2c_bus *bus)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	if (bus->master)
798c2ecf20Sopenharmony_ci		mutex_unlock(&bus->master->lock_mutex);
808c2ecf20Sopenharmony_ci	else
818c2ecf20Sopenharmony_ci		mutex_unlock(&bus->lock_mutex);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciint snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count);
858c2ecf20Sopenharmony_ciint snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count);
868c2ecf20Sopenharmony_ciint snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#endif /* __SOUND_I2C_H */
89