treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / dsa / microchip / ksz8795_spi.c
blob8b00f8e6c02f4f2a2fbc545c026cb2daaa48528c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Microchip KSZ8795 series register access through SPI
5 * Copyright (C) 2017 Microchip Technology Inc.
6 * Tristram Ha <Tristram.Ha@microchip.com>
7 */
9 #include <asm/unaligned.h>
11 #include <linux/delay.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/spi/spi.h>
17 #include "ksz_common.h"
19 #define SPI_ADDR_SHIFT 12
20 #define SPI_ADDR_ALIGN 3
21 #define SPI_TURNAROUND_SHIFT 1
23 KSZ_REGMAP_TABLE(ksz8795, 16, SPI_ADDR_SHIFT,
24 SPI_TURNAROUND_SHIFT, SPI_ADDR_ALIGN);
26 static int ksz8795_spi_probe(struct spi_device *spi)
28 struct regmap_config rc;
29 struct ksz_device *dev;
30 int i, ret;
32 dev = ksz_switch_alloc(&spi->dev, spi);
33 if (!dev)
34 return -ENOMEM;
36 for (i = 0; i < ARRAY_SIZE(ksz8795_regmap_config); i++) {
37 rc = ksz8795_regmap_config[i];
38 rc.lock_arg = &dev->regmap_mutex;
39 dev->regmap[i] = devm_regmap_init_spi(spi, &rc);
40 if (IS_ERR(dev->regmap[i])) {
41 ret = PTR_ERR(dev->regmap[i]);
42 dev_err(&spi->dev,
43 "Failed to initialize regmap%i: %d\n",
44 ksz8795_regmap_config[i].val_bits, ret);
45 return ret;
49 if (spi->dev.platform_data)
50 dev->pdata = spi->dev.platform_data;
52 ret = ksz8795_switch_register(dev);
54 /* Main DSA driver may not be started yet. */
55 if (ret)
56 return ret;
58 spi_set_drvdata(spi, dev);
60 return 0;
63 static int ksz8795_spi_remove(struct spi_device *spi)
65 struct ksz_device *dev = spi_get_drvdata(spi);
67 if (dev)
68 ksz_switch_remove(dev);
70 return 0;
73 static void ksz8795_spi_shutdown(struct spi_device *spi)
75 struct ksz_device *dev = spi_get_drvdata(spi);
77 if (dev && dev->dev_ops->shutdown)
78 dev->dev_ops->shutdown(dev);
81 static const struct of_device_id ksz8795_dt_ids[] = {
82 { .compatible = "microchip,ksz8765" },
83 { .compatible = "microchip,ksz8794" },
84 { .compatible = "microchip,ksz8795" },
85 {},
87 MODULE_DEVICE_TABLE(of, ksz8795_dt_ids);
89 static struct spi_driver ksz8795_spi_driver = {
90 .driver = {
91 .name = "ksz8795-switch",
92 .owner = THIS_MODULE,
93 .of_match_table = of_match_ptr(ksz8795_dt_ids),
95 .probe = ksz8795_spi_probe,
96 .remove = ksz8795_spi_remove,
97 .shutdown = ksz8795_spi_shutdown,
100 module_spi_driver(ksz8795_spi_driver);
102 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
103 MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch SPI Driver");
104 MODULE_LICENSE("GPL");