Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi: mt7621: allow GPIO chip select lines

Extract a magic number, from mt7621_spi_probe(), used to
declare the number of chip select lines (which co-incides
with the native chip select count of 2) to a macro.

Use the newly defined MT7621_NATIVE_CS_COUNT macro to
instead populate both the spi_controller's max_native_cs
and num_chipselect members.

Declare that the spi_controller should use_gpio_descriptors
if present in the device properties (such as those declared
in the cs-gpio property of a "ralink,mt7621-spi" compatible
device-tree node) so that the SPI core will recalculcate
num_chipselect to account for the GPIO descriptors that
it should have populated in the cs_gpiod array member.

Remove the assignment of mt7621_spi_transfer_one_message()
to the spi_controller's transfer_one_message hook.

Refactor the mt7621_spi_transfer_one_message() logic into
mt7621_spi_prepare_message() and mt7621_spi_transfer_one()
and assign both to the spi_controller's prepare_message
and transfer_one hooks respectively.

Migrate the call mt7621_spi_transfer_one_message() made to
mt7621_spi_flush() just before chip select deactivation,
to the end of mt7621_spi_write_half_duplex() to ensure
that any pending data is shifted out of MOSI before the SPI
core deactivates the chip select line.

As chip select activation is now taken care of by the SPI
core, due to the use of the transfer_one hook instead of
transfer_one_message, the calls to mt7621_spi_set_cs()
from mt7621_spi_transfer_one_message() have fallen away.

And although the SPI core will handle activation for GPIO
chip select lines behind the scenes, it requires a callback
to allow the driver to perform controller-specific
operations to control its native chip select lines.

Rename mt7621_spi_set_cs() to mt7621_spi_set_native_cs()
and make sure that it takes into account the activation
polarity of the chip select line it's acting upon, as the
passed enable parameter represents the desired line level
and not the desired activation state, and then assign
mt7621_set_cs() to the spi_controller's set_cs hook.

Signed-off-by: Justin Swartz <justin.swartz@risingedge.co.za>
Link: https://msgid.link/r/20240316010302.20776-1-justin.swartz@risingedge.co.za
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Justin Swartz and committed by
Mark Brown
2a741cd6 4cece764

