^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * SiRFstar GNSS receiver driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2018 Johan Hovold <johan@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/gnss.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/serdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SIRF_BOOT_DELAY 500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define SIRF_ON_OFF_PULSE_TIME 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define SIRF_ACTIVATE_TIMEOUT 200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define SIRF_HIBERNATE_TIMEOUT 200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * If no data arrives for this time, we assume that the chip is off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * REVISIT: The report cycle is configurable and can be several minutes long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * so this will only work reliably if the report cycle is set to a reasonable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * low value. Also power saving settings (like send data only on movement)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * might things work even worse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Workaround might be to parse shutdown or bootup messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define SIRF_REPORT_CYCLE 2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct sirf_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct gnss_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct serdev_device *serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) speed_t speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct regulator *vcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct regulator *lna;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct gpio_desc *on_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct gpio_desc *wakeup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) bool active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct mutex gdev_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) bool open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct mutex serdev_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int serdev_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) wait_queue_head_t power_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int sirf_serdev_open(struct sirf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) mutex_lock(&data->serdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (++data->serdev_count == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret = serdev_device_open(data->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) data->serdev_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) serdev_device_set_baudrate(data->serdev, data->speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) serdev_device_set_flow_control(data->serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) mutex_unlock(&data->serdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static void sirf_serdev_close(struct sirf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) mutex_lock(&data->serdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (--data->serdev_count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) serdev_device_close(data->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) mutex_unlock(&data->serdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int sirf_open(struct gnss_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct sirf_data *data = gnss_get_drvdata(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct serdev_device *serdev = data->serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) mutex_lock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) data->open = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mutex_unlock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ret = sirf_serdev_open(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) mutex_lock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) data->open = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) mutex_unlock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ret = pm_runtime_get_sync(&serdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dev_err(&gdev->dev, "failed to runtime resume: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pm_runtime_put_noidle(&serdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) goto err_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) err_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sirf_serdev_close(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) mutex_lock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) data->open = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) mutex_unlock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static void sirf_close(struct gnss_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct sirf_data *data = gnss_get_drvdata(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct serdev_device *serdev = data->serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) sirf_serdev_close(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pm_runtime_put(&serdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) mutex_lock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) data->open = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) mutex_unlock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int sirf_write_raw(struct gnss_device *gdev, const unsigned char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct sirf_data *data = gnss_get_drvdata(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct serdev_device *serdev = data->serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* write is only buffered synchronously */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (ret < 0 || ret < count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* FIXME: determine if interrupted? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) serdev_device_wait_until_sent(serdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static const struct gnss_operations sirf_gnss_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .open = sirf_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .close = sirf_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .write_raw = sirf_write_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int sirf_receive_buf(struct serdev_device *serdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) const unsigned char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct sirf_data *data = serdev_device_get_drvdata(serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct gnss_device *gdev = data->gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!data->wakeup && !data->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) data->active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) wake_up_interruptible(&data->power_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) mutex_lock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (data->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ret = gnss_insert_raw(gdev, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) mutex_unlock(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static const struct serdev_device_ops sirf_serdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .receive_buf = sirf_receive_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .write_wakeup = serdev_device_write_wakeup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static irqreturn_t sirf_wakeup_handler(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct sirf_data *data = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct device *dev = &data->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = gpiod_get_value_cansleep(data->wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) dev_dbg(dev, "%s - wakeup = %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) data->active = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) wake_up_interruptible(&data->power_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int sirf_wait_for_power_state_nowakeup(struct sirf_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) bool active,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /* Wait for state change (including any shutdown messages). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) msleep(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* Wait for data reception or timeout. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) data->active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ret = wait_event_interruptible_timeout(data->power_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) data->active, msecs_to_jiffies(SIRF_REPORT_CYCLE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (ret > 0 && !active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (ret == 0 && active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int sirf_wait_for_power_state(struct sirf_data *data, bool active,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!data->wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return sirf_wait_for_power_state_nowakeup(data, active, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ret = wait_event_interruptible_timeout(data->power_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) data->active == active, msecs_to_jiffies(timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) dev_warn(&data->serdev->dev, "timeout waiting for active state = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static void sirf_pulse_on_off(struct sirf_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) gpiod_set_value_cansleep(data->on_off, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) msleep(SIRF_ON_OFF_PULSE_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) gpiod_set_value_cansleep(data->on_off, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int sirf_set_active(struct sirf_data *data, bool active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int retries = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) timeout = SIRF_ACTIVATE_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) timeout = SIRF_HIBERNATE_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!data->wakeup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ret = sirf_serdev_open(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) sirf_pulse_on_off(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ret = sirf_wait_for_power_state(data, active, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) } while (ret == -ETIMEDOUT && retries--);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (!data->wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) sirf_serdev_close(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int sirf_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct sirf_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) int ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (data->on_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ret = sirf_set_active(data, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) ret = regulator_disable(data->vcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ret = regulator_disable(data->lna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto err_reenable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) err_reenable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (data->on_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ret2 = sirf_set_active(data, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret2 = regulator_enable(data->vcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (ret2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) "failed to reenable power on failed suspend: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ret2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int sirf_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct sirf_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ret = regulator_enable(data->lna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (data->on_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = sirf_set_active(data, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ret = regulator_enable(data->vcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) goto err_disable_lna;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) err_disable_lna:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) regulator_disable(data->lna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int __maybe_unused sirf_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct sirf_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (!pm_runtime_suspended(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) ret = sirf_runtime_suspend(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (data->wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) disable_irq(data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static int __maybe_unused sirf_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct sirf_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (data->wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) enable_irq(data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!pm_runtime_suspended(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ret = sirf_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static const struct dev_pm_ops sirf_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) SET_SYSTEM_SLEEP_PM_OPS(sirf_suspend, sirf_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) SET_RUNTIME_PM_OPS(sirf_runtime_suspend, sirf_runtime_resume, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static int sirf_parse_dt(struct serdev_device *serdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct sirf_data *data = serdev_device_get_drvdata(serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct device_node *node = serdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) u32 speed = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) of_property_read_u32(node, "current-speed", &speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) data->speed = speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static int sirf_probe(struct serdev_device *serdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct device *dev = &serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct gnss_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct sirf_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) gdev = gnss_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (!gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) gdev->type = GNSS_TYPE_SIRF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) gdev->ops = &sirf_gnss_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) gnss_set_drvdata(gdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) data->serdev = serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) data->gdev = gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) mutex_init(&data->gdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) mutex_init(&data->serdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) init_waitqueue_head(&data->power_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) serdev_device_set_drvdata(serdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) serdev_device_set_client_ops(serdev, &sirf_serdev_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) ret = sirf_parse_dt(serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) data->vcc = devm_regulator_get(dev, "vcc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (IS_ERR(data->vcc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) ret = PTR_ERR(data->vcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) data->lna = devm_regulator_get(dev, "lna");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (IS_ERR(data->lna)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) ret = PTR_ERR(data->lna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) data->on_off = devm_gpiod_get_optional(dev, "sirf,onoff",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (IS_ERR(data->on_off)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) ret = PTR_ERR(data->on_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (data->on_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) data->wakeup = devm_gpiod_get_optional(dev, "sirf,wakeup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (IS_ERR(data->wakeup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) ret = PTR_ERR(data->wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ret = regulator_enable(data->vcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /* Wait for chip to boot into hibernate mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) msleep(SIRF_BOOT_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (data->wakeup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) ret = gpiod_get_value_cansleep(data->wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) goto err_disable_vcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) data->active = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ret = gpiod_to_irq(data->wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) goto err_disable_vcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) data->irq = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) ret = request_threaded_irq(data->irq, NULL, sirf_wakeup_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) "wakeup", data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) goto err_disable_vcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (data->on_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (!data->wakeup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) data->active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ret = sirf_serdev_open(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) goto err_disable_vcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) msleep(SIRF_REPORT_CYCLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) sirf_serdev_close(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* Force hibernate mode if already active. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (data->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) ret = sirf_set_active(data, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) dev_err(dev, "failed to set hibernate mode: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) goto err_free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (IS_ENABLED(CONFIG_PM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) pm_runtime_set_suspended(dev); /* clear runtime_error flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) pm_runtime_enable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) ret = sirf_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) goto err_free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) ret = gnss_register_device(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) goto err_disable_rpm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) err_disable_rpm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (IS_ENABLED(CONFIG_PM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) pm_runtime_disable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) sirf_runtime_suspend(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) err_free_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (data->wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) free_irq(data->irq, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) err_disable_vcc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (data->on_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) regulator_disable(data->vcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) err_put_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) gnss_put_device(data->gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static void sirf_remove(struct serdev_device *serdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) struct sirf_data *data = serdev_device_get_drvdata(serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) gnss_deregister_device(data->gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (IS_ENABLED(CONFIG_PM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) pm_runtime_disable(&serdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) sirf_runtime_suspend(&serdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (data->wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) free_irq(data->irq, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (data->on_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) regulator_disable(data->vcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) gnss_put_device(data->gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static const struct of_device_id sirf_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) { .compatible = "fastrax,uc430" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) { .compatible = "linx,r4" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) { .compatible = "wi2wi,w2sg0004" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) { .compatible = "wi2wi,w2sg0008i" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) { .compatible = "wi2wi,w2sg0084i" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) MODULE_DEVICE_TABLE(of, sirf_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static struct serdev_device_driver sirf_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) .name = "gnss-sirf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) .of_match_table = of_match_ptr(sirf_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) .pm = &sirf_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) .probe = sirf_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) .remove = sirf_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) module_serdev_device_driver(sirf_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) MODULE_DESCRIPTION("SiRFstar GNSS receiver driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) MODULE_LICENSE("GPL v2");