Skip to content

Commit d7ad87d

Browse files
committed
spi: microchip-core: Code improvements
Merge series from Andy Shevchenko <andriy.shevchenko@linux.intel.com>: While reading some other stuff, I noticed that this driver may be improved. Here is the set of refactoring and cleaning it up.
2 parents a9277a8 + f458fc9 commit d7ad87d

1 file changed

Lines changed: 28 additions & 36 deletions

File tree

drivers/spi/spi-microchip-core-spi.c

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ struct mchp_corespi {
7474
u8 *rx_buf;
7575
u32 clk_gen;
7676
int irq;
77-
int tx_len;
78-
int rx_len;
77+
unsigned int tx_len;
78+
unsigned int rx_len;
7979
u32 fifo_depth;
8080
};
8181

@@ -160,8 +160,6 @@ static void mchp_corespi_set_cs(struct spi_device *spi, bool disable)
160160

161161
static int mchp_corespi_setup(struct spi_device *spi)
162162
{
163-
u32 dev_mode = spi->mode & (SPI_CPOL | SPI_CPHA);
164-
165163
if (spi_get_csgpiod(spi, 0))
166164
return 0;
167165

@@ -170,7 +168,7 @@ static int mchp_corespi_setup(struct spi_device *spi)
170168
return -EOPNOTSUPP;
171169
}
172170

173-
if (dev_mode & ~spi->controller->mode_bits) {
171+
if (spi->mode & SPI_MODE_X_MASK & ~spi->controller->mode_bits) {
174172
dev_err(&spi->dev, "incompatible CPOL/CPHA, must match controller's Motorola mode\n");
175173
return -EINVAL;
176174
}
@@ -214,7 +212,7 @@ static irqreturn_t mchp_corespi_interrupt(int irq, void *dev_id)
214212
spi->regs + MCHP_CORESPI_REG_INTCLEAR);
215213
finalise = true;
216214
dev_err(&host->dev,
217-
"RX OVERFLOW: rxlen: %d, txlen: %d\n",
215+
"RX OVERFLOW: rxlen: %u, txlen: %u\n",
218216
spi->rx_len, spi->tx_len);
219217
}
220218

@@ -223,7 +221,7 @@ static irqreturn_t mchp_corespi_interrupt(int irq, void *dev_id)
223221
spi->regs + MCHP_CORESPI_REG_INTCLEAR);
224222
finalise = true;
225223
dev_err(&host->dev,
226-
"TX UNDERFLOW: rxlen: %d, txlen: %d\n",
224+
"TX UNDERFLOW: rxlen: %u, txlen: %u\n",
227225
spi->rx_len, spi->tx_len);
228226
}
229227

