Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) |
| 2 | // Copyright(c) 2015-18 Intel Corporation. |
| 3 | |
| 4 | /* |
| 5 | * stream.c - SoundWire Bus stream operations. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/mod_devicetable.h> |
| 13 | #include <linux/slab.h> |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 14 | #include <linux/soundwire/sdw_registers.h> |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 15 | #include <linux/soundwire/sdw.h> |
Pierre-Louis Bossart | 4550569 | 2020-07-01 02:43:53 +0800 | [diff] [blame] | 16 | #include <sound/soc.h> |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 17 | #include "bus.h" |
| 18 | |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 19 | /* |
| 20 | * Array of supported rows and columns as per MIPI SoundWire Specification 1.1 |
| 21 | * |
| 22 | * The rows are arranged as per the array index value programmed |
| 23 | * in register. The index 15 has dummy value 0 in order to fill hole. |
| 24 | */ |
Pierre-Louis Bossart | fe4b70f | 2019-08-05 19:55:10 -0500 | [diff] [blame] | 25 | int sdw_rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147, |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 26 | 96, 100, 120, 128, 150, 160, 250, 0, |
| 27 | 192, 200, 240, 256, 72, 144, 90, 180}; |
Bard Liao | 9026118 | 2020-09-08 21:15:20 +0800 | [diff] [blame] | 28 | EXPORT_SYMBOL(sdw_rows); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 29 | |
Pierre-Louis Bossart | fe4b70f | 2019-08-05 19:55:10 -0500 | [diff] [blame] | 30 | int sdw_cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16}; |
Bard Liao | 9026118 | 2020-09-08 21:15:20 +0800 | [diff] [blame] | 31 | EXPORT_SYMBOL(sdw_cols); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 32 | |
Pierre-Louis Bossart | fe4b70f | 2019-08-05 19:55:10 -0500 | [diff] [blame] | 33 | int sdw_find_col_index(int col) |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 34 | { |
| 35 | int i; |
| 36 | |
| 37 | for (i = 0; i < SDW_FRAME_COLS; i++) { |
Pierre-Louis Bossart | fe4b70f | 2019-08-05 19:55:10 -0500 | [diff] [blame] | 38 | if (sdw_cols[i] == col) |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 39 | return i; |
| 40 | } |
| 41 | |
| 42 | pr_warn("Requested column not found, selecting lowest column no: 2\n"); |
| 43 | return 0; |
| 44 | } |
Pierre-Louis Bossart | fe4b70f | 2019-08-05 19:55:10 -0500 | [diff] [blame] | 45 | EXPORT_SYMBOL(sdw_find_col_index); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 46 | |
Pierre-Louis Bossart | fe4b70f | 2019-08-05 19:55:10 -0500 | [diff] [blame] | 47 | int sdw_find_row_index(int row) |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 48 | { |
| 49 | int i; |
| 50 | |
| 51 | for (i = 0; i < SDW_FRAME_ROWS; i++) { |
Pierre-Louis Bossart | fe4b70f | 2019-08-05 19:55:10 -0500 | [diff] [blame] | 52 | if (sdw_rows[i] == row) |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 53 | return i; |
| 54 | } |
| 55 | |
| 56 | pr_warn("Requested row not found, selecting lowest row no: 48\n"); |
| 57 | return 0; |
| 58 | } |
Pierre-Louis Bossart | fe4b70f | 2019-08-05 19:55:10 -0500 | [diff] [blame] | 59 | EXPORT_SYMBOL(sdw_find_row_index); |
Vinod Koul | 897fe40 | 2019-05-02 16:29:29 +0530 | [diff] [blame] | 60 | |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 61 | static int _sdw_program_slave_port_params(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 62 | struct sdw_slave *slave, |
| 63 | struct sdw_transport_params *t_params, |
| 64 | enum sdw_dpn_type type) |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 65 | { |
| 66 | u32 addr1, addr2, addr3, addr4; |
| 67 | int ret; |
| 68 | u16 wbuf; |
| 69 | |
| 70 | if (bus->params.next_bank) { |
| 71 | addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num); |
| 72 | addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num); |
| 73 | addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num); |
| 74 | addr4 = SDW_DPN_HCTRL_B1(t_params->port_num); |
| 75 | } else { |
| 76 | addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num); |
| 77 | addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num); |
| 78 | addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num); |
| 79 | addr4 = SDW_DPN_HCTRL_B0(t_params->port_num); |
| 80 | } |
| 81 | |
| 82 | /* Program DPN_OffsetCtrl2 registers */ |
| 83 | ret = sdw_write(slave, addr1, t_params->offset2); |
| 84 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 85 | dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n"); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | /* Program DPN_BlockCtrl3 register */ |
| 90 | ret = sdw_write(slave, addr2, t_params->blk_pkg_mode); |
| 91 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 92 | dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n"); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Data ports are FULL, SIMPLE and REDUCED. This function handles |
Vinod Koul | 7d3b3cd | 2019-05-02 16:29:27 +0530 | [diff] [blame] | 98 | * FULL and REDUCED only and beyond this point only FULL is |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 99 | * handled, so bail out if we are not FULL data port type |
| 100 | */ |
| 101 | if (type != SDW_DPN_FULL) |
| 102 | return ret; |
| 103 | |
| 104 | /* Program DPN_SampleCtrl2 register */ |
Vinod Koul | 41ff917 | 2020-09-03 17:14:59 +0530 | [diff] [blame] | 105 | wbuf = FIELD_GET(SDW_DPN_SAMPLECTRL_HIGH, t_params->sample_interval - 1); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 106 | |
| 107 | ret = sdw_write(slave, addr3, wbuf); |
| 108 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 109 | dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n"); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | /* Program DPN_HCtrl register */ |
Vinod Koul | 41ff917 | 2020-09-03 17:14:59 +0530 | [diff] [blame] | 114 | wbuf = FIELD_PREP(SDW_DPN_HCTRL_HSTART, t_params->hstart); |
| 115 | wbuf |= FIELD_PREP(SDW_DPN_HCTRL_HSTOP, t_params->hstop); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 116 | |
| 117 | ret = sdw_write(slave, addr4, wbuf); |
| 118 | if (ret < 0) |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 119 | dev_err(bus->dev, "DPN_HCtrl register write failed\n"); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static int sdw_program_slave_port_params(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 125 | struct sdw_slave_runtime *s_rt, |
| 126 | struct sdw_port_runtime *p_rt) |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 127 | { |
| 128 | struct sdw_transport_params *t_params = &p_rt->transport_params; |
| 129 | struct sdw_port_params *p_params = &p_rt->port_params; |
| 130 | struct sdw_slave_prop *slave_prop = &s_rt->slave->prop; |
| 131 | u32 addr1, addr2, addr3, addr4, addr5, addr6; |
| 132 | struct sdw_dpn_prop *dpn_prop; |
| 133 | int ret; |
| 134 | u8 wbuf; |
| 135 | |
| 136 | dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 137 | s_rt->direction, |
| 138 | t_params->port_num); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 139 | if (!dpn_prop) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | addr1 = SDW_DPN_PORTCTRL(t_params->port_num); |
| 143 | addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num); |
| 144 | |
| 145 | if (bus->params.next_bank) { |
| 146 | addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num); |
| 147 | addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num); |
| 148 | addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num); |
| 149 | addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num); |
| 150 | |
| 151 | } else { |
| 152 | addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num); |
| 153 | addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num); |
| 154 | addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num); |
| 155 | addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num); |
| 156 | } |
| 157 | |
| 158 | /* Program DPN_PortCtrl register */ |
Vinod Koul | 41ff917 | 2020-09-03 17:14:59 +0530 | [diff] [blame] | 159 | wbuf = FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE, p_params->data_mode); |
| 160 | wbuf |= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE, p_params->flow_mode); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 161 | |
| 162 | ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf); |
| 163 | if (ret < 0) { |
| 164 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 165 | "DPN_PortCtrl register write failed for port %d\n", |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 166 | t_params->port_num); |
| 167 | return ret; |
| 168 | } |
| 169 | |
Srinivas Kandagatla | a9107de | 2020-03-11 11:35:44 +0000 | [diff] [blame] | 170 | if (!dpn_prop->read_only_wordlength) { |
| 171 | /* Program DPN_BlockCtrl1 register */ |
| 172 | ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1)); |
| 173 | if (ret < 0) { |
| 174 | dev_err(&s_rt->slave->dev, |
| 175 | "DPN_BlockCtrl1 register write failed for port %d\n", |
| 176 | t_params->port_num); |
| 177 | return ret; |
| 178 | } |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Program DPN_SampleCtrl1 register */ |
| 182 | wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW; |
| 183 | ret = sdw_write(s_rt->slave, addr3, wbuf); |
| 184 | if (ret < 0) { |
| 185 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 186 | "DPN_SampleCtrl1 register write failed for port %d\n", |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 187 | t_params->port_num); |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | /* Program DPN_OffsetCtrl1 registers */ |
| 192 | ret = sdw_write(s_rt->slave, addr4, t_params->offset1); |
| 193 | if (ret < 0) { |
| 194 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 195 | "DPN_OffsetCtrl1 register write failed for port %d\n", |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 196 | t_params->port_num); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | /* Program DPN_BlockCtrl2 register*/ |
| 201 | if (t_params->blk_grp_ctrl_valid) { |
| 202 | ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl); |
| 203 | if (ret < 0) { |
| 204 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 205 | "DPN_BlockCtrl2 reg write failed for port %d\n", |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 206 | t_params->port_num); |
| 207 | return ret; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* program DPN_LaneCtrl register */ |
| 212 | if (slave_prop->lane_control_support) { |
| 213 | ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl); |
| 214 | if (ret < 0) { |
| 215 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 216 | "DPN_LaneCtrl register write failed for port %d\n", |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 217 | t_params->port_num); |
| 218 | return ret; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (dpn_prop->type != SDW_DPN_SIMPLE) { |
| 223 | ret = _sdw_program_slave_port_params(bus, s_rt->slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 224 | t_params, dpn_prop->type); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 225 | if (ret < 0) |
| 226 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 227 | "Transport reg write failed for port: %d\n", |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 228 | t_params->port_num); |
| 229 | } |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static int sdw_program_master_port_params(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 235 | struct sdw_port_runtime *p_rt) |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 236 | { |
| 237 | int ret; |
| 238 | |
| 239 | /* |
| 240 | * we need to set transport and port parameters for the port. |
Vinod Koul | 7d3b3cd | 2019-05-02 16:29:27 +0530 | [diff] [blame] | 241 | * Transport parameters refers to the sample interval, offsets and |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 242 | * hstart/stop etc of the data. Port parameters refers to word |
| 243 | * length, flow mode etc of the port |
| 244 | */ |
| 245 | ret = bus->port_ops->dpn_set_port_transport_params(bus, |
| 246 | &p_rt->transport_params, |
| 247 | bus->params.next_bank); |
| 248 | if (ret < 0) |
| 249 | return ret; |
| 250 | |
| 251 | return bus->port_ops->dpn_set_port_params(bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 252 | &p_rt->port_params, |
| 253 | bus->params.next_bank); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * sdw_program_port_params() - Programs transport parameters of Master(s) |
| 258 | * and Slave(s) |
| 259 | * |
| 260 | * @m_rt: Master stream runtime |
| 261 | */ |
| 262 | static int sdw_program_port_params(struct sdw_master_runtime *m_rt) |
| 263 | { |
Pierre-Louis Bossart | 5920a29 | 2021-03-02 17:11:21 +0800 | [diff] [blame] | 264 | struct sdw_slave_runtime *s_rt; |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 265 | struct sdw_bus *bus = m_rt->bus; |
| 266 | struct sdw_port_runtime *p_rt; |
| 267 | int ret = 0; |
| 268 | |
| 269 | /* Program transport & port parameters for Slave(s) */ |
| 270 | list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 271 | list_for_each_entry(p_rt, &s_rt->port_list, port_node) { |
| 272 | ret = sdw_program_slave_port_params(bus, s_rt, p_rt); |
| 273 | if (ret < 0) |
| 274 | return ret; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* Program transport & port parameters for Master(s) */ |
| 279 | list_for_each_entry(p_rt, &m_rt->port_list, port_node) { |
| 280 | ret = sdw_program_master_port_params(bus, p_rt); |
| 281 | if (ret < 0) |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 288 | /** |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 289 | * sdw_enable_disable_slave_ports: Enable/disable slave data port |
| 290 | * |
| 291 | * @bus: bus instance |
| 292 | * @s_rt: slave runtime |
| 293 | * @p_rt: port runtime |
| 294 | * @en: enable or disable operation |
| 295 | * |
| 296 | * This function only sets the enable/disable bits in the relevant bank, the |
| 297 | * actual enable/disable is done with a bank switch |
| 298 | */ |
| 299 | static int sdw_enable_disable_slave_ports(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 300 | struct sdw_slave_runtime *s_rt, |
| 301 | struct sdw_port_runtime *p_rt, |
| 302 | bool en) |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 303 | { |
| 304 | struct sdw_transport_params *t_params = &p_rt->transport_params; |
| 305 | u32 addr; |
| 306 | int ret; |
| 307 | |
| 308 | if (bus->params.next_bank) |
| 309 | addr = SDW_DPN_CHANNELEN_B1(p_rt->num); |
| 310 | else |
| 311 | addr = SDW_DPN_CHANNELEN_B0(p_rt->num); |
| 312 | |
| 313 | /* |
| 314 | * Since bus doesn't support sharing a port across two streams, |
| 315 | * it is safe to reset this register |
| 316 | */ |
| 317 | if (en) |
Srinivas Kandagatla | 0b43fef | 2020-03-12 10:01:05 +0000 | [diff] [blame] | 318 | ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 319 | else |
Srinivas Kandagatla | 0b43fef | 2020-03-12 10:01:05 +0000 | [diff] [blame] | 320 | ret = sdw_write(s_rt->slave, addr, 0x0); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 321 | |
| 322 | if (ret < 0) |
| 323 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 324 | "Slave chn_en reg write failed:%d port:%d\n", |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 325 | ret, t_params->port_num); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 331 | struct sdw_port_runtime *p_rt, |
| 332 | bool en) |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 333 | { |
| 334 | struct sdw_transport_params *t_params = &p_rt->transport_params; |
| 335 | struct sdw_bus *bus = m_rt->bus; |
| 336 | struct sdw_enable_ch enable_ch; |
Pierre-Louis Bossart | a25eab2 | 2019-04-10 22:17:00 -0500 | [diff] [blame] | 337 | int ret; |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 338 | |
| 339 | enable_ch.port_num = p_rt->num; |
| 340 | enable_ch.ch_mask = p_rt->ch_mask; |
| 341 | enable_ch.enable = en; |
| 342 | |
| 343 | /* Perform Master port channel(s) enable/disable */ |
| 344 | if (bus->port_ops->dpn_port_enable_ch) { |
| 345 | ret = bus->port_ops->dpn_port_enable_ch(bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 346 | &enable_ch, |
| 347 | bus->params.next_bank); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 348 | if (ret < 0) { |
| 349 | dev_err(bus->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 350 | "Master chn_en write failed:%d port:%d\n", |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 351 | ret, t_params->port_num); |
| 352 | return ret; |
| 353 | } |
| 354 | } else { |
| 355 | dev_err(bus->dev, |
| 356 | "dpn_port_enable_ch not supported, %s failed\n", |
| 357 | en ? "enable" : "disable"); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * sdw_enable_disable_ports() - Enable/disable port(s) for Master and |
| 366 | * Slave(s) |
| 367 | * |
| 368 | * @m_rt: Master stream runtime |
| 369 | * @en: mode (enable/disable) |
| 370 | */ |
| 371 | static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en) |
| 372 | { |
| 373 | struct sdw_port_runtime *s_port, *m_port; |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 374 | struct sdw_slave_runtime *s_rt; |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 375 | int ret = 0; |
| 376 | |
| 377 | /* Enable/Disable Slave port(s) */ |
| 378 | list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 379 | list_for_each_entry(s_port, &s_rt->port_list, port_node) { |
| 380 | ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 381 | s_port, en); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 382 | if (ret < 0) |
| 383 | return ret; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* Enable/Disable Master port(s) */ |
| 388 | list_for_each_entry(m_port, &m_rt->port_list, port_node) { |
| 389 | ret = sdw_enable_disable_master_ports(m_rt, m_port, en); |
| 390 | if (ret < 0) |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 398 | struct sdw_prepare_ch prep_ch, |
| 399 | enum sdw_port_prep_ops cmd) |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 400 | { |
| 401 | const struct sdw_slave_ops *ops = s_rt->slave->ops; |
| 402 | int ret; |
| 403 | |
| 404 | if (ops->port_prep) { |
| 405 | ret = ops->port_prep(s_rt->slave, &prep_ch, cmd); |
| 406 | if (ret < 0) { |
| 407 | dev_err(&s_rt->slave->dev, |
Vinod Koul | 62f0cec | 2019-05-02 16:29:24 +0530 | [diff] [blame] | 408 | "Slave Port Prep cmd %d failed: %d\n", |
| 409 | cmd, ret); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 410 | return ret; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 418 | struct sdw_slave_runtime *s_rt, |
| 419 | struct sdw_port_runtime *p_rt, |
| 420 | bool prep) |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 421 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 422 | struct completion *port_ready; |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 423 | struct sdw_dpn_prop *dpn_prop; |
| 424 | struct sdw_prepare_ch prep_ch; |
| 425 | unsigned int time_left; |
| 426 | bool intr = false; |
| 427 | int ret = 0, val; |
| 428 | u32 addr; |
| 429 | |
| 430 | prep_ch.num = p_rt->num; |
| 431 | prep_ch.ch_mask = p_rt->ch_mask; |
| 432 | |
| 433 | dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 434 | s_rt->direction, |
| 435 | prep_ch.num); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 436 | if (!dpn_prop) { |
| 437 | dev_err(bus->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 438 | "Slave Port:%d properties not found\n", prep_ch.num); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 439 | return -EINVAL; |
| 440 | } |
| 441 | |
| 442 | prep_ch.prepare = prep; |
| 443 | |
| 444 | prep_ch.bank = bus->params.next_bank; |
| 445 | |
Pierre-Louis Bossart | dd87a72 | 2020-09-21 03:32:05 +0800 | [diff] [blame] | 446 | if (dpn_prop->imp_def_interrupts || !dpn_prop->simple_ch_prep_sm || |
| 447 | bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 448 | intr = true; |
| 449 | |
| 450 | /* |
| 451 | * Enable interrupt before Port prepare. |
| 452 | * For Port de-prepare, it is assumed that port |
| 453 | * was prepared earlier |
| 454 | */ |
| 455 | if (prep && intr) { |
| 456 | ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep, |
Pierre-Louis Bossart | 8acbbfe | 2019-05-22 14:47:25 -0500 | [diff] [blame] | 457 | dpn_prop->imp_def_interrupts); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 458 | if (ret < 0) |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | /* Inform slave about the impending port prepare */ |
| 463 | sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_PRE_PREP); |
| 464 | |
| 465 | /* Prepare Slave port implementing CP_SM */ |
| 466 | if (!dpn_prop->simple_ch_prep_sm) { |
| 467 | addr = SDW_DPN_PREPARECTRL(p_rt->num); |
| 468 | |
| 469 | if (prep) |
Srinivas Kandagatla | 0b43fef | 2020-03-12 10:01:05 +0000 | [diff] [blame] | 470 | ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 471 | else |
Srinivas Kandagatla | 0b43fef | 2020-03-12 10:01:05 +0000 | [diff] [blame] | 472 | ret = sdw_write(s_rt->slave, addr, 0x0); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 473 | |
| 474 | if (ret < 0) { |
| 475 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 476 | "Slave prep_ctrl reg write failed\n"); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | /* Wait for completion on port ready */ |
| 481 | port_ready = &s_rt->slave->port_ready[prep_ch.num]; |
| 482 | time_left = wait_for_completion_timeout(port_ready, |
| 483 | msecs_to_jiffies(dpn_prop->ch_prep_timeout)); |
| 484 | |
| 485 | val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num)); |
| 486 | val &= p_rt->ch_mask; |
| 487 | if (!time_left || val) { |
| 488 | dev_err(&s_rt->slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 489 | "Chn prep failed for port:%d\n", prep_ch.num); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 490 | return -ETIMEDOUT; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* Inform slaves about ports prepared */ |
| 495 | sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_POST_PREP); |
| 496 | |
| 497 | /* Disable interrupt after Port de-prepare */ |
| 498 | if (!prep && intr) |
| 499 | ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep, |
Pierre-Louis Bossart | 8acbbfe | 2019-05-22 14:47:25 -0500 | [diff] [blame] | 500 | dpn_prop->imp_def_interrupts); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 501 | |
| 502 | return ret; |
| 503 | } |
| 504 | |
| 505 | static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 506 | struct sdw_port_runtime *p_rt, |
| 507 | bool prep) |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 508 | { |
| 509 | struct sdw_transport_params *t_params = &p_rt->transport_params; |
| 510 | struct sdw_bus *bus = m_rt->bus; |
| 511 | const struct sdw_master_port_ops *ops = bus->port_ops; |
| 512 | struct sdw_prepare_ch prep_ch; |
| 513 | int ret = 0; |
| 514 | |
| 515 | prep_ch.num = p_rt->num; |
| 516 | prep_ch.ch_mask = p_rt->ch_mask; |
| 517 | prep_ch.prepare = prep; /* Prepare/De-prepare */ |
| 518 | prep_ch.bank = bus->params.next_bank; |
| 519 | |
| 520 | /* Pre-prepare/Pre-deprepare port(s) */ |
| 521 | if (ops->dpn_port_prep) { |
| 522 | ret = ops->dpn_port_prep(bus, &prep_ch); |
| 523 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 524 | dev_err(bus->dev, "Port prepare failed for port:%d\n", |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 525 | t_params->port_num); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 526 | return ret; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and |
| 535 | * Slave(s) |
| 536 | * |
| 537 | * @m_rt: Master runtime handle |
| 538 | * @prep: Prepare or De-prepare |
| 539 | */ |
| 540 | static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep) |
| 541 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 542 | struct sdw_slave_runtime *s_rt; |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 543 | struct sdw_port_runtime *p_rt; |
| 544 | int ret = 0; |
| 545 | |
| 546 | /* Prepare/De-prepare Slave port(s) */ |
| 547 | list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 548 | list_for_each_entry(p_rt, &s_rt->port_list, port_node) { |
| 549 | ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 550 | p_rt, prep); |
Sanyog Kale | 79df15b | 2018-04-26 18:38:23 +0530 | [diff] [blame] | 551 | if (ret < 0) |
| 552 | return ret; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* Prepare/De-prepare Master port(s) */ |
| 557 | list_for_each_entry(p_rt, &m_rt->port_list, port_node) { |
| 558 | ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep); |
| 559 | if (ret < 0) |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | /** |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 567 | * sdw_notify_config() - Notify bus configuration |
| 568 | * |
| 569 | * @m_rt: Master runtime handle |
| 570 | * |
| 571 | * This function notifies the Master(s) and Slave(s) of the |
| 572 | * new bus configuration. |
| 573 | */ |
| 574 | static int sdw_notify_config(struct sdw_master_runtime *m_rt) |
| 575 | { |
| 576 | struct sdw_slave_runtime *s_rt; |
| 577 | struct sdw_bus *bus = m_rt->bus; |
| 578 | struct sdw_slave *slave; |
| 579 | int ret = 0; |
| 580 | |
| 581 | if (bus->ops->set_bus_conf) { |
| 582 | ret = bus->ops->set_bus_conf(bus, &bus->params); |
| 583 | if (ret < 0) |
| 584 | return ret; |
| 585 | } |
| 586 | |
| 587 | list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 588 | slave = s_rt->slave; |
| 589 | |
| 590 | if (slave->ops->bus_config) { |
| 591 | ret = slave->ops->bus_config(slave, &bus->params); |
Rander Wang | 6083502 | 2020-01-14 17:52:26 -0600 | [diff] [blame] | 592 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 593 | dev_err(bus->dev, "Notify Slave: %d failed\n", |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 594 | slave->dev_num); |
Rander Wang | 6083502 | 2020-01-14 17:52:26 -0600 | [diff] [blame] | 595 | return ret; |
| 596 | } |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * sdw_program_params() - Program transport and port parameters for Master(s) |
| 605 | * and Slave(s) |
| 606 | * |
| 607 | * @bus: SDW bus instance |
Rander Wang | bfaa354 | 2020-01-14 17:52:27 -0600 | [diff] [blame] | 608 | * @prepare: true if sdw_program_params() is called by _prepare. |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 609 | */ |
Rander Wang | bfaa354 | 2020-01-14 17:52:27 -0600 | [diff] [blame] | 610 | static int sdw_program_params(struct sdw_bus *bus, bool prepare) |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 611 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 612 | struct sdw_master_runtime *m_rt; |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 613 | int ret = 0; |
| 614 | |
| 615 | list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { |
Rander Wang | bfaa354 | 2020-01-14 17:52:27 -0600 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * this loop walks through all master runtimes for a |
| 619 | * bus, but the ports can only be configured while |
| 620 | * explicitly preparing a stream or handling an |
| 621 | * already-prepared stream otherwise. |
| 622 | */ |
| 623 | if (!prepare && |
| 624 | m_rt->stream->state == SDW_STREAM_CONFIGURED) |
| 625 | continue; |
| 626 | |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 627 | ret = sdw_program_port_params(m_rt); |
| 628 | if (ret < 0) { |
| 629 | dev_err(bus->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 630 | "Program transport params failed: %d\n", ret); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | ret = sdw_notify_config(m_rt); |
| 635 | if (ret < 0) { |
Vinod Koul | 62f0cec | 2019-05-02 16:29:24 +0530 | [diff] [blame] | 636 | dev_err(bus->dev, |
| 637 | "Notify bus config failed: %d\n", ret); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | /* Enable port(s) on alternate bank for all active streams */ |
| 642 | if (m_rt->stream->state != SDW_STREAM_ENABLED) |
| 643 | continue; |
| 644 | |
| 645 | ret = sdw_enable_disable_ports(m_rt, true); |
| 646 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 647 | dev_err(bus->dev, "Enable channel failed: %d\n", ret); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 648 | return ret; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | return ret; |
| 653 | } |
| 654 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 655 | static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count) |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 656 | { |
| 657 | int col_index, row_index; |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 658 | bool multi_link; |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 659 | struct sdw_msg *wr_msg; |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 660 | u8 *wbuf; |
| 661 | int ret; |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 662 | u16 addr; |
| 663 | |
| 664 | wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL); |
| 665 | if (!wr_msg) |
| 666 | return -ENOMEM; |
| 667 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 668 | bus->defer_msg.msg = wr_msg; |
| 669 | |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 670 | wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL); |
| 671 | if (!wbuf) { |
| 672 | ret = -ENOMEM; |
| 673 | goto error_1; |
| 674 | } |
| 675 | |
| 676 | /* Get row and column index to program register */ |
| 677 | col_index = sdw_find_col_index(bus->params.col); |
| 678 | row_index = sdw_find_row_index(bus->params.row); |
| 679 | wbuf[0] = col_index | (row_index << 3); |
| 680 | |
| 681 | if (bus->params.next_bank) |
| 682 | addr = SDW_SCP_FRAMECTRL_B1; |
| 683 | else |
| 684 | addr = SDW_SCP_FRAMECTRL_B0; |
| 685 | |
| 686 | sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 687 | SDW_MSG_FLAG_WRITE, wbuf); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 688 | wr_msg->ssp_sync = true; |
| 689 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 690 | /* |
| 691 | * Set the multi_link flag only when both the hardware supports |
Pierre-Louis Bossart | 063ff4e | 2020-09-01 23:05:53 +0800 | [diff] [blame] | 692 | * and hardware-based sync is required |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 693 | */ |
Pierre-Louis Bossart | 063ff4e | 2020-09-01 23:05:53 +0800 | [diff] [blame] | 694 | multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 695 | |
| 696 | if (multi_link) |
| 697 | ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg); |
| 698 | else |
| 699 | ret = sdw_transfer(bus, wr_msg); |
| 700 | |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 701 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 702 | dev_err(bus->dev, "Slave frame_ctrl reg write failed\n"); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 703 | goto error; |
| 704 | } |
| 705 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 706 | if (!multi_link) { |
| 707 | kfree(wr_msg); |
| 708 | kfree(wbuf); |
| 709 | bus->defer_msg.msg = NULL; |
| 710 | bus->params.curr_bank = !bus->params.curr_bank; |
| 711 | bus->params.next_bank = !bus->params.next_bank; |
| 712 | } |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 713 | |
| 714 | return 0; |
| 715 | |
| 716 | error: |
| 717 | kfree(wbuf); |
| 718 | error_1: |
| 719 | kfree(wr_msg); |
Tom Rix | 3fbbf21 | 2020-09-02 13:26:50 -0700 | [diff] [blame] | 720 | bus->defer_msg.msg = NULL; |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 721 | return ret; |
| 722 | } |
| 723 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 724 | /** |
| 725 | * sdw_ml_sync_bank_switch: Multilink register bank switch |
| 726 | * |
| 727 | * @bus: SDW bus instance |
| 728 | * |
| 729 | * Caller function should free the buffers on error |
| 730 | */ |
| 731 | static int sdw_ml_sync_bank_switch(struct sdw_bus *bus) |
| 732 | { |
| 733 | unsigned long time_left; |
| 734 | |
| 735 | if (!bus->multi_link) |
| 736 | return 0; |
| 737 | |
| 738 | /* Wait for completion of transfer */ |
| 739 | time_left = wait_for_completion_timeout(&bus->defer_msg.complete, |
| 740 | bus->bank_switch_timeout); |
| 741 | |
| 742 | if (!time_left) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 743 | dev_err(bus->dev, "Controller Timed out on bank switch\n"); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 744 | return -ETIMEDOUT; |
| 745 | } |
| 746 | |
| 747 | bus->params.curr_bank = !bus->params.curr_bank; |
| 748 | bus->params.next_bank = !bus->params.next_bank; |
| 749 | |
| 750 | if (bus->defer_msg.msg) { |
| 751 | kfree(bus->defer_msg.msg->buf); |
| 752 | kfree(bus->defer_msg.msg); |
| 753 | } |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 758 | static int do_bank_switch(struct sdw_stream_runtime *stream) |
| 759 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 760 | struct sdw_master_runtime *m_rt; |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 761 | const struct sdw_master_ops *ops; |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 762 | struct sdw_bus *bus; |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 763 | bool multi_link = false; |
Pierre-Louis Bossart | 063ff4e | 2020-09-01 23:05:53 +0800 | [diff] [blame] | 764 | int m_rt_count; |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 765 | int ret = 0; |
| 766 | |
Pierre-Louis Bossart | 063ff4e | 2020-09-01 23:05:53 +0800 | [diff] [blame] | 767 | m_rt_count = stream->m_rt_count; |
| 768 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 769 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 770 | bus = m_rt->bus; |
| 771 | ops = bus->ops; |
| 772 | |
Pierre-Louis Bossart | 063ff4e | 2020-09-01 23:05:53 +0800 | [diff] [blame] | 773 | if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) { |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 774 | multi_link = true; |
| 775 | mutex_lock(&bus->msg_lock); |
| 776 | } |
| 777 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 778 | /* Pre-bank switch */ |
| 779 | if (ops->pre_bank_switch) { |
| 780 | ret = ops->pre_bank_switch(bus); |
| 781 | if (ret < 0) { |
| 782 | dev_err(bus->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 783 | "Pre bank switch op failed: %d\n", ret); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 784 | goto msg_unlock; |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 788 | /* |
| 789 | * Perform Bank switch operation. |
| 790 | * For multi link cases, the actual bank switch is |
| 791 | * synchronized across all Masters and happens later as a |
| 792 | * part of post_bank_switch ops. |
| 793 | */ |
Pierre-Louis Bossart | 063ff4e | 2020-09-01 23:05:53 +0800 | [diff] [blame] | 794 | ret = sdw_bank_switch(bus, m_rt_count); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 795 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 796 | dev_err(bus->dev, "Bank switch failed: %d\n", ret); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 797 | goto error; |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 801 | /* |
| 802 | * For multi link cases, it is expected that the bank switch is |
| 803 | * triggered by the post_bank_switch for the first Master in the list |
| 804 | * and for the other Masters the post_bank_switch() should return doing |
| 805 | * nothing. |
| 806 | */ |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 807 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 808 | bus = m_rt->bus; |
| 809 | ops = bus->ops; |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 810 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 811 | /* Post-bank switch */ |
| 812 | if (ops->post_bank_switch) { |
| 813 | ret = ops->post_bank_switch(bus); |
| 814 | if (ret < 0) { |
| 815 | dev_err(bus->dev, |
Vinod Koul | 62f0cec | 2019-05-02 16:29:24 +0530 | [diff] [blame] | 816 | "Post bank switch op failed: %d\n", |
| 817 | ret); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 818 | goto error; |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 819 | } |
Pierre-Louis Bossart | 063ff4e | 2020-09-01 23:05:53 +0800 | [diff] [blame] | 820 | } else if (multi_link) { |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 821 | dev_err(bus->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 822 | "Post bank switch ops not implemented\n"); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 823 | goto error; |
| 824 | } |
| 825 | |
| 826 | /* Set the bank switch timeout to default, if not set */ |
| 827 | if (!bus->bank_switch_timeout) |
| 828 | bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT; |
| 829 | |
| 830 | /* Check if bank switch was successful */ |
| 831 | ret = sdw_ml_sync_bank_switch(bus); |
| 832 | if (ret < 0) { |
| 833 | dev_err(bus->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 834 | "multi link bank switch failed: %d\n", ret); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 835 | goto error; |
| 836 | } |
| 837 | |
Pierre-Louis Bossart | 063ff4e | 2020-09-01 23:05:53 +0800 | [diff] [blame] | 838 | if (multi_link) |
Srinivas Kandagatla | 9315d90 | 2019-06-06 12:22:22 +0100 | [diff] [blame] | 839 | mutex_unlock(&bus->msg_lock); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | return ret; |
| 843 | |
| 844 | error: |
| 845 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 846 | bus = m_rt->bus; |
Tom Rix | 3fbbf21 | 2020-09-02 13:26:50 -0700 | [diff] [blame] | 847 | if (bus->defer_msg.msg) { |
| 848 | kfree(bus->defer_msg.msg->buf); |
| 849 | kfree(bus->defer_msg.msg); |
| 850 | } |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | msg_unlock: |
| 854 | |
| 855 | if (multi_link) { |
| 856 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 857 | bus = m_rt->bus; |
| 858 | if (mutex_is_locked(&bus->msg_lock)) |
| 859 | mutex_unlock(&bus->msg_lock); |
Sanyog Kale | 99b8a5d | 2018-04-26 18:38:28 +0530 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | /** |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 867 | * sdw_release_stream() - Free the assigned stream runtime |
| 868 | * |
| 869 | * @stream: SoundWire stream runtime |
| 870 | * |
| 871 | * sdw_release_stream should be called only once per stream |
| 872 | */ |
| 873 | void sdw_release_stream(struct sdw_stream_runtime *stream) |
| 874 | { |
| 875 | kfree(stream); |
| 876 | } |
| 877 | EXPORT_SYMBOL(sdw_release_stream); |
| 878 | |
| 879 | /** |
| 880 | * sdw_alloc_stream() - Allocate and return stream runtime |
| 881 | * |
| 882 | * @stream_name: SoundWire stream name |
| 883 | * |
| 884 | * Allocates a SoundWire stream runtime instance. |
| 885 | * sdw_alloc_stream should be called only once per stream. Typically |
| 886 | * invoked from ALSA/ASoC machine/platform driver. |
| 887 | */ |
Srinivas Kandagatla | dfcff3f | 2019-08-13 09:35:47 +0100 | [diff] [blame] | 888 | struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 889 | { |
| 890 | struct sdw_stream_runtime *stream; |
| 891 | |
| 892 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
| 893 | if (!stream) |
| 894 | return NULL; |
| 895 | |
| 896 | stream->name = stream_name; |
Sanyog Kale | 0c4a104 | 2018-07-27 14:44:13 +0530 | [diff] [blame] | 897 | INIT_LIST_HEAD(&stream->master_list); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 898 | stream->state = SDW_STREAM_ALLOCATED; |
Shreyas NC | 9b5c132 | 2018-07-27 14:44:15 +0530 | [diff] [blame] | 899 | stream->m_rt_count = 0; |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 900 | |
| 901 | return stream; |
| 902 | } |
| 903 | EXPORT_SYMBOL(sdw_alloc_stream); |
| 904 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 905 | static struct sdw_master_runtime |
| 906 | *sdw_find_master_rt(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 907 | struct sdw_stream_runtime *stream) |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 908 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 909 | struct sdw_master_runtime *m_rt; |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 910 | |
| 911 | /* Retrieve Bus handle if already available */ |
| 912 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 913 | if (m_rt->bus == bus) |
| 914 | return m_rt; |
| 915 | } |
| 916 | |
| 917 | return NULL; |
| 918 | } |
| 919 | |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 920 | /** |
| 921 | * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle |
| 922 | * |
| 923 | * @bus: SDW bus instance |
| 924 | * @stream_config: Stream configuration |
| 925 | * @stream: Stream runtime handle. |
| 926 | * |
| 927 | * This function is to be called with bus_lock held. |
| 928 | */ |
| 929 | static struct sdw_master_runtime |
| 930 | *sdw_alloc_master_rt(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 931 | struct sdw_stream_config *stream_config, |
| 932 | struct sdw_stream_runtime *stream) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 933 | { |
| 934 | struct sdw_master_runtime *m_rt; |
| 935 | |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 936 | /* |
| 937 | * check if Master is already allocated (as a result of Slave adding |
| 938 | * it first), if so skip allocation and go to configure |
| 939 | */ |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 940 | m_rt = sdw_find_master_rt(bus, stream); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 941 | if (m_rt) |
| 942 | goto stream_config; |
| 943 | |
| 944 | m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL); |
| 945 | if (!m_rt) |
| 946 | return NULL; |
| 947 | |
| 948 | /* Initialization of Master runtime handle */ |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 949 | INIT_LIST_HEAD(&m_rt->port_list); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 950 | INIT_LIST_HEAD(&m_rt->slave_rt_list); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 951 | list_add_tail(&m_rt->stream_node, &stream->master_list); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 952 | |
| 953 | list_add_tail(&m_rt->bus_node, &bus->m_rt_list); |
| 954 | |
| 955 | stream_config: |
| 956 | m_rt->ch_count = stream_config->ch_count; |
| 957 | m_rt->bus = bus; |
| 958 | m_rt->stream = stream; |
| 959 | m_rt->direction = stream_config->direction; |
| 960 | |
| 961 | return m_rt; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle. |
| 966 | * |
| 967 | * @slave: Slave handle |
| 968 | * @stream_config: Stream configuration |
| 969 | * @stream: Stream runtime handle |
| 970 | * |
| 971 | * This function is to be called with bus_lock held. |
| 972 | */ |
| 973 | static struct sdw_slave_runtime |
| 974 | *sdw_alloc_slave_rt(struct sdw_slave *slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 975 | struct sdw_stream_config *stream_config, |
| 976 | struct sdw_stream_runtime *stream) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 977 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 978 | struct sdw_slave_runtime *s_rt; |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 979 | |
| 980 | s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL); |
| 981 | if (!s_rt) |
| 982 | return NULL; |
| 983 | |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 984 | INIT_LIST_HEAD(&s_rt->port_list); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 985 | s_rt->ch_count = stream_config->ch_count; |
| 986 | s_rt->direction = stream_config->direction; |
| 987 | s_rt->slave = slave; |
| 988 | |
| 989 | return s_rt; |
| 990 | } |
| 991 | |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 992 | static void sdw_master_port_release(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 993 | struct sdw_master_runtime *m_rt) |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 994 | { |
| 995 | struct sdw_port_runtime *p_rt, *_p_rt; |
| 996 | |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 997 | list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) { |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 998 | list_del(&p_rt->port_node); |
| 999 | kfree(p_rt); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | static void sdw_slave_port_release(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1004 | struct sdw_slave *slave, |
| 1005 | struct sdw_stream_runtime *stream) |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1006 | { |
| 1007 | struct sdw_port_runtime *p_rt, *_p_rt; |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1008 | struct sdw_master_runtime *m_rt; |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1009 | struct sdw_slave_runtime *s_rt; |
| 1010 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1011 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1012 | list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1013 | if (s_rt->slave != slave) |
| 1014 | continue; |
| 1015 | |
| 1016 | list_for_each_entry_safe(p_rt, _p_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1017 | &s_rt->port_list, port_node) { |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1018 | list_del(&p_rt->port_node); |
| 1019 | kfree(p_rt); |
| 1020 | } |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1025 | /** |
| 1026 | * sdw_release_slave_stream() - Free Slave(s) runtime handle |
| 1027 | * |
| 1028 | * @slave: Slave handle. |
| 1029 | * @stream: Stream runtime handle. |
| 1030 | * |
| 1031 | * This function is to be called with bus_lock held. |
| 1032 | */ |
| 1033 | static void sdw_release_slave_stream(struct sdw_slave *slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1034 | struct sdw_stream_runtime *stream) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1035 | { |
| 1036 | struct sdw_slave_runtime *s_rt, *_s_rt; |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1037 | struct sdw_master_runtime *m_rt; |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1038 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1039 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1040 | /* Retrieve Slave runtime handle */ |
| 1041 | list_for_each_entry_safe(s_rt, _s_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1042 | &m_rt->slave_rt_list, m_rt_node) { |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1043 | if (s_rt->slave == slave) { |
| 1044 | list_del(&s_rt->m_rt_node); |
| 1045 | kfree(s_rt); |
| 1046 | return; |
| 1047 | } |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * sdw_release_master_stream() - Free Master runtime handle |
| 1054 | * |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1055 | * @m_rt: Master runtime node |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1056 | * @stream: Stream runtime handle. |
| 1057 | * |
| 1058 | * This function is to be called with bus_lock held |
| 1059 | * It frees the Master runtime handle and associated Slave(s) runtime |
| 1060 | * handle. If this is called first then sdw_release_slave_stream() will have |
| 1061 | * no effect as Slave(s) runtime handle would already be freed up. |
| 1062 | */ |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1063 | static void sdw_release_master_stream(struct sdw_master_runtime *m_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1064 | struct sdw_stream_runtime *stream) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1065 | { |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1066 | struct sdw_slave_runtime *s_rt, *_s_rt; |
| 1067 | |
Sanyog Kale | 8d6ccf5 | 2018-07-27 14:44:10 +0530 | [diff] [blame] | 1068 | list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) { |
| 1069 | sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream); |
| 1070 | sdw_release_slave_stream(s_rt->slave, stream); |
| 1071 | } |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1072 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1073 | list_del(&m_rt->stream_node); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1074 | list_del(&m_rt->bus_node); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1075 | kfree(m_rt); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /** |
| 1079 | * sdw_stream_remove_master() - Remove master from sdw_stream |
| 1080 | * |
| 1081 | * @bus: SDW Bus instance |
| 1082 | * @stream: SoundWire stream |
| 1083 | * |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1084 | * This removes and frees port_rt and master_rt from a stream |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1085 | */ |
| 1086 | int sdw_stream_remove_master(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1087 | struct sdw_stream_runtime *stream) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1088 | { |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1089 | struct sdw_master_runtime *m_rt, *_m_rt; |
| 1090 | |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1091 | mutex_lock(&bus->bus_lock); |
| 1092 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1093 | list_for_each_entry_safe(m_rt, _m_rt, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1094 | &stream->master_list, stream_node) { |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1095 | if (m_rt->bus != bus) |
| 1096 | continue; |
| 1097 | |
| 1098 | sdw_master_port_release(bus, m_rt); |
| 1099 | sdw_release_master_stream(m_rt, stream); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 1100 | stream->m_rt_count--; |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | if (list_empty(&stream->master_list)) |
| 1104 | stream->state = SDW_STREAM_RELEASED; |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1105 | |
| 1106 | mutex_unlock(&bus->bus_lock); |
| 1107 | |
| 1108 | return 0; |
| 1109 | } |
| 1110 | EXPORT_SYMBOL(sdw_stream_remove_master); |
| 1111 | |
| 1112 | /** |
| 1113 | * sdw_stream_remove_slave() - Remove slave from sdw_stream |
| 1114 | * |
| 1115 | * @slave: SDW Slave instance |
| 1116 | * @stream: SoundWire stream |
| 1117 | * |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1118 | * This removes and frees port_rt and slave_rt from a stream |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1119 | */ |
| 1120 | int sdw_stream_remove_slave(struct sdw_slave *slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1121 | struct sdw_stream_runtime *stream) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1122 | { |
| 1123 | mutex_lock(&slave->bus->bus_lock); |
| 1124 | |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1125 | sdw_slave_port_release(slave->bus, slave, stream); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1126 | sdw_release_slave_stream(slave, stream); |
| 1127 | |
| 1128 | mutex_unlock(&slave->bus->bus_lock); |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | EXPORT_SYMBOL(sdw_stream_remove_slave); |
| 1133 | |
| 1134 | /** |
| 1135 | * sdw_config_stream() - Configure the allocated stream |
| 1136 | * |
| 1137 | * @dev: SDW device |
| 1138 | * @stream: SoundWire stream |
| 1139 | * @stream_config: Stream configuration for audio stream |
| 1140 | * @is_slave: is API called from Slave or Master |
| 1141 | * |
| 1142 | * This function is to be called with bus_lock held. |
| 1143 | */ |
| 1144 | static int sdw_config_stream(struct device *dev, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1145 | struct sdw_stream_runtime *stream, |
| 1146 | struct sdw_stream_config *stream_config, |
| 1147 | bool is_slave) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1148 | { |
| 1149 | /* |
| 1150 | * Update the stream rate, channel and bps based on data |
| 1151 | * source. For more than one data source (multilink), |
| 1152 | * match the rate, bps, stream type and increment number of channels. |
| 1153 | * |
| 1154 | * If rate/bps is zero, it means the values are not set, so skip |
| 1155 | * comparison and allow the value to be set and stored in stream |
| 1156 | */ |
| 1157 | if (stream->params.rate && |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1158 | stream->params.rate != stream_config->frame_rate) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1159 | dev_err(dev, "rate not matching, stream:%s\n", stream->name); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1160 | return -EINVAL; |
| 1161 | } |
| 1162 | |
| 1163 | if (stream->params.bps && |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1164 | stream->params.bps != stream_config->bps) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1165 | dev_err(dev, "bps not matching, stream:%s\n", stream->name); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1166 | return -EINVAL; |
| 1167 | } |
| 1168 | |
| 1169 | stream->type = stream_config->type; |
| 1170 | stream->params.rate = stream_config->frame_rate; |
| 1171 | stream->params.bps = stream_config->bps; |
| 1172 | |
| 1173 | /* TODO: Update this check during Device-device support */ |
| 1174 | if (is_slave) |
| 1175 | stream->params.ch_count += stream_config->ch_count; |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1180 | static int sdw_is_valid_port_range(struct device *dev, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1181 | struct sdw_port_runtime *p_rt) |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1182 | { |
| 1183 | if (!SDW_VALID_PORT_RANGE(p_rt->num)) { |
| 1184 | dev_err(dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1185 | "SoundWire: Invalid port number :%d\n", p_rt->num); |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1186 | return -EINVAL; |
| 1187 | } |
| 1188 | |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1192 | static struct sdw_port_runtime |
| 1193 | *sdw_port_alloc(struct device *dev, |
| 1194 | struct sdw_port_config *port_config, |
| 1195 | int port_index) |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1196 | { |
| 1197 | struct sdw_port_runtime *p_rt; |
| 1198 | |
| 1199 | p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL); |
| 1200 | if (!p_rt) |
| 1201 | return NULL; |
| 1202 | |
| 1203 | p_rt->ch_mask = port_config[port_index].ch_mask; |
| 1204 | p_rt->num = port_config[port_index].num; |
| 1205 | |
| 1206 | return p_rt; |
| 1207 | } |
| 1208 | |
| 1209 | static int sdw_master_port_config(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1210 | struct sdw_master_runtime *m_rt, |
| 1211 | struct sdw_port_config *port_config, |
| 1212 | unsigned int num_ports) |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1213 | { |
| 1214 | struct sdw_port_runtime *p_rt; |
| 1215 | int i; |
| 1216 | |
| 1217 | /* Iterate for number of ports to perform initialization */ |
| 1218 | for (i = 0; i < num_ports; i++) { |
| 1219 | p_rt = sdw_port_alloc(bus->dev, port_config, i); |
| 1220 | if (!p_rt) |
| 1221 | return -ENOMEM; |
| 1222 | |
| 1223 | /* |
| 1224 | * TODO: Check port capabilities for requested |
| 1225 | * configuration (audio mode support) |
| 1226 | */ |
| 1227 | |
| 1228 | list_add_tail(&p_rt->port_node, &m_rt->port_list); |
| 1229 | } |
| 1230 | |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static int sdw_slave_port_config(struct sdw_slave *slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1235 | struct sdw_slave_runtime *s_rt, |
| 1236 | struct sdw_port_config *port_config, |
| 1237 | unsigned int num_config) |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1238 | { |
| 1239 | struct sdw_port_runtime *p_rt; |
| 1240 | int i, ret; |
| 1241 | |
| 1242 | /* Iterate for number of ports to perform initialization */ |
| 1243 | for (i = 0; i < num_config; i++) { |
| 1244 | p_rt = sdw_port_alloc(&slave->dev, port_config, i); |
| 1245 | if (!p_rt) |
| 1246 | return -ENOMEM; |
| 1247 | |
| 1248 | /* |
| 1249 | * TODO: Check valid port range as defined by DisCo/ |
| 1250 | * slave |
| 1251 | */ |
| 1252 | ret = sdw_is_valid_port_range(&slave->dev, p_rt); |
| 1253 | if (ret < 0) { |
| 1254 | kfree(p_rt); |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * TODO: Check port capabilities for requested |
| 1260 | * configuration (audio mode support) |
| 1261 | */ |
| 1262 | |
| 1263 | list_add_tail(&p_rt->port_node, &s_rt->port_list); |
| 1264 | } |
| 1265 | |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1269 | /** |
| 1270 | * sdw_stream_add_master() - Allocate and add master runtime to a stream |
| 1271 | * |
| 1272 | * @bus: SDW Bus instance |
| 1273 | * @stream_config: Stream configuration for audio stream |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1274 | * @port_config: Port configuration for audio stream |
| 1275 | * @num_ports: Number of ports |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1276 | * @stream: SoundWire stream |
| 1277 | */ |
| 1278 | int sdw_stream_add_master(struct sdw_bus *bus, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1279 | struct sdw_stream_config *stream_config, |
| 1280 | struct sdw_port_config *port_config, |
| 1281 | unsigned int num_ports, |
| 1282 | struct sdw_stream_runtime *stream) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1283 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1284 | struct sdw_master_runtime *m_rt; |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1285 | int ret; |
| 1286 | |
| 1287 | mutex_lock(&bus->bus_lock); |
| 1288 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 1289 | /* |
| 1290 | * For multi link streams, add the second master only if |
| 1291 | * the bus supports it. |
| 1292 | * Check if bus->multi_link is set |
| 1293 | */ |
| 1294 | if (!bus->multi_link && stream->m_rt_count > 0) { |
| 1295 | dev_err(bus->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1296 | "Multilink not supported, link %d\n", bus->link_id); |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 1297 | ret = -EINVAL; |
| 1298 | goto unlock; |
| 1299 | } |
| 1300 | |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1301 | m_rt = sdw_alloc_master_rt(bus, stream_config, stream); |
| 1302 | if (!m_rt) { |
| 1303 | dev_err(bus->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1304 | "Master runtime config failed for stream:%s\n", |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1305 | stream->name); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1306 | ret = -ENOMEM; |
Shreyas NC | 3fef1a2 | 2018-07-27 14:44:09 +0530 | [diff] [blame] | 1307 | goto unlock; |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | ret = sdw_config_stream(bus->dev, stream, stream_config, false); |
| 1311 | if (ret) |
| 1312 | goto stream_error; |
| 1313 | |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1314 | ret = sdw_master_port_config(bus, m_rt, port_config, num_ports); |
| 1315 | if (ret) |
| 1316 | goto stream_error; |
| 1317 | |
Shreyas NC | ce6e74d | 2018-07-27 14:44:16 +0530 | [diff] [blame] | 1318 | stream->m_rt_count++; |
| 1319 | |
Shreyas NC | 3fef1a2 | 2018-07-27 14:44:09 +0530 | [diff] [blame] | 1320 | goto unlock; |
| 1321 | |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1322 | stream_error: |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1323 | sdw_release_master_stream(m_rt, stream); |
Shreyas NC | 3fef1a2 | 2018-07-27 14:44:09 +0530 | [diff] [blame] | 1324 | unlock: |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1325 | mutex_unlock(&bus->bus_lock); |
| 1326 | return ret; |
| 1327 | } |
| 1328 | EXPORT_SYMBOL(sdw_stream_add_master); |
| 1329 | |
| 1330 | /** |
| 1331 | * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream |
| 1332 | * |
| 1333 | * @slave: SDW Slave instance |
| 1334 | * @stream_config: Stream configuration for audio stream |
| 1335 | * @stream: SoundWire stream |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1336 | * @port_config: Port configuration for audio stream |
| 1337 | * @num_ports: Number of ports |
Shreyas NC | 0aebe40 | 2018-07-27 14:44:08 +0530 | [diff] [blame] | 1338 | * |
| 1339 | * It is expected that Slave is added before adding Master |
| 1340 | * to the Stream. |
| 1341 | * |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1342 | */ |
| 1343 | int sdw_stream_add_slave(struct sdw_slave *slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1344 | struct sdw_stream_config *stream_config, |
| 1345 | struct sdw_port_config *port_config, |
| 1346 | unsigned int num_ports, |
| 1347 | struct sdw_stream_runtime *stream) |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1348 | { |
| 1349 | struct sdw_slave_runtime *s_rt; |
| 1350 | struct sdw_master_runtime *m_rt; |
| 1351 | int ret; |
| 1352 | |
| 1353 | mutex_lock(&slave->bus->bus_lock); |
| 1354 | |
| 1355 | /* |
| 1356 | * If this API is invoked by Slave first then m_rt is not valid. |
| 1357 | * So, allocate m_rt and add Slave to it. |
| 1358 | */ |
| 1359 | m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream); |
| 1360 | if (!m_rt) { |
| 1361 | dev_err(&slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1362 | "alloc master runtime failed for stream:%s\n", |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1363 | stream->name); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1364 | ret = -ENOMEM; |
| 1365 | goto error; |
| 1366 | } |
| 1367 | |
| 1368 | s_rt = sdw_alloc_slave_rt(slave, stream_config, stream); |
| 1369 | if (!s_rt) { |
| 1370 | dev_err(&slave->dev, |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1371 | "Slave runtime config failed for stream:%s\n", |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1372 | stream->name); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1373 | ret = -ENOMEM; |
| 1374 | goto stream_error; |
| 1375 | } |
| 1376 | |
| 1377 | ret = sdw_config_stream(&slave->dev, stream, stream_config, true); |
Rander Wang | 48f17f9 | 2021-03-31 08:46:10 +0800 | [diff] [blame] | 1378 | if (ret) { |
| 1379 | /* |
| 1380 | * sdw_release_master_stream will release s_rt in slave_rt_list in |
| 1381 | * stream_error case, but s_rt is only added to slave_rt_list |
| 1382 | * when sdw_config_stream is successful, so free s_rt explicitly |
| 1383 | * when sdw_config_stream is failed. |
| 1384 | */ |
| 1385 | kfree(s_rt); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1386 | goto stream_error; |
Rander Wang | 48f17f9 | 2021-03-31 08:46:10 +0800 | [diff] [blame] | 1387 | } |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1388 | |
| 1389 | list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list); |
| 1390 | |
Sanyog Kale | bbe7379 | 2018-04-26 18:38:13 +0530 | [diff] [blame] | 1391 | ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports); |
| 1392 | if (ret) |
| 1393 | goto stream_error; |
| 1394 | |
Shreyas NC | 0aebe40 | 2018-07-27 14:44:08 +0530 | [diff] [blame] | 1395 | /* |
| 1396 | * Change stream state to CONFIGURED on first Slave add. |
| 1397 | * Bus is not aware of number of Slave(s) in a stream at this |
| 1398 | * point so cannot depend on all Slave(s) to be added in order to |
| 1399 | * change stream state to CONFIGURED. |
| 1400 | */ |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1401 | stream->state = SDW_STREAM_CONFIGURED; |
| 1402 | goto error; |
| 1403 | |
| 1404 | stream_error: |
| 1405 | /* |
| 1406 | * we hit error so cleanup the stream, release all Slave(s) and |
| 1407 | * Master runtime |
| 1408 | */ |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1409 | sdw_release_master_stream(m_rt, stream); |
Sanyog Kale | 89e5905 | 2018-04-26 18:38:08 +0530 | [diff] [blame] | 1410 | error: |
| 1411 | mutex_unlock(&slave->bus->bus_lock); |
| 1412 | return ret; |
| 1413 | } |
| 1414 | EXPORT_SYMBOL(sdw_stream_add_slave); |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 1415 | |
| 1416 | /** |
| 1417 | * sdw_get_slave_dpn_prop() - Get Slave port capabilities |
| 1418 | * |
| 1419 | * @slave: Slave handle |
| 1420 | * @direction: Data direction. |
| 1421 | * @port_num: Port number |
| 1422 | */ |
| 1423 | struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave, |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1424 | enum sdw_data_direction direction, |
| 1425 | unsigned int port_num) |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 1426 | { |
| 1427 | struct sdw_dpn_prop *dpn_prop; |
| 1428 | u8 num_ports; |
| 1429 | int i; |
| 1430 | |
| 1431 | if (direction == SDW_DATA_DIR_TX) { |
| 1432 | num_ports = hweight32(slave->prop.source_ports); |
| 1433 | dpn_prop = slave->prop.src_dpn_prop; |
| 1434 | } else { |
| 1435 | num_ports = hweight32(slave->prop.sink_ports); |
| 1436 | dpn_prop = slave->prop.sink_dpn_prop; |
| 1437 | } |
| 1438 | |
| 1439 | for (i = 0; i < num_ports; i++) { |
Srinivas Kandagatla | 03ecad9 | 2019-05-22 17:24:43 +0100 | [diff] [blame] | 1440 | if (dpn_prop[i].num == port_num) |
Sanyog Kale | f8101c7 | 2018-04-26 18:38:17 +0530 | [diff] [blame] | 1441 | return &dpn_prop[i]; |
| 1442 | } |
| 1443 | |
| 1444 | return NULL; |
| 1445 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1446 | |
Sanyog Kale | 0c4a104 | 2018-07-27 14:44:13 +0530 | [diff] [blame] | 1447 | /** |
| 1448 | * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s) |
| 1449 | * |
| 1450 | * @stream: SoundWire stream |
| 1451 | * |
| 1452 | * Acquire bus_lock for each of the master runtime(m_rt) part of this |
| 1453 | * stream to reconfigure the bus. |
| 1454 | * NOTE: This function is called from SoundWire stream ops and is |
| 1455 | * expected that a global lock is held before acquiring bus_lock. |
| 1456 | */ |
| 1457 | static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream) |
| 1458 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1459 | struct sdw_master_runtime *m_rt; |
Pierre-Louis Bossart | 53e0a30 | 2021-03-02 17:11:22 +0800 | [diff] [blame] | 1460 | struct sdw_bus *bus; |
Sanyog Kale | 0c4a104 | 2018-07-27 14:44:13 +0530 | [diff] [blame] | 1461 | |
| 1462 | /* Iterate for all Master(s) in Master list */ |
| 1463 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1464 | bus = m_rt->bus; |
| 1465 | |
| 1466 | mutex_lock(&bus->bus_lock); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * sdw_release_bus_lock: Release bus lock for all Master runtime(s) |
| 1472 | * |
| 1473 | * @stream: SoundWire stream |
| 1474 | * |
| 1475 | * Release the previously held bus_lock after reconfiguring the bus. |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1476 | * NOTE: This function is called from SoundWire stream ops and is |
| 1477 | * expected that a global lock is held before releasing bus_lock. |
Sanyog Kale | 0c4a104 | 2018-07-27 14:44:13 +0530 | [diff] [blame] | 1478 | */ |
| 1479 | static void sdw_release_bus_lock(struct sdw_stream_runtime *stream) |
| 1480 | { |
Pierre-Louis Bossart | 5920a29 | 2021-03-02 17:11:21 +0800 | [diff] [blame] | 1481 | struct sdw_master_runtime *m_rt; |
Pierre-Louis Bossart | 53e0a30 | 2021-03-02 17:11:22 +0800 | [diff] [blame] | 1482 | struct sdw_bus *bus; |
Sanyog Kale | 0c4a104 | 2018-07-27 14:44:13 +0530 | [diff] [blame] | 1483 | |
| 1484 | /* Iterate for all Master(s) in Master list */ |
| 1485 | list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) { |
| 1486 | bus = m_rt->bus; |
| 1487 | mutex_unlock(&bus->bus_lock); |
| 1488 | } |
| 1489 | } |
| 1490 | |
Pierre-Louis Bossart | c7a8f04 | 2020-01-14 17:52:25 -0600 | [diff] [blame] | 1491 | static int _sdw_prepare_stream(struct sdw_stream_runtime *stream, |
| 1492 | bool update_params) |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1493 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1494 | struct sdw_master_runtime *m_rt; |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1495 | struct sdw_bus *bus = NULL; |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1496 | struct sdw_master_prop *prop; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1497 | struct sdw_bus_params params; |
| 1498 | int ret; |
| 1499 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1500 | /* Prepare Master(s) and Slave(s) port(s) associated with stream */ |
| 1501 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1502 | bus = m_rt->bus; |
| 1503 | prop = &bus->prop; |
| 1504 | memcpy(¶ms, &bus->params, sizeof(params)); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1505 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1506 | /* TODO: Support Asynchronous mode */ |
Pierre-Louis Bossart | 3424305 | 2019-05-22 14:47:22 -0500 | [diff] [blame] | 1507 | if ((prop->max_clk_freq % stream->params.rate) != 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1508 | dev_err(bus->dev, "Async mode not supported\n"); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1509 | return -EINVAL; |
| 1510 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1511 | |
Pierre-Louis Bossart | c7a8f04 | 2020-01-14 17:52:25 -0600 | [diff] [blame] | 1512 | if (!update_params) |
| 1513 | goto program_params; |
| 1514 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1515 | /* Increment cumulative bus bandwidth */ |
| 1516 | /* TODO: Update this during Device-Device support */ |
| 1517 | bus->params.bandwidth += m_rt->stream->params.rate * |
| 1518 | m_rt->ch_count * m_rt->stream->params.bps; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1519 | |
Vinod Koul | c7578c1 | 2019-08-13 09:35:46 +0100 | [diff] [blame] | 1520 | /* Compute params */ |
| 1521 | if (bus->compute_params) { |
| 1522 | ret = bus->compute_params(bus); |
| 1523 | if (ret < 0) { |
Pierre-Louis Bossart | 6122d3b | 2021-03-23 08:58:54 +0800 | [diff] [blame] | 1524 | dev_err(bus->dev, "Compute params failed: %d\n", |
Vinod Koul | c7578c1 | 2019-08-13 09:35:46 +0100 | [diff] [blame] | 1525 | ret); |
| 1526 | return ret; |
| 1527 | } |
| 1528 | } |
| 1529 | |
Pierre-Louis Bossart | c7a8f04 | 2020-01-14 17:52:25 -0600 | [diff] [blame] | 1530 | program_params: |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1531 | /* Program params */ |
Rander Wang | bfaa354 | 2020-01-14 17:52:27 -0600 | [diff] [blame] | 1532 | ret = sdw_program_params(bus, true); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1533 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1534 | dev_err(bus->dev, "Program params failed: %d\n", ret); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1535 | goto restore_params; |
| 1536 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1537 | } |
| 1538 | |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1539 | if (!bus) { |
| 1540 | pr_err("Configuration error in %s\n", __func__); |
| 1541 | return -EINVAL; |
| 1542 | } |
| 1543 | |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1544 | ret = do_bank_switch(stream); |
| 1545 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1546 | dev_err(bus->dev, "Bank switch failed: %d\n", ret); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1547 | goto restore_params; |
| 1548 | } |
| 1549 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1550 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1551 | bus = m_rt->bus; |
| 1552 | |
| 1553 | /* Prepare port(s) on the new clock configuration */ |
| 1554 | ret = sdw_prep_deprep_ports(m_rt, true); |
| 1555 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1556 | dev_err(bus->dev, "Prepare port(s) failed ret = %d\n", |
Pierre-Louis Bossart | 1fe74a5e | 2019-05-01 10:57:35 -0500 | [diff] [blame] | 1557 | ret); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1558 | return ret; |
| 1559 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | stream->state = SDW_STREAM_PREPARED; |
| 1563 | |
| 1564 | return ret; |
| 1565 | |
| 1566 | restore_params: |
| 1567 | memcpy(&bus->params, ¶ms, sizeof(params)); |
| 1568 | return ret; |
| 1569 | } |
| 1570 | |
| 1571 | /** |
| 1572 | * sdw_prepare_stream() - Prepare SoundWire stream |
| 1573 | * |
| 1574 | * @stream: Soundwire stream |
| 1575 | * |
Mauro Carvalho Chehab | 34962fb | 2018-05-08 15:14:57 -0300 | [diff] [blame] | 1576 | * Documentation/driver-api/soundwire/stream.rst explains this API in detail |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1577 | */ |
| 1578 | int sdw_prepare_stream(struct sdw_stream_runtime *stream) |
| 1579 | { |
Pierre-Louis Bossart | c7a8f04 | 2020-01-14 17:52:25 -0600 | [diff] [blame] | 1580 | bool update_params = true; |
Bard Liao | c32464c | 2020-01-14 17:52:24 -0600 | [diff] [blame] | 1581 | int ret; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1582 | |
| 1583 | if (!stream) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1584 | pr_err("SoundWire: Handle not found for stream\n"); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1585 | return -EINVAL; |
| 1586 | } |
| 1587 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1588 | sdw_acquire_bus_lock(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1589 | |
Bard Liao | c32464c | 2020-01-14 17:52:24 -0600 | [diff] [blame] | 1590 | if (stream->state == SDW_STREAM_PREPARED) { |
| 1591 | ret = 0; |
| 1592 | goto state_err; |
| 1593 | } |
| 1594 | |
Pierre-Louis Bossart | 5952880 | 2020-01-14 17:52:23 -0600 | [diff] [blame] | 1595 | if (stream->state != SDW_STREAM_CONFIGURED && |
| 1596 | stream->state != SDW_STREAM_DEPREPARED && |
| 1597 | stream->state != SDW_STREAM_DISABLED) { |
| 1598 | pr_err("%s: %s: inconsistent state state %d\n", |
| 1599 | __func__, stream->name, stream->state); |
| 1600 | ret = -EINVAL; |
| 1601 | goto state_err; |
| 1602 | } |
| 1603 | |
Pierre-Louis Bossart | c7a8f04 | 2020-01-14 17:52:25 -0600 | [diff] [blame] | 1604 | /* |
| 1605 | * when the stream is DISABLED, this means sdw_prepare_stream() |
| 1606 | * is called as a result of an underflow or a resume operation. |
| 1607 | * In this case, the bus parameters shall not be recomputed, but |
| 1608 | * still need to be re-applied |
| 1609 | */ |
| 1610 | if (stream->state == SDW_STREAM_DISABLED) |
| 1611 | update_params = false; |
| 1612 | |
| 1613 | ret = _sdw_prepare_stream(stream, update_params); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1614 | |
Pierre-Louis Bossart | 5952880 | 2020-01-14 17:52:23 -0600 | [diff] [blame] | 1615 | state_err: |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1616 | sdw_release_bus_lock(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1617 | return ret; |
| 1618 | } |
| 1619 | EXPORT_SYMBOL(sdw_prepare_stream); |
| 1620 | |
| 1621 | static int _sdw_enable_stream(struct sdw_stream_runtime *stream) |
| 1622 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1623 | struct sdw_master_runtime *m_rt; |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1624 | struct sdw_bus *bus = NULL; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1625 | int ret; |
| 1626 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1627 | /* Enable Master(s) and Slave(s) port(s) associated with stream */ |
| 1628 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1629 | bus = m_rt->bus; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1630 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1631 | /* Program params */ |
Rander Wang | bfaa354 | 2020-01-14 17:52:27 -0600 | [diff] [blame] | 1632 | ret = sdw_program_params(bus, false); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1633 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1634 | dev_err(bus->dev, "Program params failed: %d\n", ret); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1635 | return ret; |
| 1636 | } |
| 1637 | |
| 1638 | /* Enable port(s) */ |
| 1639 | ret = sdw_enable_disable_ports(m_rt, true); |
| 1640 | if (ret < 0) { |
Vinod Koul | 62f0cec | 2019-05-02 16:29:24 +0530 | [diff] [blame] | 1641 | dev_err(bus->dev, |
| 1642 | "Enable port(s) failed ret: %d\n", ret); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1643 | return ret; |
| 1644 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1645 | } |
| 1646 | |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1647 | if (!bus) { |
| 1648 | pr_err("Configuration error in %s\n", __func__); |
| 1649 | return -EINVAL; |
| 1650 | } |
| 1651 | |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1652 | ret = do_bank_switch(stream); |
| 1653 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1654 | dev_err(bus->dev, "Bank switch failed: %d\n", ret); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1655 | return ret; |
| 1656 | } |
| 1657 | |
| 1658 | stream->state = SDW_STREAM_ENABLED; |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | /** |
| 1663 | * sdw_enable_stream() - Enable SoundWire stream |
| 1664 | * |
| 1665 | * @stream: Soundwire stream |
| 1666 | * |
Mauro Carvalho Chehab | 34962fb | 2018-05-08 15:14:57 -0300 | [diff] [blame] | 1667 | * Documentation/driver-api/soundwire/stream.rst explains this API in detail |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1668 | */ |
| 1669 | int sdw_enable_stream(struct sdw_stream_runtime *stream) |
| 1670 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1671 | int ret; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1672 | |
| 1673 | if (!stream) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1674 | pr_err("SoundWire: Handle not found for stream\n"); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1675 | return -EINVAL; |
| 1676 | } |
| 1677 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1678 | sdw_acquire_bus_lock(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1679 | |
Pierre-Louis Bossart | 5952880 | 2020-01-14 17:52:23 -0600 | [diff] [blame] | 1680 | if (stream->state != SDW_STREAM_PREPARED && |
| 1681 | stream->state != SDW_STREAM_DISABLED) { |
| 1682 | pr_err("%s: %s: inconsistent state state %d\n", |
| 1683 | __func__, stream->name, stream->state); |
| 1684 | ret = -EINVAL; |
| 1685 | goto state_err; |
| 1686 | } |
| 1687 | |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1688 | ret = _sdw_enable_stream(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1689 | |
Pierre-Louis Bossart | 5952880 | 2020-01-14 17:52:23 -0600 | [diff] [blame] | 1690 | state_err: |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1691 | sdw_release_bus_lock(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1692 | return ret; |
| 1693 | } |
| 1694 | EXPORT_SYMBOL(sdw_enable_stream); |
| 1695 | |
| 1696 | static int _sdw_disable_stream(struct sdw_stream_runtime *stream) |
| 1697 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1698 | struct sdw_master_runtime *m_rt; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1699 | int ret; |
| 1700 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1701 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1702 | struct sdw_bus *bus = m_rt->bus; |
| 1703 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1704 | /* Disable port(s) */ |
| 1705 | ret = sdw_enable_disable_ports(m_rt, false); |
| 1706 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1707 | dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1708 | return ret; |
| 1709 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1710 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1711 | stream->state = SDW_STREAM_DISABLED; |
| 1712 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1713 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1714 | struct sdw_bus *bus = m_rt->bus; |
| 1715 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1716 | /* Program params */ |
Rander Wang | bfaa354 | 2020-01-14 17:52:27 -0600 | [diff] [blame] | 1717 | ret = sdw_program_params(bus, false); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1718 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1719 | dev_err(bus->dev, "Program params failed: %d\n", ret); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1720 | return ret; |
| 1721 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1722 | } |
| 1723 | |
Pierre-Louis Bossart | e0279b6 | 2019-08-05 19:55:13 -0500 | [diff] [blame] | 1724 | ret = do_bank_switch(stream); |
| 1725 | if (ret < 0) { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1726 | pr_err("Bank switch failed: %d\n", ret); |
Pierre-Louis Bossart | e0279b6 | 2019-08-05 19:55:13 -0500 | [diff] [blame] | 1727 | return ret; |
| 1728 | } |
| 1729 | |
| 1730 | /* make sure alternate bank (previous current) is also disabled */ |
| 1731 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1732 | struct sdw_bus *bus = m_rt->bus; |
| 1733 | |
Pierre-Louis Bossart | e0279b6 | 2019-08-05 19:55:13 -0500 | [diff] [blame] | 1734 | /* Disable port(s) */ |
| 1735 | ret = sdw_enable_disable_ports(m_rt, false); |
| 1736 | if (ret < 0) { |
| 1737 | dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); |
| 1738 | return ret; |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | return 0; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | /** |
| 1746 | * sdw_disable_stream() - Disable SoundWire stream |
| 1747 | * |
| 1748 | * @stream: Soundwire stream |
| 1749 | * |
Mauro Carvalho Chehab | 34962fb | 2018-05-08 15:14:57 -0300 | [diff] [blame] | 1750 | * Documentation/driver-api/soundwire/stream.rst explains this API in detail |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1751 | */ |
| 1752 | int sdw_disable_stream(struct sdw_stream_runtime *stream) |
| 1753 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1754 | int ret; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1755 | |
| 1756 | if (!stream) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1757 | pr_err("SoundWire: Handle not found for stream\n"); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1758 | return -EINVAL; |
| 1759 | } |
| 1760 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1761 | sdw_acquire_bus_lock(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1762 | |
Pierre-Louis Bossart | 5952880 | 2020-01-14 17:52:23 -0600 | [diff] [blame] | 1763 | if (stream->state != SDW_STREAM_ENABLED) { |
| 1764 | pr_err("%s: %s: inconsistent state state %d\n", |
| 1765 | __func__, stream->name, stream->state); |
| 1766 | ret = -EINVAL; |
| 1767 | goto state_err; |
| 1768 | } |
| 1769 | |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1770 | ret = _sdw_disable_stream(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1771 | |
Pierre-Louis Bossart | 5952880 | 2020-01-14 17:52:23 -0600 | [diff] [blame] | 1772 | state_err: |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1773 | sdw_release_bus_lock(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1774 | return ret; |
| 1775 | } |
| 1776 | EXPORT_SYMBOL(sdw_disable_stream); |
| 1777 | |
| 1778 | static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream) |
| 1779 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1780 | struct sdw_master_runtime *m_rt; |
| 1781 | struct sdw_bus *bus; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1782 | int ret = 0; |
| 1783 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1784 | list_for_each_entry(m_rt, &stream->master_list, stream_node) { |
| 1785 | bus = m_rt->bus; |
| 1786 | /* De-prepare port(s) */ |
| 1787 | ret = sdw_prep_deprep_ports(m_rt, false); |
| 1788 | if (ret < 0) { |
Vinod Koul | 62f0cec | 2019-05-02 16:29:24 +0530 | [diff] [blame] | 1789 | dev_err(bus->dev, |
| 1790 | "De-prepare port(s) failed: %d\n", ret); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1791 | return ret; |
| 1792 | } |
| 1793 | |
| 1794 | /* TODO: Update this during Device-Device support */ |
| 1795 | bus->params.bandwidth -= m_rt->stream->params.rate * |
| 1796 | m_rt->ch_count * m_rt->stream->params.bps; |
| 1797 | |
Bard Liao | 9026118 | 2020-09-08 21:15:20 +0800 | [diff] [blame] | 1798 | /* Compute params */ |
| 1799 | if (bus->compute_params) { |
| 1800 | ret = bus->compute_params(bus); |
| 1801 | if (ret < 0) { |
Pierre-Louis Bossart | 6122d3b | 2021-03-23 08:58:54 +0800 | [diff] [blame] | 1802 | dev_err(bus->dev, "Compute params failed: %d\n", |
Bard Liao | 9026118 | 2020-09-08 21:15:20 +0800 | [diff] [blame] | 1803 | ret); |
| 1804 | return ret; |
| 1805 | } |
| 1806 | } |
| 1807 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1808 | /* Program params */ |
Rander Wang | bfaa354 | 2020-01-14 17:52:27 -0600 | [diff] [blame] | 1809 | ret = sdw_program_params(bus, false); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1810 | if (ret < 0) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1811 | dev_err(bus->dev, "Program params failed: %d\n", ret); |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1812 | return ret; |
| 1813 | } |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | stream->state = SDW_STREAM_DEPREPARED; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1817 | return do_bank_switch(stream); |
| 1818 | } |
| 1819 | |
| 1820 | /** |
| 1821 | * sdw_deprepare_stream() - Deprepare SoundWire stream |
| 1822 | * |
| 1823 | * @stream: Soundwire stream |
| 1824 | * |
Mauro Carvalho Chehab | 34962fb | 2018-05-08 15:14:57 -0300 | [diff] [blame] | 1825 | * Documentation/driver-api/soundwire/stream.rst explains this API in detail |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1826 | */ |
| 1827 | int sdw_deprepare_stream(struct sdw_stream_runtime *stream) |
| 1828 | { |
Pierre-Louis Bossart | 3a0be1a | 2019-08-05 19:55:14 -0500 | [diff] [blame] | 1829 | int ret; |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1830 | |
| 1831 | if (!stream) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1832 | pr_err("SoundWire: Handle not found for stream\n"); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1833 | return -EINVAL; |
| 1834 | } |
| 1835 | |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1836 | sdw_acquire_bus_lock(stream); |
Pierre-Louis Bossart | 5952880 | 2020-01-14 17:52:23 -0600 | [diff] [blame] | 1837 | |
| 1838 | if (stream->state != SDW_STREAM_PREPARED && |
| 1839 | stream->state != SDW_STREAM_DISABLED) { |
| 1840 | pr_err("%s: %s: inconsistent state state %d\n", |
| 1841 | __func__, stream->name, stream->state); |
| 1842 | ret = -EINVAL; |
| 1843 | goto state_err; |
| 1844 | } |
| 1845 | |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1846 | ret = _sdw_deprepare_stream(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1847 | |
Pierre-Louis Bossart | 5952880 | 2020-01-14 17:52:23 -0600 | [diff] [blame] | 1848 | state_err: |
Vinod Koul | 4894972 | 2018-07-27 14:44:14 +0530 | [diff] [blame] | 1849 | sdw_release_bus_lock(stream); |
Sanyog Kale | 5c3eb9f | 2018-04-26 18:38:33 +0530 | [diff] [blame] | 1850 | return ret; |
| 1851 | } |
| 1852 | EXPORT_SYMBOL(sdw_deprepare_stream); |
Pierre-Louis Bossart | 4550569 | 2020-07-01 02:43:53 +0800 | [diff] [blame] | 1853 | |
| 1854 | static int set_stream(struct snd_pcm_substream *substream, |
| 1855 | struct sdw_stream_runtime *sdw_stream) |
| 1856 | { |
| 1857 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1858 | struct snd_soc_dai *dai; |
| 1859 | int ret = 0; |
| 1860 | int i; |
| 1861 | |
| 1862 | /* Set stream pointer on all DAIs */ |
| 1863 | for_each_rtd_dais(rtd, i, dai) { |
| 1864 | ret = snd_soc_dai_set_sdw_stream(dai, sdw_stream, substream->stream); |
| 1865 | if (ret < 0) { |
Pierre-Louis Bossart | 6122d3b | 2021-03-23 08:58:54 +0800 | [diff] [blame] | 1866 | dev_err(rtd->dev, "failed to set stream pointer on dai %s\n", dai->name); |
Pierre-Louis Bossart | 4550569 | 2020-07-01 02:43:53 +0800 | [diff] [blame] | 1867 | break; |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | return ret; |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * sdw_startup_stream() - Startup SoundWire stream |
| 1876 | * |
Vinod Koul | 3b71c69 | 2020-07-15 15:27:02 +0530 | [diff] [blame] | 1877 | * @sdw_substream: Soundwire stream |
Pierre-Louis Bossart | 4550569 | 2020-07-01 02:43:53 +0800 | [diff] [blame] | 1878 | * |
| 1879 | * Documentation/driver-api/soundwire/stream.rst explains this API in detail |
| 1880 | */ |
| 1881 | int sdw_startup_stream(void *sdw_substream) |
| 1882 | { |
| 1883 | struct snd_pcm_substream *substream = sdw_substream; |
| 1884 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1885 | struct sdw_stream_runtime *sdw_stream; |
| 1886 | char *name; |
| 1887 | int ret; |
| 1888 | |
| 1889 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 1890 | name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name); |
| 1891 | else |
| 1892 | name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name); |
| 1893 | |
| 1894 | if (!name) |
| 1895 | return -ENOMEM; |
| 1896 | |
| 1897 | sdw_stream = sdw_alloc_stream(name); |
| 1898 | if (!sdw_stream) { |
Pierre-Louis Bossart | 6122d3b | 2021-03-23 08:58:54 +0800 | [diff] [blame] | 1899 | dev_err(rtd->dev, "alloc stream failed for substream DAI %s\n", substream->name); |
Pierre-Louis Bossart | 4550569 | 2020-07-01 02:43:53 +0800 | [diff] [blame] | 1900 | ret = -ENOMEM; |
| 1901 | goto error; |
| 1902 | } |
| 1903 | |
| 1904 | ret = set_stream(substream, sdw_stream); |
| 1905 | if (ret < 0) |
| 1906 | goto release_stream; |
| 1907 | return 0; |
| 1908 | |
| 1909 | release_stream: |
| 1910 | sdw_release_stream(sdw_stream); |
| 1911 | set_stream(substream, NULL); |
| 1912 | error: |
| 1913 | kfree(name); |
| 1914 | return ret; |
| 1915 | } |
| 1916 | EXPORT_SYMBOL(sdw_startup_stream); |
| 1917 | |
| 1918 | /** |
| 1919 | * sdw_shutdown_stream() - Shutdown SoundWire stream |
| 1920 | * |
Vinod Koul | 3b71c69 | 2020-07-15 15:27:02 +0530 | [diff] [blame] | 1921 | * @sdw_substream: Soundwire stream |
Pierre-Louis Bossart | 4550569 | 2020-07-01 02:43:53 +0800 | [diff] [blame] | 1922 | * |
| 1923 | * Documentation/driver-api/soundwire/stream.rst explains this API in detail |
| 1924 | */ |
| 1925 | void sdw_shutdown_stream(void *sdw_substream) |
| 1926 | { |
| 1927 | struct snd_pcm_substream *substream = sdw_substream; |
| 1928 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1929 | struct sdw_stream_runtime *sdw_stream; |
| 1930 | struct snd_soc_dai *dai; |
| 1931 | |
| 1932 | /* Find stream from first CPU DAI */ |
| 1933 | dai = asoc_rtd_to_cpu(rtd, 0); |
| 1934 | |
| 1935 | sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream); |
| 1936 | |
Pierre-Louis Bossart | 3471d2a | 2020-09-04 04:47:36 +0800 | [diff] [blame] | 1937 | if (IS_ERR(sdw_stream)) { |
Pierre-Louis Bossart | 6122d3b | 2021-03-23 08:58:54 +0800 | [diff] [blame] | 1938 | dev_err(rtd->dev, "no stream found for DAI %s\n", dai->name); |
Pierre-Louis Bossart | 4550569 | 2020-07-01 02:43:53 +0800 | [diff] [blame] | 1939 | return; |
| 1940 | } |
| 1941 | |
| 1942 | /* release memory */ |
| 1943 | kfree(sdw_stream->name); |
| 1944 | sdw_release_stream(sdw_stream); |
| 1945 | |
| 1946 | /* clear DAI data */ |
| 1947 | set_stream(substream, NULL); |
| 1948 | } |
| 1949 | EXPORT_SYMBOL(sdw_shutdown_stream); |