Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #ifndef _LINUX_SOUNDWIRE_H |
| 14 | #define _LINUX_SOUNDWIRE_H |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/mod_devicetable.h> |
| 18 | |
| 19 | extern struct bus_type soundwire_type; |
| 20 | |
| 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 | |
| 32 | /* Indicates soundwire devices group information */ |
| 33 | enum { |
| 34 | SWR_GROUP_NONE = 0, |
| 35 | SWR_GROUP_12 = 12, |
| 36 | SWR_GROUP_13 = 13, |
| 37 | SWR_BROADCAST = 15, |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * struct swr_port_info - represent soundwire frame shape |
| 42 | * @dev_id: logical device number of the soundwire slave device |
| 43 | * @port_en: flag indicates whether the port is enabled |
| 44 | * @port_id: logical port number of the soundwire slave device |
| 45 | * @offset1: sample offset indicating the offset of the channel |
| 46 | * from the start of the frame |
| 47 | * @offset2: channel offset indicating offset between to channels |
| 48 | * @sinterval: sample interval indicates spacing from one sample |
| 49 | * event to the next |
| 50 | * @ch_en: channels in a port that need to be enabled |
| 51 | * @num_ch: number of channels enabled in a port |
| 52 | * @ch_rate: sampling rate of the channel with which data will be |
| 53 | * transferred |
| 54 | * |
| 55 | * Soundwire frame shape is created based on swr_port_info struct |
| 56 | * parameters. |
| 57 | */ |
| 58 | struct swr_port_info { |
| 59 | u8 dev_id; |
| 60 | u8 port_en; |
| 61 | u8 port_id; |
| 62 | u8 offset1; |
| 63 | u8 offset2; |
| 64 | u8 sinterval; |
| 65 | u8 ch_en; |
| 66 | u8 num_ch; |
| 67 | u32 ch_rate; |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * struct swr_params - represent transfer of data from soundwire slave |
| 72 | * to soundwire master |
| 73 | * @tid: transaction ID to track each transaction |
| 74 | * @dev_id: logical device number of the soundwire slave device |
| 75 | * @num_port: number of ports that needs to be configured |
| 76 | * @port_id: array of logical port numbers of the soundwire slave device |
| 77 | * @num_ch: array of number of channels enabled |
| 78 | * @ch_rate: array of sampling rate of different channels that need to |
| 79 | * be configured |
| 80 | * @ch_en: array of channels mask for all the ports |
| 81 | */ |
| 82 | struct swr_params { |
| 83 | u8 tid; |
| 84 | u8 dev_id; |
| 85 | u8 num_port; |
| 86 | u8 port_id[SWR_MAX_DEV_PORT_NUM]; |
| 87 | u8 num_ch[SWR_MAX_DEV_PORT_NUM]; |
| 88 | u32 ch_rate[SWR_MAX_DEV_PORT_NUM]; |
| 89 | u8 ch_en[SWR_MAX_DEV_PORT_NUM]; |
| 90 | }; |
| 91 | |
| 92 | /* |
| 93 | * struct swr_reg - struct to handle soundwire slave register read/writes |
| 94 | * @tid: transaction id for reg read/writes |
| 95 | * @dev_id: logical device number of the soundwire slave device |
| 96 | * @regaddr: 16 bit regaddr of soundwire slave |
| 97 | * @buf: value to be written/read to/from regaddr |
| 98 | * @len: length of the buffer buf |
| 99 | */ |
| 100 | struct swr_reg { |
| 101 | u8 tid; |
| 102 | u8 dev_id; |
| 103 | u32 regaddr; |
| 104 | u32 *buf; |
| 105 | u32 len; |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * struct swr_master - Interface to the soundwire master controller |
| 110 | * @dev: device interface to this driver |
| 111 | * @list: link with other soundwire master controllers |
| 112 | * @bus_num: board/SoC specific identifier for a soundwire master |
| 113 | * @mlock: mutex protecting master data structures |
| 114 | * @devices: list of devices on this master |
| 115 | * @port: logical port numbers of the soundwire master. This array |
| 116 | * can hold maximum master ports which is equal to number of slave |
| 117 | * devices multiplied by number of ports in each slave device |
| 118 | * @port_txn: table of port config transactions with transaction id |
| 119 | * @reg_txn: table of register transactions with transaction id |
| 120 | * @last_tid: size of table port_txn (can't grow beyond 256 since |
| 121 | * tid is 8 bits) |
| 122 | * @num_port: number of active ports on soundwire master |
| 123 | * @gr_sid: slave id used by the group for write operations |
| 124 | * @connect_port: callback for configuration of soundwire port(s) |
| 125 | * @disconnect_port: callback for disable of soundwire port(s) |
| 126 | * @read: callback for soundwire slave register read |
| 127 | * @write: callback for soundwire slave register write |
| 128 | * @get_logical_dev_num: callback to get soundwire slave logical |
| 129 | * device number |
| 130 | */ |
| 131 | struct swr_master { |
| 132 | struct device dev; |
| 133 | struct list_head list; |
| 134 | unsigned int bus_num; |
| 135 | struct mutex mlock; |
| 136 | struct list_head devices; |
| 137 | struct swr_port_info port[SWR_MAX_MSTR_PORT_NUM]; |
| 138 | struct swr_params **port_txn; |
| 139 | struct swr_reg **reg_txn; |
| 140 | u8 last_tid; |
| 141 | u8 num_port; |
| 142 | u8 num_dev; |
| 143 | u8 gr_sid; |
| 144 | int (*connect_port)(struct swr_master *mstr, struct swr_params *txn); |
| 145 | int (*disconnect_port)(struct swr_master *mstr, struct swr_params *txn); |
| 146 | int (*read)(struct swr_master *mstr, u8 dev_num, u16 reg_addr, |
| 147 | void *buf, u32 len); |
| 148 | int (*write)(struct swr_master *mstr, u8 dev_num, u16 reg_addr, |
| 149 | const void *buf); |
| 150 | int (*bulk_write)(struct swr_master *master, u8 dev_num, void *reg, |
| 151 | const void *buf, size_t len); |
| 152 | int (*get_logical_dev_num)(struct swr_master *mstr, u64 dev_id, |
| 153 | u8 *dev_num); |
| 154 | void (*slvdev_datapath_control)(struct swr_master *mstr, bool enable); |
| 155 | bool (*remove_from_group)(struct swr_master *mstr); |
| 156 | }; |
| 157 | |
| 158 | static inline struct swr_master *to_swr_master(struct device *dev) |
| 159 | { |
| 160 | return dev ? container_of(dev, struct swr_master, dev) : NULL; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * struct swr_device - represent a soundwire slave device |
| 165 | * @name: indicates the name of the device, defined in devicetree |
| 166 | * binding under soundwire slave device node as a compatible field. |
| 167 | * @master: soundwire master managing the bus hosting this device |
| 168 | * @driver: Device's driver. Pointer to access routines |
| 169 | * @dev_list: list of devices on a controller |
| 170 | * @dev_num: logical device number of the soundwire slave device |
| 171 | * @dev: driver model representation of the device |
| 172 | * @addr: represents "ea-addr" which is unique-id of soundwire slave |
| 173 | * device |
| 174 | * @group_id: group id supported by the slave device |
| 175 | */ |
| 176 | struct swr_device { |
| 177 | char name[SOUNDWIRE_NAME_SIZE]; |
| 178 | struct swr_master *master; |
| 179 | struct swr_driver *driver; |
| 180 | struct list_head dev_list; |
| 181 | u8 dev_num; |
| 182 | struct device dev; |
| 183 | unsigned long addr; |
| 184 | u8 group_id; |
| 185 | }; |
| 186 | |
| 187 | static inline struct swr_device *to_swr_device(struct device *dev) |
| 188 | { |
| 189 | return dev ? container_of(dev, struct swr_device, dev) : NULL; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * struct swr_driver - Manage soundwire slave device driver |
| 194 | * @probe: binds this driver to soundwire device |
| 195 | * @remove: unbinds this driver from soundwire device |
| 196 | * @shutdown: standard shutdown callback used during power down/halt |
| 197 | * @suspend: standard suspend callback used during system suspend |
| 198 | * @resume: standard resume callback used during system resume |
| 199 | * @driver: soundwire device drivers should initialize name and |
| 200 | * owner field of this structure |
| 201 | * @id_table: list of soundwire devices supported by this driver |
| 202 | */ |
| 203 | struct swr_driver { |
| 204 | int (*probe)(struct swr_device *swr); |
| 205 | int (*remove)(struct swr_device *swr); |
| 206 | void (*shutdown)(struct swr_device *swr); |
| 207 | int (*suspend)(struct swr_device *swr, pm_message_t pmesg); |
| 208 | int (*resume)(struct swr_device *swr); |
| 209 | int (*device_up)(struct swr_device *swr); |
| 210 | int (*device_down)(struct swr_device *swr); |
| 211 | int (*reset_device)(struct swr_device *swr); |
| 212 | struct device_driver driver; |
| 213 | const struct swr_device_id *id_table; |
| 214 | }; |
| 215 | |
| 216 | static inline struct swr_driver *to_swr_driver(struct device_driver *drv) |
| 217 | { |
| 218 | return drv ? container_of(drv, struct swr_driver, driver) : NULL; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * struct swr_boardinfo - Declare board info for soundwire device bringup |
| 223 | * @name: name to initialize swr_device.name |
| 224 | * @bus_num: identifies which soundwire master parents the soundwire |
| 225 | * slave_device |
| 226 | * @addr: represents "ea-addr" of soundwire slave device |
| 227 | * @of_node: pointer to OpenFirmware device node |
| 228 | * @swr_slave: device to be registered with soundwire |
| 229 | */ |
| 230 | struct swr_boardinfo { |
| 231 | char name[SOUNDWIRE_NAME_SIZE]; |
| 232 | int bus_num; |
| 233 | u64 addr; |
| 234 | struct device_node *of_node; |
| 235 | struct swr_device *swr_slave; |
| 236 | }; |
| 237 | |
| 238 | static inline void *swr_get_ctrl_data(const struct swr_master *master) |
| 239 | { |
| 240 | return master ? dev_get_drvdata(&master->dev) : NULL; |
| 241 | } |
| 242 | |
| 243 | static inline void swr_set_ctrl_data(struct swr_master *master, void *data) |
| 244 | { |
| 245 | dev_set_drvdata(&master->dev, data); |
| 246 | } |
| 247 | |
| 248 | static inline void *swr_get_dev_data(const struct swr_device *dev) |
| 249 | { |
| 250 | return dev ? dev_get_drvdata(&dev->dev) : NULL; |
| 251 | } |
| 252 | |
| 253 | static inline void swr_set_dev_data(struct swr_device *dev, void *data) |
| 254 | { |
| 255 | dev_set_drvdata(&dev->dev, data); |
| 256 | } |
| 257 | |
| 258 | extern int swr_startup_devices(struct swr_device *swr_dev); |
| 259 | |
| 260 | extern struct swr_device *swr_new_device(struct swr_master *master, |
| 261 | struct swr_boardinfo const *info); |
| 262 | |
| 263 | extern int of_register_swr_devices(struct swr_master *master); |
| 264 | |
| 265 | extern void swr_port_response(struct swr_master *mstr, u8 tid); |
| 266 | |
| 267 | extern int swr_get_logical_dev_num(struct swr_device *dev, u64 dev_id, |
| 268 | u8 *dev_num); |
| 269 | |
| 270 | extern int swr_read(struct swr_device *dev, u8 dev_num, u16 reg_addr, |
| 271 | void *buf, u32 len); |
| 272 | |
| 273 | extern int swr_write(struct swr_device *dev, u8 dev_num, u16 reg_addr, |
| 274 | const void *buf); |
| 275 | |
| 276 | extern int swr_bulk_write(struct swr_device *dev, u8 dev_num, void *reg_addr, |
| 277 | const void *buf, size_t len); |
| 278 | |
| 279 | extern int swr_connect_port(struct swr_device *dev, u8 *port_id, u8 num_port, |
| 280 | u8 *ch_mask, u32 *ch_rate, u8 *num_ch); |
| 281 | |
| 282 | extern int swr_disconnect_port(struct swr_device *dev, |
| 283 | u8 *port_id, u8 num_port); |
| 284 | |
| 285 | extern int swr_set_device_group(struct swr_device *swr_dev, u8 id); |
| 286 | |
| 287 | extern int swr_driver_register(struct swr_driver *drv); |
| 288 | |
| 289 | extern void swr_driver_unregister(struct swr_driver *drv); |
| 290 | |
| 291 | extern int swr_add_device(struct swr_master *master, |
| 292 | struct swr_device *swrdev); |
| 293 | extern void swr_remove_device(struct swr_device *swr); |
| 294 | |
| 295 | extern void swr_master_add_boarddevices(struct swr_master *master); |
| 296 | |
| 297 | extern void swr_unregister_master(struct swr_master *master); |
| 298 | |
| 299 | extern int swr_register_master(struct swr_master *master); |
| 300 | |
| 301 | extern int swr_device_up(struct swr_device *swr_dev); |
| 302 | |
| 303 | extern int swr_device_down(struct swr_device *swr_dev); |
| 304 | |
| 305 | extern int swr_reset_device(struct swr_device *swr_dev); |
| 306 | |
| 307 | extern int swr_slvdev_datapath_control(struct swr_device *swr_dev, u8 dev_num, |
| 308 | bool enable); |
| 309 | extern int swr_remove_from_group(struct swr_device *dev, u8 dev_num); |
| 310 | |
| 311 | extern void swr_remove_device(struct swr_device *swr_dev); |
| 312 | #endif /* _LINUX_SOUNDWIRE_H */ |