@@ -283,7 +281,7 @@ static int mchp_corespi_transfer_one(struct spi_controller *host,
283281
spi->rx_len = xfer->len;
284282

285283
while (spi->tx_len) {
286-
int fifo_max = min_t(int, spi->tx_len, spi->fifo_depth);
284+
unsigned int fifo_max = min(spi->tx_len, spi->fifo_depth);
287285

288286
mchp_corespi_write_fifo(spi, fifo_max);
289287
mchp_corespi_read_fifo(spi, fifo_max);
@@ -296,56 +294,56 @@ static int mchp_corespi_transfer_one(struct spi_controller *host,
296294
static int mchp_corespi_probe(struct platform_device *pdev)
297295
{
298296
const char *protocol = "motorola";
297+
struct device *dev = &pdev->dev;
299298
struct spi_controller *host;
300299
struct mchp_corespi *spi;
301300
struct resource *res;
302301
u32 num_cs, mode, frame_size;
303302
bool assert_ssel;
304303
int ret = 0;
305304

306-
host = devm_spi_alloc_host(&pdev->dev, sizeof(*spi));
305+
host = devm_spi_alloc_host(dev, sizeof(*spi));
307306
if (!host)
308-
return dev_err_probe(&pdev->dev, -ENOMEM,
309-
"unable to allocate host for SPI controller\n");
307+
return -ENOMEM;
310308

311309
platform_set_drvdata(pdev, host);
312310

313-
if (of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs))
311+
if (of_property_read_u32(dev->of_node, "num-cs", &num_cs))
314312
num_cs = MCHP_CORESPI_MAX_CS;
315313

316314
/*
317315
* Protocol: CFG_MODE
318316
* CoreSPI can be configured for Motorola, TI or NSC.
319317
* The current driver supports only Motorola mode.
320318
*/
321-
ret = of_property_read_string(pdev->dev.of_node, "microchip,protocol-configuration",
319+
ret = of_property_read_string(dev->of_node, "microchip,protocol-configuration",
322320
&protocol);
323321
if (ret && ret != -EINVAL)
324-
return dev_err_probe(&pdev->dev, ret, "Error reading protocol-configuration\n");
322+
return dev_err_probe(dev, ret, "Error reading protocol-configuration\n");
325323
if (strcmp(protocol, "motorola") != 0)
326-
return dev_err_probe(&pdev->dev, -EINVAL,
324+
return dev_err_probe(dev, -EINVAL,
327325
"CoreSPI: protocol '%s' not supported by this driver\n",
328326
protocol);
329327

330328
/*
331329
* Motorola mode (0-3): CFG_MOT_MODE
332330
* Mode is fixed in the IP configurator.
333331
*/
334-
ret = of_property_read_u32(pdev->dev.of_node, "microchip,motorola-mode", &mode);
332+
ret = of_property_read_u32(dev->of_node, "microchip,motorola-mode", &mode);
335333
if (ret)
336334
mode = MCHP_CORESPI_DEFAULT_MOTOROLA_MODE;
337335
else if (mode > 3)
338-
return dev_err_probe(&pdev->dev, -EINVAL,
336+
return dev_err_probe(dev, -EINVAL,
339337
"invalid 'microchip,motorola-mode' value %u\n", mode);
340338

341339
/*
342340
* Frame size: CFG_FRAME_SIZE
343341
* The hardware allows frame sizes <= APB data width.
344342
* However, this driver currently only supports 8-bit frames.
345343
*/
346-
ret = of_property_read_u32(pdev->dev.of_node, "microchip,frame-size", &frame_size);
344+
ret = of_property_read_u32(dev->of_node, "microchip,frame-size", &frame_size);
347345
if (!ret && frame_size != 8)
348-
return dev_err_probe(&pdev->dev, -EINVAL,
346+
return dev_err_probe(dev, -EINVAL,
349347
"CoreSPI: frame size %u not supported by this driver\n",
350348
frame_size);
351349

@@ -355,9 +353,9 @@ static int mchp_corespi_probe(struct platform_device *pdev)
355353
* To prevent CS deassertion when TX FIFO drains, the ssel-active property
356354
* keeps CS asserted for the full SPI transfer.
357355
*/
358-
assert_ssel = of_property_read_bool(pdev->dev.of_node, "microchip,ssel-active");
356+
assert_ssel = of_property_read_bool(dev->of_node, "microchip,ssel-active");
359357
if (!assert_ssel)
360-
return dev_err_probe(&pdev->dev, -EINVAL,
358+
return dev_err_probe(dev, -EINVAL,
361359
"hardware must enable 'microchip,ssel-active' to keep CS asserted for the SPI transfer\n");
362360

363361
spi = spi_controller_get_devdata(host);
@@ -369,9 +367,9 @@ static int mchp_corespi_probe(struct platform_device *pdev)
369367
host->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
370368
host->transfer_one = mchp_corespi_transfer_one;
371369
host->set_cs = mchp_corespi_set_cs;
372-
host->dev.of_node = pdev->dev.of_node;
370+
host->dev.of_node = dev->of_node;
373371

374-
ret = of_property_read_u32(pdev->dev.of_node, "fifo-depth", &spi->fifo_depth);
372+
ret = of_property_read_u32(dev->of_node, "fifo-depth", &spi->fifo_depth);
375373
if (ret)
376374
spi->fifo_depth = MCHP_CORESPI_DEFAULT_FIFO_DEPTH;
377375

@@ -383,24 +381,21 @@ static int mchp_corespi_probe(struct platform_device *pdev)
383381
if (spi->irq < 0)
384382
return spi->irq;
385383

386-
ret = devm_request_irq(&pdev->dev, spi->irq, mchp_corespi_interrupt,
387-
IRQF_SHARED, dev_name(&pdev->dev), host);
384+
ret = devm_request_irq(dev, spi->irq, mchp_corespi_interrupt, IRQF_SHARED,
385+
dev_name(dev), host);
388386
if (ret)
389-
return dev_err_probe(&pdev->dev, ret,
390-
"could not request irq\n");
387+
return dev_err_probe(dev, ret, "could not request irq\n");
391388

392-
spi->clk = devm_clk_get_enabled(&pdev->dev, NULL);
389+
spi->clk = devm_clk_get_enabled(dev, NULL);
393390
if (IS_ERR(spi->clk))
394-
return dev_err_probe(&pdev->dev, PTR_ERR(spi->clk),
395-
"could not get clk\n");
391+
return dev_err_probe(dev, PTR_ERR(spi->clk), "could not get clk\n");
396392

397393
mchp_corespi_init(host, spi);
398394

399-
ret = devm_spi_register_controller(&pdev->dev, host);
395+
ret = devm_spi_register_controller(dev, host);
400396
if (ret) {
401397
mchp_corespi_disable(spi);
402-
return dev_err_probe(&pdev->dev, ret,
403-
"unable to register host for CoreSPI controller\n");
398+
return dev_err_probe(dev, ret, "unable to register host for CoreSPI controller\n");
404399
}
405400

406401
return 0;
@@ -415,8 +410,6 @@ static void mchp_corespi_remove(struct platform_device *pdev)
415410
mchp_corespi_disable(spi);
416411
}
417412

418-
#define MICROCHIP_SPI_PM_OPS (NULL)
419-
420413
/*
421414
* Platform driver data structure
422415
*/
@@ -433,7 +426,6 @@ static struct platform_driver mchp_corespi_driver = {
433426
.probe = mchp_corespi_probe,
434427
.driver = {
435428
.name = "microchip-corespi",
436-
.pm = MICROCHIP_SPI_PM_OPS,
437429
.of_match_table = of_match_ptr(mchp_corespi_dt_ids),
438430
},
439431
.remove = mchp_corespi_remove,

0 commit comments

Comments
 (0)