From 6f4db584da6334d5687f35b7e126f3db516dfb91 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:31:14 -0647 Subject: [PATCH 1/7] dt-bindings: gnss: add Samsung's Kepler GNSS core on SPI The GNSS receiver of the Pixel 9a is Samsung's Kepler core inside the S5300 modem. Its data goes over SPI 21 of the Tensor G4 (USI3 of HSI0), and two lines pace the transfers: the core's request line (gpa5-4, which is also its interrupt) and the host's (gpp2-3). Google's kernel describes them with two nodes, which Google's gnssif_spi driver binds: a "samsung,gnss-spi" device on the SPI bus and a "samsung,exynos-gnss" node holding the two lines (kernel/devices/google/zuma, dts/zuma-cp-s5400-phone-gnss.dtsi, included by tegu's MP board). Document both, as the 9a's device tree will use them. The differences from Google's nodes: - The lines are gnss2ap-spi-gpios and ap2gnss-spi-gpios, for GPIO descriptors, instead of gpio_gnss2ap_spi and gpio_ap2gnss_spi. Google's nodes flag them GPIO_ACTIVE_LOW, but Google's driver uses raw levels through the legacy GPIO numbers, and both lines are active high: the core's request is a level-high interrupt, and the host's line idles low. So the binding's example says GPIO_ACTIVE_HIGH. - device,name ("KEPLER", used only in log lines) and device_node_name ("gnss_ipc", the name of the character device the GNSS daemon opens) are left out: neither describes the hardware, "device" is not a vendor prefix, and the driver uses fixed names instead. The compatibles stay Google's generic names, as stock's device tree and gnssd know the device by them. A specific compatible for the S5300's GNSS core belongs with the single-node binding an upstream submission would want, not with this port. The SPI device takes Samsung's SPI controller-data for the feedback delay; stock's is 0. Stock's swap-mode belongs to the vendor SPI controller's node and is not part of these bindings. Link: https://android.googlesource.com/kernel/devices/google/zuma/+/42395e8adcb880aa336f0da6bb2a19ed53929c40/dts/zuma-cp-s5400-phone-gnss.dtsi Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- .../bindings/gnss/samsung,exynos-gnss.yaml | 50 ++++++++++++++++++ .../bindings/gnss/samsung,gnss-spi.yaml | 52 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 Documentation/devicetree/bindings/gnss/samsung,exynos-gnss.yaml create mode 100644 Documentation/devicetree/bindings/gnss/samsung,gnss-spi.yaml diff --git a/Documentation/devicetree/bindings/gnss/samsung,exynos-gnss.yaml b/Documentation/devicetree/bindings/gnss/samsung,exynos-gnss.yaml new file mode 100644 index 0000000000..6051df2eeb --- /dev/null +++ b/Documentation/devicetree/bindings/gnss/samsung,exynos-gnss.yaml @@ -0,0 +1,50 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/gnss/samsung,exynos-gnss.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Samsung Kepler GNSS core, handshake lines + +maintainers: + - W4HO <87742609+W4HO-ham@users.noreply.github.com> + +description: | + The control side of the Kepler GNSS core's SPI link (samsung,gnss-spi): + two lines between the core and the host that pace the transfers. The + core raises its line while it has data for the host, and to answer the + host's request to write. The host raises its line to ask to write, and + pulses it to acknowledge the core's request before reading. The node has + no registers, clocks or supplies of its own. + +properties: + compatible: + const: samsung,exynos-gnss + + gnss2ap-spi-gpios: + maxItems: 1 + description: + Core to host request line, active high. It is also the host's + interrupt, so its GPIO controller must be able to raise one. + + ap2gnss-spi-gpios: + maxItems: 1 + description: + Host to core request line, active high. Low when the host is idle. + +required: + - compatible + - gnss2ap-spi-gpios + - ap2gnss-spi-gpios + +additionalProperties: false + +examples: + - | + #include + + gnss { + compatible = "samsung,exynos-gnss"; + gnss2ap-spi-gpios = <&gpa5 4 GPIO_ACTIVE_HIGH>; + ap2gnss-spi-gpios = <&gpp2 3 GPIO_ACTIVE_HIGH>; + }; diff --git a/Documentation/devicetree/bindings/gnss/samsung,gnss-spi.yaml b/Documentation/devicetree/bindings/gnss/samsung,gnss-spi.yaml new file mode 100644 index 0000000000..75939d415b --- /dev/null +++ b/Documentation/devicetree/bindings/gnss/samsung,gnss-spi.yaml @@ -0,0 +1,52 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/gnss/samsung,gnss-spi.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Samsung Kepler GNSS core, SPI data link + +maintainers: + - W4HO <87742609+W4HO-ham@users.noreply.github.com> + +description: | + Samsung's Kepler GNSS core is part of the S5300 modem in the Google + Pixel 9a. It runs firmware that the host loads into the modem's shared + memory, and it exchanges its data with the host over an SPI bus of the + application processor (USI3 of HSI0 on Tensor G4), in blocks of 64 + bytes. The core and its supplies belong to the modem; this node is only + the SPI device. The two lines that pace the transfers are described by + a samsung,exynos-gnss node. + +allOf: + - $ref: /schemas/spi/spi-peripheral-props.yaml# + +properties: + compatible: + const: samsung,gnss-spi + + reg: + maxItems: 1 + +required: + - compatible + - reg + +unevaluatedProperties: false + +examples: + - | + spi { + #address-cells = <1>; + #size-cells = <0>; + + gnss@0 { + compatible = "samsung,gnss-spi"; + reg = <0>; + spi-max-frequency = <3125000>; + + controller-data { + samsung,spi-feedback-delay = <0>; + }; + }; + }; From 4cac67ef63be0a1935a09cc0d010c0130613ac4d Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:31:35 -0647 Subject: [PATCH 2/7] soc: google: gnssif_spi: import Google's GNSS SPI interface driver Import Google's driver for the Kepler GNSS core of the Pixel 9a's S5300 modem (drivers/soc/google/gnssif_spi of google-modules/soc/gs in Google's GPL kernel for tegu, branch android-gs-tegu-6.1-android16, at 204078758737, "Update kernel to ab/13436148"). It is Samsung's code, GPL-2.0, and makes two modules: - gnss_spi.ko, the SPI device driver for "samsung,gnss-spi": one function to write a buffer and one to read one; - gnssif.ko, the platform driver for "samsung,exynos-gnss": the two request lines and their handshake (gnss_keplerctl_device.c), the read work and the write path over gnss_spi (gnss_link_device.c), and the character device the GNSS daemon talks to, /dev/gnss_ipc (gnss_io_device.c, gnss_main.c, gnss_utils.c). The kernel side of the core's firmware loading is not here: the daemon loads the firmware through the modem driver (CPIF's gnss_boot device). The ten .c and .h files are byte for byte Google's, with Google's style; Google's Kconfig and Makefile are not imported. Nothing builds them yet: the next commits port the driver to this kernel and wire it up. Link: https://android.googlesource.com/kernel/google-modules/soc/gs/+/204078758737776959ecc85c527a00fd2cabb62f/drivers/soc/google/gnssif_spi Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- .../soc/google/gnssif_spi/gnss_io_device.c | 263 ++++++++++++++++++ .../google/gnssif_spi/gnss_keplerctl_device.c | 172 ++++++++++++ .../soc/google/gnssif_spi/gnss_link_device.c | 247 ++++++++++++++++ drivers/soc/google/gnssif_spi/gnss_main.c | 248 +++++++++++++++++ drivers/soc/google/gnssif_spi/gnss_prj.h | 77 +++++ drivers/soc/google/gnssif_spi/gnss_spi.c | 112 ++++++++ drivers/soc/google/gnssif_spi/gnss_spi.h | 28 ++ drivers/soc/google/gnssif_spi/gnss_utils.c | 105 +++++++ drivers/soc/google/gnssif_spi/gnss_utils.h | 84 ++++++ drivers/soc/google/gnssif_spi/include/gnss.h | 95 +++++++ 10 files changed, 1431 insertions(+) create mode 100644 drivers/soc/google/gnssif_spi/gnss_io_device.c create mode 100644 drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c create mode 100644 drivers/soc/google/gnssif_spi/gnss_link_device.c create mode 100644 drivers/soc/google/gnssif_spi/gnss_main.c create mode 100644 drivers/soc/google/gnssif_spi/gnss_prj.h create mode 100644 drivers/soc/google/gnssif_spi/gnss_spi.c create mode 100644 drivers/soc/google/gnssif_spi/gnss_spi.h create mode 100644 drivers/soc/google/gnssif_spi/gnss_utils.c create mode 100644 drivers/soc/google/gnssif_spi/gnss_utils.h create mode 100644 drivers/soc/google/gnssif_spi/include/gnss.h diff --git a/drivers/soc/google/gnssif_spi/gnss_io_device.c b/drivers/soc/google/gnssif_spi/gnss_io_device.c new file mode 100644 index 0000000000..0fd47356bb --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_io_device.c @@ -0,0 +1,263 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022 Samsung Electronics. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gnss_prj.h" +#include "gnss_utils.h" + +#define WAKE_TIME (HZ/2) /* 500 msec */ +#define MAX_RXQ_DEPTH 1024 + +static inline void iodev_lock_wlock(struct io_device *iod) +{ + if (iod->waketime > 0 && !gnssif_wlock_active(iod->ws)) { + gnssif_wlock_unlock(iod->ws); + gnssif_wlock_lock_timeout(iod->ws, iod->waketime); + } +} + +static void io_dev_recv_betp(struct io_device *iod, + struct link_device *ld, struct sk_buff *skb) +{ + struct sk_buff_head *rxq = &iod->sk_rx_q; + + iodev_lock_wlock(iod); + + if (unlikely(rxq->qlen >= MAX_RXQ_DEPTH)) { + gif_err_limited("Too much rx betps are queued: total %d\n", + MAX_RXQ_DEPTH); + dev_kfree_skb_any(skb); + goto wakeup; + } + skb_queue_tail(rxq, skb); + +wakeup: + wake_up(&iod->wq); + +} + +static int misc_open(struct inode *inode, struct file *filp) +{ + struct io_device *iod = container_of(filp->private_data, struct io_device, + miscdev); + struct link_device *ld; + int ref_cnt; + + filp->private_data = (void *)iod; + + ld = iod->ld; + + ref_cnt = atomic_inc_return(&iod->opened); + + gif_info("%s (opened %d)\n", iod->name, ref_cnt); + + return 0; +} + +static int misc_release(struct inode *inode, struct file *filp) +{ + struct io_device *iod = (struct io_device *)filp->private_data; + int ref_cnt; + + ref_cnt = atomic_dec_return(&iod->opened); + if (ref_cnt == 0) + skb_queue_purge(&iod->sk_rx_q); + + gif_info("%s (opened %d)\n", iod->name, ref_cnt); + + return 0; +} + +static unsigned int misc_poll(struct file *filp, struct poll_table_struct *wait) +{ + struct io_device *iod = (struct io_device *)filp->private_data; + + if (skb_queue_empty(&iod->sk_rx_q)) + poll_wait(filp, &iod->wq, wait); + + if (!skb_queue_empty(&iod->sk_rx_q)) + return POLLIN | POLLRDNORM; + + return 0; +} + +static long misc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) +{ + struct io_device *iod = (struct io_device *)filp->private_data; + + gif_err("%s: ERR! undefined cmd 0x%X\n", iod->name, cmd); + + return -EINVAL; +} + +static ssize_t misc_write(struct file *filp, const char __user *data, + size_t count, loff_t *fpos) +{ + struct io_device *iod = (struct io_device *)filp->private_data; + struct link_device *ld = iod->ld; + char *buff; + int ret; + + /* Store IPC message */ + buff = kzalloc(roundup(count, ALIGNMENT_4BYTE), GFP_KERNEL); + if (!buff) + return -ENOMEM; + + if (copy_from_user(buff, data, count)) { + gif_err("%s->%s: ERR! copy_from_user fail (count %ld)\n", + iod->name, ld->name, count); + kfree(buff); + return -EFAULT; + } + + ret = ld->send(ld, iod, buff, roundup(count, ALIGNMENT_4BYTE)); + if (ret < 0) { + gif_err("%s->%s: ERR! ld->send fail (err %d, count %ld)\n", + iod->name, ld->name, ret, count); + kfree(buff); + return ret; + } + + kfree(buff); + return count; +} + +static ssize_t misc_read(struct file *filp, char *buf, size_t count, + loff_t *fpos) +{ + struct io_device *iod = (struct io_device *)filp->private_data; + struct sk_buff_head *rxq = &iod->sk_rx_q; + struct sk_buff *skb; + int copied = 0; + + if (skb_queue_empty(rxq)) { + gif_err("%s: ERR! no data in rxq\n", iod->name); + return 0; + } + + skb = skb_dequeue(rxq); + if (unlikely(!skb)) { + gif_err("%s: No data in RXQ\n", iod->name); + return 0; + } + + if (count <= 0) { + gif_err("%s: bytes to read not specified\n", iod->name); + return 0; + } + + copied = skb->len > count ? count : skb->len; + + if (copy_to_user(buf, skb->data, copied)) { + gif_err("%s: ERR! copy_to_user fail\n", iod->name); + dev_kfree_skb_any(skb); + return -EFAULT; + } + + gif_debug("%s: data:%d copied:%d qlen:%d\n", + iod->name, skb->len, copied, rxq->qlen); + + if (skb->len > count) { + skb_pull(skb, count); + skb_queue_head(rxq, skb); + } else { + dev_kfree_skb_any(skb); + } + + return copied; +} + +static const struct file_operations misc_io_fops = { + .owner = THIS_MODULE, + .open = misc_open, + .release = misc_release, + .poll = misc_poll, + .unlocked_ioctl = misc_ioctl, +#if IS_ENABLED(CONFIG_COMPAT) + .compat_ioctl = misc_ioctl, +#endif + .write = misc_write, + .read = misc_read, +}; + +int init_io_device(struct io_device *iod, struct device *dev) +{ + int ret = 0; + + gif_info("+++\n"); + + iod->recv_betp = io_dev_recv_betp; + + init_waitqueue_head(&iod->wq); + skb_queue_head_init(&iod->sk_rx_q); + + iod->miscdev.minor = MISC_DYNAMIC_MINOR; + iod->miscdev.name = iod->name; + iod->miscdev.fops = &misc_io_fops; + iod->waketime = WAKE_TIME; + iod->ws = gnssif_wlock_register(dev, iod->name); + if (iod->ws == NULL) { + gif_err("%s: wakeup_source_register fail\n", iod->name); + return -EINVAL; + } + + ret = misc_register(&iod->miscdev); + if (ret) + gif_err("%s: ERR! misc_register failed\n", iod->name); + + gif_info("---\n"); + + return ret; +} + +struct io_device *create_io_device(struct platform_device *pdev, + struct link_device *ld, struct gnss_ctl *gc, struct gnss_pdata *pdata) +{ + int ret; + struct device *dev = &pdev->dev; + struct io_device *iod; + + gif_info("+++\n"); + iod = devm_kzalloc(dev, sizeof(struct io_device), GFP_KERNEL); + if (!iod) { + gif_err("iod is NULL\n"); + return NULL; + } + + iod->name = pdata->node_name; + atomic_set(&iod->opened, 0); + + iod->gc = gc; + gc->iod = iod; + + iod->ld = ld; + ld->iod = iod; + + ret = init_io_device(iod, dev); + if (ret) { + devm_kfree(dev, iod); + gif_err("init_io_device fail (%d)\n", ret); + return NULL; + } + + gif_info("---\n"); + + return iod; +} diff --git a/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c b/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c new file mode 100644 index 0000000000..d005bec9ff --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c @@ -0,0 +1,172 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022 Samsung Electronics. + * + */ + +#include +#include +#include +#include +#include + +#include "include/gnss.h" +#include "gnss_prj.h" +#include "gnss_utils.h" + +/* handling interrupt by BETP messages from kepler through SPI */ +static irqreturn_t kepler_spi_irq_handler(int irq, void *data) +{ + struct gnss_ctl *gc = data; + struct link_device *ld = gc->iod->ld; + + gif_disable_irq_nosync(&gc->irq_gnss2ap_spi); + + if (atomic_read(&gc->wait_rdy)) { + atomic_set(&gc->wait_rdy, 0); + complete_all(&gc->gnss_rdy_cmpl); + return IRQ_HANDLED; + } + + if (atomic_read(&gc->rx_in_progress) == 0) { + queue_delayed_work(ld->rx_wq, &ld->rx_dwork, 0); + } + + return IRQ_HANDLED; +} + +static int kepler_spi_status(struct gnss_ctl *gc) +{ + return gpio_get_value(gc->gpio_gnss2ap_spi.num); +} + +static void kepler_send_betp_int(struct gnss_ctl *gc, int value) +{ + gif_debug("About to Set ap2gnss_spi value to %d\n", value); + gpio_set_value(gc->gpio_ap2gnss_spi.num, value); + gif_debug("complete to set ap2gnss_spi value to %d\n", value); +} + +static int kepler_betp_int_status(struct gnss_ctl *gc) +{ + return gpio_get_value(gc->gpio_ap2gnss_spi.num); +} + +static void gnss_set_ops(struct gnss_ctl *gc) +{ + gc->ops.gnss_spi_status = kepler_spi_status; + gc->ops.gnss_send_betp_int = kepler_send_betp_int; + gc->ops.gnss_betp_int_status = kepler_betp_int_status; +} + +static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) +{ + int ret; + struct platform_device *pdev; + struct device_node *np; + + gnss_set_ops(gc); + + dev_set_drvdata(gc->dev, gc); + + pdev = to_platform_device(gc->dev); + np = pdev->dev.of_node; + + /* GNSS2AP SPI INT */ + gc->gpio_gnss2ap_spi.num = of_get_named_gpio(np, "gpio_gnss2ap_spi", 0); + if (gc->gpio_gnss2ap_spi.num < 0) { + gif_err("Can't get gpio_gnss2ap_spi!\n"); + return -ENODEV; + } + gc->gpio_gnss2ap_spi.label = "GNSS2AP_SPI"; + ret = gpio_request(gc->gpio_gnss2ap_spi.num, gc->gpio_gnss2ap_spi.label); + if (ret) { + gif_err("Request gpio fail - gpio_gnss2ap_spi.num failed(%d)\n", ret); + return ret; + } + gc->irq_gnss2ap_spi.num = gpio_to_irq(gc->gpio_gnss2ap_spi.num); + + /* Disable the interrupt until gnss_main.c probe function finish */ + gif_configure_irq(&gc->irq_gnss2ap_spi, gc->irq_gnss2ap_spi.num, + "kepler_spi_irq_handler", IRQF_TRIGGER_HIGH | IRQF_NO_AUTOEN); + + ret = gif_request_irq(&gc->irq_gnss2ap_spi, kepler_spi_irq_handler, gc); + if (ret) { + gif_err("Request irq fail - kepler_spi_irq_handler(%d)\n", ret); + goto free_gpio_gnss2ap; + } + + ret = enable_irq_wake(gc->irq_gnss2ap_spi.num); + if (ret) { + gif_err("Failed to enable wakeup_source - enable_irq_wake(%d)\n", ret); + goto free_gpio_irq; + } + + gc->gpio_ap2gnss_spi.num = + of_get_named_gpio(np, "gpio_ap2gnss_spi", 0); + if (gc->gpio_ap2gnss_spi.num < 0) { + gif_err("Can't get gpio_ap2gnss_spi!\n"); + ret = -ENODEV; + goto free_gpio_irq; + } + gc->gpio_ap2gnss_spi.label = "AP2GNSS_SPI"; + ret = gpio_request(gc->gpio_ap2gnss_spi.num, gc->gpio_ap2gnss_spi.label); + if (ret) { + gif_err("Request gpio fail - gpio_ap2gnss_spi.num failed(%d)\n", ret); + goto free_gpio_irq; + } + + ret = gpio_direction_output(gc->gpio_ap2gnss_spi.num, 0); + if (ret) { + gif_err("Set gpio direction fail - gpio_ap2gnss_spi.num failed(%d)\n", ret); + goto free_gpio_ap2gnss; + } + + /* initialize gpio values to all zero */ + gpio_set_value(gc->gpio_ap2gnss_spi.num, 0); + + init_completion(&gc->gnss_rdy_cmpl); + + atomic_set(&gc->wait_rdy, 0); + atomic_set(&gc->tx_in_progress, 0); + atomic_set(&gc->rx_in_progress, 0); + + return ret; + +free_gpio_ap2gnss: + gpio_free(gc->gpio_ap2gnss_spi.num); + +free_gpio_irq: + free_irq(gc->irq_gnss2ap_spi.num, &gc); + +free_gpio_gnss2ap: + gpio_free(gc->gpio_gnss2ap_spi.num); + + return ret; + +} + +struct gnss_ctl *create_ctl_device(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct gnss_pdata *pdata = pdev->dev.platform_data; + struct gnss_ctl *gc; + int ret; + + gc = devm_kzalloc(dev, sizeof(struct gnss_ctl), GFP_KERNEL); + if (!gc) + return NULL; + gc->dev = dev; + gc->pdata = pdata; + gc->name = pdata->name; + + ret = init_ctl_device(gc, pdata); + if (ret) { + gif_err("%s: init_ctl_device fail (err %d)\n", + pdata->name, ret); + devm_kfree(dev, gc); + return NULL; + } + + return gc; +} diff --git a/drivers/soc/google/gnssif_spi/gnss_link_device.c b/drivers/soc/google/gnssif_spi/gnss_link_device.c new file mode 100644 index 0000000000..abd81efe77 --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_link_device.c @@ -0,0 +1,247 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022 Samsung Electronics. + * + */ + +#include +#include +#include +#include +#include +#include + +#include "include/gnss.h" +#include "gnss_prj.h" +#include "gnss_utils.h" +#include "gnss_spi.h" + +/* #define GNSS_PKT_DEBUG 1 */ + +#if defined(GNSS_PKT_DEBUG) +#define PR_BUFFER_SIZE 64 + +static inline void dump2hex(char *buff, size_t buff_size, + const char *data, size_t data_len) +{ + static const char *hex = "0123456789abcdef"; + char *dest = buff; + size_t len; + size_t i; + + if (buff_size < (data_len * 3)) + len = buff_size / 3; + else + len = data_len; + + for (i = 0; i < len; i++) { + *dest++ = hex[(data[i] >> 4) & 0xf]; + *dest++ = hex[data[i] & 0xf]; + *dest++ = ' '; + } + + /* The last character must be overwritten with NULL */ + if (likely(len > 0)) + dest--; + + *dest = 0; +} + +static int pr_buffer(const char *tag, const char *data, size_t data_len, + size_t max_len) +{ + size_t len = min(data_len, max_len); + unsigned char str[PR_BUFFER_SIZE * 3]; /* 1 <= sizeof <= max_len*3 */ + + if (len > PR_BUFFER_SIZE) + len = PR_BUFFER_SIZE; + + dump2hex(str, (len ? len * 3 : 1), data, len); + + /* don't change this printk to mif_debug for print this as level7 */ + return pr_info("%s: %s(%ld): %s%s\n", "gnss_spi", tag, (long)data_len, + str, (len == data_len) ? "" : " ..."); +} +#else /* GNSS_PKT_DEBUG */ +static inline int pr_buffer(const char *tag, const char *data, size_t data_len, + size_t max_len) +{ + return 0; +} +#endif /* GNSS_PKT_DEBUG */ + +static void rx_work(struct work_struct *ws) +{ + struct link_device *ld = container_of(ws, struct link_device, + rx_dwork.work); + struct gnss_ctl *gc = ld->gc; + struct io_device *iod = ld->iod; + struct sk_buff *skb = NULL; + char *buff = NULL; + unsigned int len = ld->spi_rx_size; + unsigned int max_len = ld->max_spi_rx_size; + unsigned int iter = 0; + int ret = 0; + + atomic_set(&gc->rx_in_progress, 1); + + do { + if (!skb) { + gc->ops.gnss_send_betp_int(gc, 1); + if (atomic_read(&gc->tx_in_progress) == 0) { + udelay(100); + gc->ops.gnss_send_betp_int(gc, 0); + } + + skb = alloc_skb(max_len, GFP_KERNEL); + if (!skb) { + gif_err("%s: ERR! rx alloc_skb fail (msg size:%u)\n", + iod->name, len); + goto exit; + } + } + + buff = skb_put(skb, len); + ret = gnss_spi_recv(buff, len); + if (ret) { + gif_err("gnss_spi_recv() error:%d\n", ret); + dev_kfree_skb_any(skb); + goto exit; + } + + iter++; + if (skb->len >= max_len || atomic_read(&gc->wait_rdy) == 1) { + pr_buffer("RX", skb->data, skb->len, skb->len); + gif_debug("Received %d(%d * %d) bytes data from kepler.\n", + skb->len, len, iter); + + iod->recv_betp(iod, ld, skb); + skb = NULL; /* skb to be freed by gnss iod*/ + iter = 0; + + if (atomic_read(&gc->wait_rdy) == 1) { + atomic_set(&gc->wait_rdy, 0); + complete_all(&gc->gnss_rdy_cmpl); + gif_debug("TX request while Rx is in-progress\n"); + + while (atomic_read(&gc->tx_in_progress) == 1) + udelay(10); + } + } + } while (gc->ops.gnss_spi_status(gc)); + + if (skb) { + pr_buffer("RX", skb->data, skb->len, skb->len); + gif_debug("Received %d(%d * %d) bytes data from kepler.\n", + skb->len, len, iter); + + iod->recv_betp(iod, ld, skb); + } +exit: + atomic_set(&gc->rx_in_progress, 0); + if (atomic_read(&gc->tx_in_progress) == 0) + gc->ops.gnss_send_betp_int(gc, 0); + + gif_debug("Done receiving from kepler\n"); + + gif_enable_irq(&gc->irq_gnss2ap_spi); +} + +static int send_betp(struct link_device *ld, struct io_device *iod, char *buff, + unsigned int size) +{ + struct gnss_ctl *gc = ld->gc; + struct sk_buff *skb; + int ret = 0; + + if (size > ld->spi_tx_size) { + gif_err("size(%d) is over %d\n", size, ld->spi_tx_size); + size = ld->spi_tx_size; + } + + skb = alloc_skb(size, GFP_KERNEL); + if (!skb) { + gif_err("%s: ERR! tx alloc_skb fail (msg size:%u)\n", + iod->name, size); + return -ENOMEM; + } + skb_put(skb, size); + + atomic_set(&gc->tx_in_progress, 1); + + reinit_completion(&gc->gnss_rdy_cmpl); + atomic_set(&gc->wait_rdy, 1); + + gc->ops.gnss_send_betp_int(gc, 1); + + gif_enable_irq(&gc->irq_gnss2ap_spi); + + ret = wait_for_completion_timeout(&gc->gnss_rdy_cmpl, GNSS_RDY_TIMEOUT); + if (ret == 0) { + gif_err("TIMEOUT(%lu): gnss ready signal not came from kepler\n", GNSS_RDY_TIMEOUT); + dev_kfree_skb_any(skb); + ret = -EBUSY; + goto exit; + } + + ret = gnss_spi_send(buff, size, (char *)skb->data); + if (ret) { + gif_err("gnss_spi_send() error:%d\n", ret); + dev_kfree_skb_any(skb); + goto exit; + } else { + gif_debug("Sent %d bytes to kepler\n", size); + pr_buffer("TX", buff, size, size); + iod->recv_betp(iod, ld, skb); + } + +exit: + atomic_set(&gc->wait_rdy, 0); + atomic_set(&gc->tx_in_progress, 0); + gc->ops.gnss_send_betp_int(gc, 0); + + gif_enable_irq(&gc->irq_gnss2ap_spi); + + return ret; +} + +struct link_device *create_link_device(struct platform_device *pdev) +{ + struct link_device *ld = NULL; + struct gnss_pdata *pdata = NULL; + struct device *dev = &pdev->dev; + + gif_info("+++\n"); + + pdata = (struct gnss_pdata *)dev->platform_data; + if (!pdata) { + gif_err("ERR! gnss == NULL\n"); + return NULL; + } + + ld = devm_kzalloc(dev, sizeof(struct link_device), GFP_KERNEL); + if (!ld) { + gif_err("ERR! link device kzalloc fail\n"); + return NULL; + } + + ld->pdata = pdata; + ld->name = "GNSS_LINK_DEVICE"; + ld->send = send_betp; + ld->spi_rx_size = DEFAULT_SPI_RX_SIZE; + ld->max_spi_rx_size = MAX_SPI_RX_SIZE; + ld->spi_tx_size = DEFAULT_SPI_TX_SIZE; + + ld->rx_wq = alloc_workqueue("gnss_spi_wq", + __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND, 1); + if (!ld->rx_wq) { + gif_err("alloc_workqueue() error\n"); + goto exit; + } + INIT_DELAYED_WORK(&ld->rx_dwork, rx_work); + + gif_info("---\n"); + +exit: + return ld; +} diff --git a/drivers/soc/google/gnssif_spi/gnss_main.c b/drivers/soc/google/gnssif_spi/gnss_main.c new file mode 100644 index 0000000000..fddc1c233e --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_main.c @@ -0,0 +1,248 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022 Samsung Electronics. + * + */ +#include +#include +#include +#include +#include +#include +#include + +#include "gnss_prj.h" +#include "gnss_utils.h" + +#define DEVICE_NAME "gnss" + +static void sscd_release(struct device *dev); + +static struct sscd_platform_data sscd_pdata; + +static struct platform_device sscd_dev = { + .name = DEVICE_NAME, + .driver_override = SSCD_NAME, + .id = -1, + .dev = { + .platform_data = &sscd_pdata, + .release = sscd_release, + }, +}; + +static void sscd_release(struct device *dev) +{ + gif_info("%s: enter\n", __FUNCTION__); +} + +static ssize_t coredump_store(struct device *dev, struct device_attribute *attr, const char *buf, + size_t count) +{ + int symbol = (int)';'; + const char *next = NULL; + char *coredump = NULL; + char *reason = NULL; + int length = 0; + static time64_t last_trigger_time = 0; + time64_t trigger_time = ktime_get_seconds(); + + // Ignore the trigger time less than 30 seconds + if (trigger_time - last_trigger_time < 30) { + goto clean; + } + last_trigger_time = trigger_time; + + gif_err("Trigger Coredump string: %s\n", buf); + next = strchr(buf, symbol); + if (next) { + length = (int)(next - buf); + reason = kmalloc(length + 1, GFP_KERNEL); + if (!reason) { + gif_err("Allocate crash reason failed\n"); + goto clean; + } + memcpy(reason, buf, length); + reason[length] = '\0'; + length = strlen(next + 1); + coredump = kmalloc(length, GFP_KERNEL); + if (!coredump) { + gif_err("Allocate crash coredump failed\n"); + goto clean; + } + memcpy(coredump, next + 1, length); + gnss_set_coredump(coredump, length, reason); + } +clean: + if (reason) + kfree(reason); + if (coredump) + kfree(coredump); + return count; +} + +static DEVICE_ATTR_WO(coredump); + +static int parse_dt_common_pdata(struct device_node *np, struct gnss_pdata *pdata) +{ + gif_dt_read_string(np, "device,name", pdata->name); + gif_dt_read_string(np, "device_node_name", pdata->node_name); + + gif_info("device name: %s node name: %s\n", pdata->name, pdata->node_name); + return 0; +} + +static struct gnss_pdata *gnss_if_parse_dt_pdata(struct device *dev) +{ + struct gnss_pdata *pdata; + int ret = 0; + + pdata = devm_kzalloc(dev, sizeof(struct gnss_pdata), GFP_KERNEL); + if (!pdata) { + gif_err("gnss_pdata: alloc fail\n"); + return ERR_PTR(-ENOMEM); + } + dev->platform_data = pdata; + + ret = parse_dt_common_pdata(dev->of_node, pdata); + if (ret != 0) { + gif_err("Failed to parse common pdata from dt\n"); + goto parse_dt_pdata_err; + } + + gif_info("DT parse complete!\n"); + return pdata; + +parse_dt_pdata_err: + if (pdata) + devm_kfree(dev, pdata); + dev->platform_data = NULL; + + return ERR_PTR(-EINVAL); +} + +static int gnss_remove(struct platform_device *pdev) +{ + struct gnss_ctl *gc = platform_get_drvdata(pdev); + + gif_disable_irq_sync(&gc->irq_gnss2ap_spi); + + gpio_set_value(gc->gpio_ap2gnss_spi.num, 0); + + free_irq(gc->irq_gnss2ap_spi.num, &gc); + + gpio_free(gc->gpio_ap2gnss_spi.num); + + gpio_free(gc->gpio_gnss2ap_spi.num); + + platform_device_unregister(&sscd_dev); + + device_remove_file(&pdev->dev, &dev_attr_coredump); + + return 0; +} + +static int gnss_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct gnss_pdata *pdata = dev->platform_data; + struct gnss_ctl *gc; + struct io_device *iod; + struct link_device *ld; + + gif_info("Exynos GNSS interface driver probe begins\n"); + + gif_info("%s: +++\n", pdev->name); + + if (!dev->of_node) { + gif_err("No DT data!\n"); + goto probe_fail; + } + + pdata = gnss_if_parse_dt_pdata(dev); + if (IS_ERR(pdata)) { + gif_err("DT parse error!\n"); + return PTR_ERR(pdata); + } + + gc = create_ctl_device(pdev); + if (!gc) { + gif_err("%s: Could not create gc\n", pdata->name); + goto probe_fail; + } + + ld = create_link_device(pdev); + if (!ld) { + gif_err("%s: Could not create ld\n", pdata->name); + goto free_gc; + } + + ld->gc = gc; + + iod = create_io_device(pdev, ld, gc, pdata); + if (!iod) { + gif_err("%s: Could not create iod\n", pdata->name); + goto free_ld; + } + + platform_set_drvdata(pdev, gc); + + platform_device_register(&sscd_dev); + + if (device_create_file(dev, &dev_attr_coredump)) + gif_err("Unable to create sysfs coredump entry"); + + /* wa: to prevent wrong irq handling during probe */ + gif_enable_irq(&gc->irq_gnss2ap_spi); + + gif_info("%s: ---\n", pdev->name); + + return 0; + +free_ld: + devm_kfree(dev, ld); +free_gc: + devm_kfree(dev, gc); +probe_fail: + gif_err("%s: xxx\n", pdata->name); + + return -ENOMEM; +} + +static const struct of_device_id gnss_dt_match[] = { + { + .compatible = "samsung,exynos-gnss", + }, + {}, +}; +MODULE_DEVICE_TABLE(of, gnss_dt_match); + +static struct platform_driver gnss_driver = { + .probe = gnss_probe, + .remove = gnss_remove, + .driver = { + .name = "gnss_interface", + .owner = THIS_MODULE, +#if IS_ENABLED(CONFIG_OF) + .of_match_table = of_match_ptr(gnss_dt_match), +#endif + }, +}; + +module_platform_driver(gnss_driver); + +int gnss_set_coredump(const char *buf, int buf_len, const char *info) +{ + struct sscd_platform_data *pdata = dev_get_platdata(&sscd_dev.dev); + struct sscd_segment seg; + + if (pdata->sscd_report) { + memset(&seg, 0, sizeof(seg)); + seg.addr = (void *)buf; + seg.size = buf_len; + pdata->sscd_report(&sscd_dev, &seg, 1, 0, info); + } + return 0; +} + +MODULE_DESCRIPTION("Exynos GNSS interface driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/soc/google/gnssif_spi/gnss_prj.h b/drivers/soc/google/gnssif_spi/gnss_prj.h new file mode 100644 index 0000000000..8e51d32d09 --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_prj.h @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2022, Samsung Electronics. + * + */ + +#ifndef __GNSS_PRJ_H__ +#define __GNSS_PRJ_H__ + +#include +#include +#include +#include + +#include "include/gnss.h" + +#define GNSS_RDY_TIMEOUT msecs_to_jiffies(5000) +#define ALIGNMENT_4BYTE 4 + +struct io_device { + char *name; + struct link_device *ld; + struct gnss_ctl *gc; + atomic_t opened; + wait_queue_head_t wq; + struct miscdevice miscdev; + struct sk_buff_head sk_rx_q; + struct wakeup_source *ws; + long waketime; + + void (*recv_betp)(struct io_device *iod, struct link_device *ld, + struct sk_buff *skb); +}; + +struct link_device { + char *name; + struct gnss_pdata *pdata; + struct gnss_ctl *gc; + struct io_device *iod; + struct gnss_spi *gnss_if; + unsigned int spi_rx_size; + unsigned int max_spi_rx_size; + unsigned int spi_tx_size; + struct workqueue_struct *rx_wq; + struct delayed_work rx_dwork; + + int (*send)(struct link_device *ld, struct io_device *iod, char *buff, + unsigned int size); +}; + +struct gnssctl_ops { + int (*gnss_spi_status)(struct gnss_ctl *gc); + void (*gnss_send_betp_int)(struct gnss_ctl *gc, int value); + int (*gnss_betp_int_status)(struct gnss_ctl *gc); +}; + +struct gnss_ctl { + struct device *dev; + char *name; + struct gnss_pdata *pdata; + struct gnssctl_ops ops; + struct io_device *iod; + struct completion gnss_rdy_cmpl; + struct gnss_irq irq_gnss2ap_spi; + struct gnss_gpio gpio_gnss2ap_spi; + struct gnss_gpio gpio_ap2gnss_spi; + atomic_t wait_rdy; + atomic_t tx_in_progress; + atomic_t rx_in_progress; +}; + +struct gnss_ctl *create_ctl_device(struct platform_device *pdev); +struct link_device *create_link_device(struct platform_device *pdev); +struct io_device *create_io_device(struct platform_device *pdev, + struct link_device *ld, struct gnss_ctl *gc, struct gnss_pdata *pdata); + +#endif /* end of __GNSS_PRJ_H__ */ diff --git a/drivers/soc/google/gnssif_spi/gnss_spi.c b/drivers/soc/google/gnssif_spi/gnss_spi.c new file mode 100644 index 0000000000..83680d08dc --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_spi.c @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022 Samsung Electronics. + * + */ + +#include +#include +#include +#include +#include +#include + +#include "include/gnss.h" +#include "gnss_spi.h" + + +int gnss_spi_send(char *buff, unsigned int size, char *rx_buff) +{ + int ret = 0; + struct spi_message msg; + struct spi_transfer tx; + + memset(&tx, 0, sizeof(struct spi_transfer)); + spi_message_init(&msg); + tx.len = size; + tx.tx_buf = buff; + tx.bits_per_word = SPI_BITS_PER_WORD; + if (rx_buff) + tx.rx_buf = rx_buff; + spi_message_add_tail(&tx, &msg); + ret = spi_sync(gnss_if.spi, &msg); + if (ret < 0) + gif_err("spi_sync() error:%d\n", ret); + + return ret; +} +EXPORT_SYMBOL(gnss_spi_send); + +int gnss_spi_recv(char *buff, unsigned int size) +{ + int ret = 0; + struct spi_message msg; + struct spi_transfer rx; + + memset(&rx, 0, sizeof(struct spi_transfer)); + rx.len = size; + rx.rx_buf = buff; + rx.bits_per_word = SPI_BITS_PER_WORD; + spi_message_init(&msg); + spi_message_add_tail(&rx, &msg); + ret = spi_sync(gnss_if.spi, &msg); + if (ret < 0) + gif_err("spi_sync() error:%d\n", ret); + + return ret; + +} +EXPORT_SYMBOL(gnss_spi_recv); + +static int gnss_spi_probe(struct spi_device *spi) +{ + int ret = 0; + + gif_info("+++\n"); + mutex_init(&gnss_if.lock); + + if (spi_setup(spi)) { + gif_err("ERR! spi_setup fail\n"); + ret = -EINVAL; + goto err_setup; + } + spi_set_drvdata(spi, &gnss_if); + gnss_if.spi = spi; + gif_info("---\n"); + + return 0; + +err_setup: + mutex_destroy(&gnss_if.lock); + + return ret; +} + +static void gnss_spi_remove (struct spi_device *spi) +{ + struct gnss_spi *gnss_if = spi_get_drvdata(spi); + + mutex_destroy(&gnss_if->lock); +} + +static const struct of_device_id gnss_spi_dt_match[] = { + { .compatible = "samsung,gnss-spi" }, + {}, +}; +MODULE_DEVICE_TABLE(of, gnss_spi_dt_match); + +static struct spi_driver gnss_spi_driver = { + .probe = gnss_spi_probe, + .remove = gnss_spi_remove, + .driver = { + .name = "gnss_spi", + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(gnss_spi_dt_match), + .suppress_bind_attrs = true, + }, +}; +module_spi_driver(gnss_spi_driver); + +MODULE_DESCRIPTION("Exynos SPI driver for GNSS communication"); +MODULE_LICENSE("GPL"); + diff --git a/drivers/soc/google/gnssif_spi/gnss_spi.h b/drivers/soc/google/gnssif_spi/gnss_spi.h new file mode 100644 index 0000000000..553c40a0d1 --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_spi.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2022, Samsung Electronics. + * + */ + +#ifndef __GNSS_SPI_H__ +#define __GNSS_SPI_H__ + +#include +#include + +#define DEFAULT_SPI_RX_SIZE 64 +#define MAX_SPI_RX_SIZE SZ_2K +#define DEFAULT_SPI_TX_SIZE SZ_4K +#define SPI_BITS_PER_WORD 32 + +struct gnss_spi { + struct spi_device *spi; + struct mutex lock; +}; + +static struct gnss_spi gnss_if; + +extern int gnss_spi_send(char *buff, unsigned int size, char *recv_buff); +extern int gnss_spi_recv(char *buff, unsigned int size); + +#endif /* __GNSS_SPI_H__ */ diff --git a/drivers/soc/google/gnssif_spi/gnss_utils.c b/drivers/soc/google/gnssif_spi/gnss_utils.c new file mode 100644 index 0000000000..ed4b4c9d12 --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_utils.c @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022 Samsung Electronics. + * + */ + +#include +#include +#include +#include +#include + +#include "gnss_prj.h" +#include "gnss_utils.h" + + +void gif_configure_irq(struct gnss_irq *irq, unsigned int num, const char *name, + unsigned long flags) +{ + spin_lock_init(&irq->lock); + irq->num = num; + strncpy(irq->name, name, sizeof(irq->name) - 1); + irq->flags = flags; + gif_info("name:%s num:%d flags:0x%08lX\n", name, num, flags); +} + +int gif_request_irq(struct gnss_irq *irq, irq_handler_t isr, void *data) +{ + int ret; + + ret = request_irq(irq->num, isr, irq->flags, irq->name, data); + if (ret) { + gif_err("%s: ERR! request_irq fail (%d)\n", irq->name, ret); + return ret; + } + + irq->active = irq->flags & IRQF_NO_AUTOEN ? false : true; + irq->registered = true; + + gif_info("%s(#%d) handler registered (flags:0x%08lX)\n", + irq->name, irq->num, irq->flags); + + return 0; +} + +void gif_enable_irq(struct gnss_irq *irq) +{ + unsigned long flags; + + spin_lock_irqsave(&irq->lock, flags); + + if (irq->active) + goto exit; + + enable_irq(irq->num); + + irq->active = true; + +exit: + spin_unlock_irqrestore(&irq->lock, flags); +} + +void gif_disable_irq_nosync(struct gnss_irq *irq) +{ + unsigned long flags; + + if (irq->registered == false) + return; + + spin_lock_irqsave(&irq->lock, flags); + + if (!irq->active) + goto exit; + disable_irq_nosync(irq->num); + irq->active = false; + +exit: + spin_unlock_irqrestore(&irq->lock, flags); +} + +void gif_disable_irq_sync(struct gnss_irq *irq) +{ + if (irq->registered == false) + return; + + spin_lock(&irq->lock); + + if (!irq->active) { + spin_unlock(&irq->lock); + gif_debug("%s(#%d) is not active <%ps>\n", + irq->name, irq->num, CALLER); + return; + } + + spin_unlock(&irq->lock); + + disable_irq(irq->num); + + spin_lock(&irq->lock); + irq->active = false; + spin_unlock(&irq->lock); + + gif_debug("%s(#%d) is disabled <%ps>\n", + irq->name, irq->num, CALLER); +} diff --git a/drivers/soc/google/gnssif_spi/gnss_utils.h b/drivers/soc/google/gnssif_spi/gnss_utils.h new file mode 100644 index 0000000000..9125e6a410 --- /dev/null +++ b/drivers/soc/google/gnssif_spi/gnss_utils.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2022, Samsung Electronics. + * + */ + +#ifndef __GNSS_UTILS_H__ +#define __GNSS_UTILS_H__ + +#include "gnss_prj.h" + +static inline struct wakeup_source *gnssif_wlock_register(struct device *dev, + const char *name) +{ + struct wakeup_source *ws = NULL; + + ws = wakeup_source_register(dev, name); + if (ws == NULL) { + gif_err("%s: wakelock register fail\n", name); + return NULL; + } + + return ws; +} + +static inline void gnssif_wlock_unregister(struct wakeup_source *ws) +{ + if (ws == NULL) { + gif_err("wakelock unregister fail\n"); + return; + } + + wakeup_source_unregister(ws); +} + +static inline void gnssif_wlock_lock(struct wakeup_source *ws) +{ + if (ws == NULL) { + gif_err("wakelock fail\n"); + return; + } + + __pm_stay_awake(ws); +} + +static inline void gnssif_wlock_lock_timeout(struct wakeup_source *ws, long timeout) +{ + if (ws == NULL) { + gif_err("wakelock timeout fail\n"); + return; + } + + __pm_wakeup_event(ws, jiffies_to_msecs(timeout)); +} + +static inline void gnssif_wlock_unlock(struct wakeup_source *ws) +{ + if (ws == NULL) { + gif_err("wake unlock fail\n"); + return; + } + + __pm_relax(ws); +} + +static inline int gnssif_wlock_active(struct wakeup_source *ws) +{ + if (ws == NULL) { + gif_err("wakelock active fail\n"); + return 0; + } + + return ws->active; +} + +/* gnss irq */ +void gif_configure_irq(struct gnss_irq *irq, unsigned int num, const char *name, + unsigned long flags); +int gif_request_irq(struct gnss_irq *irq, irq_handler_t isr, void *data); +void gif_enable_irq(struct gnss_irq *irq); +void gif_disable_irq_nosync(struct gnss_irq *irq); +void gif_disable_irq_sync(struct gnss_irq *irq); + +#endif /* end of __GNSS_UTILS_H__ */ diff --git a/drivers/soc/google/gnssif_spi/include/gnss.h b/drivers/soc/google/gnssif_spi/include/gnss.h new file mode 100644 index 0000000000..e7d4cc421b --- /dev/null +++ b/drivers/soc/google/gnssif_spi/include/gnss.h @@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2022, Samsung Electronics. + * + */ + +#ifndef __GNSS_IF_H__ +#define __GNSS_IF_H__ + +#include +#include + +#define MAX_NAME_LEN 64 + +struct gnss_pdata { + char *name; /* device name (ex. KEPLER) */ + char *node_name; /* device node (ex. gnss_ipc) */ + + int irq_gnss2ap_spi; + int irq_gnss2ap_wdt; + + struct gnss_io_t *iodev; + struct gnss_link_dev *link_dev; + +}; + +struct gnss_irq { + spinlock_t lock; + unsigned int num; + char name[MAX_NAME_LEN]; + unsigned long flags; + bool active; + bool registered; +}; + +struct gnss_gpio { + unsigned int num; + const char *label; +}; + +#define gif_dt_read_enum(np, prop, dest) \ + do { \ + u32 val; \ + if (of_property_read_u32(np, prop, &val)) \ + return -EINVAL; \ + dest = (__typeof__(dest))(val); \ + } while (0) + +#define gif_dt_read_bool(np, prop, dest) \ + do { \ + u32 val; \ + if (of_property_read_u32(np, prop, &val)) \ + return -EINVAL; \ + dest = val ? true : false; \ + } while (0) + +#define gif_dt_read_string(np, prop, dest) \ + do { \ + if (of_property_read_string(np, prop, \ + (const char **)&dest)) \ + return -EINVAL; \ + } while (0) + +#define gif_dt_read_u32(np, prop, dest) \ + do { \ + u32 val; \ + if (of_property_read_u32(np, prop, &val)) \ + return -EINVAL; \ + dest = val; \ + } while (0) +#define gif_dt_read_u32_array(np, prop, dest, sz) \ + do { \ + if (of_property_read_u32_array(np, prop, dest, (sz))) \ + return -EINVAL; \ + } while (0) + +#define LOG_TAG "gif: " +#define CALLEE (__func__) +#define CALLER (__builtin_return_address(0)) + +#define gif_err_limited(fmt, ...) \ + printk_ratelimited(KERN_ERR LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__) +#define gif_err(fmt, ...) \ + pr_err(LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__) +#define gif_debug(fmt, ...) \ + pr_debug(LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__) +#define gif_info(fmt, ...) \ + pr_info(LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__) +#define gif_trace(fmt, ...) \ + printk(KERN_DEBUG "gif: %s: %d: called(%pF): " fmt, \ + __func__, __LINE__, __builtin_return_address(0), ##__VA_ARGS__) + +int gnss_set_coredump(const char *buf, int buf_len, const char *info); + +#endif /* __GNSS_IF_H__ */ From 45d2034f226479d03b125deb996038912cc8fe12 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Mon, 2 Nov 2026 12:51:41 -0647 Subject: [PATCH 3/7] soc: google: gnssif_spi: port to Linux 7.3 Build Google's GNSS SPI interface driver as the two modules Google builds, gnss_spi.ko and gnssif.ko, under EXYNOS_GNSS_IF_SPI (Google's option name), and make it take its lines and names as the binding describes: - drivers/soc/google is new on omamobile. Its Kconfig menu and Makefile are laid out as the curated series adds them for the GSA and AoC drivers (linux-omarchy-mobile#141), so whichever lands second keeps both sets of lines. - GPIOs: of_gpio.h is gone. The two lines are GPIO descriptors, gnss2ap-spi-gpios (an input, also the interrupt, through gpiod_to_irq()) and ap2gnss-spi-gpios (an output that starts low, as Google's driver sets it). devm releases them. The node's lines are active high, so the logical values are Google's raw ones. - The error path freed the interrupt with &gc as its cookie instead of gc, which never matches; it now passes gc, as remove() does too. - A failed enable_irq_wake() no longer fails the probe: GNSS works without it, it just can't wake the AP. - device,name and device_node_name are not in the binding: the names are fixed ("KEPLER", which only appears in log lines, and "gnss_ipc", the device the GNSS daemon opens), and the macros that read them go. - Google's subsystem coredump (sscoredump) is not in this kernel. A dump the daemon writes to the coredump attribute goes to devcoredump, its counterpart here, with the reason logged. - API drift: remove() returns void, strscpy() instead of strncpy(), __poll_t and EPOLL*, __user on the read buffer, %zu for size_t; the unused gif_trace() (with the gone %pF) and the leftover linux/gpio.h includes go. - W=1: init_io_device() is static, and an unused variable in open goes. - checkpatch: the static last_trigger_time loses its "= 0". - gnss_spi gets an spi_device_id table ("gnss-spi"): without one the SPI core warns, when the driver registers, that it has none for the compatible. The SPI transfers are still Google's; the next commits change them. Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- drivers/soc/google/Kconfig | 1 + drivers/soc/google/Makefile | 1 + drivers/soc/google/gnssif_spi/Kconfig | 14 ++++ drivers/soc/google/gnssif_spi/Makefile | 6 ++ .../soc/google/gnssif_spi/gnss_io_device.c | 18 ++--- .../google/gnssif_spi/gnss_keplerctl_device.c | 73 ++++++----------- .../soc/google/gnssif_spi/gnss_link_device.c | 1 - drivers/soc/google/gnssif_spi/gnss_main.c | 80 ++++++------------- drivers/soc/google/gnssif_spi/gnss_spi.c | 7 ++ drivers/soc/google/gnssif_spi/gnss_utils.c | 4 +- drivers/soc/google/gnssif_spi/include/gnss.h | 43 +--------- 11 files changed, 86 insertions(+), 162 deletions(-) create mode 100644 drivers/soc/google/gnssif_spi/Kconfig create mode 100644 drivers/soc/google/gnssif_spi/Makefile diff --git a/drivers/soc/google/Kconfig b/drivers/soc/google/Kconfig index f4dd87cb8c..7fb05aefa5 100644 --- a/drivers/soc/google/Kconfig +++ b/drivers/soc/google/Kconfig @@ -95,6 +95,7 @@ config GS201_AOC_ALSA limits in device/cheetah/AUDIO-PLAN.md. source "drivers/soc/google/cpif/Kconfig" +source "drivers/soc/google/gnssif_spi/Kconfig" endmenu diff --git a/drivers/soc/google/Makefile b/drivers/soc/google/Makefile index db9ae79805..3cf5691f67 100644 --- a/drivers/soc/google/Makefile +++ b/drivers/soc/google/Makefile @@ -6,3 +6,4 @@ obj-$(CONFIG_GS201_AOC_RAILS) += gs201-aoc-rails.o obj-$(CONFIG_GS201_CAM_POWER) += gs201-cam-power.o obj-y += aoc/ obj-y += cpif/ +obj-$(CONFIG_EXYNOS_GNSS_IF_SPI) += gnssif_spi/ diff --git a/drivers/soc/google/gnssif_spi/Kconfig b/drivers/soc/google/gnssif_spi/Kconfig new file mode 100644 index 0000000000..22432974fa --- /dev/null +++ b/drivers/soc/google/gnssif_spi/Kconfig @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0-only +config EXYNOS_GNSS_IF_SPI + tristate "Samsung Kepler GNSS interface over SPI" + depends on SPI && GPIOLIB && OF + select WANT_DEV_COREDUMP + help + Google's driver for the Kepler GNSS core in Samsung's S5300 modem, + as in the Pixel 9a. gnss_spi binds the core's SPI device, and + gnssif binds its two request lines and gives the GNSS daemon the + character device /dev/gnss_ipc. The core runs only while the modem + is up, with firmware the daemon loads through the modem driver. + + To compile this driver as modules, choose M here: the modules will + be called gnss_spi and gnssif. diff --git a/drivers/soc/google/gnssif_spi/Makefile b/drivers/soc/google/gnssif_spi/Makefile new file mode 100644 index 0000000000..e97a1ed40f --- /dev/null +++ b/drivers/soc/google/gnssif_spi/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_EXYNOS_GNSS_IF_SPI) += gnss_spi.o + +obj-$(CONFIG_EXYNOS_GNSS_IF_SPI) += gnssif.o +gnssif-y += gnss_main.o gnss_io_device.o gnss_keplerctl_device.o gnss_link_device.o +gnssif-y += gnss_utils.o diff --git a/drivers/soc/google/gnssif_spi/gnss_io_device.c b/drivers/soc/google/gnssif_spi/gnss_io_device.c index 0fd47356bb..6852c6bfba 100644 --- a/drivers/soc/google/gnssif_spi/gnss_io_device.c +++ b/drivers/soc/google/gnssif_spi/gnss_io_device.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -57,13 +56,10 @@ static int misc_open(struct inode *inode, struct file *filp) { struct io_device *iod = container_of(filp->private_data, struct io_device, miscdev); - struct link_device *ld; int ref_cnt; filp->private_data = (void *)iod; - ld = iod->ld; - ref_cnt = atomic_inc_return(&iod->opened); gif_info("%s (opened %d)\n", iod->name, ref_cnt); @@ -85,7 +81,7 @@ static int misc_release(struct inode *inode, struct file *filp) return 0; } -static unsigned int misc_poll(struct file *filp, struct poll_table_struct *wait) +static __poll_t misc_poll(struct file *filp, struct poll_table_struct *wait) { struct io_device *iod = (struct io_device *)filp->private_data; @@ -93,7 +89,7 @@ static unsigned int misc_poll(struct file *filp, struct poll_table_struct *wait) poll_wait(filp, &iod->wq, wait); if (!skb_queue_empty(&iod->sk_rx_q)) - return POLLIN | POLLRDNORM; + return EPOLLIN | EPOLLRDNORM; return 0; } @@ -121,7 +117,7 @@ static ssize_t misc_write(struct file *filp, const char __user *data, return -ENOMEM; if (copy_from_user(buff, data, count)) { - gif_err("%s->%s: ERR! copy_from_user fail (count %ld)\n", + gif_err("%s->%s: ERR! copy_from_user fail (count %zu)\n", iod->name, ld->name, count); kfree(buff); return -EFAULT; @@ -129,7 +125,7 @@ static ssize_t misc_write(struct file *filp, const char __user *data, ret = ld->send(ld, iod, buff, roundup(count, ALIGNMENT_4BYTE)); if (ret < 0) { - gif_err("%s->%s: ERR! ld->send fail (err %d, count %ld)\n", + gif_err("%s->%s: ERR! ld->send fail (err %d, count %zu)\n", iod->name, ld->name, ret, count); kfree(buff); return ret; @@ -139,8 +135,8 @@ static ssize_t misc_write(struct file *filp, const char __user *data, return count; } -static ssize_t misc_read(struct file *filp, char *buf, size_t count, - loff_t *fpos) +static ssize_t misc_read(struct file *filp, char __user *buf, size_t count, + loff_t *fpos) { struct io_device *iod = (struct io_device *)filp->private_data; struct sk_buff_head *rxq = &iod->sk_rx_q; @@ -197,7 +193,7 @@ static const struct file_operations misc_io_fops = { .read = misc_read, }; -int init_io_device(struct io_device *iod, struct device *dev) +static int init_io_device(struct io_device *iod, struct device *dev) { int ret = 0; diff --git a/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c b/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c index d005bec9ff..397985600e 100644 --- a/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c +++ b/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c @@ -6,8 +6,7 @@ #include #include -#include -#include +#include #include #include "include/gnss.h" @@ -37,19 +36,19 @@ static irqreturn_t kepler_spi_irq_handler(int irq, void *data) static int kepler_spi_status(struct gnss_ctl *gc) { - return gpio_get_value(gc->gpio_gnss2ap_spi.num); + return gpiod_get_value(gc->gpio_gnss2ap_spi.desc); } static void kepler_send_betp_int(struct gnss_ctl *gc, int value) { gif_debug("About to Set ap2gnss_spi value to %d\n", value); - gpio_set_value(gc->gpio_ap2gnss_spi.num, value); + gpiod_set_value(gc->gpio_ap2gnss_spi.desc, value); gif_debug("complete to set ap2gnss_spi value to %d\n", value); } static int kepler_betp_int_status(struct gnss_ctl *gc) { - return gpio_get_value(gc->gpio_ap2gnss_spi.num); + return gpiod_get_value(gc->gpio_ap2gnss_spi.desc); } static void gnss_set_ops(struct gnss_ctl *gc) @@ -62,29 +61,24 @@ static void gnss_set_ops(struct gnss_ctl *gc) static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) { int ret; - struct platform_device *pdev; - struct device_node *np; gnss_set_ops(gc); dev_set_drvdata(gc->dev, gc); - pdev = to_platform_device(gc->dev); - np = pdev->dev.of_node; - /* GNSS2AP SPI INT */ - gc->gpio_gnss2ap_spi.num = of_get_named_gpio(np, "gpio_gnss2ap_spi", 0); - if (gc->gpio_gnss2ap_spi.num < 0) { - gif_err("Can't get gpio_gnss2ap_spi!\n"); - return -ENODEV; - } gc->gpio_gnss2ap_spi.label = "GNSS2AP_SPI"; - ret = gpio_request(gc->gpio_gnss2ap_spi.num, gc->gpio_gnss2ap_spi.label); - if (ret) { - gif_err("Request gpio fail - gpio_gnss2ap_spi.num failed(%d)\n", ret); + gc->gpio_gnss2ap_spi.desc = devm_gpiod_get(gc->dev, "gnss2ap-spi", GPIOD_IN); + if (IS_ERR(gc->gpio_gnss2ap_spi.desc)) { + gif_err("Can't get gnss2ap-spi-gpios!\n"); + return PTR_ERR(gc->gpio_gnss2ap_spi.desc); + } + ret = gpiod_to_irq(gc->gpio_gnss2ap_spi.desc); + if (ret < 0) { + gif_err("gnss2ap-spi-gpios has no interrupt (%d)\n", ret); return ret; } - gc->irq_gnss2ap_spi.num = gpio_to_irq(gc->gpio_gnss2ap_spi.num); + gc->irq_gnss2ap_spi.num = ret; /* Disable the interrupt until gnss_main.c probe function finish */ gif_configure_irq(&gc->irq_gnss2ap_spi, gc->irq_gnss2ap_spi.num, @@ -93,54 +87,33 @@ static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) ret = gif_request_irq(&gc->irq_gnss2ap_spi, kepler_spi_irq_handler, gc); if (ret) { gif_err("Request irq fail - kepler_spi_irq_handler(%d)\n", ret); - goto free_gpio_gnss2ap; + return ret; } + /* Without a wake source GNSS still works; it can't wake the AP. */ ret = enable_irq_wake(gc->irq_gnss2ap_spi.num); - if (ret) { + if (ret) gif_err("Failed to enable wakeup_source - enable_irq_wake(%d)\n", ret); - goto free_gpio_irq; - } - gc->gpio_ap2gnss_spi.num = - of_get_named_gpio(np, "gpio_ap2gnss_spi", 0); - if (gc->gpio_ap2gnss_spi.num < 0) { - gif_err("Can't get gpio_ap2gnss_spi!\n"); - ret = -ENODEV; - goto free_gpio_irq; - } + /* The line starts low, as Google's driver sets it. */ gc->gpio_ap2gnss_spi.label = "AP2GNSS_SPI"; - ret = gpio_request(gc->gpio_ap2gnss_spi.num, gc->gpio_ap2gnss_spi.label); - if (ret) { - gif_err("Request gpio fail - gpio_ap2gnss_spi.num failed(%d)\n", ret); + gc->gpio_ap2gnss_spi.desc = devm_gpiod_get(gc->dev, "ap2gnss-spi", GPIOD_OUT_LOW); + if (IS_ERR(gc->gpio_ap2gnss_spi.desc)) { + ret = PTR_ERR(gc->gpio_ap2gnss_spi.desc); + gif_err("Can't get ap2gnss-spi-gpios (%d)\n", ret); goto free_gpio_irq; } - ret = gpio_direction_output(gc->gpio_ap2gnss_spi.num, 0); - if (ret) { - gif_err("Set gpio direction fail - gpio_ap2gnss_spi.num failed(%d)\n", ret); - goto free_gpio_ap2gnss; - } - - /* initialize gpio values to all zero */ - gpio_set_value(gc->gpio_ap2gnss_spi.num, 0); - init_completion(&gc->gnss_rdy_cmpl); atomic_set(&gc->wait_rdy, 0); atomic_set(&gc->tx_in_progress, 0); atomic_set(&gc->rx_in_progress, 0); - return ret; - -free_gpio_ap2gnss: - gpio_free(gc->gpio_ap2gnss_spi.num); + return 0; free_gpio_irq: - free_irq(gc->irq_gnss2ap_spi.num, &gc); - -free_gpio_gnss2ap: - gpio_free(gc->gpio_gnss2ap_spi.num); + free_irq(gc->irq_gnss2ap_spi.num, gc); return ret; diff --git a/drivers/soc/google/gnssif_spi/gnss_link_device.c b/drivers/soc/google/gnssif_spi/gnss_link_device.c index abd81efe77..fca0798a66 100644 --- a/drivers/soc/google/gnssif_spi/gnss_link_device.c +++ b/drivers/soc/google/gnssif_spi/gnss_link_device.c @@ -5,7 +5,6 @@ */ #include -#include #include #include #include diff --git a/drivers/soc/google/gnssif_spi/gnss_main.c b/drivers/soc/google/gnssif_spi/gnss_main.c index fddc1c233e..0d0dd279c0 100644 --- a/drivers/soc/google/gnssif_spi/gnss_main.c +++ b/drivers/soc/google/gnssif_spi/gnss_main.c @@ -3,36 +3,24 @@ * Copyright (C) 2022 Samsung Electronics. * */ +#include #include -#include +#include #include #include #include -#include #include +#include #include "gnss_prj.h" #include "gnss_utils.h" -#define DEVICE_NAME "gnss" - -static void sscd_release(struct device *dev); - -static struct sscd_platform_data sscd_pdata; - -static struct platform_device sscd_dev = { - .name = DEVICE_NAME, - .driver_override = SSCD_NAME, - .id = -1, - .dev = { - .platform_data = &sscd_pdata, - .release = sscd_release, - }, -}; - -static void sscd_release(struct device *dev) +/* Hand a dump to devcoredump, which frees it. */ +static void gnss_set_coredump(struct device *dev, void *dump, size_t len, + const char *reason) { - gif_info("%s: enter\n", __FUNCTION__); + gif_err("GNSS coredump: %s (%zu bytes)\n", reason, len); + dev_coredumpv(dev, dump, len, GFP_KERNEL); } static ssize_t coredump_store(struct device *dev, struct device_attribute *attr, const char *buf, @@ -43,7 +31,7 @@ static ssize_t coredump_store(struct device *dev, struct device_attribute *attr, char *coredump = NULL; char *reason = NULL; int length = 0; - static time64_t last_trigger_time = 0; + static time64_t last_trigger_time; time64_t trigger_time = ktime_get_seconds(); // Ignore the trigger time less than 30 seconds @@ -64,19 +52,18 @@ static ssize_t coredump_store(struct device *dev, struct device_attribute *attr, memcpy(reason, buf, length); reason[length] = '\0'; length = strlen(next + 1); - coredump = kmalloc(length, GFP_KERNEL); + coredump = vmalloc(length); if (!coredump) { gif_err("Allocate crash coredump failed\n"); goto clean; } memcpy(coredump, next + 1, length); - gnss_set_coredump(coredump, length, reason); + gnss_set_coredump(dev, coredump, length, reason); + coredump = NULL; } clean: - if (reason) - kfree(reason); - if (coredump) - kfree(coredump); + kfree(reason); + vfree(coredump); return count; } @@ -84,8 +71,13 @@ static DEVICE_ATTR_WO(coredump); static int parse_dt_common_pdata(struct device_node *np, struct gnss_pdata *pdata) { - gif_dt_read_string(np, "device,name", pdata->name); - gif_dt_read_string(np, "device_node_name", pdata->node_name); + /* + * Google's device tree names these in device,name and + * device_node_name. They describe no hardware, and the GNSS daemon + * opens /dev/gnss_ipc, so they are fixed here. + */ + pdata->name = "KEPLER"; + pdata->node_name = "gnss_ipc"; gif_info("device name: %s node name: %s\n", pdata->name, pdata->node_name); return 0; @@ -120,25 +112,17 @@ static struct gnss_pdata *gnss_if_parse_dt_pdata(struct device *dev) return ERR_PTR(-EINVAL); } -static int gnss_remove(struct platform_device *pdev) +static void gnss_remove(struct platform_device *pdev) { struct gnss_ctl *gc = platform_get_drvdata(pdev); gif_disable_irq_sync(&gc->irq_gnss2ap_spi); - gpio_set_value(gc->gpio_ap2gnss_spi.num, 0); - - free_irq(gc->irq_gnss2ap_spi.num, &gc); - - gpio_free(gc->gpio_ap2gnss_spi.num); - - gpio_free(gc->gpio_gnss2ap_spi.num); + gpiod_set_value(gc->gpio_ap2gnss_spi.desc, 0); - platform_device_unregister(&sscd_dev); + free_irq(gc->irq_gnss2ap_spi.num, gc); device_remove_file(&pdev->dev, &dev_attr_coredump); - - return 0; } static int gnss_probe(struct platform_device *pdev) @@ -186,8 +170,6 @@ static int gnss_probe(struct platform_device *pdev) platform_set_drvdata(pdev, gc); - platform_device_register(&sscd_dev); - if (device_create_file(dev, &dev_attr_coredump)) gif_err("Unable to create sysfs coredump entry"); @@ -230,19 +212,5 @@ static struct platform_driver gnss_driver = { module_platform_driver(gnss_driver); -int gnss_set_coredump(const char *buf, int buf_len, const char *info) -{ - struct sscd_platform_data *pdata = dev_get_platdata(&sscd_dev.dev); - struct sscd_segment seg; - - if (pdata->sscd_report) { - memset(&seg, 0, sizeof(seg)); - seg.addr = (void *)buf; - seg.size = buf_len; - pdata->sscd_report(&sscd_dev, &seg, 1, 0, info); - } - return 0; -} - MODULE_DESCRIPTION("Exynos GNSS interface driver"); MODULE_LICENSE("GPL"); diff --git a/drivers/soc/google/gnssif_spi/gnss_spi.c b/drivers/soc/google/gnssif_spi/gnss_spi.c index 83680d08dc..eeec109d1f 100644 --- a/drivers/soc/google/gnssif_spi/gnss_spi.c +++ b/drivers/soc/google/gnssif_spi/gnss_spi.c @@ -95,7 +95,14 @@ static const struct of_device_id gnss_spi_dt_match[] = { }; MODULE_DEVICE_TABLE(of, gnss_spi_dt_match); +static const struct spi_device_id gnss_spi_ids[] = { + { "gnss-spi" }, + { } +}; +MODULE_DEVICE_TABLE(spi, gnss_spi_ids); + static struct spi_driver gnss_spi_driver = { + .id_table = gnss_spi_ids, .probe = gnss_spi_probe, .remove = gnss_spi_remove, .driver = { diff --git a/drivers/soc/google/gnssif_spi/gnss_utils.c b/drivers/soc/google/gnssif_spi/gnss_utils.c index ed4b4c9d12..0825d80b85 100644 --- a/drivers/soc/google/gnssif_spi/gnss_utils.c +++ b/drivers/soc/google/gnssif_spi/gnss_utils.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include "gnss_prj.h" #include "gnss_utils.h" @@ -19,7 +19,7 @@ void gif_configure_irq(struct gnss_irq *irq, unsigned int num, const char *name, { spin_lock_init(&irq->lock); irq->num = num; - strncpy(irq->name, name, sizeof(irq->name) - 1); + strscpy(irq->name, name, sizeof(irq->name)); irq->flags = flags; gif_info("name:%s num:%d flags:0x%08lX\n", name, num, flags); } diff --git a/drivers/soc/google/gnssif_spi/include/gnss.h b/drivers/soc/google/gnssif_spi/include/gnss.h index e7d4cc421b..ccbd91faad 100644 --- a/drivers/soc/google/gnssif_spi/include/gnss.h +++ b/drivers/soc/google/gnssif_spi/include/gnss.h @@ -34,46 +34,10 @@ struct gnss_irq { }; struct gnss_gpio { - unsigned int num; + struct gpio_desc *desc; const char *label; }; -#define gif_dt_read_enum(np, prop, dest) \ - do { \ - u32 val; \ - if (of_property_read_u32(np, prop, &val)) \ - return -EINVAL; \ - dest = (__typeof__(dest))(val); \ - } while (0) - -#define gif_dt_read_bool(np, prop, dest) \ - do { \ - u32 val; \ - if (of_property_read_u32(np, prop, &val)) \ - return -EINVAL; \ - dest = val ? true : false; \ - } while (0) - -#define gif_dt_read_string(np, prop, dest) \ - do { \ - if (of_property_read_string(np, prop, \ - (const char **)&dest)) \ - return -EINVAL; \ - } while (0) - -#define gif_dt_read_u32(np, prop, dest) \ - do { \ - u32 val; \ - if (of_property_read_u32(np, prop, &val)) \ - return -EINVAL; \ - dest = val; \ - } while (0) -#define gif_dt_read_u32_array(np, prop, dest, sz) \ - do { \ - if (of_property_read_u32_array(np, prop, dest, (sz))) \ - return -EINVAL; \ - } while (0) - #define LOG_TAG "gif: " #define CALLEE (__func__) #define CALLER (__builtin_return_address(0)) @@ -86,10 +50,5 @@ struct gnss_gpio { pr_debug(LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__) #define gif_info(fmt, ...) \ pr_info(LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__) -#define gif_trace(fmt, ...) \ - printk(KERN_DEBUG "gif: %s: %d: called(%pF): " fmt, \ - __func__, __LINE__, __builtin_return_address(0), ##__VA_ARGS__) - -int gnss_set_coredump(const char *buf, int buf_len, const char *info); #endif /* __GNSS_IF_H__ */ From fa196c0c662c053fc6bf558033a729df15fdc749 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:37:29 -0647 Subject: [PATCH 4/7] soc: google: gnssif_spi: release everything on probe failure and remove Google's gnssif leaves most of what it sets up behind: - Every create_*_device() returns NULL on failure and the probe returns -ENOMEM, so an -EPROBE_DEFER from the GPIO lookup is lost. On the Pixel 9a the core's request line is on GPA5 of the FAR_ALIVE pin controller, which can bind after this driver; the driver would then never bind. Return the error and pass it on with dev_err_probe(). - A failure after the interrupt is requested frees the control data with the handler still installed, and the probe's last message dereferences pdata, which is NULL when the node is missing. - The work queue, the wakeup source and the character device are never released, on a failed probe or on remove, so after rmmod /dev/gnss_ipc points at freed memory. remove() does not stop the read work either, which enables the interrupt again when it ends. - gif_disable_irq_sync() takes the interrupt's lock without disabling interrupts, and the handler takes the same lock (through gif_disable_irq_nosync()): a deadlock if the interrupt arrives on that CPU. Use devm for the interrupt (and its wake setting), the work queue (an ordered one, the current form of Google's unbound queue with one active work item), the wakeup source and the character device. In remove(), disable the interrupt, wait for the read work, and disable it again before devm releases the rest. The coredump attribute moves to the driver's dev_groups. Take the lock with interrupts disabled in gif_disable_irq_sync(). Suppress the bind attributes, as gnss_spi already does: the module can't be removed while the GNSS daemon holds /dev/gnss_ipc open, but a manual unbind could. Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- .../soc/google/gnssif_spi/gnss_io_device.c | 30 ++++++++-- .../google/gnssif_spi/gnss_keplerctl_device.c | 29 ++++++---- .../soc/google/gnssif_spi/gnss_link_device.c | 11 ++-- drivers/soc/google/gnssif_spi/gnss_main.c | 58 +++++++++---------- drivers/soc/google/gnssif_spi/gnss_utils.c | 17 +++--- drivers/soc/google/gnssif_spi/gnss_utils.h | 3 +- 6 files changed, 87 insertions(+), 61 deletions(-) diff --git a/drivers/soc/google/gnssif_spi/gnss_io_device.c b/drivers/soc/google/gnssif_spi/gnss_io_device.c index 6852c6bfba..0bbd224c71 100644 --- a/drivers/soc/google/gnssif_spi/gnss_io_device.c +++ b/drivers/soc/google/gnssif_spi/gnss_io_device.c @@ -193,6 +193,19 @@ static const struct file_operations misc_io_fops = { .read = misc_read, }; +static void gnss_wlock_unregister(void *data) +{ + gnssif_wlock_unregister(data); +} + +static void gnss_misc_deregister(void *data) +{ + struct io_device *iod = data; + + misc_deregister(&iod->miscdev); + skb_queue_purge(&iod->sk_rx_q); +} + static int init_io_device(struct io_device *iod, struct device *dev) { int ret = 0; @@ -213,14 +226,22 @@ static int init_io_device(struct io_device *iod, struct device *dev) gif_err("%s: wakeup_source_register fail\n", iod->name); return -EINVAL; } + ret = devm_add_action_or_reset(dev, gnss_wlock_unregister, iod->ws); + if (ret) + return ret; ret = misc_register(&iod->miscdev); - if (ret) + if (ret) { gif_err("%s: ERR! misc_register failed\n", iod->name); + return ret; + } + ret = devm_add_action_or_reset(dev, gnss_misc_deregister, iod); + if (ret) + return ret; gif_info("---\n"); - return ret; + return 0; } struct io_device *create_io_device(struct platform_device *pdev, @@ -234,7 +255,7 @@ struct io_device *create_io_device(struct platform_device *pdev, iod = devm_kzalloc(dev, sizeof(struct io_device), GFP_KERNEL); if (!iod) { gif_err("iod is NULL\n"); - return NULL; + return ERR_PTR(-ENOMEM); } iod->name = pdata->node_name; @@ -248,9 +269,8 @@ struct io_device *create_io_device(struct platform_device *pdev, ret = init_io_device(iod, dev); if (ret) { - devm_kfree(dev, iod); gif_err("init_io_device fail (%d)\n", ret); - return NULL; + return ERR_PTR(ret); } gif_info("---\n"); diff --git a/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c b/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c index 397985600e..0288031b69 100644 --- a/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c +++ b/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c @@ -51,6 +51,13 @@ static int kepler_betp_int_status(struct gnss_ctl *gc) return gpiod_get_value(gc->gpio_ap2gnss_spi.desc); } +static void gnss_disable_irq_wake(void *data) +{ + struct gnss_ctl *gc = data; + + disable_irq_wake(gc->irq_gnss2ap_spi.num); +} + static void gnss_set_ops(struct gnss_ctl *gc) { gc->ops.gnss_spi_status = kepler_spi_status; @@ -84,7 +91,7 @@ static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) gif_configure_irq(&gc->irq_gnss2ap_spi, gc->irq_gnss2ap_spi.num, "kepler_spi_irq_handler", IRQF_TRIGGER_HIGH | IRQF_NO_AUTOEN); - ret = gif_request_irq(&gc->irq_gnss2ap_spi, kepler_spi_irq_handler, gc); + ret = gif_request_irq(gc->dev, &gc->irq_gnss2ap_spi, kepler_spi_irq_handler, gc); if (ret) { gif_err("Request irq fail - kepler_spi_irq_handler(%d)\n", ret); return ret; @@ -92,8 +99,13 @@ static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) /* Without a wake source GNSS still works; it can't wake the AP. */ ret = enable_irq_wake(gc->irq_gnss2ap_spi.num); - if (ret) + if (ret) { gif_err("Failed to enable wakeup_source - enable_irq_wake(%d)\n", ret); + } else { + ret = devm_add_action_or_reset(gc->dev, gnss_disable_irq_wake, gc); + if (ret) + return ret; + } /* The line starts low, as Google's driver sets it. */ gc->gpio_ap2gnss_spi.label = "AP2GNSS_SPI"; @@ -101,7 +113,7 @@ static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) if (IS_ERR(gc->gpio_ap2gnss_spi.desc)) { ret = PTR_ERR(gc->gpio_ap2gnss_spi.desc); gif_err("Can't get ap2gnss-spi-gpios (%d)\n", ret); - goto free_gpio_irq; + return ret; } init_completion(&gc->gnss_rdy_cmpl); @@ -111,12 +123,6 @@ static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) atomic_set(&gc->rx_in_progress, 0); return 0; - -free_gpio_irq: - free_irq(gc->irq_gnss2ap_spi.num, gc); - - return ret; - } struct gnss_ctl *create_ctl_device(struct platform_device *pdev) @@ -128,7 +134,7 @@ struct gnss_ctl *create_ctl_device(struct platform_device *pdev) gc = devm_kzalloc(dev, sizeof(struct gnss_ctl), GFP_KERNEL); if (!gc) - return NULL; + return ERR_PTR(-ENOMEM); gc->dev = dev; gc->pdata = pdata; gc->name = pdata->name; @@ -137,8 +143,7 @@ struct gnss_ctl *create_ctl_device(struct platform_device *pdev) if (ret) { gif_err("%s: init_ctl_device fail (err %d)\n", pdata->name, ret); - devm_kfree(dev, gc); - return NULL; + return ERR_PTR(ret); } return gc; diff --git a/drivers/soc/google/gnssif_spi/gnss_link_device.c b/drivers/soc/google/gnssif_spi/gnss_link_device.c index fca0798a66..1d402ffba6 100644 --- a/drivers/soc/google/gnssif_spi/gnss_link_device.c +++ b/drivers/soc/google/gnssif_spi/gnss_link_device.c @@ -215,13 +215,13 @@ struct link_device *create_link_device(struct platform_device *pdev) pdata = (struct gnss_pdata *)dev->platform_data; if (!pdata) { gif_err("ERR! gnss == NULL\n"); - return NULL; + return ERR_PTR(-EINVAL); } ld = devm_kzalloc(dev, sizeof(struct link_device), GFP_KERNEL); if (!ld) { gif_err("ERR! link device kzalloc fail\n"); - return NULL; + return ERR_PTR(-ENOMEM); } ld->pdata = pdata; @@ -231,16 +231,15 @@ struct link_device *create_link_device(struct platform_device *pdev) ld->max_spi_rx_size = MAX_SPI_RX_SIZE; ld->spi_tx_size = DEFAULT_SPI_TX_SIZE; - ld->rx_wq = alloc_workqueue("gnss_spi_wq", - __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND, 1); + /* Google's: unbound, one work item at a time */ + ld->rx_wq = devm_alloc_ordered_workqueue(dev, "gnss_spi_wq", WQ_MEM_RECLAIM); if (!ld->rx_wq) { gif_err("alloc_workqueue() error\n"); - goto exit; + return ERR_PTR(-ENOMEM); } INIT_DELAYED_WORK(&ld->rx_dwork, rx_work); gif_info("---\n"); -exit: return ld; } diff --git a/drivers/soc/google/gnssif_spi/gnss_main.c b/drivers/soc/google/gnssif_spi/gnss_main.c index 0d0dd279c0..5c0bb81796 100644 --- a/drivers/soc/google/gnssif_spi/gnss_main.c +++ b/drivers/soc/google/gnssif_spi/gnss_main.c @@ -69,6 +69,12 @@ static ssize_t coredump_store(struct device *dev, struct device_attribute *attr, static DEVICE_ATTR_WO(coredump); +static struct attribute *gnss_attrs[] = { + &dev_attr_coredump.attr, + NULL +}; +ATTRIBUTE_GROUPS(gnss); + static int parse_dt_common_pdata(struct device_node *np, struct gnss_pdata *pdata) { /* @@ -115,14 +121,18 @@ static struct gnss_pdata *gnss_if_parse_dt_pdata(struct device *dev) static void gnss_remove(struct platform_device *pdev) { struct gnss_ctl *gc = platform_get_drvdata(pdev); + struct link_device *ld = gc->iod->ld; + /* + * The read work enables the interrupt again when it ends, so stop it + * before the interrupt is disabled for good. devm then releases the + * device node, the work queue and the interrupt. + */ + gif_disable_irq_sync(&gc->irq_gnss2ap_spi); + cancel_delayed_work_sync(&ld->rx_dwork); gif_disable_irq_sync(&gc->irq_gnss2ap_spi); gpiod_set_value(gc->gpio_ap2gnss_spi.desc, 0); - - free_irq(gc->irq_gnss2ap_spi.num, gc); - - device_remove_file(&pdev->dev, &dev_attr_coredump); } static int gnss_probe(struct platform_device *pdev) @@ -139,7 +149,7 @@ static int gnss_probe(struct platform_device *pdev) if (!dev->of_node) { gif_err("No DT data!\n"); - goto probe_fail; + return -ENODEV; } pdata = gnss_if_parse_dt_pdata(dev); @@ -148,46 +158,32 @@ static int gnss_probe(struct platform_device *pdev) return PTR_ERR(pdata); } + /* The lines' controller may come later: keep -EPROBE_DEFER. */ gc = create_ctl_device(pdev); - if (!gc) { - gif_err("%s: Could not create gc\n", pdata->name); - goto probe_fail; - } + if (IS_ERR(gc)) + return dev_err_probe(dev, PTR_ERR(gc), "%s: Could not create gc\n", + pdata->name); ld = create_link_device(pdev); - if (!ld) { - gif_err("%s: Could not create ld\n", pdata->name); - goto free_gc; - } + if (IS_ERR(ld)) + return dev_err_probe(dev, PTR_ERR(ld), "%s: Could not create ld\n", + pdata->name); ld->gc = gc; iod = create_io_device(pdev, ld, gc, pdata); - if (!iod) { - gif_err("%s: Could not create iod\n", pdata->name); - goto free_ld; - } + if (IS_ERR(iod)) + return dev_err_probe(dev, PTR_ERR(iod), "%s: Could not create iod\n", + pdata->name); platform_set_drvdata(pdev, gc); - if (device_create_file(dev, &dev_attr_coredump)) - gif_err("Unable to create sysfs coredump entry"); - /* wa: to prevent wrong irq handling during probe */ gif_enable_irq(&gc->irq_gnss2ap_spi); gif_info("%s: ---\n", pdev->name); return 0; - -free_ld: - devm_kfree(dev, ld); -free_gc: - devm_kfree(dev, gc); -probe_fail: - gif_err("%s: xxx\n", pdata->name); - - return -ENOMEM; } static const struct of_device_id gnss_dt_match[] = { @@ -203,7 +199,9 @@ static struct platform_driver gnss_driver = { .remove = gnss_remove, .driver = { .name = "gnss_interface", - .owner = THIS_MODULE, + .dev_groups = gnss_groups, + /* gnssd may hold the device node open */ + .suppress_bind_attrs = true, #if IS_ENABLED(CONFIG_OF) .of_match_table = of_match_ptr(gnss_dt_match), #endif diff --git a/drivers/soc/google/gnssif_spi/gnss_utils.c b/drivers/soc/google/gnssif_spi/gnss_utils.c index 0825d80b85..36499d30c3 100644 --- a/drivers/soc/google/gnssif_spi/gnss_utils.c +++ b/drivers/soc/google/gnssif_spi/gnss_utils.c @@ -24,11 +24,12 @@ void gif_configure_irq(struct gnss_irq *irq, unsigned int num, const char *name, gif_info("name:%s num:%d flags:0x%08lX\n", name, num, flags); } -int gif_request_irq(struct gnss_irq *irq, irq_handler_t isr, void *data) +int gif_request_irq(struct device *dev, struct gnss_irq *irq, irq_handler_t isr, + void *data) { int ret; - ret = request_irq(irq->num, isr, irq->flags, irq->name, data); + ret = devm_request_irq(dev, irq->num, isr, irq->flags, irq->name, data); if (ret) { gif_err("%s: ERR! request_irq fail (%d)\n", irq->name, ret); return ret; @@ -80,25 +81,27 @@ void gif_disable_irq_nosync(struct gnss_irq *irq) void gif_disable_irq_sync(struct gnss_irq *irq) { + unsigned long flags; + if (irq->registered == false) return; - spin_lock(&irq->lock); + spin_lock_irqsave(&irq->lock, flags); if (!irq->active) { - spin_unlock(&irq->lock); + spin_unlock_irqrestore(&irq->lock, flags); gif_debug("%s(#%d) is not active <%ps>\n", irq->name, irq->num, CALLER); return; } - spin_unlock(&irq->lock); + spin_unlock_irqrestore(&irq->lock, flags); disable_irq(irq->num); - spin_lock(&irq->lock); + spin_lock_irqsave(&irq->lock, flags); irq->active = false; - spin_unlock(&irq->lock); + spin_unlock_irqrestore(&irq->lock, flags); gif_debug("%s(#%d) is disabled <%ps>\n", irq->name, irq->num, CALLER); diff --git a/drivers/soc/google/gnssif_spi/gnss_utils.h b/drivers/soc/google/gnssif_spi/gnss_utils.h index 9125e6a410..7b089e4e08 100644 --- a/drivers/soc/google/gnssif_spi/gnss_utils.h +++ b/drivers/soc/google/gnssif_spi/gnss_utils.h @@ -76,7 +76,8 @@ static inline int gnssif_wlock_active(struct wakeup_source *ws) /* gnss irq */ void gif_configure_irq(struct gnss_irq *irq, unsigned int num, const char *name, unsigned long flags); -int gif_request_irq(struct gnss_irq *irq, irq_handler_t isr, void *data); +int gif_request_irq(struct device *dev, struct gnss_irq *irq, irq_handler_t isr, + void *data); void gif_enable_irq(struct gnss_irq *irq); void gif_disable_irq_nosync(struct gnss_irq *irq); void gif_disable_irq_sync(struct gnss_irq *irq); From dbb99b940afdaeebf4061b83e8dcf9625a198881 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:38:17 -0647 Subject: [PATCH 5/7] soc: google: gnssif_spi: bind gnssif only once the SPI device is there gnssif reads and writes through functions gnss_spi.ko exports, which use the SPI device gnss_spi stored at probe. Loading gnssif loads gnss_spi for its symbols, but nothing makes the SPI device bind first: if gnssif binds before it (built in, or the SPI controller probing later), the first request from the core makes the read work call spi_sync() on a NULL device. Google's header also defines that device holder static, so every file that includes it gets an unused copy. Keep the holder in gnss_spi.c, export gnss_spi_bound(), and defer gnssif's probe until it is true; the SPI device's binding retries the deferred probe. The transfer functions return -ENODEV without a device, and gnss_spi's remove() clears it. The exports become EXPORT_SYMBOL_GPL, and the driver structure loses .owner, which the SPI core sets. Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- drivers/soc/google/gnssif_spi/gnss_main.c | 9 +++++++++ drivers/soc/google/gnssif_spi/gnss_spi.c | 22 ++++++++++++++++++---- drivers/soc/google/gnssif_spi/gnss_spi.h | 7 +++---- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/drivers/soc/google/gnssif_spi/gnss_main.c b/drivers/soc/google/gnssif_spi/gnss_main.c index 5c0bb81796..f6ac72ec03 100644 --- a/drivers/soc/google/gnssif_spi/gnss_main.c +++ b/drivers/soc/google/gnssif_spi/gnss_main.c @@ -13,6 +13,7 @@ #include #include "gnss_prj.h" +#include "gnss_spi.h" #include "gnss_utils.h" /* Hand a dump to devcoredump, which frees it. */ @@ -152,6 +153,14 @@ static int gnss_probe(struct platform_device *pdev) return -ENODEV; } + /* + * The core's requests are read over gnss_spi: without its device + * the read work would find nothing to read from while the request + * line stays high. + */ + if (!gnss_spi_bound()) + return dev_err_probe(dev, -EPROBE_DEFER, "no GNSS SPI device yet\n"); + pdata = gnss_if_parse_dt_pdata(dev); if (IS_ERR(pdata)) { gif_err("DT parse error!\n"); diff --git a/drivers/soc/google/gnssif_spi/gnss_spi.c b/drivers/soc/google/gnssif_spi/gnss_spi.c index eeec109d1f..dd88cd4c5b 100644 --- a/drivers/soc/google/gnssif_spi/gnss_spi.c +++ b/drivers/soc/google/gnssif_spi/gnss_spi.c @@ -14,6 +14,14 @@ #include "include/gnss.h" #include "gnss_spi.h" +static struct gnss_spi gnss_if; + +/* gnssif binds only once the SPI device is there. */ +bool gnss_spi_bound(void) +{ + return !!READ_ONCE(gnss_if.spi); +} +EXPORT_SYMBOL_GPL(gnss_spi_bound); int gnss_spi_send(char *buff, unsigned int size, char *rx_buff) { @@ -21,6 +29,9 @@ int gnss_spi_send(char *buff, unsigned int size, char *rx_buff) struct spi_message msg; struct spi_transfer tx; + if (!gnss_spi_bound()) + return -ENODEV; + memset(&tx, 0, sizeof(struct spi_transfer)); spi_message_init(&msg); tx.len = size; @@ -35,7 +46,7 @@ int gnss_spi_send(char *buff, unsigned int size, char *rx_buff) return ret; } -EXPORT_SYMBOL(gnss_spi_send); +EXPORT_SYMBOL_GPL(gnss_spi_send); int gnss_spi_recv(char *buff, unsigned int size) { @@ -43,6 +54,9 @@ int gnss_spi_recv(char *buff, unsigned int size) struct spi_message msg; struct spi_transfer rx; + if (!gnss_spi_bound()) + return -ENODEV; + memset(&rx, 0, sizeof(struct spi_transfer)); rx.len = size; rx.rx_buf = buff; @@ -56,7 +70,7 @@ int gnss_spi_recv(char *buff, unsigned int size) return ret; } -EXPORT_SYMBOL(gnss_spi_recv); +EXPORT_SYMBOL_GPL(gnss_spi_recv); static int gnss_spi_probe(struct spi_device *spi) { @@ -71,7 +85,7 @@ static int gnss_spi_probe(struct spi_device *spi) goto err_setup; } spi_set_drvdata(spi, &gnss_if); - gnss_if.spi = spi; + WRITE_ONCE(gnss_if.spi, spi); gif_info("---\n"); return 0; @@ -86,6 +100,7 @@ static void gnss_spi_remove (struct spi_device *spi) { struct gnss_spi *gnss_if = spi_get_drvdata(spi); + WRITE_ONCE(gnss_if->spi, NULL); mutex_destroy(&gnss_if->lock); } @@ -107,7 +122,6 @@ static struct spi_driver gnss_spi_driver = { .remove = gnss_spi_remove, .driver = { .name = "gnss_spi", - .owner = THIS_MODULE, .of_match_table = of_match_ptr(gnss_spi_dt_match), .suppress_bind_attrs = true, }, diff --git a/drivers/soc/google/gnssif_spi/gnss_spi.h b/drivers/soc/google/gnssif_spi/gnss_spi.h index 553c40a0d1..a3d41fbaa2 100644 --- a/drivers/soc/google/gnssif_spi/gnss_spi.h +++ b/drivers/soc/google/gnssif_spi/gnss_spi.h @@ -20,9 +20,8 @@ struct gnss_spi { struct mutex lock; }; -static struct gnss_spi gnss_if; - -extern int gnss_spi_send(char *buff, unsigned int size, char *recv_buff); -extern int gnss_spi_recv(char *buff, unsigned int size); +bool gnss_spi_bound(void); +int gnss_spi_send(char *buff, unsigned int size, char *recv_buff); +int gnss_spi_recv(char *buff, unsigned int size); #endif /* __GNSS_SPI_H__ */ From cf6a09477dcc4575dd7c279160ed3b6e01d785a7 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:39:07 -0647 Subject: [PATCH 6/7] soc: google: gnssif_spi: swap the 32-bit words here, in 64-byte transfers Google's gnss_spi sends 32-bit words in one transfer per call, and relies on Google's SPI controller driver for two things this kernel's spi-s3c64xx does not do: - The board's SPI 21 node has swap-mode = <1>, for which Google's driver programs the controller's SWAP_CFG to swap the bytes and half-words of each word on TX and RX, so the bytes go on the wire in memory order. spi-s3c64xx shifts a 32-bit word out most significant byte first, which on a little-endian CPU reverses every group of four bytes. Swap each word in gnss_spi instead, on the way out and on the way in, and give the caller its TX buffer back unswapped. Every transfer is a whole number of words (gnssif rounds writes up to 4 bytes and reads 64), so an odd size is refused. - The core's data comes in 64-byte blocks, and reads are one block per transfer. spi-s3c64xx moves a transfer that fills the 64-byte FIFO in one piece on zumapro, but cuts a longer one into 60-byte pieces. Send writes as one message of transfers of 64 bytes at most, as the reads are. On the Pixel 9a (MODEM-PLAN G3, 2026-10-30, the bring-up driver of linux-omarchy-mobile#137 with these two settings, SPI mode 0, stock's feedback delay 0 and the controller driving chip select) the receiver got its position fix: in a session by a window 54 of 56 GGA sentences had a fix, with 10 satellites, HDOP 1.2, and no CHPP checksum error, ACK timeout or permanent failure. That boot had no swap mode in the controller, which a bring-up patch also tried with the same result, so neither the device tree nor spi-s3c64xx needs one. The palmtop has run this setting since. 8-bit words would put the bytes in memory order too, but they have not been tried since the SPI flush fix ("spi: s3c64xx: turn the channels off before clearing the packet count") that ended the link's bit slips, so this keeps stock's word size. The bring-up's module parameters for the word size, the transfer size and the SPI mode are not carried over: these are fixed, and the mode and clock come from the device tree. Link: https://github.com/derekross/linux-omarchy-mobile/pull/137 Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- drivers/soc/google/gnssif_spi/gnss_spi.c | 90 ++++++++++++++++-------- drivers/soc/google/gnssif_spi/gnss_spi.h | 2 + 2 files changed, 63 insertions(+), 29 deletions(-) diff --git a/drivers/soc/google/gnssif_spi/gnss_spi.c b/drivers/soc/google/gnssif_spi/gnss_spi.c index dd88cd4c5b..8e47f1315a 100644 --- a/drivers/soc/google/gnssif_spi/gnss_spi.c +++ b/drivers/soc/google/gnssif_spi/gnss_spi.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include "include/gnss.h" #include "gnss_spi.h" @@ -23,52 +25,82 @@ bool gnss_spi_bound(void) } EXPORT_SYMBOL_GPL(gnss_spi_bound); -int gnss_spi_send(char *buff, unsigned int size, char *rx_buff) +/* + * Swap the bytes of each 32-bit word. Google's SPI controller driver + * sends GNSS's 32-bit words in its swap mode (swap-mode in the board's + * device tree: bytes and half-words swapped on TX and RX), which puts + * them on the wire in memory order. spi-s3c64xx has no swap mode and + * shifts each word out most significant byte first, so swap here. + */ +static void gnss_spi_swab(char *buf, unsigned int size) { - int ret = 0; - struct spi_message msg; - struct spi_transfer tx; + u32 *w = (u32 *)buf; + unsigned int i; - if (!gnss_spi_bound()) - return -ENODEV; - - memset(&tx, 0, sizeof(struct spi_transfer)); - spi_message_init(&msg); - tx.len = size; - tx.tx_buf = buff; - tx.bits_per_word = SPI_BITS_PER_WORD; - if (rx_buff) - tx.rx_buf = rx_buff; - spi_message_add_tail(&tx, &msg); - ret = spi_sync(gnss_if.spi, &msg); - if (ret < 0) - gif_err("spi_sync() error:%d\n", ret); - - return ret; + for (i = 0; i < size / 4; i++) + w[i] = swab32(w[i]); } -EXPORT_SYMBOL_GPL(gnss_spi_send); -int gnss_spi_recv(char *buff, unsigned int size) +/* + * One message of transfers of GNSS_SPI_BLOCK bytes at most. The core's + * data comes in 64-byte blocks, and Google's driver reads one block per + * transfer. spi-s3c64xx moves a transfer that fills its 64-byte FIFO in + * one piece, but cuts a longer one into 60-byte pieces, and on the Pixel + * 9a the cuts lost data; so writes go in blocks too. + */ +static int gnss_spi_xfer(char *tx_buf, char *rx_buf, unsigned int size) { - int ret = 0; + unsigned int n = DIV_ROUND_UP(size, GNSS_SPI_BLOCK); + struct spi_transfer *t; struct spi_message msg; - struct spi_transfer rx; + unsigned int i, off; + int ret; if (!gnss_spi_bound()) return -ENODEV; + if (!size || size % sizeof(u32)) + return -EINVAL; + + t = kcalloc(n, sizeof(*t), GFP_KERNEL); + if (!t) + return -ENOMEM; + + if (tx_buf) + gnss_spi_swab(tx_buf, size); - memset(&rx, 0, sizeof(struct spi_transfer)); - rx.len = size; - rx.rx_buf = buff; - rx.bits_per_word = SPI_BITS_PER_WORD; spi_message_init(&msg); - spi_message_add_tail(&rx, &msg); + for (i = 0, off = 0; i < n; i++, off += GNSS_SPI_BLOCK) { + t[i].len = min(size - off, GNSS_SPI_BLOCK); + t[i].tx_buf = tx_buf ? tx_buf + off : NULL; + t[i].rx_buf = rx_buf ? rx_buf + off : NULL; + t[i].bits_per_word = SPI_BITS_PER_WORD; + spi_message_add_tail(&t[i], &msg); + } + ret = spi_sync(gnss_if.spi, &msg); if (ret < 0) gif_err("spi_sync() error:%d\n", ret); + /* Give the caller its buffer back as it was. */ + if (tx_buf) + gnss_spi_swab(tx_buf, size); + if (rx_buf && !ret) + gnss_spi_swab(rx_buf, size); + + kfree(t); + return ret; +} + +int gnss_spi_send(char *buff, unsigned int size, char *rx_buff) +{ + return gnss_spi_xfer(buff, rx_buff, size); +} +EXPORT_SYMBOL_GPL(gnss_spi_send); +int gnss_spi_recv(char *buff, unsigned int size) +{ + return gnss_spi_xfer(NULL, buff, size); } EXPORT_SYMBOL_GPL(gnss_spi_recv); diff --git a/drivers/soc/google/gnssif_spi/gnss_spi.h b/drivers/soc/google/gnssif_spi/gnss_spi.h index a3d41fbaa2..e10067c0b6 100644 --- a/drivers/soc/google/gnssif_spi/gnss_spi.h +++ b/drivers/soc/google/gnssif_spi/gnss_spi.h @@ -14,6 +14,8 @@ #define MAX_SPI_RX_SIZE SZ_2K #define DEFAULT_SPI_TX_SIZE SZ_4K #define SPI_BITS_PER_WORD 32 +/* the core's block, and the FIFO depth of the SPI controller it is on */ +#define GNSS_SPI_BLOCK 64U struct gnss_spi { struct spi_device *spi; From 8a1df8461744532d89980f2b2758b65d062cd952 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:40:28 -0647 Subject: [PATCH 7/7] soc: google: gnssif_spi: take the core's requests only while gnss_ipc is open Google's gnssif enables the core's request interrupt at the end of its probe, so from the moment the module loads, a high level on the line starts the read work, which keeps reading 64-byte blocks for as long as the line stays high, whether or not anyone will read them: what it queues is thrown away at the last release of /dev/gnss_ipc anyway. On the Pixel 9a the line is GPA5 pin 4, the same bank as the modem's own lines, and the core has nothing to say until the modem is up and the GNSS daemon has loaded its firmware, which it does before it talks to the core. Loading the driver should not be able to take an interrupt there early. Enable the interrupt, and arm it as a wake source, on the first open of /dev/gnss_ipc, and disable and disarm it on the last release, after waiting for the read work (which enables the interrupt again when it ends). The line is level-triggered, so a request the core raises before the open is taken as soon as the interrupt is enabled. A mutex keeps a first open and a last release from interleaving. The interrupt is still requested at probe, disabled, so a missing interrupt still fails the probe. This is MODEM-PLAN K5's "IRQ requested at open rather than probe", done by enabling rather than requesting it there. Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- .../soc/google/gnssif_spi/gnss_io_device.c | 11 ++++- .../google/gnssif_spi/gnss_keplerctl_device.c | 46 +++++++++++++------ drivers/soc/google/gnssif_spi/gnss_main.c | 12 ++--- drivers/soc/google/gnssif_spi/gnss_prj.h | 5 ++ 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/drivers/soc/google/gnssif_spi/gnss_io_device.c b/drivers/soc/google/gnssif_spi/gnss_io_device.c index 0bbd224c71..deafeacf2a 100644 --- a/drivers/soc/google/gnssif_spi/gnss_io_device.c +++ b/drivers/soc/google/gnssif_spi/gnss_io_device.c @@ -60,7 +60,11 @@ static int misc_open(struct inode *inode, struct file *filp) filp->private_data = (void *)iod; + mutex_lock(&iod->open_lock); ref_cnt = atomic_inc_return(&iod->opened); + if (ref_cnt == 1) + gnss_ctl_irq_on(iod->gc); + mutex_unlock(&iod->open_lock); gif_info("%s (opened %d)\n", iod->name, ref_cnt); @@ -72,9 +76,13 @@ static int misc_release(struct inode *inode, struct file *filp) struct io_device *iod = (struct io_device *)filp->private_data; int ref_cnt; + mutex_lock(&iod->open_lock); ref_cnt = atomic_dec_return(&iod->opened); - if (ref_cnt == 0) + if (ref_cnt == 0) { + gnss_ctl_irq_off(iod->gc); skb_queue_purge(&iod->sk_rx_q); + } + mutex_unlock(&iod->open_lock); gif_info("%s (opened %d)\n", iod->name, ref_cnt); @@ -216,6 +224,7 @@ static int init_io_device(struct io_device *iod, struct device *dev) init_waitqueue_head(&iod->wq); skb_queue_head_init(&iod->sk_rx_q); + mutex_init(&iod->open_lock); iod->miscdev.minor = MISC_DYNAMIC_MINOR; iod->miscdev.name = iod->name; diff --git a/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c b/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c index 0288031b69..a2a216d0d4 100644 --- a/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c +++ b/drivers/soc/google/gnssif_spi/gnss_keplerctl_device.c @@ -51,11 +51,39 @@ static int kepler_betp_int_status(struct gnss_ctl *gc) return gpiod_get_value(gc->gpio_ap2gnss_spi.desc); } -static void gnss_disable_irq_wake(void *data) +/* + * Take the core's requests only while /dev/gnss_ipc is open: what it + * sends with nobody listening is thrown away at the last release anyway, + * and a request line that rises while the modem is off or starting can't + * set the read work going. + */ +void gnss_ctl_irq_on(struct gnss_ctl *gc) { - struct gnss_ctl *gc = data; + int ret; - disable_irq_wake(gc->irq_gnss2ap_spi.num); + /* Without a wake source GNSS still works; it can't wake the AP. */ + ret = enable_irq_wake(gc->irq_gnss2ap_spi.num); + if (ret) + gif_err("Failed to enable wakeup_source - enable_irq_wake(%d)\n", ret); + else + gc->irq_wake = true; + + gif_enable_irq(&gc->irq_gnss2ap_spi); +} + +void gnss_ctl_irq_off(struct gnss_ctl *gc) +{ + struct link_device *ld = gc->iod->ld; + + /* The read work enables the interrupt again when it ends. */ + gif_disable_irq_sync(&gc->irq_gnss2ap_spi); + cancel_delayed_work_sync(&ld->rx_dwork); + gif_disable_irq_sync(&gc->irq_gnss2ap_spi); + + if (gc->irq_wake) { + disable_irq_wake(gc->irq_gnss2ap_spi.num); + gc->irq_wake = false; + } } static void gnss_set_ops(struct gnss_ctl *gc) @@ -87,7 +115,7 @@ static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) } gc->irq_gnss2ap_spi.num = ret; - /* Disable the interrupt until gnss_main.c probe function finish */ + /* Disabled until /dev/gnss_ipc is opened */ gif_configure_irq(&gc->irq_gnss2ap_spi, gc->irq_gnss2ap_spi.num, "kepler_spi_irq_handler", IRQF_TRIGGER_HIGH | IRQF_NO_AUTOEN); @@ -97,16 +125,6 @@ static int init_ctl_device(struct gnss_ctl *gc, struct gnss_pdata *pdata) return ret; } - /* Without a wake source GNSS still works; it can't wake the AP. */ - ret = enable_irq_wake(gc->irq_gnss2ap_spi.num); - if (ret) { - gif_err("Failed to enable wakeup_source - enable_irq_wake(%d)\n", ret); - } else { - ret = devm_add_action_or_reset(gc->dev, gnss_disable_irq_wake, gc); - if (ret) - return ret; - } - /* The line starts low, as Google's driver sets it. */ gc->gpio_ap2gnss_spi.label = "AP2GNSS_SPI"; gc->gpio_ap2gnss_spi.desc = devm_gpiod_get(gc->dev, "ap2gnss-spi", GPIOD_OUT_LOW); diff --git a/drivers/soc/google/gnssif_spi/gnss_main.c b/drivers/soc/google/gnssif_spi/gnss_main.c index f6ac72ec03..5db532baf6 100644 --- a/drivers/soc/google/gnssif_spi/gnss_main.c +++ b/drivers/soc/google/gnssif_spi/gnss_main.c @@ -122,16 +122,13 @@ static struct gnss_pdata *gnss_if_parse_dt_pdata(struct device *dev) static void gnss_remove(struct platform_device *pdev) { struct gnss_ctl *gc = platform_get_drvdata(pdev); - struct link_device *ld = gc->iod->ld; /* - * The read work enables the interrupt again when it ends, so stop it - * before the interrupt is disabled for good. devm then releases the - * device node, the work queue and the interrupt. + * Nobody holds /dev/gnss_ipc open (it pins the module), so the + * interrupt is off already; devm releases the device node, the work + * queue and the interrupt. */ gif_disable_irq_sync(&gc->irq_gnss2ap_spi); - cancel_delayed_work_sync(&ld->rx_dwork); - gif_disable_irq_sync(&gc->irq_gnss2ap_spi); gpiod_set_value(gc->gpio_ap2gnss_spi.desc, 0); } @@ -187,9 +184,6 @@ static int gnss_probe(struct platform_device *pdev) platform_set_drvdata(pdev, gc); - /* wa: to prevent wrong irq handling during probe */ - gif_enable_irq(&gc->irq_gnss2ap_spi); - gif_info("%s: ---\n", pdev->name); return 0; diff --git a/drivers/soc/google/gnssif_spi/gnss_prj.h b/drivers/soc/google/gnssif_spi/gnss_prj.h index 8e51d32d09..2aefdeea9a 100644 --- a/drivers/soc/google/gnssif_spi/gnss_prj.h +++ b/drivers/soc/google/gnssif_spi/gnss_prj.h @@ -8,6 +8,7 @@ #define __GNSS_PRJ_H__ #include +#include #include #include #include @@ -22,6 +23,7 @@ struct io_device { struct link_device *ld; struct gnss_ctl *gc; atomic_t opened; + struct mutex open_lock; /* the first open and the last release */ wait_queue_head_t wq; struct miscdevice miscdev; struct sk_buff_head sk_rx_q; @@ -67,9 +69,12 @@ struct gnss_ctl { atomic_t wait_rdy; atomic_t tx_in_progress; atomic_t rx_in_progress; + bool irq_wake; }; struct gnss_ctl *create_ctl_device(struct platform_device *pdev); +void gnss_ctl_irq_on(struct gnss_ctl *gc); +void gnss_ctl_irq_off(struct gnss_ctl *gc); struct link_device *create_link_device(struct platform_device *pdev); struct io_device *create_io_device(struct platform_device *pdev, struct link_device *ld, struct gnss_ctl *gc, struct gnss_pdata *pdata);