soundwire.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #ifndef _LINUX_SOUNDWIRE_H
  6. #define _LINUX_SOUNDWIRE_H
  7. #include <linux/device.h>
  8. #include <linux/mutex.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/regmap.h>
  11. #include "audio_mod_devicetable.h"
  12. #define SWR_CLK_RATE_0P3MHZ 300000
  13. #define SWR_CLK_RATE_0P6MHZ 600000
  14. #define SWR_CLK_RATE_1P2MHZ 1200000
  15. #define SWR_CLK_RATE_2P4MHZ 2400000
  16. #define SWR_CLK_RATE_4P8MHZ 4800000
  17. #define SWR_CLK_RATE_9P6MHZ 9600000
  18. #define SWR_CLK_RATE_11P2896MHZ 1128960
  19. extern struct bus_type soundwire_type;
  20. struct swr_device;
  21. /* Soundwire supports max. of 8 channels per port */
  22. #define SWR_MAX_CHANNEL_NUM 8
  23. /* Soundwire supports max. of 14 ports on each device */
  24. #define SWR_MAX_DEV_PORT_NUM 14
  25. /* Maximum number of slave devices that a master can control */
  26. #define SWR_MAX_DEV_NUM 11
  27. /* Maximum number of ports on master so that it can accommodate all the port
  28. * configurations of all devices
  29. */
  30. #define SWR_MAX_MSTR_PORT_NUM (SWR_MAX_DEV_NUM * SWR_MAX_DEV_PORT_NUM)
  31. /* Regmap support for soundwire interface */
  32. struct regmap *__devm_regmap_init_swr(struct swr_device *dev,
  33. const struct regmap_config *config,
  34. struct lock_class_key *lock_key,
  35. const char *lock_name);
  36. /**
  37. * regmap_init_swr(): Initialise register map
  38. *
  39. * @swr: Device that will be interacted with
  40. * @config: Configuration for register map
  41. *
  42. * The return value will be an ERR_PTR() on error or a valid pointer to
  43. * a struct regmap.
  44. */
  45. #define regmap_init_swr(swr, config) \
  46. __regmap_lockdep_wrapper(__regmap_init_swr, #config, \
  47. swr, config)
  48. /**
  49. * devm_regmap_init_swr(): Initialise managed register map
  50. *
  51. * @swr: Device that will be interacted with
  52. * @config: Configuration for register map
  53. *
  54. * The return value will be an ERR_PTR() on error or a valid pointer
  55. * to a struct regmap. The regmap will be automatically freed by the
  56. * device management code.
  57. */
  58. #define devm_regmap_init_swr(swr, config) \
  59. __regmap_lockdep_wrapper(__devm_regmap_init_swr, #config, \
  60. swr, config)
  61. /* Indicates soundwire devices group information */
  62. enum {
  63. SWR_GROUP_NONE = 0,
  64. SWR_GROUP_12 = 12,
  65. SWR_GROUP_13 = 13,
  66. SWR_BROADCAST = 15,
  67. };
  68. /*
  69. * struct swr_port_info - represents new soundwire frame shape
  70. * with full data ports
  71. * @list: link with other soundwire port info nodes
  72. * @dev_num: logical device number of the soundwire slave device
  73. * @port_en: flag indicates whether the port is enabled
  74. * @slave_port_id: logical port number of the soundwire slave device
  75. * @offset1: sample offset indicating the offset of the channel
  76. * from the start of the frame
  77. * @offset2: channel offset indicating offset between to channels
  78. * @hstart: start offset for subframe window.
  79. * @hstop: start offset for subframe window.
  80. * @master_port_id: logical port number of corresponding soundwire master device
  81. * @blk_grp_count: grouping count for n.o of channels.
  82. * @blk_pack_mode: packing mode for channels in each port.
  83. * @sinterval: sample interval indicates spacing from one sample
  84. * event to the next
  85. * @ch_en: channels enabled in a port.
  86. * @req_ch: channels requested to be enabled in a port.
  87. * @num_ch: number of channels enabled in a port
  88. * @ch_rate: sampling rate of the channel with which data will be
  89. * transferred
  90. *
  91. * Soundwire frame shape is created based on swr_port_info struct
  92. * parameters.
  93. */
  94. struct swr_port_info {
  95. u8 dev_num;
  96. u8 port_en;
  97. u8 slave_port_id;
  98. u8 offset1;
  99. u8 offset2;
  100. u8 sinterval;
  101. struct list_head list;
  102. u8 master_port_id;
  103. u8 hstart;
  104. u8 hstop;
  105. u8 blk_grp_count;
  106. u8 blk_pack_mode;
  107. u8 word_length;
  108. u8 lane_ctrl;
  109. u8 ch_en;
  110. u8 req_ch;
  111. u8 num_ch;
  112. u32 ch_rate;
  113. };
  114. /*
  115. * struct swr_params - represent transfer of data from soundwire slave
  116. * to soundwire master
  117. * @tid: transaction ID to track each transaction
  118. * @dev_num: logical device number of the soundwire slave device
  119. * @num_port: number of ports that needs to be configured
  120. * @port_id: array of logical port numbers of the soundwire slave device
  121. * @num_ch: array of number of channels enabled
  122. * @ch_rate: array of sampling rate of different channels that need to
  123. * be configured
  124. * @ch_en: array of channels mask for all the ports
  125. * @port_type: the required master port type
  126. */
  127. struct swr_params {
  128. u8 tid;
  129. u8 dev_num;
  130. u8 num_port;
  131. u8 port_id[SWR_MAX_DEV_PORT_NUM];
  132. u8 num_ch[SWR_MAX_DEV_PORT_NUM];
  133. u32 ch_rate[SWR_MAX_DEV_PORT_NUM];
  134. u8 ch_en[SWR_MAX_DEV_PORT_NUM];
  135. u8 port_type[SWR_MAX_DEV_PORT_NUM];
  136. };
  137. /*
  138. * struct swr_reg - struct to handle soundwire slave register read/writes
  139. * @tid: transaction id for reg read/writes
  140. * @dev_id: logical device number of the soundwire slave device
  141. * @regaddr: 16 bit regaddr of soundwire slave
  142. * @buf: value to be written/read to/from regaddr
  143. * @len: length of the buffer buf
  144. */
  145. struct swr_reg {
  146. u8 tid;
  147. u8 dev_id;
  148. u32 regaddr;
  149. u32 *buf;
  150. u32 len;
  151. };
  152. /*
  153. * struct swr_master - Interface to the soundwire master controller
  154. * @dev: device interface to this driver
  155. * @list: link with other soundwire master controllers
  156. * @bus_num: board/SoC specific identifier for a soundwire master
  157. * @mlock: mutex protecting master data structures
  158. * @devices: list of devices on this master
  159. * @port: logical port numbers of the soundwire master. This array
  160. * can hold maximum master ports which is equal to number of slave
  161. * devices multiplied by number of ports in each slave device
  162. * @port_txn: table of port config transactions with transaction id
  163. * @reg_txn: table of register transactions with transaction id
  164. * @last_tid: size of table port_txn (can't grow beyond 256 since
  165. * tid is 8 bits)
  166. * @num_port: number of active ports on soundwire master
  167. * @gr_sid: slave id used by the group for write operations
  168. * @connect_port: callback for configuration of soundwire port(s)
  169. * @disconnect_port: callback for disable of soundwire port(s)
  170. * @read: callback for soundwire slave register read
  171. * @write: callback for soundwire slave register write
  172. * @get_logical_dev_num: callback to get soundwire slave logical
  173. * device number
  174. * @port_en_mask: bit mask of active ports on soundwire master
  175. */
  176. struct swr_master {
  177. struct device dev;
  178. struct list_head list;
  179. unsigned int bus_num;
  180. struct mutex mlock;
  181. struct list_head devices;
  182. struct swr_port_info port[SWR_MAX_MSTR_PORT_NUM];
  183. struct swr_params **port_txn;
  184. struct swr_reg **reg_txn;
  185. u8 last_tid;
  186. u8 num_port;
  187. u8 num_dev;
  188. u8 gr_sid;
  189. int (*connect_port)(struct swr_master *mstr, struct swr_params *txn);
  190. int (*disconnect_port)(struct swr_master *mstr, struct swr_params *txn);
  191. int (*read)(struct swr_master *mstr, u8 dev_num, u16 reg_addr,
  192. void *buf, u32 len);
  193. int (*write)(struct swr_master *mstr, u8 dev_num, u16 reg_addr,
  194. const void *buf);
  195. int (*bulk_write)(struct swr_master *master, u8 dev_num, void *reg,
  196. const void *buf, size_t len);
  197. int (*get_logical_dev_num)(struct swr_master *mstr, u64 dev_id,
  198. u8 *dev_num);
  199. int (*slvdev_datapath_control)(struct swr_master *mstr, bool enable);
  200. bool (*remove_from_group)(struct swr_master *mstr);
  201. void (*device_wakeup_vote)(struct swr_master *mstr);
  202. void (*device_wakeup_unvote)(struct swr_master *mstr);
  203. u16 port_en_mask;
  204. };
  205. static inline struct swr_master *to_swr_master(struct device *dev)
  206. {
  207. return dev ? container_of(dev, struct swr_master, dev) : NULL;
  208. }
  209. /*
  210. * struct swr_device - represent a soundwire slave device
  211. * @name: indicates the name of the device, defined in devicetree
  212. * binding under soundwire slave device node as a compatible field.
  213. * @master: soundwire master managing the bus hosting this device
  214. * @driver: Device's driver. Pointer to access routines
  215. * @dev_list: list of devices on a controller
  216. * @dev_num: logical device number of the soundwire slave device
  217. * @dev: driver model representation of the device
  218. * @addr: represents "ea-addr" which is unique-id of soundwire slave
  219. * device
  220. * @group_id: group id supported by the slave device
  221. * @slave_irq: irq handle of slave to be invoked by master
  222. * during slave interrupt
  223. */
  224. struct swr_device {
  225. char name[SOUNDWIRE_NAME_SIZE];
  226. struct swr_master *master;
  227. struct swr_driver *driver;
  228. struct list_head dev_list;
  229. u8 dev_num;
  230. struct device dev;
  231. u64 addr;
  232. u8 group_id;
  233. struct irq_domain *slave_irq;
  234. bool slave_irq_pending;
  235. };
  236. static inline struct swr_device *to_swr_device(struct device *dev)
  237. {
  238. return dev ? container_of(dev, struct swr_device, dev) : NULL;
  239. }
  240. /*
  241. * struct swr_driver - Manage soundwire slave device driver
  242. * @probe: binds this driver to soundwire device
  243. * @remove: unbinds this driver from soundwire device
  244. * @shutdown: standard shutdown callback used during power down/halt
  245. * @suspend: standard suspend callback used during system suspend
  246. * @resume: standard resume callback used during system resume
  247. * @driver: soundwire device drivers should initialize name and
  248. * owner field of this structure
  249. * @id_table: list of soundwire devices supported by this driver
  250. */
  251. struct swr_driver {
  252. int (*probe)(struct swr_device *swr);
  253. int (*remove)(struct swr_device *swr);
  254. void (*shutdown)(struct swr_device *swr);
  255. int (*suspend)(struct swr_device *swr, pm_message_t pmesg);
  256. int (*resume)(struct swr_device *swr);
  257. int (*device_up)(struct swr_device *swr);
  258. int (*device_down)(struct swr_device *swr);
  259. int (*reset_device)(struct swr_device *swr);
  260. struct device_driver driver;
  261. const struct swr_device_id *id_table;
  262. };
  263. static inline struct swr_driver *to_swr_driver(struct device_driver *drv)
  264. {
  265. return drv ? container_of(drv, struct swr_driver, driver) : NULL;
  266. }
  267. /*
  268. * struct swr_boardinfo - Declare board info for soundwire device bringup
  269. * @name: name to initialize swr_device.name
  270. * @bus_num: identifies which soundwire master parents the soundwire
  271. * slave_device
  272. * @addr: represents "ea-addr" of soundwire slave device
  273. * @of_node: pointer to OpenFirmware device node
  274. * @swr_slave: device to be registered with soundwire
  275. */
  276. struct swr_boardinfo {
  277. char name[SOUNDWIRE_NAME_SIZE];
  278. int bus_num;
  279. u64 addr;
  280. struct device_node *of_node;
  281. struct swr_device *swr_slave;
  282. };
  283. static inline void *swr_get_ctrl_data(const struct swr_master *master)
  284. {
  285. return master ? dev_get_drvdata(&master->dev) : NULL;
  286. }
  287. static inline void swr_set_ctrl_data(struct swr_master *master, void *data)
  288. {
  289. dev_set_drvdata(&master->dev, data);
  290. }
  291. static inline void *swr_get_dev_data(const struct swr_device *dev)
  292. {
  293. return dev ? dev_get_drvdata(&dev->dev) : NULL;
  294. }
  295. static inline void swr_set_dev_data(struct swr_device *dev, void *data)
  296. {
  297. dev_set_drvdata(&dev->dev, data);
  298. }
  299. extern int swr_startup_devices(struct swr_device *swr_dev);
  300. extern struct swr_device *swr_new_device(struct swr_master *master,
  301. struct swr_boardinfo const *info);
  302. extern int of_register_swr_devices(struct swr_master *master);
  303. extern void swr_port_response(struct swr_master *mstr, u8 tid);
  304. extern int swr_get_logical_dev_num(struct swr_device *dev, u64 dev_id,
  305. u8 *dev_num);
  306. extern int swr_read(struct swr_device *dev, u8 dev_num, u16 reg_addr,
  307. void *buf, u32 len);
  308. extern int swr_write(struct swr_device *dev, u8 dev_num, u16 reg_addr,
  309. const void *buf);
  310. extern int swr_bulk_write(struct swr_device *dev, u8 dev_num, void *reg_addr,
  311. const void *buf, size_t len);
  312. extern int swr_connect_port(struct swr_device *dev, u8 *port_id, u8 num_port,
  313. u8 *ch_mask, u32 *ch_rate, u8 *num_ch,
  314. u8 *port_type);
  315. extern int swr_disconnect_port(struct swr_device *dev,
  316. u8 *port_id, u8 num_port, u8 *ch_mask,
  317. u8 *port_type);
  318. extern int swr_set_device_group(struct swr_device *swr_dev, u8 id);
  319. extern int swr_driver_register(struct swr_driver *drv);
  320. extern void swr_driver_unregister(struct swr_driver *drv);
  321. extern int swr_add_device(struct swr_master *master,
  322. struct swr_device *swrdev);
  323. extern void swr_remove_device(struct swr_device *swr);
  324. extern void swr_master_add_boarddevices(struct swr_master *master);
  325. extern void swr_unregister_master(struct swr_master *master);
  326. extern int swr_register_master(struct swr_master *master);
  327. extern int swr_device_up(struct swr_device *swr_dev);
  328. extern int swr_device_down(struct swr_device *swr_dev);
  329. extern int swr_reset_device(struct swr_device *swr_dev);
  330. extern int swr_slvdev_datapath_control(struct swr_device *swr_dev, u8 dev_num,
  331. bool enable);
  332. extern int swr_remove_from_group(struct swr_device *dev, u8 dev_num);
  333. extern void swr_remove_device(struct swr_device *swr_dev);
  334. extern struct swr_device *get_matching_swr_slave_device(struct device_node *np);
  335. extern int swr_device_wakeup_vote(struct swr_device *dev);
  336. extern int swr_device_wakeup_unvote(struct swr_device *dev);
  337. #endif /* _LINUX_SOUNDWIRE_H */