+45 -50
+45 -50
drivers/spi/spi-mt7621.c
··· 52 52 #define MT7621_CPOL BIT(4) 53 53 #define MT7621_LSB_FIRST BIT(3) 54 54 55 + #define MT7621_NATIVE_CS_COUNT 2 56 + 55 57 struct mt7621_spi { 56 58 struct spi_controller *host; 57 59 void __iomem *base; ··· 77 75 iowrite32(val, rs->base + reg); 78 76 } 79 77 80 - static void mt7621_spi_set_cs(struct spi_device *spi, int enable) 78 + static void mt7621_spi_set_native_cs(struct spi_device *spi, bool enable) 81 79 { 82 80 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi); 83 81 int cs = spi_get_chipselect(spi, 0); 82 + bool active = spi->mode & SPI_CS_HIGH ? enable : !enable; 84 83 u32 polar = 0; 85 84 u32 host; 86 85 ··· 97 94 98 95 rs->pending_write = 0; 99 96 100 - if (enable) 97 + if (active) 101 98 polar = BIT(cs); 102 99 mt7621_spi_write(rs, MT7621_SPI_POLAR, polar); 103 100 } ··· 155 152 } 156 153 157 154 return -ETIMEDOUT; 155 + } 156 + 157 + static int mt7621_spi_prepare_message(struct spi_controller *host, 158 + struct spi_message *m) 159 + { 160 + struct mt7621_spi *rs = spi_controller_get_devdata(host); 161 + struct spi_device *spi = m->spi; 162 + unsigned int speed = spi->max_speed_hz; 163 + struct spi_transfer *t = NULL; 164 + 165 + mt7621_spi_wait_till_ready(rs); 166 + 167 + list_for_each_entry(t, &m->transfers, transfer_list) 168 + if (t->speed_hz < speed) 169 + speed = t->speed_hz; 170 + 171 + return mt7621_spi_prepare(spi, speed); 158 172 } 159 173 160 174 static void mt7621_spi_read_half_duplex(struct mt7621_spi *rs, ··· 263 243 } 264 244 265 245 rs->pending_write = len; 246 + mt7621_spi_flush(rs); 266 247 } 267 248 268 - static int mt7621_spi_transfer_one_message(struct spi_controller *host, 269 - struct spi_message *m) 249 + static int mt7621_spi_transfer_one(struct spi_controller *host, 250 + struct spi_device *spi, 251 + struct spi_transfer *t) 270 252 { 271 253 struct mt7621_spi *rs = spi_controller_get_devdata(host); 272 - struct spi_device *spi = m->spi; 273 - unsigned int speed = spi->max_speed_hz; 274 - struct spi_transfer *t = NULL; 275 - int status = 0; 276 254 277 - mt7621_spi_wait_till_ready(rs); 278 - 279 - list_for_each_entry(t, &m->transfers, transfer_list) 280 - if (t->speed_hz < speed) 281 - speed = t->speed_hz; 282 - 283 - if (mt7621_spi_prepare(spi, speed)) { 284 - status = -EIO; 285 - goto msg_done; 255 + if ((t->rx_buf) && (t->tx_buf)) { 256 + /* 257 + * This controller will shift some extra data out 258 + * of spi_opcode if (mosi_bit_cnt > 0) && 259 + * (cmd_bit_cnt == 0). So the claimed full-duplex 260 + * support is broken since we have no way to read 261 + * the MISO value during that bit. 262 + */ 263 + return -EIO; 264 + } else if (t->rx_buf) { 265 + mt7621_spi_read_half_duplex(rs, t->len, t->rx_buf); 266 + } else if (t->tx_buf) { 267 + mt7621_spi_write_half_duplex(rs, t->len, t->tx_buf); 286 268 } 287 - 288 - /* Assert CS */ 289 - mt7621_spi_set_cs(spi, 1); 290 - 291 - m->actual_length = 0; 292 - list_for_each_entry(t, &m->transfers, transfer_list) { 293 - if ((t->rx_buf) && (t->tx_buf)) { 294 - /* 295 - * This controller will shift some extra data out 296 - * of spi_opcode if (mosi_bit_cnt > 0) && 297 - * (cmd_bit_cnt == 0). So the claimed full-duplex 298 - * support is broken since we have no way to read 299 - * the MISO value during that bit. 300 - */ 301 - status = -EIO; 302 - goto msg_done; 303 - } else if (t->rx_buf) { 304 - mt7621_spi_read_half_duplex(rs, t->len, t->rx_buf); 305 - } else if (t->tx_buf) { 306 - mt7621_spi_write_half_duplex(rs, t->len, t->tx_buf); 307 - } 308 - m->actual_length += t->len; 309 - } 310 - 311 - /* Flush data and deassert CS */ 312 - mt7621_spi_flush(rs); 313 - mt7621_spi_set_cs(spi, 0); 314 - 315 - msg_done: 316 - m->status = status; 317 - spi_finalize_current_message(host); 318 269 319 270 return 0; 320 271 } ··· 344 353 host->mode_bits = SPI_LSB_FIRST; 345 354 host->flags = SPI_CONTROLLER_HALF_DUPLEX; 346 355 host->setup = mt7621_spi_setup; 347 - host->transfer_one_message = mt7621_spi_transfer_one_message; 356 + host->prepare_message = mt7621_spi_prepare_message; 357 + host->set_cs = mt7621_spi_set_native_cs; 358 + host->transfer_one = mt7621_spi_transfer_one; 348 359 host->bits_per_word_mask = SPI_BPW_MASK(8); 349 360 host->dev.of_node = pdev->dev.of_node; 350 - host->num_chipselect = 2; 361 + host->max_native_cs = MT7621_NATIVE_CS_COUNT; 362 + host->num_chipselect = MT7621_NATIVE_CS_COUNT; 363 + host->use_gpio_descriptors = true; 351 364 352 365 dev_set_drvdata(&pdev->dev, host); 353 366