1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip KSZ9477 series register access through SPI
5 * Copyright (C) 2017-2019 Microchip Technology Inc.
8 #include <asm/unaligned.h>
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/spi/spi.h>
16 #include "ksz_common.h"
18 #define SPI_ADDR_SHIFT 24
19 #define SPI_ADDR_ALIGN 3
20 #define SPI_TURNAROUND_SHIFT 5
22 KSZ_REGMAP_TABLE(ksz9477
, 32, SPI_ADDR_SHIFT
,
23 SPI_TURNAROUND_SHIFT
, SPI_ADDR_ALIGN
);
25 static int ksz9477_spi_probe(struct spi_device
*spi
)
27 struct regmap_config rc
;
28 struct ksz_device
*dev
;
31 dev
= ksz_switch_alloc(&spi
->dev
, spi
);
35 for (i
= 0; i
< ARRAY_SIZE(ksz9477_regmap_config
); i
++) {
36 rc
= ksz9477_regmap_config
[i
];
37 rc
.lock_arg
= &dev
->regmap_mutex
;
38 dev
->regmap
[i
] = devm_regmap_init_spi(spi
, &rc
);
39 if (IS_ERR(dev
->regmap
[i
])) {
40 ret
= PTR_ERR(dev
->regmap
[i
]);
42 "Failed to initialize regmap%i: %d\n",
43 ksz9477_regmap_config
[i
].val_bits
, ret
);
48 if (spi
->dev
.platform_data
)
49 dev
->pdata
= spi
->dev
.platform_data
;
52 spi
->mode
= SPI_MODE_3
;
57 ret
= ksz9477_switch_register(dev
);
59 /* Main DSA driver may not be started yet. */
63 spi_set_drvdata(spi
, dev
);
68 static int ksz9477_spi_remove(struct spi_device
*spi
)
70 struct ksz_device
*dev
= spi_get_drvdata(spi
);
73 ksz_switch_remove(dev
);
78 static void ksz9477_spi_shutdown(struct spi_device
*spi
)
80 struct ksz_device
*dev
= spi_get_drvdata(spi
);
82 if (dev
&& dev
->dev_ops
->shutdown
)
83 dev
->dev_ops
->shutdown(dev
);
86 static const struct of_device_id ksz9477_dt_ids
[] = {
87 { .compatible
= "microchip,ksz9477" },
88 { .compatible
= "microchip,ksz9897" },
89 { .compatible
= "microchip,ksz9893" },
90 { .compatible
= "microchip,ksz9563" },
91 { .compatible
= "microchip,ksz8563" },
92 { .compatible
= "microchip,ksz9567" },
95 MODULE_DEVICE_TABLE(of
, ksz9477_dt_ids
);
97 static struct spi_driver ksz9477_spi_driver
= {
99 .name
= "ksz9477-switch",
100 .owner
= THIS_MODULE
,
101 .of_match_table
= of_match_ptr(ksz9477_dt_ids
),
103 .probe
= ksz9477_spi_probe
,
104 .remove
= ksz9477_spi_remove
,
105 .shutdown
= ksz9477_spi_shutdown
,
108 module_spi_driver(ksz9477_spi_driver
);
110 MODULE_ALIAS("spi:ksz9477");
111 MODULE_ALIAS("spi:ksz9897");
112 MODULE_ALIAS("spi:ksz9893");
113 MODULE_ALIAS("spi:ksz9563");
114 MODULE_ALIAS("spi:ksz8563");
115 MODULE_ALIAS("spi:ksz9567");
116 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
117 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch SPI access Driver");
118 MODULE_LICENSE("GPL");