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