sirf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SiRFstar GNSS receiver driver
  4. *
  5. * Copyright (C) 2018 Johan Hovold <johan@kernel.org>
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/gnss.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/pm.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/sched.h>
  19. #include <linux/serdev.h>
  20. #include <linux/slab.h>
  21. #include <linux/wait.h>
  22. #define SIRF_BOOT_DELAY 500
  23. #define SIRF_ON_OFF_PULSE_TIME 100
  24. #define SIRF_ACTIVATE_TIMEOUT 200
  25. #define SIRF_HIBERNATE_TIMEOUT 200
  26. struct sirf_data {
  27. struct gnss_device *gdev;
  28. struct serdev_device *serdev;
  29. speed_t speed;
  30. struct regulator *vcc;
  31. struct gpio_desc *on_off;
  32. struct gpio_desc *wakeup;
  33. int irq;
  34. bool active;
  35. wait_queue_head_t power_wait;
  36. };
  37. static int sirf_open(struct gnss_device *gdev)
  38. {
  39. struct sirf_data *data = gnss_get_drvdata(gdev);
  40. struct serdev_device *serdev = data->serdev;
  41. int ret;
  42. ret = serdev_device_open(serdev);
  43. if (ret)
  44. return ret;
  45. serdev_device_set_baudrate(serdev, data->speed);
  46. serdev_device_set_flow_control(serdev, false);
  47. ret = pm_runtime_get_sync(&serdev->dev);
  48. if (ret < 0) {
  49. dev_err(&gdev->dev, "failed to runtime resume: %d\n", ret);
  50. pm_runtime_put_noidle(&serdev->dev);
  51. goto err_close;
  52. }
  53. return 0;
  54. err_close:
  55. serdev_device_close(serdev);
  56. return ret;
  57. }
  58. static void sirf_close(struct gnss_device *gdev)
  59. {
  60. struct sirf_data *data = gnss_get_drvdata(gdev);
  61. struct serdev_device *serdev = data->serdev;
  62. serdev_device_close(serdev);
  63. pm_runtime_put(&serdev->dev);
  64. }
  65. static int sirf_write_raw(struct gnss_device *gdev, const unsigned char *buf,
  66. size_t count)
  67. {
  68. struct sirf_data *data = gnss_get_drvdata(gdev);
  69. struct serdev_device *serdev = data->serdev;
  70. int ret;
  71. /* write is only buffered synchronously */
  72. ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT);
  73. if (ret < 0)
  74. return ret;
  75. /* FIXME: determine if interrupted? */
  76. serdev_device_wait_until_sent(serdev, 0);
  77. return count;
  78. }
  79. static const struct gnss_operations sirf_gnss_ops = {
  80. .open = sirf_open,
  81. .close = sirf_close,
  82. .write_raw = sirf_write_raw,
  83. };
  84. static int sirf_receive_buf(struct serdev_device *serdev,
  85. const unsigned char *buf, size_t count)
  86. {
  87. struct sirf_data *data = serdev_device_get_drvdata(serdev);
  88. struct gnss_device *gdev = data->gdev;
  89. return gnss_insert_raw(gdev, buf, count);
  90. }
  91. static const struct serdev_device_ops sirf_serdev_ops = {
  92. .receive_buf = sirf_receive_buf,
  93. .write_wakeup = serdev_device_write_wakeup,
  94. };
  95. static irqreturn_t sirf_wakeup_handler(int irq, void *dev_id)
  96. {
  97. struct sirf_data *data = dev_id;
  98. struct device *dev = &data->serdev->dev;
  99. int ret;
  100. ret = gpiod_get_value_cansleep(data->wakeup);
  101. dev_dbg(dev, "%s - wakeup = %d\n", __func__, ret);
  102. if (ret < 0)
  103. goto out;
  104. data->active = !!ret;
  105. wake_up_interruptible(&data->power_wait);
  106. out:
  107. return IRQ_HANDLED;
  108. }
  109. static int sirf_wait_for_power_state(struct sirf_data *data, bool active,
  110. unsigned long timeout)
  111. {
  112. int ret;
  113. ret = wait_event_interruptible_timeout(data->power_wait,
  114. data->active == active, msecs_to_jiffies(timeout));
  115. if (ret < 0)
  116. return ret;
  117. if (ret == 0) {
  118. dev_warn(&data->serdev->dev, "timeout waiting for active state = %d\n",
  119. active);
  120. return -ETIMEDOUT;
  121. }
  122. return 0;
  123. }
  124. static void sirf_pulse_on_off(struct sirf_data *data)
  125. {
  126. gpiod_set_value_cansleep(data->on_off, 1);
  127. msleep(SIRF_ON_OFF_PULSE_TIME);
  128. gpiod_set_value_cansleep(data->on_off, 0);
  129. }
  130. static int sirf_set_active(struct sirf_data *data, bool active)
  131. {
  132. unsigned long timeout;
  133. int retries = 3;
  134. int ret;
  135. if (active)
  136. timeout = SIRF_ACTIVATE_TIMEOUT;
  137. else
  138. timeout = SIRF_HIBERNATE_TIMEOUT;
  139. do {
  140. sirf_pulse_on_off(data);
  141. ret = sirf_wait_for_power_state(data, active, timeout);
  142. if (ret < 0) {
  143. if (ret == -ETIMEDOUT)
  144. continue;
  145. return ret;
  146. }
  147. break;
  148. } while (retries--);
  149. if (retries < 0)
  150. return -ETIMEDOUT;
  151. return 0;
  152. }
  153. static int sirf_runtime_suspend(struct device *dev)
  154. {
  155. struct sirf_data *data = dev_get_drvdata(dev);
  156. if (!data->on_off)
  157. return regulator_disable(data->vcc);
  158. return sirf_set_active(data, false);
  159. }
  160. static int sirf_runtime_resume(struct device *dev)
  161. {
  162. struct sirf_data *data = dev_get_drvdata(dev);
  163. if (!data->on_off)
  164. return regulator_enable(data->vcc);
  165. return sirf_set_active(data, true);
  166. }
  167. static int __maybe_unused sirf_suspend(struct device *dev)
  168. {
  169. struct sirf_data *data = dev_get_drvdata(dev);
  170. int ret = 0;
  171. if (!pm_runtime_suspended(dev))
  172. ret = sirf_runtime_suspend(dev);
  173. if (data->wakeup)
  174. disable_irq(data->irq);
  175. return ret;
  176. }
  177. static int __maybe_unused sirf_resume(struct device *dev)
  178. {
  179. struct sirf_data *data = dev_get_drvdata(dev);
  180. int ret = 0;
  181. if (data->wakeup)
  182. enable_irq(data->irq);
  183. if (!pm_runtime_suspended(dev))
  184. ret = sirf_runtime_resume(dev);
  185. return ret;
  186. }
  187. static const struct dev_pm_ops sirf_pm_ops = {
  188. SET_SYSTEM_SLEEP_PM_OPS(sirf_suspend, sirf_resume)
  189. SET_RUNTIME_PM_OPS(sirf_runtime_suspend, sirf_runtime_resume, NULL)
  190. };
  191. static int sirf_parse_dt(struct serdev_device *serdev)
  192. {
  193. struct sirf_data *data = serdev_device_get_drvdata(serdev);
  194. struct device_node *node = serdev->dev.of_node;
  195. u32 speed = 9600;
  196. of_property_read_u32(node, "current-speed", &speed);
  197. data->speed = speed;
  198. return 0;
  199. }
  200. static int sirf_probe(struct serdev_device *serdev)
  201. {
  202. struct device *dev = &serdev->dev;
  203. struct gnss_device *gdev;
  204. struct sirf_data *data;
  205. int ret;
  206. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  207. if (!data)
  208. return -ENOMEM;
  209. gdev = gnss_allocate_device(dev);
  210. if (!gdev)
  211. return -ENOMEM;
  212. gdev->type = GNSS_TYPE_SIRF;
  213. gdev->ops = &sirf_gnss_ops;
  214. gnss_set_drvdata(gdev, data);
  215. data->serdev = serdev;
  216. data->gdev = gdev;
  217. init_waitqueue_head(&data->power_wait);
  218. serdev_device_set_drvdata(serdev, data);
  219. serdev_device_set_client_ops(serdev, &sirf_serdev_ops);
  220. ret = sirf_parse_dt(serdev);
  221. if (ret)
  222. goto err_put_device;
  223. data->vcc = devm_regulator_get(dev, "vcc");
  224. if (IS_ERR(data->vcc)) {
  225. ret = PTR_ERR(data->vcc);
  226. goto err_put_device;
  227. }
  228. data->on_off = devm_gpiod_get_optional(dev, "sirf,onoff",
  229. GPIOD_OUT_LOW);
  230. if (IS_ERR(data->on_off))
  231. goto err_put_device;
  232. if (data->on_off) {
  233. data->wakeup = devm_gpiod_get_optional(dev, "sirf,wakeup",
  234. GPIOD_IN);
  235. if (IS_ERR(data->wakeup))
  236. goto err_put_device;
  237. /*
  238. * Configurations where WAKEUP has been left not connected,
  239. * are currently not supported.
  240. */
  241. if (!data->wakeup) {
  242. dev_err(dev, "no wakeup gpio specified\n");
  243. ret = -ENODEV;
  244. goto err_put_device;
  245. }
  246. ret = regulator_enable(data->vcc);
  247. if (ret)
  248. goto err_put_device;
  249. /* Wait for chip to boot into hibernate mode. */
  250. msleep(SIRF_BOOT_DELAY);
  251. }
  252. if (data->wakeup) {
  253. ret = gpiod_to_irq(data->wakeup);
  254. if (ret < 0)
  255. goto err_disable_vcc;
  256. data->irq = ret;
  257. ret = request_threaded_irq(data->irq, NULL, sirf_wakeup_handler,
  258. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  259. "wakeup", data);
  260. if (ret)
  261. goto err_disable_vcc;
  262. }
  263. if (IS_ENABLED(CONFIG_PM)) {
  264. pm_runtime_set_suspended(dev); /* clear runtime_error flag */
  265. pm_runtime_enable(dev);
  266. } else {
  267. ret = sirf_runtime_resume(dev);
  268. if (ret < 0)
  269. goto err_free_irq;
  270. }
  271. ret = gnss_register_device(gdev);
  272. if (ret)
  273. goto err_disable_rpm;
  274. return 0;
  275. err_disable_rpm:
  276. if (IS_ENABLED(CONFIG_PM))
  277. pm_runtime_disable(dev);
  278. else
  279. sirf_runtime_suspend(dev);
  280. err_free_irq:
  281. if (data->wakeup)
  282. free_irq(data->irq, data);
  283. err_disable_vcc:
  284. if (data->on_off)
  285. regulator_disable(data->vcc);
  286. err_put_device:
  287. gnss_put_device(data->gdev);
  288. return ret;
  289. }
  290. static void sirf_remove(struct serdev_device *serdev)
  291. {
  292. struct sirf_data *data = serdev_device_get_drvdata(serdev);
  293. gnss_deregister_device(data->gdev);
  294. if (IS_ENABLED(CONFIG_PM))
  295. pm_runtime_disable(&serdev->dev);
  296. else
  297. sirf_runtime_suspend(&serdev->dev);
  298. if (data->wakeup)
  299. free_irq(data->irq, data);
  300. if (data->on_off)
  301. regulator_disable(data->vcc);
  302. gnss_put_device(data->gdev);
  303. };
  304. #ifdef CONFIG_OF
  305. static const struct of_device_id sirf_of_match[] = {
  306. { .compatible = "fastrax,uc430" },
  307. { .compatible = "linx,r4" },
  308. { .compatible = "wi2wi,w2sg0008i" },
  309. { .compatible = "wi2wi,w2sg0084i" },
  310. {},
  311. };
  312. MODULE_DEVICE_TABLE(of, sirf_of_match);
  313. #endif
  314. static struct serdev_device_driver sirf_driver = {
  315. .driver = {
  316. .name = "gnss-sirf",
  317. .of_match_table = of_match_ptr(sirf_of_match),
  318. .pm = &sirf_pm_ops,
  319. },
  320. .probe = sirf_probe,
  321. .remove = sirf_remove,
  322. };
  323. module_serdev_device_driver(sirf_driver);
  324. MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
  325. MODULE_DESCRIPTION("SiRFstar GNSS receiver driver");
  326. MODULE_LICENSE("GPL v2");