1caee8e38SWentong Wu // SPDX-License-Identifier: GPL-2.0-only
2caee8e38SWentong Wu /*
3caee8e38SWentong Wu * Intel La Jolla Cove Adapter USB-SPI driver
4caee8e38SWentong Wu *
5caee8e38SWentong Wu * Copyright (c) 2023, Intel Corporation.
6caee8e38SWentong Wu */
7caee8e38SWentong Wu
8caee8e38SWentong Wu #include <linux/auxiliary_bus.h>
9caee8e38SWentong Wu #include <linux/bitfield.h>
10caee8e38SWentong Wu #include <linux/bits.h>
11caee8e38SWentong Wu #include <linux/dev_printk.h>
12caee8e38SWentong Wu #include <linux/module.h>
13caee8e38SWentong Wu #include <linux/spi/spi.h>
14caee8e38SWentong Wu #include <linux/usb/ljca.h>
15caee8e38SWentong Wu
16caee8e38SWentong Wu #define LJCA_SPI_BUS_MAX_HZ 48000000
17caee8e38SWentong Wu
18caee8e38SWentong Wu #define LJCA_SPI_BUF_SIZE 60u
19caee8e38SWentong Wu #define LJCA_SPI_MAX_XFER_SIZE \
20caee8e38SWentong Wu (LJCA_SPI_BUF_SIZE - sizeof(struct ljca_spi_xfer_packet))
21caee8e38SWentong Wu
22caee8e38SWentong Wu #define LJCA_SPI_CLK_MODE_POLARITY BIT(0)
23caee8e38SWentong Wu #define LJCA_SPI_CLK_MODE_PHASE BIT(1)
24caee8e38SWentong Wu
25caee8e38SWentong Wu #define LJCA_SPI_XFER_INDICATOR_ID GENMASK(5, 0)
26caee8e38SWentong Wu #define LJCA_SPI_XFER_INDICATOR_CMPL BIT(6)
27caee8e38SWentong Wu #define LJCA_SPI_XFER_INDICATOR_INDEX BIT(7)
28caee8e38SWentong Wu
29caee8e38SWentong Wu /* SPI commands */
30caee8e38SWentong Wu enum ljca_spi_cmd {
31caee8e38SWentong Wu LJCA_SPI_INIT = 1,
32caee8e38SWentong Wu LJCA_SPI_READ,
33caee8e38SWentong Wu LJCA_SPI_WRITE,
34caee8e38SWentong Wu LJCA_SPI_WRITEREAD,
35caee8e38SWentong Wu LJCA_SPI_DEINIT,
36caee8e38SWentong Wu };
37caee8e38SWentong Wu
38caee8e38SWentong Wu enum {
39caee8e38SWentong Wu LJCA_SPI_BUS_SPEED_24M,
40caee8e38SWentong Wu LJCA_SPI_BUS_SPEED_12M,
41caee8e38SWentong Wu LJCA_SPI_BUS_SPEED_8M,
42caee8e38SWentong Wu LJCA_SPI_BUS_SPEED_6M,
43caee8e38SWentong Wu LJCA_SPI_BUS_SPEED_4_8M, /*4.8MHz*/
44caee8e38SWentong Wu LJCA_SPI_BUS_SPEED_MIN = LJCA_SPI_BUS_SPEED_4_8M,
45caee8e38SWentong Wu };
46caee8e38SWentong Wu
47caee8e38SWentong Wu enum {
48caee8e38SWentong Wu LJCA_SPI_CLOCK_LOW_POLARITY,
49caee8e38SWentong Wu LJCA_SPI_CLOCK_HIGH_POLARITY,
50caee8e38SWentong Wu };
51caee8e38SWentong Wu
52caee8e38SWentong Wu enum {
53caee8e38SWentong Wu LJCA_SPI_CLOCK_FIRST_PHASE,
54caee8e38SWentong Wu LJCA_SPI_CLOCK_SECOND_PHASE,
55caee8e38SWentong Wu };
56caee8e38SWentong Wu
57caee8e38SWentong Wu struct ljca_spi_init_packet {
58caee8e38SWentong Wu u8 index;
59caee8e38SWentong Wu u8 speed;
60caee8e38SWentong Wu u8 mode;
61caee8e38SWentong Wu } __packed;
62caee8e38SWentong Wu
63caee8e38SWentong Wu struct ljca_spi_xfer_packet {
64caee8e38SWentong Wu u8 indicator;
65caee8e38SWentong Wu u8 len;
66caee8e38SWentong Wu u8 data[] __counted_by(len);
67caee8e38SWentong Wu } __packed;
68caee8e38SWentong Wu
69caee8e38SWentong Wu struct ljca_spi_dev {
70caee8e38SWentong Wu struct ljca_client *ljca;
71caee8e38SWentong Wu struct spi_controller *controller;
72caee8e38SWentong Wu struct ljca_spi_info *spi_info;
73caee8e38SWentong Wu u8 speed;
74caee8e38SWentong Wu u8 mode;
75caee8e38SWentong Wu
76caee8e38SWentong Wu u8 obuf[LJCA_SPI_BUF_SIZE];
77caee8e38SWentong Wu u8 ibuf[LJCA_SPI_BUF_SIZE];
78caee8e38SWentong Wu };
79caee8e38SWentong Wu
ljca_spi_read_write(struct ljca_spi_dev * ljca_spi,const u8 * w_data,u8 * r_data,int len,int id,int complete,int cmd)80caee8e38SWentong Wu static int ljca_spi_read_write(struct ljca_spi_dev *ljca_spi, const u8 *w_data,
81caee8e38SWentong Wu u8 *r_data, int len, int id, int complete,
82caee8e38SWentong Wu int cmd)
83caee8e38SWentong Wu {
84caee8e38SWentong Wu struct ljca_spi_xfer_packet *w_packet =
85caee8e38SWentong Wu (struct ljca_spi_xfer_packet *)ljca_spi->obuf;
86caee8e38SWentong Wu struct ljca_spi_xfer_packet *r_packet =
87caee8e38SWentong Wu (struct ljca_spi_xfer_packet *)ljca_spi->ibuf;
88caee8e38SWentong Wu int ret;
89caee8e38SWentong Wu
90caee8e38SWentong Wu w_packet->indicator = FIELD_PREP(LJCA_SPI_XFER_INDICATOR_ID, id) |
91caee8e38SWentong Wu FIELD_PREP(LJCA_SPI_XFER_INDICATOR_CMPL, complete) |
92caee8e38SWentong Wu FIELD_PREP(LJCA_SPI_XFER_INDICATOR_INDEX,
93caee8e38SWentong Wu ljca_spi->spi_info->id);
94caee8e38SWentong Wu
95caee8e38SWentong Wu if (cmd == LJCA_SPI_READ) {
96caee8e38SWentong Wu w_packet->len = sizeof(u16);
97caee8e38SWentong Wu *(__le16 *)&w_packet->data[0] = cpu_to_le16(len);
98caee8e38SWentong Wu } else {
99caee8e38SWentong Wu w_packet->len = len;
100caee8e38SWentong Wu memcpy(w_packet->data, w_data, len);
101caee8e38SWentong Wu }
102caee8e38SWentong Wu
103caee8e38SWentong Wu ret = ljca_transfer(ljca_spi->ljca, cmd, (u8 *)w_packet,
104caee8e38SWentong Wu struct_size(w_packet, data, w_packet->len),
105caee8e38SWentong Wu (u8 *)r_packet, LJCA_SPI_BUF_SIZE);
106caee8e38SWentong Wu if (ret < 0)
107caee8e38SWentong Wu return ret;
108caee8e38SWentong Wu else if (ret < sizeof(*r_packet) || r_packet->len <= 0)
109caee8e38SWentong Wu return -EIO;
110caee8e38SWentong Wu
111caee8e38SWentong Wu if (r_data)
112caee8e38SWentong Wu memcpy(r_data, r_packet->data, r_packet->len);
113caee8e38SWentong Wu
114caee8e38SWentong Wu return 0;
115caee8e38SWentong Wu }
116caee8e38SWentong Wu
ljca_spi_init(struct ljca_spi_dev * ljca_spi,u8 div,u8 mode)117caee8e38SWentong Wu static int ljca_spi_init(struct ljca_spi_dev *ljca_spi, u8 div, u8 mode)
118caee8e38SWentong Wu {
119caee8e38SWentong Wu struct ljca_spi_init_packet w_packet = {};
120caee8e38SWentong Wu int ret;
121caee8e38SWentong Wu
122caee8e38SWentong Wu if (ljca_spi->mode == mode && ljca_spi->speed == div)
123caee8e38SWentong Wu return 0;
124caee8e38SWentong Wu
125caee8e38SWentong Wu w_packet.index = ljca_spi->spi_info->id;
126caee8e38SWentong Wu w_packet.speed = div;
127caee8e38SWentong Wu w_packet.mode = FIELD_PREP(LJCA_SPI_CLK_MODE_POLARITY,
128caee8e38SWentong Wu (mode & SPI_CPOL) ? LJCA_SPI_CLOCK_HIGH_POLARITY :
129caee8e38SWentong Wu LJCA_SPI_CLOCK_LOW_POLARITY) |
130caee8e38SWentong Wu FIELD_PREP(LJCA_SPI_CLK_MODE_PHASE,
131caee8e38SWentong Wu (mode & SPI_CPHA) ? LJCA_SPI_CLOCK_SECOND_PHASE :
132caee8e38SWentong Wu LJCA_SPI_CLOCK_FIRST_PHASE);
133caee8e38SWentong Wu
134caee8e38SWentong Wu ret = ljca_transfer(ljca_spi->ljca, LJCA_SPI_INIT, (u8 *)&w_packet,
135caee8e38SWentong Wu sizeof(w_packet), NULL, 0);
136caee8e38SWentong Wu if (ret < 0)
137caee8e38SWentong Wu return ret;
138caee8e38SWentong Wu
139caee8e38SWentong Wu ljca_spi->mode = mode;
140caee8e38SWentong Wu ljca_spi->speed = div;
141caee8e38SWentong Wu
142caee8e38SWentong Wu return 0;
143caee8e38SWentong Wu }
144caee8e38SWentong Wu
ljca_spi_deinit(struct ljca_spi_dev * ljca_spi)145caee8e38SWentong Wu static int ljca_spi_deinit(struct ljca_spi_dev *ljca_spi)
146caee8e38SWentong Wu {
147caee8e38SWentong Wu struct ljca_spi_init_packet w_packet = {};
148caee8e38SWentong Wu int ret;
149caee8e38SWentong Wu
150caee8e38SWentong Wu w_packet.index = ljca_spi->spi_info->id;
151caee8e38SWentong Wu
152caee8e38SWentong Wu ret = ljca_transfer(ljca_spi->ljca, LJCA_SPI_DEINIT, (u8 *)&w_packet,
153caee8e38SWentong Wu sizeof(w_packet), NULL, 0);
154caee8e38SWentong Wu
155caee8e38SWentong Wu return ret < 0 ? ret : 0;
156caee8e38SWentong Wu }
157caee8e38SWentong Wu
ljca_spi_transfer(struct ljca_spi_dev * ljca_spi,const u8 * tx_data,u8 * rx_data,u16 len)158caee8e38SWentong Wu static inline int ljca_spi_transfer(struct ljca_spi_dev *ljca_spi,
159caee8e38SWentong Wu const u8 *tx_data, u8 *rx_data, u16 len)
160caee8e38SWentong Wu {
161caee8e38SWentong Wu int complete, cur_len;
162caee8e38SWentong Wu int remaining = len;
163caee8e38SWentong Wu int cmd, ret, i;
164caee8e38SWentong Wu int offset = 0;
165caee8e38SWentong Wu
166caee8e38SWentong Wu if (tx_data && rx_data)
167caee8e38SWentong Wu cmd = LJCA_SPI_WRITEREAD;
168caee8e38SWentong Wu else if (tx_data)
169caee8e38SWentong Wu cmd = LJCA_SPI_WRITE;
170caee8e38SWentong Wu else if (rx_data)
171caee8e38SWentong Wu cmd = LJCA_SPI_READ;
172caee8e38SWentong Wu else
173caee8e38SWentong Wu return -EINVAL;
174caee8e38SWentong Wu
175caee8e38SWentong Wu for (i = 0; remaining > 0; i++) {
176caee8e38SWentong Wu cur_len = min_t(unsigned int, remaining, LJCA_SPI_MAX_XFER_SIZE);
177caee8e38SWentong Wu complete = (cur_len == remaining);
178caee8e38SWentong Wu
179caee8e38SWentong Wu ret = ljca_spi_read_write(ljca_spi,
180caee8e38SWentong Wu tx_data ? tx_data + offset : NULL,
181caee8e38SWentong Wu rx_data ? rx_data + offset : NULL,
182caee8e38SWentong Wu cur_len, i, complete, cmd);
183caee8e38SWentong Wu if (ret)
184caee8e38SWentong Wu return ret;
185caee8e38SWentong Wu
186caee8e38SWentong Wu offset += cur_len;
187caee8e38SWentong Wu remaining -= cur_len;
188caee8e38SWentong Wu }
189caee8e38SWentong Wu
190caee8e38SWentong Wu return 0;
191caee8e38SWentong Wu }
192caee8e38SWentong Wu
ljca_spi_transfer_one(struct spi_controller * controller,struct spi_device * spi,struct spi_transfer * xfer)193caee8e38SWentong Wu static int ljca_spi_transfer_one(struct spi_controller *controller,
194caee8e38SWentong Wu struct spi_device *spi,
195caee8e38SWentong Wu struct spi_transfer *xfer)
196caee8e38SWentong Wu {
197caee8e38SWentong Wu u8 div = DIV_ROUND_UP(controller->max_speed_hz, xfer->speed_hz) / 2 - 1;
198caee8e38SWentong Wu struct ljca_spi_dev *ljca_spi = spi_controller_get_devdata(controller);
199caee8e38SWentong Wu int ret;
200caee8e38SWentong Wu
201caee8e38SWentong Wu div = min_t(u8, LJCA_SPI_BUS_SPEED_MIN, div);
202caee8e38SWentong Wu
203caee8e38SWentong Wu ret = ljca_spi_init(ljca_spi, div, spi->mode);
204caee8e38SWentong Wu if (ret) {
205caee8e38SWentong Wu dev_err(&ljca_spi->ljca->auxdev.dev,
206caee8e38SWentong Wu "cannot initialize transfer ret %d\n", ret);
207caee8e38SWentong Wu return ret;
208caee8e38SWentong Wu }
209caee8e38SWentong Wu
210caee8e38SWentong Wu ret = ljca_spi_transfer(ljca_spi, xfer->tx_buf, xfer->rx_buf, xfer->len);
211caee8e38SWentong Wu if (ret)
212caee8e38SWentong Wu dev_err(&ljca_spi->ljca->auxdev.dev,
213caee8e38SWentong Wu "transfer failed len: %d\n", xfer->len);
214caee8e38SWentong Wu
215caee8e38SWentong Wu return ret;
216caee8e38SWentong Wu }
217caee8e38SWentong Wu
ljca_spi_probe(struct auxiliary_device * auxdev,const struct auxiliary_device_id * aux_dev_id)218caee8e38SWentong Wu static int ljca_spi_probe(struct auxiliary_device *auxdev,
219caee8e38SWentong Wu const struct auxiliary_device_id *aux_dev_id)
220caee8e38SWentong Wu {
221caee8e38SWentong Wu struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
222caee8e38SWentong Wu struct spi_controller *controller;
223caee8e38SWentong Wu struct ljca_spi_dev *ljca_spi;
224caee8e38SWentong Wu int ret;
225caee8e38SWentong Wu
226*4ac9ed81SYang Yingliang controller = devm_spi_alloc_host(&auxdev->dev, sizeof(*ljca_spi));
227caee8e38SWentong Wu if (!controller)
228caee8e38SWentong Wu return -ENOMEM;
229caee8e38SWentong Wu
230caee8e38SWentong Wu ljca_spi = spi_controller_get_devdata(controller);
231caee8e38SWentong Wu ljca_spi->ljca = ljca;
232caee8e38SWentong Wu ljca_spi->spi_info = dev_get_platdata(&auxdev->dev);
233caee8e38SWentong Wu ljca_spi->controller = controller;
234caee8e38SWentong Wu
235caee8e38SWentong Wu controller->bus_num = -1;
236caee8e38SWentong Wu controller->mode_bits = SPI_CPHA | SPI_CPOL;
237caee8e38SWentong Wu controller->transfer_one = ljca_spi_transfer_one;
238caee8e38SWentong Wu controller->auto_runtime_pm = false;
239caee8e38SWentong Wu controller->max_speed_hz = LJCA_SPI_BUS_MAX_HZ;
240caee8e38SWentong Wu
241caee8e38SWentong Wu device_set_node(&ljca_spi->controller->dev, dev_fwnode(&auxdev->dev));
242caee8e38SWentong Wu auxiliary_set_drvdata(auxdev, controller);
243caee8e38SWentong Wu
244caee8e38SWentong Wu ret = spi_register_controller(controller);
245caee8e38SWentong Wu if (ret)
246caee8e38SWentong Wu dev_err(&auxdev->dev, "Failed to register controller\n");
247caee8e38SWentong Wu
248caee8e38SWentong Wu return ret;
249caee8e38SWentong Wu }
250caee8e38SWentong Wu
ljca_spi_dev_remove(struct auxiliary_device * auxdev)251caee8e38SWentong Wu static void ljca_spi_dev_remove(struct auxiliary_device *auxdev)
252caee8e38SWentong Wu {
253caee8e38SWentong Wu struct spi_controller *controller = auxiliary_get_drvdata(auxdev);
254caee8e38SWentong Wu struct ljca_spi_dev *ljca_spi = spi_controller_get_devdata(controller);
255caee8e38SWentong Wu
256caee8e38SWentong Wu spi_unregister_controller(controller);
257caee8e38SWentong Wu ljca_spi_deinit(ljca_spi);
258caee8e38SWentong Wu }
259caee8e38SWentong Wu
ljca_spi_dev_suspend(struct device * dev)260caee8e38SWentong Wu static int ljca_spi_dev_suspend(struct device *dev)
261caee8e38SWentong Wu {
262caee8e38SWentong Wu struct spi_controller *controller = dev_get_drvdata(dev);
263caee8e38SWentong Wu
264caee8e38SWentong Wu return spi_controller_suspend(controller);
265caee8e38SWentong Wu }
266caee8e38SWentong Wu
ljca_spi_dev_resume(struct device * dev)267caee8e38SWentong Wu static int ljca_spi_dev_resume(struct device *dev)
268caee8e38SWentong Wu {
269caee8e38SWentong Wu struct spi_controller *controller = dev_get_drvdata(dev);
270caee8e38SWentong Wu
271caee8e38SWentong Wu return spi_controller_resume(controller);
272caee8e38SWentong Wu }
273caee8e38SWentong Wu
274caee8e38SWentong Wu static const struct dev_pm_ops ljca_spi_pm = {
275caee8e38SWentong Wu SYSTEM_SLEEP_PM_OPS(ljca_spi_dev_suspend, ljca_spi_dev_resume)
276caee8e38SWentong Wu };
277caee8e38SWentong Wu
278caee8e38SWentong Wu static const struct auxiliary_device_id ljca_spi_id_table[] = {
279caee8e38SWentong Wu { "usb_ljca.ljca-spi", 0 },
280caee8e38SWentong Wu { /* sentinel */ }
281caee8e38SWentong Wu };
282caee8e38SWentong Wu MODULE_DEVICE_TABLE(auxiliary, ljca_spi_id_table);
283caee8e38SWentong Wu
284caee8e38SWentong Wu static struct auxiliary_driver ljca_spi_driver = {
285caee8e38SWentong Wu .driver.pm = &ljca_spi_pm,
286caee8e38SWentong Wu .probe = ljca_spi_probe,
287caee8e38SWentong Wu .remove = ljca_spi_dev_remove,
288caee8e38SWentong Wu .id_table = ljca_spi_id_table,
289caee8e38SWentong Wu };
290caee8e38SWentong Wu module_auxiliary_driver(ljca_spi_driver);
291caee8e38SWentong Wu
292caee8e38SWentong Wu MODULE_AUTHOR("Wentong Wu <wentong.wu@intel.com>");
293caee8e38SWentong Wu MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>");
294caee8e38SWentong Wu MODULE_AUTHOR("Lixu Zhang <lixu.zhang@intel.com>");
295caee8e38SWentong Wu MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB-SPI driver");
296caee8e38SWentong Wu MODULE_LICENSE("GPL");
297caee8e38SWentong Wu MODULE_IMPORT_NS(LJCA);
298