1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel La Jolla Cove Adapter USB-SPI driver
5 * Copyright (c) 2023, Intel Corporation.
8 #include <linux/auxiliary_bus.h>
9 #include <linux/bitfield.h>
10 #include <linux/bits.h>
11 #include <linux/dev_printk.h>
12 #include <linux/module.h>
13 #include <linux/spi/spi.h>
14 #include <linux/usb/ljca.h>
16 #define LJCA_SPI_BUS_MAX_HZ 48000000
18 #define LJCA_SPI_BUF_SIZE 60u
19 #define LJCA_SPI_MAX_XFER_SIZE \
20 (LJCA_SPI_BUF_SIZE - sizeof(struct ljca_spi_xfer_packet))
22 #define LJCA_SPI_CLK_MODE_POLARITY BIT(0)
23 #define LJCA_SPI_CLK_MODE_PHASE BIT(1)
25 #define LJCA_SPI_XFER_INDICATOR_ID GENMASK(5, 0)
26 #define LJCA_SPI_XFER_INDICATOR_CMPL BIT(6)
27 #define LJCA_SPI_XFER_INDICATOR_INDEX BIT(7)
39 LJCA_SPI_BUS_SPEED_24M
,
40 LJCA_SPI_BUS_SPEED_12M
,
41 LJCA_SPI_BUS_SPEED_8M
,
42 LJCA_SPI_BUS_SPEED_6M
,
43 LJCA_SPI_BUS_SPEED_4_8M
, /*4.8MHz*/
44 LJCA_SPI_BUS_SPEED_MIN
= LJCA_SPI_BUS_SPEED_4_8M
,
48 LJCA_SPI_CLOCK_LOW_POLARITY
,
49 LJCA_SPI_CLOCK_HIGH_POLARITY
,
53 LJCA_SPI_CLOCK_FIRST_PHASE
,
54 LJCA_SPI_CLOCK_SECOND_PHASE
,
57 struct ljca_spi_init_packet
{
63 struct ljca_spi_xfer_packet
{
66 u8 data
[] __counted_by(len
);
70 struct ljca_client
*ljca
;
71 struct spi_controller
*controller
;
72 struct ljca_spi_info
*spi_info
;
76 u8 obuf
[LJCA_SPI_BUF_SIZE
];
77 u8 ibuf
[LJCA_SPI_BUF_SIZE
];
80 static int ljca_spi_read_write(struct ljca_spi_dev
*ljca_spi
, const u8
*w_data
,
81 u8
*r_data
, int len
, int id
, int complete
,
84 struct ljca_spi_xfer_packet
*w_packet
=
85 (struct ljca_spi_xfer_packet
*)ljca_spi
->obuf
;
86 struct ljca_spi_xfer_packet
*r_packet
=
87 (struct ljca_spi_xfer_packet
*)ljca_spi
->ibuf
;
90 w_packet
->indicator
= FIELD_PREP(LJCA_SPI_XFER_INDICATOR_ID
, id
) |
91 FIELD_PREP(LJCA_SPI_XFER_INDICATOR_CMPL
, complete
) |
92 FIELD_PREP(LJCA_SPI_XFER_INDICATOR_INDEX
,
93 ljca_spi
->spi_info
->id
);
95 if (cmd
== LJCA_SPI_READ
) {
96 w_packet
->len
= sizeof(u16
);
97 *(__le16
*)&w_packet
->data
[0] = cpu_to_le16(len
);
100 memcpy(w_packet
->data
, w_data
, len
);
103 ret
= ljca_transfer(ljca_spi
->ljca
, cmd
, (u8
*)w_packet
,
104 struct_size(w_packet
, data
, w_packet
->len
),
105 (u8
*)r_packet
, LJCA_SPI_BUF_SIZE
);
108 else if (ret
< sizeof(*r_packet
) || r_packet
->len
<= 0)
112 memcpy(r_data
, r_packet
->data
, r_packet
->len
);
117 static int ljca_spi_init(struct ljca_spi_dev
*ljca_spi
, u8 div
, u8 mode
)
119 struct ljca_spi_init_packet w_packet
= {};
122 if (ljca_spi
->mode
== mode
&& ljca_spi
->speed
== div
)
125 w_packet
.index
= ljca_spi
->spi_info
->id
;
126 w_packet
.speed
= div
;
127 w_packet
.mode
= FIELD_PREP(LJCA_SPI_CLK_MODE_POLARITY
,
128 (mode
& SPI_CPOL
) ? LJCA_SPI_CLOCK_HIGH_POLARITY
:
129 LJCA_SPI_CLOCK_LOW_POLARITY
) |
130 FIELD_PREP(LJCA_SPI_CLK_MODE_PHASE
,
131 (mode
& SPI_CPHA
) ? LJCA_SPI_CLOCK_SECOND_PHASE
:
132 LJCA_SPI_CLOCK_FIRST_PHASE
);
134 ret
= ljca_transfer(ljca_spi
->ljca
, LJCA_SPI_INIT
, (u8
*)&w_packet
,
135 sizeof(w_packet
), NULL
, 0);
139 ljca_spi
->mode
= mode
;
140 ljca_spi
->speed
= div
;
145 static int ljca_spi_deinit(struct ljca_spi_dev
*ljca_spi
)
147 struct ljca_spi_init_packet w_packet
= {};
150 w_packet
.index
= ljca_spi
->spi_info
->id
;
152 ret
= ljca_transfer(ljca_spi
->ljca
, LJCA_SPI_DEINIT
, (u8
*)&w_packet
,
153 sizeof(w_packet
), NULL
, 0);
155 return ret
< 0 ? ret
: 0;
158 static inline int ljca_spi_transfer(struct ljca_spi_dev
*ljca_spi
,
159 const u8
*tx_data
, u8
*rx_data
, u16 len
)
161 int complete
, cur_len
;
166 if (tx_data
&& rx_data
)
167 cmd
= LJCA_SPI_WRITEREAD
;
169 cmd
= LJCA_SPI_WRITE
;
175 for (i
= 0; remaining
> 0; i
++) {
176 cur_len
= min_t(unsigned int, remaining
, LJCA_SPI_MAX_XFER_SIZE
);
177 complete
= (cur_len
== remaining
);
179 ret
= ljca_spi_read_write(ljca_spi
,
180 tx_data
? tx_data
+ offset
: NULL
,
181 rx_data
? rx_data
+ offset
: NULL
,
182 cur_len
, i
, complete
, cmd
);
187 remaining
-= cur_len
;
193 static int ljca_spi_transfer_one(struct spi_controller
*controller
,
194 struct spi_device
*spi
,
195 struct spi_transfer
*xfer
)
197 u8 div
= DIV_ROUND_UP(controller
->max_speed_hz
, xfer
->speed_hz
) / 2 - 1;
198 struct ljca_spi_dev
*ljca_spi
= spi_controller_get_devdata(controller
);
201 div
= min_t(u8
, LJCA_SPI_BUS_SPEED_MIN
, div
);
203 ret
= ljca_spi_init(ljca_spi
, div
, spi
->mode
);
205 dev_err(&ljca_spi
->ljca
->auxdev
.dev
,
206 "cannot initialize transfer ret %d\n", ret
);
210 ret
= ljca_spi_transfer(ljca_spi
, xfer
->tx_buf
, xfer
->rx_buf
, xfer
->len
);
212 dev_err(&ljca_spi
->ljca
->auxdev
.dev
,
213 "transfer failed len: %d\n", xfer
->len
);
218 static int ljca_spi_probe(struct auxiliary_device
*auxdev
,
219 const struct auxiliary_device_id
*aux_dev_id
)
221 struct ljca_client
*ljca
= auxiliary_dev_to_ljca_client(auxdev
);
222 struct spi_controller
*controller
;
223 struct ljca_spi_dev
*ljca_spi
;
226 controller
= devm_spi_alloc_host(&auxdev
->dev
, sizeof(*ljca_spi
));
230 ljca_spi
= spi_controller_get_devdata(controller
);
231 ljca_spi
->ljca
= ljca
;
232 ljca_spi
->spi_info
= dev_get_platdata(&auxdev
->dev
);
233 ljca_spi
->controller
= controller
;
235 controller
->bus_num
= -1;
236 controller
->mode_bits
= SPI_CPHA
| SPI_CPOL
;
237 controller
->transfer_one
= ljca_spi_transfer_one
;
238 controller
->auto_runtime_pm
= false;
239 controller
->max_speed_hz
= LJCA_SPI_BUS_MAX_HZ
;
241 device_set_node(&ljca_spi
->controller
->dev
, dev_fwnode(&auxdev
->dev
));
242 auxiliary_set_drvdata(auxdev
, controller
);
244 ret
= spi_register_controller(controller
);
246 dev_err(&auxdev
->dev
, "Failed to register controller\n");
251 static void ljca_spi_dev_remove(struct auxiliary_device
*auxdev
)
253 struct spi_controller
*controller
= auxiliary_get_drvdata(auxdev
);
254 struct ljca_spi_dev
*ljca_spi
= spi_controller_get_devdata(controller
);
256 spi_unregister_controller(controller
);
257 ljca_spi_deinit(ljca_spi
);
260 static int ljca_spi_dev_suspend(struct device
*dev
)
262 struct spi_controller
*controller
= dev_get_drvdata(dev
);
264 return spi_controller_suspend(controller
);
267 static int ljca_spi_dev_resume(struct device
*dev
)
269 struct spi_controller
*controller
= dev_get_drvdata(dev
);
271 return spi_controller_resume(controller
);
274 static const struct dev_pm_ops ljca_spi_pm
= {
275 SYSTEM_SLEEP_PM_OPS(ljca_spi_dev_suspend
, ljca_spi_dev_resume
)
278 static const struct auxiliary_device_id ljca_spi_id_table
[] = {
279 { "usb_ljca.ljca-spi", 0 },
282 MODULE_DEVICE_TABLE(auxiliary
, ljca_spi_id_table
);
284 static struct auxiliary_driver ljca_spi_driver
= {
285 .driver
.pm
= &ljca_spi_pm
,
286 .probe
= ljca_spi_probe
,
287 .remove
= ljca_spi_dev_remove
,
288 .id_table
= ljca_spi_id_table
,
290 module_auxiliary_driver(ljca_spi_driver
);
292 MODULE_AUTHOR("Wentong Wu <wentong.wu@intel.com>");
293 MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>");
294 MODULE_AUTHOR("Lixu Zhang <lixu.zhang@intel.com>");
295 MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB-SPI driver");
296 MODULE_LICENSE("GPL");
297 MODULE_IMPORT_NS("LJCA");