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