1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel La Jolla Cove Adapter USB driver
5 * Copyright (c) 2023, Intel Corporation.
8 #include <linux/acpi.h>
9 #include <linux/auxiliary_bus.h>
10 #include <linux/dev_printk.h>
11 #include <linux/kernel.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/types.h>
18 #include <linux/usb.h>
19 #include <linux/usb/ljca.h>
21 #include <linux/unaligned.h>
24 #define LJCA_ACK_FLAG BIT(0)
25 #define LJCA_RESP_FLAG BIT(1)
26 #define LJCA_CMPL_FLAG BIT(2)
28 #define LJCA_MAX_PACKET_SIZE 64u
29 #define LJCA_MAX_PAYLOAD_SIZE \
30 (LJCA_MAX_PACKET_SIZE - sizeof(struct ljca_msg))
32 #define LJCA_WRITE_TIMEOUT_MS 200
33 #define LJCA_WRITE_ACK_TIMEOUT_MS 500
34 #define LJCA_ENUM_CLIENT_TIMEOUT_MS 20
36 /* ljca client type */
37 enum ljca_client_type {
44 /* MNG client commands */
47 LJCA_MNG_ENUM_GPIO = 4,
48 LJCA_MNG_ENUM_I2C = 5,
49 LJCA_MNG_ENUM_SPI = 8,
52 /* ljca client acpi _ADR */
53 enum ljca_client_acpi_adr {
59 LJCA_CLIENT_ACPI_ADR_MAX,
62 /* ljca cmd message structure */
68 u8 data[] __counted_by(len);
71 struct ljca_i2c_ctr_info {
77 struct ljca_i2c_descriptor {
79 struct ljca_i2c_ctr_info info[] __counted_by(num);
82 struct ljca_spi_ctr_info {
88 struct ljca_spi_descriptor {
90 struct ljca_spi_ctr_info info[] __counted_by(num);
93 struct ljca_bank_descriptor {
97 /* 1 bit for each gpio, 1 means valid */
101 struct ljca_gpio_descriptor {
104 struct ljca_bank_descriptor bank_desc[] __counted_by(bank_num);
108 * struct ljca_adapter - represent a ljca adapter
110 * @intf: the usb interface for this ljca adapter
111 * @usb_dev: the usb device for this ljca adapter
112 * @dev: the specific device info of the usb interface
113 * @rx_pipe: bulk in pipe for receive data from firmware
114 * @tx_pipe: bulk out pipe for send data to firmware
115 * @rx_urb: urb used for the bulk in pipe
116 * @rx_buf: buffer used to receive command response and event
117 * @rx_len: length of rx buffer
118 * @ex_buf: external buffer to save command response
119 * @ex_buf_len: length of external buffer
120 * @actual_length: actual length of data copied to external buffer
121 * @tx_buf: buffer used to download command to firmware
122 * @tx_buf_len: length of tx buffer
123 * @lock: spinlock to protect tx_buf and ex_buf
124 * @cmd_completion: completion object as the command receives ack
125 * @mutex: mutex to avoid command download concurrently
126 * @client_list: client device list
127 * @disconnect: usb disconnect ongoing or not
128 * @reset_id: used to reset firmware
130 struct ljca_adapter {
131 struct usb_interface *intf;
132 struct usb_device *usb_dev;
135 unsigned int rx_pipe;
136 unsigned int tx_pipe;
151 struct completion cmd_completion;
154 struct list_head client_list;
161 struct ljca_match_ids_walk_data {
162 const struct acpi_device_id *ids;
164 struct acpi_device *adev;
167 static const struct acpi_device_id ljca_gpio_hids[] = {
176 static const struct acpi_device_id ljca_i2c_hids[] = {
184 static const struct acpi_device_id ljca_spi_hids[] = {
192 static void ljca_handle_event(struct ljca_adapter *adap,
193 struct ljca_msg *header)
195 struct ljca_client *client;
197 list_for_each_entry(client, &adap->client_list, link) {
199 * Currently only GPIO register event callback, but
200 * firmware message structure should include id when
201 * multiple same type clients register event callback.
203 if (client->type == header->type) {
206 spin_lock_irqsave(&client->event_cb_lock, flags);
207 client->event_cb(client->context, header->cmd,
208 header->data, header->len);
209 spin_unlock_irqrestore(&client->event_cb_lock, flags);
216 /* process command ack and received data if available */
217 static void ljca_handle_cmd_ack(struct ljca_adapter *adap, struct ljca_msg *header)
219 struct ljca_msg *tx_header = adap->tx_buf;
220 u8 ibuf_len, actual_len = 0;
224 spin_lock_irqsave(&adap->lock, flags);
226 if (tx_header->type != header->type || tx_header->cmd != header->cmd) {
227 spin_unlock_irqrestore(&adap->lock, flags);
228 dev_err(adap->dev, "cmd ack mismatch error\n");
232 ibuf_len = adap->ex_buf_len;
235 if (ibuf && ibuf_len) {
236 actual_len = min(header->len, ibuf_len);
238 /* copy received data to external buffer */
239 memcpy(ibuf, header->data, actual_len);
241 /* update copied data length */
242 adap->actual_length = actual_len;
244 spin_unlock_irqrestore(&adap->lock, flags);
246 complete(&adap->cmd_completion);
249 static void ljca_recv(struct urb *urb)
251 struct ljca_msg *header = urb->transfer_buffer;
252 struct ljca_adapter *adap = urb->context;
255 switch (urb->status) {
261 * directly complete the possible ongoing transfer
264 if (adap->disconnect)
265 complete(&adap->cmd_completion);
270 /* rx urb is terminated */
271 dev_dbg(adap->dev, "rx urb terminated with status: %d\n",
275 dev_dbg(adap->dev, "rx urb error: %d\n", urb->status);
279 if (header->len + sizeof(*header) != urb->actual_length)
282 if (header->flags & LJCA_ACK_FLAG)
283 ljca_handle_cmd_ack(adap, header);
285 ljca_handle_event(adap, header);
288 ret = usb_submit_urb(urb, GFP_ATOMIC);
289 if (ret && ret != -EPERM)
290 dev_err(adap->dev, "resubmit rx urb error %d\n", ret);
293 static int ljca_send(struct ljca_adapter *adap, u8 type, u8 cmd,
294 const u8 *obuf, u8 obuf_len, u8 *ibuf, u8 ibuf_len,
295 bool ack, unsigned long timeout)
297 unsigned int msg_len = sizeof(struct ljca_msg) + obuf_len;
298 struct ljca_msg *header = adap->tx_buf;
299 unsigned int transferred;
303 if (adap->disconnect)
306 if (msg_len > adap->tx_buf_len)
309 mutex_lock(&adap->mutex);
311 spin_lock_irqsave(&adap->lock, flags);
315 header->len = obuf_len;
317 memcpy(header->data, obuf, obuf_len);
319 header->flags = LJCA_CMPL_FLAG | (ack ? LJCA_ACK_FLAG : 0);
322 adap->ex_buf_len = ibuf_len;
323 adap->actual_length = 0;
325 spin_unlock_irqrestore(&adap->lock, flags);
327 reinit_completion(&adap->cmd_completion);
329 ret = usb_autopm_get_interface(adap->intf);
333 ret = usb_bulk_msg(adap->usb_dev, adap->tx_pipe, header,
334 msg_len, &transferred, LJCA_WRITE_TIMEOUT_MS);
337 if (transferred != msg_len) {
343 ret = wait_for_completion_timeout(&adap->cmd_completion,
350 ret = adap->actual_length;
353 usb_autopm_put_interface(adap->intf);
356 spin_lock_irqsave(&adap->lock, flags);
358 adap->ex_buf_len = 0;
360 memset(header, 0, sizeof(*header));
361 spin_unlock_irqrestore(&adap->lock, flags);
363 mutex_unlock(&adap->mutex);
368 int ljca_transfer(struct ljca_client *client, u8 cmd, const u8 *obuf,
369 u8 obuf_len, u8 *ibuf, u8 ibuf_len)
371 return ljca_send(client->adapter, client->type, cmd,
372 obuf, obuf_len, ibuf, ibuf_len, true,
373 LJCA_WRITE_ACK_TIMEOUT_MS);
375 EXPORT_SYMBOL_NS_GPL(ljca_transfer, "LJCA");
377 int ljca_transfer_noack(struct ljca_client *client, u8 cmd, const u8 *obuf,
380 return ljca_send(client->adapter, client->type, cmd, obuf,
381 obuf_len, NULL, 0, false, LJCA_WRITE_ACK_TIMEOUT_MS);
383 EXPORT_SYMBOL_NS_GPL(ljca_transfer_noack, "LJCA");
385 int ljca_register_event_cb(struct ljca_client *client, ljca_event_cb_t event_cb,
393 spin_lock_irqsave(&client->event_cb_lock, flags);
395 if (client->event_cb) {
396 spin_unlock_irqrestore(&client->event_cb_lock, flags);
400 client->event_cb = event_cb;
401 client->context = context;
403 spin_unlock_irqrestore(&client->event_cb_lock, flags);
407 EXPORT_SYMBOL_NS_GPL(ljca_register_event_cb, "LJCA");
409 void ljca_unregister_event_cb(struct ljca_client *client)
413 spin_lock_irqsave(&client->event_cb_lock, flags);
415 client->event_cb = NULL;
416 client->context = NULL;
418 spin_unlock_irqrestore(&client->event_cb_lock, flags);
420 EXPORT_SYMBOL_NS_GPL(ljca_unregister_event_cb, "LJCA");
422 static int ljca_match_device_ids(struct acpi_device *adev, void *data)
424 struct ljca_match_ids_walk_data *wd = data;
425 const char *uid = acpi_device_uid(adev);
427 if (acpi_match_device_ids(adev, wd->ids))
435 * Some DSDTs have only one ACPI companion for the two I2C
436 * controllers and they don't set a UID at all (e.g. Dell
437 * Latitude 9420). On these platforms only the first I2C
438 * controller is used, so if a HID match has no UID we use
439 * "0" as the UID and assign ACPI companion to the first
444 uid = strchr(uid, wd->uid[0]);
446 if (!uid || strcmp(uid, wd->uid))
455 /* bind auxiliary device to acpi device */
456 static void ljca_auxdev_acpi_bind(struct ljca_adapter *adap,
457 struct auxiliary_device *auxdev,
460 struct ljca_match_ids_walk_data wd = { 0 };
461 struct device *dev = adap->dev;
462 struct acpi_device *parent;
465 parent = ACPI_COMPANION(dev);
470 * Currently LJCA hw doesn't use _ADR instead the shipped
471 * platforms use _HID to distinguish children devices.
474 case LJCA_GPIO_ACPI_ADR:
475 wd.ids = ljca_gpio_hids;
477 case LJCA_I2C1_ACPI_ADR:
478 case LJCA_I2C2_ACPI_ADR:
479 snprintf(uid, sizeof(uid), "%d", id);
481 wd.ids = ljca_i2c_hids;
483 case LJCA_SPI1_ACPI_ADR:
484 case LJCA_SPI2_ACPI_ADR:
485 wd.ids = ljca_spi_hids;
488 dev_warn(dev, "unsupported _ADR\n");
492 acpi_dev_for_each_child(parent, ljca_match_device_ids, &wd);
494 ACPI_COMPANION_SET(&auxdev->dev, wd.adev);
498 parent = ACPI_COMPANION(dev->parent->parent);
502 acpi_dev_for_each_child(parent, ljca_match_device_ids, &wd);
504 ACPI_COMPANION_SET(&auxdev->dev, wd.adev);
507 static void ljca_auxdev_release(struct device *dev)
509 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
511 kfree(auxdev->dev.platform_data);
514 static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
515 char *name, void *data, u64 adr)
517 struct auxiliary_device *auxdev;
518 struct ljca_client *client;
521 client = kzalloc(sizeof *client, GFP_KERNEL);
529 client->adapter = adap;
530 spin_lock_init(&client->event_cb_lock);
532 auxdev = &client->auxdev;
536 auxdev->dev.parent = adap->dev;
537 auxdev->dev.platform_data = data;
538 auxdev->dev.release = ljca_auxdev_release;
540 ret = auxiliary_device_init(auxdev);
546 ljca_auxdev_acpi_bind(adap, auxdev, adr, id);
548 ret = auxiliary_device_add(auxdev);
552 list_add_tail(&client->link, &adap->client_list);
557 auxiliary_device_uninit(auxdev);
565 static int ljca_enumerate_gpio(struct ljca_adapter *adap)
567 u32 valid_pin[LJCA_MAX_GPIO_NUM / BITS_PER_TYPE(u32)];
568 struct ljca_gpio_descriptor *desc;
569 struct ljca_gpio_info *gpio_info;
570 u8 buf[LJCA_MAX_PAYLOAD_SIZE];
574 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_GPIO, NULL, 0, buf,
575 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
579 /* check firmware response */
580 desc = (struct ljca_gpio_descriptor *)buf;
581 if (ret != struct_size(desc, bank_desc, desc->bank_num))
584 gpio_num = desc->pins_per_bank * desc->bank_num;
585 if (gpio_num > LJCA_MAX_GPIO_NUM)
588 /* construct platform data */
589 gpio_info = kzalloc(sizeof *gpio_info, GFP_KERNEL);
592 gpio_info->num = gpio_num;
594 for (i = 0; i < desc->bank_num; i++)
595 valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
596 bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
598 return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
599 gpio_info, LJCA_GPIO_ACPI_ADR);
602 static int ljca_enumerate_i2c(struct ljca_adapter *adap)
604 struct ljca_i2c_descriptor *desc;
605 struct ljca_i2c_info *i2c_info;
606 u8 buf[LJCA_MAX_PAYLOAD_SIZE];
610 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_I2C, NULL, 0, buf,
611 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
615 /* check firmware response */
616 desc = (struct ljca_i2c_descriptor *)buf;
617 if (ret != struct_size(desc, info, desc->num))
620 for (i = 0; i < desc->num; i++) {
621 /* construct platform data */
622 i2c_info = kzalloc(sizeof *i2c_info, GFP_KERNEL);
626 i2c_info->id = desc->info[i].id;
627 i2c_info->capacity = desc->info[i].capacity;
628 i2c_info->intr_pin = desc->info[i].intr_pin;
630 ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
631 "ljca-i2c", i2c_info,
632 LJCA_I2C1_ACPI_ADR + i);
640 static int ljca_enumerate_spi(struct ljca_adapter *adap)
642 struct ljca_spi_descriptor *desc;
643 struct ljca_spi_info *spi_info;
644 u8 buf[LJCA_MAX_PAYLOAD_SIZE];
648 /* Not all LJCA chips implement SPI, a timeout reading the descriptors is normal */
649 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_SPI, NULL, 0, buf,
650 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
652 return (ret == -ETIMEDOUT) ? 0 : ret;
654 /* check firmware response */
655 desc = (struct ljca_spi_descriptor *)buf;
656 if (ret != struct_size(desc, info, desc->num))
659 for (i = 0; i < desc->num; i++) {
660 /* construct platform data */
661 spi_info = kzalloc(sizeof *spi_info, GFP_KERNEL);
665 spi_info->id = desc->info[i].id;
666 spi_info->capacity = desc->info[i].capacity;
668 ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
669 "ljca-spi", spi_info,
670 LJCA_SPI1_ACPI_ADR + i);
678 static int ljca_reset_handshake(struct ljca_adapter *adap)
680 __le32 reset_id = cpu_to_le32(adap->reset_id);
681 __le32 reset_id_ret = 0;
686 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_RESET, (u8 *)&reset_id,
687 sizeof(__le32), (u8 *)&reset_id_ret, sizeof(__le32),
688 true, LJCA_WRITE_ACK_TIMEOUT_MS);
692 if (reset_id_ret != reset_id)
698 static int ljca_enumerate_clients(struct ljca_adapter *adap)
700 struct ljca_client *client, *next;
703 ret = ljca_reset_handshake(adap);
707 ret = ljca_enumerate_gpio(adap);
709 dev_err(adap->dev, "enumerate GPIO error\n");
713 ret = ljca_enumerate_i2c(adap);
715 dev_err(adap->dev, "enumerate I2C error\n");
719 ret = ljca_enumerate_spi(adap);
721 dev_err(adap->dev, "enumerate SPI error\n");
728 adap->disconnect = true;
730 usb_kill_urb(adap->rx_urb);
732 list_for_each_entry_safe_reverse(client, next, &adap->client_list, link) {
733 auxiliary_device_delete(&client->auxdev);
734 auxiliary_device_uninit(&client->auxdev);
736 list_del_init(&client->link);
743 static int ljca_probe(struct usb_interface *interface,
744 const struct usb_device_id *id)
746 struct usb_device *usb_dev = interface_to_usbdev(interface);
747 struct usb_host_interface *alt = interface->cur_altsetting;
748 struct usb_endpoint_descriptor *ep_in, *ep_out;
749 struct device *dev = &interface->dev;
750 struct ljca_adapter *adap;
753 adap = devm_kzalloc(dev, sizeof(*adap), GFP_KERNEL);
757 /* separate tx buffer allocation for alignment */
758 adap->tx_buf = devm_kzalloc(dev, LJCA_MAX_PACKET_SIZE, GFP_KERNEL);
761 adap->tx_buf_len = LJCA_MAX_PACKET_SIZE;
763 mutex_init(&adap->mutex);
764 spin_lock_init(&adap->lock);
765 init_completion(&adap->cmd_completion);
766 INIT_LIST_HEAD(&adap->client_list);
768 adap->intf = usb_get_intf(interface);
769 adap->usb_dev = usb_dev;
773 * find the first bulk in and out endpoints.
776 ret = usb_find_common_endpoints(alt, &ep_in, &ep_out, NULL, NULL);
778 dev_err(dev, "bulk endpoints not found\n");
781 adap->rx_pipe = usb_rcvbulkpipe(usb_dev, usb_endpoint_num(ep_in));
782 adap->tx_pipe = usb_sndbulkpipe(usb_dev, usb_endpoint_num(ep_out));
784 /* setup rx buffer */
785 adap->rx_len = usb_endpoint_maxp(ep_in);
786 adap->rx_buf = devm_kzalloc(dev, adap->rx_len, GFP_KERNEL);
793 adap->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
798 usb_fill_bulk_urb(adap->rx_urb, usb_dev, adap->rx_pipe,
799 adap->rx_buf, adap->rx_len, ljca_recv, adap);
801 usb_set_intfdata(interface, adap);
803 /* submit rx urb before enumerate clients */
804 ret = usb_submit_urb(adap->rx_urb, GFP_KERNEL);
806 dev_err(dev, "submit rx urb failed: %d\n", ret);
810 ret = ljca_enumerate_clients(adap);
815 * This works around problems with ov2740 initialization on some
816 * Lenovo platforms. The autosuspend delay, has to be smaller than
817 * the delay after setting the reset_gpio line in ov2740_resume().
818 * Otherwise the sensor randomly fails to initialize.
820 pm_runtime_set_autosuspend_delay(&usb_dev->dev, 10);
822 usb_enable_autosuspend(usb_dev);
827 usb_free_urb(adap->rx_urb);
830 usb_put_intf(adap->intf);
832 mutex_destroy(&adap->mutex);
837 static void ljca_disconnect(struct usb_interface *interface)
839 struct ljca_adapter *adap = usb_get_intfdata(interface);
840 struct ljca_client *client, *next;
842 adap->disconnect = true;
844 usb_kill_urb(adap->rx_urb);
846 list_for_each_entry_safe_reverse(client, next, &adap->client_list, link) {
847 auxiliary_device_delete(&client->auxdev);
848 auxiliary_device_uninit(&client->auxdev);
850 list_del_init(&client->link);
854 usb_free_urb(adap->rx_urb);
856 usb_put_intf(adap->intf);
858 mutex_destroy(&adap->mutex);
861 static int ljca_suspend(struct usb_interface *interface, pm_message_t message)
863 struct ljca_adapter *adap = usb_get_intfdata(interface);
865 usb_kill_urb(adap->rx_urb);
870 static int ljca_resume(struct usb_interface *interface)
872 struct ljca_adapter *adap = usb_get_intfdata(interface);
874 return usb_submit_urb(adap->rx_urb, GFP_KERNEL);
877 static const struct usb_device_id ljca_table[] = {
878 { USB_DEVICE(0x8086, 0x0b63) },
881 MODULE_DEVICE_TABLE(usb, ljca_table);
883 static struct usb_driver ljca_driver = {
885 .id_table = ljca_table,
887 .disconnect = ljca_disconnect,
888 .suspend = ljca_suspend,
889 .resume = ljca_resume,
890 .supports_autosuspend = 1,
892 module_usb_driver(ljca_driver);
897 MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB driver");
898 MODULE_LICENSE("GPL");