1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Microchip KSZ8795 series register access through SPI
5 * Copyright (C) 2017 Microchip Technology Inc.
6 * Tristram Ha <Tristram.Ha@microchip.com>
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
;
32 dev
= ksz_switch_alloc(&spi
->dev
, spi
);
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
]);
43 "Failed to initialize regmap%i: %d\n",
44 ksz8795_regmap_config
[i
].val_bits
, 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. */
58 spi_set_drvdata(spi
, dev
);
63 static int ksz8795_spi_remove(struct spi_device
*spi
)
65 struct ksz_device
*dev
= spi_get_drvdata(spi
);
68 ksz_switch_remove(dev
);
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" },
87 MODULE_DEVICE_TABLE(of
, ksz8795_dt_ids
);
89 static struct spi_driver ksz8795_spi_driver
= {
91 .name
= "ksz8795-switch",
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");