soundwire.h 13 KB

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