soundwire.h 13 KB

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