[RFC net-next 14/15] net: lora: Prepare Semtech SX1257

Andreas Färber
2018-07-01T11:11:48Z

The Semtech SX1257 and Sx1255 are usually used for radios A/B of SX1301.
This will not implement a netdev itself, but rather needs to interface
with SX130x somehow.

Cc: Ben Whitten <ben.whitten@lairdtech.com>
Cc: Steve deRosier <derosier@gmail.com>
Cc: Mark Brown <broonie@kernel.org>
Cc: Michael Röder <michael.roeder@avnet.eu>
Cc: Ken Yu (禹凯) <ken.yu@rakwireless.com>
Signed-off-by: Andreas Färber <afaerber@suse.de>
---
drivers/net/lora/Kconfig | 7 ++++
drivers/net/lora/Makefile | 3 ++
drivers/net/lora/sx1257.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 106 insertions(+)
create mode 100644 drivers/net/lora/sx1257.c

diff --git a/drivers/net/lora/Kconfig b/drivers/net/lora/Kconfig
index 3e384493cbdd..68c7480d7812 100644
--- a/drivers/net/lora/Kconfig
+++ b/drivers/net/lora/Kconfig
@@ -31,6 +31,13 @@ config LORA_RN2483
help
Microchip RN2483/2903

+config LORA_SX1257
+ tristate "Semtech SX125x SPI driver"
+ default y
+ depends on SPI
+ help
+ Semtech SX1255/1257
+
config LORA_SX1276
tristate "Semtech SX127x SPI driver"
default y
diff --git a/drivers/net/lora/Makefile b/drivers/net/lora/Makefile
index 6b6870ffbfd8..44c578bde7d5 100644
--- a/drivers/net/lora/Makefile
+++ b/drivers/net/lora/Makefile
@@ -16,6 +16,9 @@ obj-$(CONFIG_LORA_RN2483) += lora-rn2483.o
lora-rn2483-y := rn2483.o
lora-rn2483-y += rn2483_cmd.o

+obj-$(CONFIG_LORA_SX1257) += lora-sx1257.o
+lora-sx1257-y := sx1257.o
+
obj-$(CONFIG_LORA_SX1276) += lora-sx1276.o
lora-sx1276-y := sx1276.o

diff --git a/drivers/net/lora/sx1257.c b/drivers/net/lora/sx1257.c
new file mode 100644
index 000000000000..c4e04ee3ec4b
--- /dev/null
+++ b/drivers/net/lora/sx1257.c
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Semtech SX1255/SX1257 LoRa transceiver
+ *
+ * Copyright (c) 2018 Andreas Färber
+ *
+ * Based on SX1301 HAL code:
+ * Copyright (c) 2013 Semtech-Cycleo
+ */
+
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/spi/spi.h>
+
+static int sx1257_write(struct spi_device *spi, u8 reg, u8 val)
+{
+ u8 buf[2];
+
+ buf[0] = reg | BIT(7);
+ buf[1] = val;
+ return spi_write(spi, buf, 2);
+}
+
+static int sx1257_read(struct spi_device *spi, u8 reg, u8 *val)
+{
+ u8 addr = reg & 0x7f;
+ return spi_write_then_read(spi, &addr, 1, val, 1);
+}
+
+static int sx1257_probe(struct spi_device *spi)
+{
+ u8 val;
+ int ret;
+
+ if (true) {
+ ret = sx1257_read(spi, 0x07, &val);
+ if (ret) {
+ dev_err(&spi->dev, "version read failed\n");
+ return ret;
+ }
+
+ dev_info(&spi->dev, "SX125x version: %02x\n", (unsigned)val);
+ }
+
+ ret = sx1257_write(spi, 0x10, 1 /* + 2 */);
+ if (ret) {
+ dev_err(&spi->dev, "clk write failed\n");
+ return ret;
+ }
+
+ dev_info(&spi->dev, "clk written\n");
+
+ if (true) {
+ ret = sx1257_write(spi, 0x26, 13 + 2 * 16);
+ if (ret) {
+ dev_err(&spi->dev, "xosc write failed\n");
+ return ret;
+ }
+ }
+
+ dev_info(&spi->dev, "SX1257 module probed\n");
+
+ return 0;
+}
+
+static int sx1257_remove(struct spi_device *spi)
+{
+ dev_info(&spi->dev, "SX1257 module removed\n");
+
+ return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id sx1257_dt_ids[] = {
+ { .compatible = "semtech,sx1255" },
+ { .compatible = "semtech,sx1257" },
+ {}
+};
+MODULE_DEVICE_TABLE(of, sx1257_dt_ids);
+#endif
+
+static struct spi_driver sx1257_spi_driver = {
+ .driver = {
+ .name = "sx1257",
+ .of_match_table = of_match_ptr(sx1257_dt_ids),
+ },
+ .probe = sx1257_probe,
+ .remove = sx1257_remove,
+};
+
+module_spi_driver(sx1257_spi_driver);
+
+MODULE_DESCRIPTION("SX1257 SPI driver");
+MODULE_AUTHOR("Andreas Färber <afaerber@suse.de>");
+MODULE_LICENSE("GPL");
--
2.16.4