ubx.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * u-blox 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/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/serdev.h>
  15. #include "serial.h"
  16. struct ubx_data {
  17. struct regulator *v_bckp;
  18. struct regulator *vcc;
  19. };
  20. static int ubx_set_active(struct gnss_serial *gserial)
  21. {
  22. struct ubx_data *data = gnss_serial_get_drvdata(gserial);
  23. int ret;
  24. ret = regulator_enable(data->vcc);
  25. if (ret)
  26. return ret;
  27. return 0;
  28. }
  29. static int ubx_set_standby(struct gnss_serial *gserial)
  30. {
  31. struct ubx_data *data = gnss_serial_get_drvdata(gserial);
  32. int ret;
  33. ret = regulator_disable(data->vcc);
  34. if (ret)
  35. return ret;
  36. return 0;
  37. }
  38. static int ubx_set_power(struct gnss_serial *gserial,
  39. enum gnss_serial_pm_state state)
  40. {
  41. switch (state) {
  42. case GNSS_SERIAL_ACTIVE:
  43. return ubx_set_active(gserial);
  44. case GNSS_SERIAL_OFF:
  45. case GNSS_SERIAL_STANDBY:
  46. return ubx_set_standby(gserial);
  47. }
  48. return -EINVAL;
  49. }
  50. static const struct gnss_serial_ops ubx_gserial_ops = {
  51. .set_power = ubx_set_power,
  52. };
  53. static int ubx_probe(struct serdev_device *serdev)
  54. {
  55. struct gnss_serial *gserial;
  56. struct ubx_data *data;
  57. int ret;
  58. gserial = gnss_serial_allocate(serdev, sizeof(*data));
  59. if (IS_ERR(gserial)) {
  60. ret = PTR_ERR(gserial);
  61. return ret;
  62. }
  63. gserial->ops = &ubx_gserial_ops;
  64. gserial->gdev->type = GNSS_TYPE_UBX;
  65. data = gnss_serial_get_drvdata(gserial);
  66. data->vcc = devm_regulator_get(&serdev->dev, "vcc");
  67. if (IS_ERR(data->vcc)) {
  68. ret = PTR_ERR(data->vcc);
  69. goto err_free_gserial;
  70. }
  71. data->v_bckp = devm_regulator_get_optional(&serdev->dev, "v-bckp");
  72. if (IS_ERR(data->v_bckp)) {
  73. ret = PTR_ERR(data->v_bckp);
  74. if (ret == -ENODEV)
  75. data->v_bckp = NULL;
  76. else
  77. goto err_free_gserial;
  78. }
  79. if (data->v_bckp) {
  80. ret = regulator_enable(data->v_bckp);
  81. if (ret)
  82. goto err_free_gserial;
  83. }
  84. ret = gnss_serial_register(gserial);
  85. if (ret)
  86. goto err_disable_v_bckp;
  87. return 0;
  88. err_disable_v_bckp:
  89. if (data->v_bckp)
  90. regulator_disable(data->v_bckp);
  91. err_free_gserial:
  92. gnss_serial_free(gserial);
  93. return ret;
  94. }
  95. static void ubx_remove(struct serdev_device *serdev)
  96. {
  97. struct gnss_serial *gserial = serdev_device_get_drvdata(serdev);
  98. struct ubx_data *data = gnss_serial_get_drvdata(gserial);
  99. gnss_serial_deregister(gserial);
  100. if (data->v_bckp)
  101. regulator_disable(data->v_bckp);
  102. gnss_serial_free(gserial);
  103. };
  104. #ifdef CONFIG_OF
  105. static const struct of_device_id ubx_of_match[] = {
  106. { .compatible = "u-blox,neo-8" },
  107. { .compatible = "u-blox,neo-m8" },
  108. {},
  109. };
  110. MODULE_DEVICE_TABLE(of, ubx_of_match);
  111. #endif
  112. static struct serdev_device_driver ubx_driver = {
  113. .driver = {
  114. .name = "gnss-ubx",
  115. .of_match_table = of_match_ptr(ubx_of_match),
  116. .pm = &gnss_serial_pm_ops,
  117. },
  118. .probe = ubx_probe,
  119. .remove = ubx_remove,
  120. };
  121. module_serdev_device_driver(ubx_driver);
  122. MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
  123. MODULE_DESCRIPTION("u-blox GNSS receiver driver");
  124. MODULE_LICENSE("GPL v2");