2 * Microchip KSZ series register access through SPI
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <asm/unaligned.h>
21 #include <linux/delay.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/spi/spi.h>
28 /* SPI frame opcodes */
32 #define SPI_ADDR_SHIFT 24
33 #define SPI_ADDR_MASK (BIT(SPI_ADDR_SHIFT) - 1)
34 #define SPI_TURNAROUND_SHIFT 5
36 static int ksz_spi_read_reg(struct spi_device
*spi
, u32 reg
, u8
*val
,
42 txbuf
= reg
& SPI_ADDR_MASK
;
43 txbuf
|= KS_SPIOP_RD
<< SPI_ADDR_SHIFT
;
44 txbuf
<<= SPI_TURNAROUND_SHIFT
;
45 txbuf
= cpu_to_be32(txbuf
);
47 ret
= spi_write_then_read(spi
, &txbuf
, 4, val
, len
);
51 static int ksz_spi_read(struct ksz_device
*dev
, u32 reg
, u8
*data
,
54 struct spi_device
*spi
= dev
->priv
;
56 return ksz_spi_read_reg(spi
, reg
, data
, len
);
59 static int ksz_spi_read8(struct ksz_device
*dev
, u32 reg
, u8
*val
)
61 return ksz_spi_read(dev
, reg
, val
, 1);
64 static int ksz_spi_read16(struct ksz_device
*dev
, u32 reg
, u16
*val
)
66 int ret
= ksz_spi_read(dev
, reg
, (u8
*)val
, 2);
69 *val
= be16_to_cpu(*val
);
74 static int ksz_spi_read24(struct ksz_device
*dev
, u32 reg
, u32
*val
)
79 ret
= ksz_spi_read(dev
, reg
, (u8
*)val
, 3);
81 *val
= be32_to_cpu(*val
);
82 /* convert to 24bit */
89 static int ksz_spi_read32(struct ksz_device
*dev
, u32 reg
, u32
*val
)
91 int ret
= ksz_spi_read(dev
, reg
, (u8
*)val
, 4);
94 *val
= be32_to_cpu(*val
);
99 static int ksz_spi_write_reg(struct spi_device
*spi
, u32 reg
, u8
*val
,
106 txbuf
= reg
& SPI_ADDR_MASK
;
107 txbuf
|= (KS_SPIOP_WR
<< SPI_ADDR_SHIFT
);
108 txbuf
<<= SPI_TURNAROUND_SHIFT
;
109 txbuf
= cpu_to_be32(txbuf
);
111 data
[0] = txbuf
& 0xFF;
112 data
[1] = (txbuf
& 0xFF00) >> 8;
113 data
[2] = (txbuf
& 0xFF0000) >> 16;
114 data
[3] = (txbuf
& 0xFF000000) >> 24;
115 for (i
= 0; i
< len
; i
++)
116 data
[i
+ 4] = val
[i
];
118 return spi_write(spi
, &data
, 4 + len
);
121 static int ksz_spi_write8(struct ksz_device
*dev
, u32 reg
, u8 value
)
123 struct spi_device
*spi
= dev
->priv
;
125 return ksz_spi_write_reg(spi
, reg
, &value
, 1);
128 static int ksz_spi_write16(struct ksz_device
*dev
, u32 reg
, u16 value
)
130 struct spi_device
*spi
= dev
->priv
;
132 value
= cpu_to_be16(value
);
133 return ksz_spi_write_reg(spi
, reg
, (u8
*)&value
, 2);
136 static int ksz_spi_write24(struct ksz_device
*dev
, u32 reg
, u32 value
)
138 struct spi_device
*spi
= dev
->priv
;
140 /* make it to big endian 24bit from MSB */
142 value
= cpu_to_be32(value
);
143 return ksz_spi_write_reg(spi
, reg
, (u8
*)&value
, 3);
146 static int ksz_spi_write32(struct ksz_device
*dev
, u32 reg
, u32 value
)
148 struct spi_device
*spi
= dev
->priv
;
150 value
= cpu_to_be32(value
);
151 return ksz_spi_write_reg(spi
, reg
, (u8
*)&value
, 4);
154 static const struct ksz_io_ops ksz_spi_ops
= {
155 .read8
= ksz_spi_read8
,
156 .read16
= ksz_spi_read16
,
157 .read24
= ksz_spi_read24
,
158 .read32
= ksz_spi_read32
,
159 .write8
= ksz_spi_write8
,
160 .write16
= ksz_spi_write16
,
161 .write24
= ksz_spi_write24
,
162 .write32
= ksz_spi_write32
,
165 static int ksz_spi_probe(struct spi_device
*spi
)
167 struct ksz_device
*dev
;
170 dev
= ksz_switch_alloc(&spi
->dev
, &ksz_spi_ops
, spi
);
174 if (spi
->dev
.platform_data
)
175 dev
->pdata
= spi
->dev
.platform_data
;
177 ret
= ksz_switch_register(dev
);
181 spi_set_drvdata(spi
, dev
);
186 static int ksz_spi_remove(struct spi_device
*spi
)
188 struct ksz_device
*dev
= spi_get_drvdata(spi
);
191 ksz_switch_remove(dev
);
196 static const struct of_device_id ksz_dt_ids
[] = {
197 { .compatible
= "microchip,ksz9477" },
198 { .compatible
= "microchip,ksz9897" },
201 MODULE_DEVICE_TABLE(of
, ksz_dt_ids
);
203 static struct spi_driver ksz_spi_driver
= {
205 .name
= "ksz9477-switch",
206 .owner
= THIS_MODULE
,
207 .of_match_table
= of_match_ptr(ksz_dt_ids
),
209 .probe
= ksz_spi_probe
,
210 .remove
= ksz_spi_remove
,
213 module_spi_driver(ksz_spi_driver
);
215 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
216 MODULE_DESCRIPTION("Microchip KSZ Series Switch SPI access Driver");
217 MODULE_LICENSE("GPL");