Skip to contentSkip to conversation

Conversing sx1301 to regmap and regfield

PATCH In review Ben Whitten 4 participants 25 messages Aug 7, 2018 – Aug 10, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Conversing sx1301 to regmap and regfield

This series converts the majority of sx1301 code to using regmap and regfield system.
The SPI subsystem is as yet untouched but will be converted to a regmap_bus in a future series with sx1257 conversions.

Interestingly the initial state did not work for me as I was getting kernel oopses somewhere todo with sx1301_page_read on my hardware.
Since conversion to regmap the sx1301 initialises properly, sx1257 still oopses because of the SPI bus read but presumably will go away with regmap_bus.

Ben Whitten (10):
net: lora: sx1301: add register, bit-fields, and helpers for regmap
net: lora: add methods for devm registration
net: lora: sx1301: convert to devm registration of netdev
net: lora: sx1301: convert probe function to regmap access
net: lora: sx1301: add device to private data and convert ram reads
net: lora: sx1301: simplify firmware loading and convert
net: lora: sx1301: convert read and write burst to take priv data
net: lora: sx1301: convert read and write to priv pointer
net: lora: sx1301: convert agc calibrate to regmap functions
net: lora: sx1301: convert all firmware to regmap

 drivers/net/lora/Kconfig  |   1 +
 drivers/net/lora/dev.c    |  20 ++
 drivers/net/lora/sx1301.c | 803 ++++++++++++++++++++++++----------------------
 drivers/net/lora/sx1301.h | 176 ++++++++++
 include/linux/lora/dev.h  |   1 +
 5 files changed, 623 insertions(+), 378 deletions(-)
 create mode 100644 drivers/net/lora/sx1301.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/10 net: lora: sx1301: add register, bit-fields, and helpers for regmap

From: Ben Whitten <[email protected]>

The register and bit-field definitions are taken from the SX1301 datasheet version 2.01 dated June 2014 with the revision information 'First released version'.

The reset state and RW capability of each field is not reflected in this patch however from the datasheet:
"Bits and registers that are not documented are reserved. They may include calibration values. It is important not to modify these bits and registers. If specific bits must be changed in a register with reserved bits, the register must be read first, specific bits modified while masking reserved bits and then the register can be written."

Then goes on to state:
"Reserved bits should be written with their reset state, they may be read different states."

Caching is currently disabled.

The version is read back using regmap_read to verify regmap operation, in doing so needs to be moved after priv and regmap allocation.

---
 drivers/net/lora/Kconfig  |   1 +
 drivers/net/lora/sx1301.c | 282 +++++++++++++++++++++++++++++++++++++++++++---
 drivers/net/lora/sx1301.h | 169 +++++++++++++++++++++++++++
 3 files changed, 439 insertions(+), 13 deletions(-)
 create mode 100644 drivers/net/lora/sx1301.h
drivers/net/lora/Kconfig +1 -0
index bb57a01..79d23f2 100644
@@ -49,6 +49,7 @@ config LORA_SX1301
 	tristate "Semtech SX1301 SPI driver"
 	default y
 	depends on SPI
+	select REGMAP_SPI
 	help
 	  Semtech SX1301
 
drivers/net/lora/sx1301.c +269 -13

338 lines not on this page · raw message

drivers/net/lora/sx1301.h new file +169 -0

169 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Am 07.08.2018 um 19:32 schrieb Ben Whitten:

Quoted 23 lines

From: Ben Whitten <[email protected]>

The register and bit-field definitions are taken from the SX1301 datasheet version 2.01 dated June 2014 with the revision information 'First released version'.

The reset state and RW capability of each field is not reflected in this patch however from the datasheet:
"Bits and registers that are not documented are reserved. They may include calibration values. It is important not to modify these bits and registers. If specific bits must be changed in a register with reserved bits, the register must be read first, specific bits modified while masking reserved bits and then the register can be written."

Then goes on to state:
"Reserved bits should be written with their reset state, they may be read different states."

Caching is currently disabled.

The version is read back using regmap_read to verify regmap operation, in doing so needs to be moved after priv and regmap allocation.

Quoted 7 lines
---
 drivers/net/lora/Kconfig  |   1 +
 drivers/net/lora/sx1301.c | 282 +++++++++++++++++++++++++++++++++++++++++++---
 drivers/net/lora/sx1301.h | 169 +++++++++++++++++++++++++++
 3 files changed, 439 insertions(+), 13 deletions(-)
 create mode 100644 drivers/net/lora/sx1301.h

My main concern about this patch is its sheer size. Normally for #defines the rule is not to add unused ones. Here I see for example FSK RSSI fields that we're surely not using yet. Any chance to strip this down some more?

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#3 lore raw

RE: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Quoted 49 lines

Subject: Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Am 07.08.2018 um 19:32 schrieb Ben Whitten:

Quoted 3 lines

From: Ben Whitten <[email protected]>

The register and bit-field definitions are taken from the

SX1301

Quoted 1 line

datasheet version 2.01 dated June 2014 with the revision

information

Quoted 3 lines

'First released version'.

The reset state and RW capability of each field is not

reflected in this

Quoted 2 lines

patch however from the datasheet:
"Bits and registers that are not documented are reserved.

They may

Quoted 1 line

include calibration values. It is important not to modify

these bits and

Quoted 1 line

registers. If specific bits must be changed in a register with

reserved

Quoted 1 line

bits, the register must be read first, specific bits modified

while

Quoted 1 line

masking reserved bits and then the register can be

written."

Quoted 3 lines

Then goes on to state:
"Reserved bits should be written with their reset state,

they may be

Quoted 5 lines

read different states."

Caching is currently disabled.

The version is read back using regmap_read to verify

regmap operation,

Quoted 1 line

in doing so needs to be moved after priv and regmap

allocation.

Quoted 2 lines
Signed-off-by Ben Whitten

<[email protected]>

Quoted 3 lines
---
 drivers/net/lora/Kconfig  |   1 +
 drivers/net/lora/sx1301.c | 282

+++++++++++++++++++++++++++++++++++++++++++---

Quoted 1 line
 drivers/net/lora/sx1301.h | 169

+++++++++++++++++++++++++++

Quoted 2 lines
 3 files changed, 439 insertions(+), 13 deletions(-)
 create mode 100644 drivers/net/lora/sx1301.h
Quoted 7 lines

My main concern about this patch is its sheer size. Normally for
#defines the rule is not to add unused ones. Here I see for example FSK
RSSI fields that we're surely not using yet. Any chance to strip this
down some more?

Sure, I'll strip the reg_fields down to those only required for loading firmware and setting clocks that will be used in the immediate term. This does clutter the driver a bit unnecessarily at the moment.
I would like to keep the full register dump in the header file though as its self-contained and gives a full picture of the chip.

Thanks,
Ben

#4 lore raw

Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Am 08.08.2018 um 14:32 schrieb Ben Whitten:

Quoted 16 lines
Quoted 8 lines
Quoted 2 lines
 drivers/net/lora/Kconfig  |   1 +
 drivers/net/lora/sx1301.c | 282

+++++++++++++++++++++++++++++++++++++++++++---

Quoted 1 line
 drivers/net/lora/sx1301.h | 169

+++++++++++++++++++++++++++

Quoted 2 lines
 3 files changed, 439 insertions(+), 13 deletions(-)
 create mode 100644 drivers/net/lora/sx1301.h
Quoted 7 lines

My main concern about this patch is its sheer size. Normally for
#defines the rule is not to add unused ones. Here I see for example FSK
RSSI fields that we're surely not using yet. Any chance to strip this
down some more?

Quoted 6 lines

Sure, I'll strip the reg_fields down to those only required for loading firmware and setting clocks that will be used in the immediate term. This does clutter the driver a bit unnecessarily at the moment.
I would like to keep the full register dump in the header file though as its self-contained and gives a full picture of the chip.

Could you do that in more steps though? I'm thinking, convert only the registers in use to regmap (that'll make it easier to review), then add more registers, then convert to regmap fields?

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#5 lore raw

RE: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Quoted 30 lines

Subject: Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Am 08.08.2018 um 14:32 schrieb Ben Whitten:

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
 drivers/net/lora/Kconfig  |   1 +
 drivers/net/lora/sx1301.c | 282

+++++++++++++++++++++++++++++++++++++++++++-

--

Quoted 6 lines
Quoted 6 lines
Quoted 1 line
 drivers/net/lora/sx1301.h | 169

+++++++++++++++++++++++++++

Quoted 2 lines
 3 files changed, 439 insertions(+), 13 deletions(-)
 create mode 100644 drivers/net/lora/sx1301.h

My main concern about this patch is its sheer size.

Normally

Quoted 2 lines
Quoted 2 lines

for
#defines the rule is not to add unused ones. Here I see

for

Quoted 6 lines
Quoted 4 lines

example FSK
RSSI fields that we're surely not using yet. Any chance to strip this
down some more?

Sure, I'll strip the reg_fields down to those only required

for

Quoted 4 lines

loading firmware and setting clocks that will be used in the immediate term. This does clutter the driver a bit unnecessarily at the moment.
I would like to keep the full register dump in the header

file

Quoted 8 lines
Quoted 1 line

though as its self-contained and gives a full picture of the

chip.

Could you do that in more steps though? I'm thinking, convert only the
registers in use to regmap (that'll make it easier to review), then add
more registers, then convert to regmap fields?

I split the conversion function by function but we can probably go further if you think it helpful.
Is it more the addition of the replacement register defines that you would like to correlate with what's being removed, so you can see in one patch that this register has the same page and the same address in the new system?

The problem I face is that these conversions are almost blind as when I run on my hardware I either oops with the sx1301_read or the cal firmware doesn't verify so I can't finish probe. I only get a full sx1301 module probe pass on physical hardware when I get right to the end of the series where it's all replaced with regmap.

Regards,
Ben Whitten

#6 lore raw

Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

+ Xue Liu + Sebastian

Am 08.08.2018 um 17:52 schrieb Ben Whitten:

Quoted 33 lines
Quoted 33 lines

Subject: Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Am 08.08.2018 um 14:32 schrieb Ben Whitten:

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
 drivers/net/lora/Kconfig  |   1 +
 drivers/net/lora/sx1301.c | 282

+++++++++++++++++++++++++++++++++++++++++++-

--

Quoted 6 lines
Quoted 6 lines
Quoted 1 line
 drivers/net/lora/sx1301.h | 169

+++++++++++++++++++++++++++

Quoted 2 lines
 3 files changed, 439 insertions(+), 13 deletions(-)
 create mode 100644 drivers/net/lora/sx1301.h

My main concern about this patch is its sheer size.

Normally

Quoted 2 lines
Quoted 2 lines

for
#defines the rule is not to add unused ones. Here I see

for

Quoted 6 lines
Quoted 4 lines

example FSK
RSSI fields that we're surely not using yet. Any chance to strip this
down some more?

Sure, I'll strip the reg_fields down to those only required

for

Quoted 4 lines

loading firmware and setting clocks that will be used in the immediate term. This does clutter the driver a bit unnecessarily at the moment.
I would like to keep the full register dump in the header

file

Quoted 1 line

though as its self-contained and gives a full picture of the

chip.

Quoted 8 lines
Quoted 5 lines

Could you do that in more steps though? I'm thinking, convert only the
registers in use to regmap (that'll make it easier to review), then add
more registers, then convert to regmap fields?

I split the conversion function by function but we can probably go further if you think it helpful.

That split feels wrong...

Quoted 4 lines

Is it more the addition of the replacement register defines that you would like to correlate with what's being removed, so you can see in one patch that this register has the same page and the same address in the new system?

I am looking for patches that are trivial to review. One aspect only. So I would much rather get a large patch with a consistent series of

-my_read
+your_read

-my_write
+your_write

that's quick to review than lots of different refactorings mixed into one patch, grouped by their file location.

So I am suggesting that if, for example, you want to pass fw to helpers, which looks like a great idea, that should be a small patch of its own, at the very beginning of your series. (git rebase -i is your friend.)

Converting to regmap_read/write I imagine to be a trivial search-and-replace type refactoring, leaving my |= and &= operations in place, to minimize the diff and avoid it mis-detecting the patch context. Without caching I'd expect regmap to work up to here - if it doesn't, then we can't apply it to my tree and need to prioritize other cleanups while we review/debug further.

Once all registers use regmap successfully, we can optimize that code by introducing regmap fields. This could be split by location, if desired.

Finally in the end you can introduce more registers and fields for future use.

Does that make sense now?

Quoted 6 lines

The problem I face is that these conversions are almost blind as when I run on my hardware I either oops with the sx1301_read or the cal firmware doesn't verify so I can't finish probe. I only get a full sx1301 module probe pass on physical hardware when I get right to the end of the series where it's all replaced with regmap.

If patches don't build or don't work then I can't apply them. Otherwise the 0-day bots will spam us with error reports, as you've seen before.

BTW we'll need this regmap conversion for the picoGW_hal, so once we have a working SPI regmap driver, we'll need to split out the SPI bits, similar to sx125x.

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#7 lore raw

RE: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Quoted 111 lines

To: Ben Whitten <[email protected]>; Ben Whitten <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Xue Liu <[email protected]>; Sebastian Heß <[email protected]>
Subject: Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

+ Xue Liu + Sebastian

Am 08.08.2018 um 17:52 schrieb Ben Whitten:

Quoted 1 line
Quoted 1 line

Subject: Re: [PATCH lora-next 01/10] net: lora: sx1301:

add

Quoted 6 lines
Quoted 6 lines

register, bit-fields, and helpers for regmap

Am 08.08.2018 um 14:32 schrieb Ben Whitten:

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
 drivers/net/lora/Kconfig  |   1 +
 drivers/net/lora/sx1301.c | 282

+++++++++++++++++++++++++++++++++++++++++++-

Quoted 10 lines
Quoted 10 lines

--

Quoted 6 lines
Quoted 6 lines
Quoted 1 line
 drivers/net/lora/sx1301.h | 169

+++++++++++++++++++++++++++

Quoted 2 lines
 3 files changed, 439 insertions(+), 13 deletions(-)
 create mode 100644 drivers/net/lora/sx1301.h

My main concern about this patch is its sheer size.

Normally

Quoted 2 lines
Quoted 2 lines

for
#defines the rule is not to add unused ones. Here I

see

Quoted 3 lines
Quoted 3 lines

for

Quoted 2 lines
Quoted 2 lines

example FSK
RSSI fields that we're surely not using yet. Any chance

to

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

strip this
down some more?

Sure, I'll strip the reg_fields down to those only

required

Quoted 2 lines
Quoted 2 lines

for

Quoted 1 line

loading firmware and setting clocks that will be used in

the

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

immediate term. This does clutter the driver a bit unnecessarily at the moment.
I would like to keep the full register dump in the header

file

Quoted 1 line

though as its self-contained and gives a full picture of

the

Quoted 5 lines
Quoted 5 lines

chip.

Could you do that in more steps though? I'm thinking, convert only the
registers in use to regmap (that'll make it easier to

review),

Quoted 4 lines
Quoted 2 lines

then add
more registers, then convert to regmap fields?

I split the conversion function by function but we can

probably go

Quoted 1 line

further if you think it helpful.

That split feels wrong...

Quoted 1 line

Is it more the addition of the replacement register defines

that you

Quoted 1 line

would like to correlate with what's being removed, so you

can see

Quoted 1 line

in one patch that this register has the same page and the

same

Quoted 1 line

address in the new system?

I am looking for patches that are trivial to review. One aspect only.
So I would much rather get a large patch with a consistent series of

(quoted diff) +2 -2
-my_read
+your_read
 
-my_write
+your_write

that's quick to review than lots of different refactorings mixed into
one patch, grouped by their file location.

So I am suggesting that if, for example, you want to pass fw to helpers,
which looks like a great idea, that should be a small patch of its own,
at the very beginning of your series. (git rebase -i is your friend.)

Converting to regmap_read/write I imagine to be a trivial search-and-replace type refactoring, leaving my |= and &= operations in
place, to minimize the diff and avoid it mis-detecting the patch context.
Without caching I'd expect regmap to work up to here - if it doesn't,
then we can't apply it to my tree and need to prioritize other cleanups
while we review/debug further.

Once all registers use regmap successfully, we can optimize that code by
introducing regmap fields. This could be split by location, if desired.

Quoted 5 lines

Finally in the end you can introduce more registers and fields for
future use.

Does that make sense now?

Yep no problem, hopefully my V2 is suitable. I stopped short of including all registers and doing any regmap_field work in this series so that we can progress.
I have the sx125x split and conversion to regmap_bus for the concentrator ready to go in the wings.

Quoted 8 lines
Quoted 1 line

The problem I face is that these conversions are almost

blind as

Quoted 1 line

when I run on my hardware I either oops with the

sx1301_read

Quoted 1 line

or the cal firmware doesn't verify so I can't finish probe. I

only

Quoted 1 line

get a full sx1301 module probe pass on physical hardware

when

Quoted 8 lines
Quoted 1 line

I get right to the end of the series where it's all replaced

with

Quoted 1 line

regmap.

If patches don't build or don't work then I can't apply them. Otherwise
the 0-day bots will spam us with error reports, as you've seen before.

Understood, each patch in v2 has been tested and I was able to probe and remove modules.

Quoted 5 lines

BTW we'll need this regmap conversion for the picoGW_hal, so once we
have a working SPI regmap driver, we'll need to split out the SPI bits,
similar to sx125x.

I am unfamiliar with the picoGW_hal, do they expose the sx1301 as a device on a regmap_bus then?

Regards,
Ben Whitten

#8 lore raw

Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Am 09.08.2018 um 15:23 schrieb Ben Whitten:

Quoted 8 lines
Quoted 5 lines

BTW we'll need this regmap conversion for the picoGW_hal, so once we
have a working SPI regmap driver, we'll need to split out the SPI bits,
similar to sx125x.

I am unfamiliar with the picoGW_hal, do they expose the sx1301 as a device on a regmap_bus then?

It uses an MCU implementing a USB CDC interface that implements UART commands corresponding to the individual SPI read and write modes like single and burst. That reference design is for SX1308, but some vendors appear to be using it for SX1301, too.

https://github.com/Lora-net/picoGW_mcu

So I believe I'll need to implement a regmap_bus in a new serdev driver to support it, once I gain access to such an implementation.

But there's other general serdev problems with USB not limited to picoGW that I'll need to start a thread about on linux-serial/linux-usb...

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#9 lore raw

Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

In reply to Ben Whitten

Am 07.08.2018 um 19:32 schrieb Ben Whitten:

Quoted 4 lines
drivers/net/lora/sx1301.c +0 -0
index 5342b61..49958f0 100644

[...]

Quoted 9 lines
drivers/net/lora/sx1301.c +3 -0
@@ -76,8 +287,29 @@ struct sx1301_priv {
 	struct gpio_desc *rst_gpio;
 	u8 cur_page;
 	struct spi_controller *radio_a_ctrl, *radio_b_ctrl;
+	struct regmap		*regmap;
+	struct regmap_field	*regmap_fields[ARRAY_SIZE(sx1301_reg_fields)];
 };
 
+static int sx1301_field_read(struct sx1301_priv *priv,

drivers/net/lora/sx1301.c struct sx1301_priv { · line 304

Quoted 10 lines
drivers/net/lora/sx1301.c +10 -0
+		enum sx1301_fields field_id)
+{
+	int ret;
+	int val;
+
+	ret = regmap_field_read(priv->regmap_fields[field_id], &val);
+	if (ret)
+		return ret;
+
+	return val;

This strikes me as a bad idea. Please keep returning the value by pointer, so that we can clearly distinguish from error values.

drivers/net/lora/sx1301.c struct sx1301_priv { · line 315

Quoted 11 lines
drivers/net/lora/sx1301.c +8 -0
+}
+
+static int sx1301_field_write(struct sx1301_priv *priv,
+		enum sx1301_fields field_id, u8 val)
+{
+	return regmap_field_write(priv->regmap_fields[field_id], val);
+}
+
 static int sx1301_read_burst(struct spi_device *spi, u8 reg, u8 *val, size_t len)
 {
 	u8 addr = reg & 0x7f;

[snip]

It looks to me as if both of those static functions are unused in this patch? Please keep things bisectable.

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#10 lore raw

RE: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Quoted 19 lines

Subject: Re: [PATCH lora-next 01/10] net: lora: sx1301: add register, bit-fields, and helpers for regmap

Am 07.08.2018 um 19:32 schrieb Ben Whitten:

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/net/lora/sx1301.c

Quoted 3 lines

index 5342b61..49958f0 100644

drivers/net/lora/sx1301.c +0 -0

[...]

drivers/net/lora/sx1301.c struct sx1301_priv { · line 291

Quoted 6 lines
drivers/net/lora/sx1301.c +2 -0
@@ -76,8 +287,29 @@ struct sx1301_priv {
 	struct gpio_desc *rst_gpio;
 	u8 cur_page;
 	struct spi_controller *radio_a_ctrl, *radio_b_ctrl;
+	struct regmap		*regmap;
+	struct regmap_field
	*regmap_fields[ARRAY_SIZE(sx1301_reg_fields)];
Quoted 2 lines
drivers/net/lora/sx1301.c +0 -0
 };
 
Quoted 16 lines

drivers/net/lora/sx1301.c struct sx1301_priv { · line 304

Quoted 12 lines
drivers/net/lora/sx1301.c +11 -0
+static int sx1301_field_read(struct sx1301_priv *priv,
+		enum sx1301_fields field_id)
+{
+	int ret;
+	int val;
+
+	ret = regmap_field_read(priv-
 regmap_fields[field_id], &val);
+	if (ret)
+		return ret;
+
+	return val;

This strikes me as a bad idea. Please keep returning the value by
pointer, so that we can clearly distinguish from error values.

Good point, will change it.

Quoted 11 lines

drivers/net/lora/sx1301.c struct sx1301_priv {

Quoted 10 lines
drivers/net/lora/sx1301.c +8 -0
+}
+
+static int sx1301_field_write(struct sx1301_priv *priv,
+		enum sx1301_fields field_id, u8 val)
+{
+	return regmap_field_write(priv-
 regmap_fields[field_id], val);
+}
+
 static int sx1301_read_burst(struct spi_device *spi, u8

reg, u8 *val, size_t len)

Quoted 7 lines
Quoted 2 lines

{
u8 addr = reg & 0x7f;

[snip]

It looks to me as if both of those static functions are unused in this
patch? Please keep things bisectable.

Sure, it was a prep patch bit I can include these functions in the next one along where they are actually used.

Thanks,
Ben

#11 lore raw

RFC 1/1 spi: add spi multiplexing functions for dt

In reply to Ben Whitten

Like I2C busses SPI devices can also sit behind multiplexers. This patch adds is based off the I2C implementation and allows description in the devicetree.

---
 drivers/spi/Kconfig     |  10 +++
 drivers/spi/Makefile    |   3 +
 drivers/spi/spi-mux.c   | 181 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/spi-mux.h |  55 +++++++++++++++
 4 files changed, 249 insertions(+)
 create mode 100644 drivers/spi/spi-mux.c
 create mode 100644 include/linux/spi-mux.h
drivers/spi/Kconfig +10 -0
index a75f2a2..58eba70 100644
@@ -51,6 +51,16 @@ config SPI_MASTER
 
 if SPI_MASTER
 
+config SPI_MUX
+	tristate "SPI bus multiplexing support"
+	help
+	  Say Y here if you want the SPI core to support the ability to
+	  handle multiplexed SPI bus topologies, by presenting each
+	  multiplexed segment as an SPI controller.
+
+	  This support is also available as a module.  If so, the module
+	  will be called spi-mux.
+
 comment "SPI Master Controller Drivers"
 
 config SPI_ALTERA
drivers/spi/Makefile +3 -0
index 8e0cda7..ef525fe 100644
@@ -109,6 +109,9 @@ obj-$(CONFIG_SPI_XLP)			+= spi-xlp.o
 obj-$(CONFIG_SPI_XTENSA_XTFPGA)		+= spi-xtensa-xtfpga.o
 obj-$(CONFIG_SPI_ZYNQMP_GQSPI)		+= spi-zynqmp-gqspi.o
 
+# SPI muxs
+obj-$(CONFIG_SPI_MUX)			+= spi-mux.o
+
 # SPI slave protocol handlers
 obj-$(CONFIG_SPI_SLAVE_TIME)		+= spi-slave-time.o
 obj-$(CONFIG_SPI_SLAVE_SYSTEM_CONTROL)	+= spi-slave-system-control.o
drivers/spi/spi-mux.c new file +181 -0

181 lines not on this page · raw message

include/linux/spi-mux.h new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.7.4
#12 lore raw

Re: [RFC] spi: add spi multiplexing functions for dt

Hi Ben,

Am 07.08.2018 um 19:32 schrieb Ben Whitten:

Quoted 5 lines

Like I2C busses SPI devices can also sit behind multiplexers. This patch adds is based off the I2C implementation and allows description in the devicetree.

Quoted 8 lines
---
 drivers/spi/Kconfig     |  10 +++
 drivers/spi/Makefile    |   3 +
 drivers/spi/spi-mux.c   | 181 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/spi-mux.h |  55 +++++++++++++++
 4 files changed, 249 insertions(+)
 create mode 100644 drivers/spi/spi-mux.c
 create mode 100644 include/linux/spi-mux.h

Did this get sent by mistake? It needs to go to linux-spi list and maintainers.

Tip: git config sendemail.cccmd "scripts/get_maintainer.pl --nogit-fallback --norolestats"

Cheers,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#13 lore raw

Re: [RFC] spi: add spi multiplexing functions for dt

Quoted 18 lines · Andreas Färber

Hi Ben,

Am 07.08.2018 um 19:32 schrieb Ben Whitten:

Quoted 5 lines

Like I2C busses SPI devices can also sit behind multiplexers. This patch adds is based off the I2C implementation and allows description in the devicetree.

Quoted 8 lines
---
 drivers/spi/Kconfig     |  10 +++
 drivers/spi/Makefile    |   3 +
 drivers/spi/spi-mux.c   | 181 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/spi-mux.h |  55 +++++++++++++++
 4 files changed, 249 insertions(+)
 create mode 100644 drivers/spi/spi-mux.c
 create mode 100644 include/linux/spi-mux.h
Quoted 2 lines

Did this get sent by mistake? It needs to go to linux-spi list and maintainers.

It certainly did, didn't spot it in my outgoing patches directory. Please disregard, sorry for the noise.

Quoted 2 lines

Tip: git config sendemail.cccmd "scripts/get_maintainer.pl --nogit-fallback --norolestats"

Thanks for the tip

Cheers,
Ben

#14 lore raw

PATCH 2/10 net: lora: add methods for devm registration

In reply to Ben Whitten

From: Ben Whitten <[email protected]>

Follow the devm model so that we can avoid lengthy unwind code.

---
 drivers/net/lora/dev.c   | 20 ++++++++++++++++++++
 include/linux/lora/dev.h |  1 +
 2 files changed, 21 insertions(+)
drivers/net/lora/dev.c +20 -0
index 8c01106..69a8b52 100644
@@ -84,6 +84,26 @@ void free_loradev(struct net_device *dev)
 }
 EXPORT_SYMBOL_GPL(free_loradev);
 
+static void devm_lora_unregister(struct device *dev, void *res)
+{
+	free_loradev(*(struct net_device **)res);
+}
+
+int devm_lora_register_netdev(struct device *dev, struct net_device *net)
+{
+	struct net_device **ptr;
+
+	ptr = devres_alloc(devm_lora_unregister, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return -ENOMEM;
+
+	*ptr = net;
+	devres_add(dev, ptr);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_lora_register_netdev);
+
 static struct rtnl_link_ops lora_link_ops __read_mostly = {
 	.kind = "lora",
 	.setup = lora_setup,
include/linux/lora/dev.h +1 -0
index 153f9b2..3f871c6 100644
@@ -32,6 +32,7 @@ static inline int lora_strtoeui(const char *str, lora_eui *val)
 
 struct net_device *alloc_loradev(int sizeof_priv);
 void free_loradev(struct net_device *dev);
+int devm_lora_register_netdev(struct device *dev, struct net_device *net);
 int register_loradev(struct net_device *dev);
 void unregister_loradev(struct net_device *dev);
 int open_loradev(struct net_device *dev);
Signature · 1 line
2.7.4
#15 lore raw

Re: [PATCH lora-next 02/10] net: lora: add methods for devm registration

Am 07.08.2018 um 19:32 schrieb Ben Whitten:

Quoted 15 lines

From: Ben Whitten <[email protected]>

Follow the devm model so that we can avoid lengthy unwind code.

---
 drivers/net/lora/dev.c   | 20 ++++++++++++++++++++
 include/linux/lora/dev.h |  1 +
 2 files changed, 21 insertions(+)
drivers/net/lora/dev.c +0 -0
index 8c01106..69a8b52 100644

drivers/net/lora/dev.c void free_loradev(struct net_device *dev) · line 89

Quoted 6 lines
drivers/net/lora/dev.c +3 -0
@@ -84,6 +84,26 @@ void free_loradev(struct net_device *dev)
 }
 EXPORT_SYMBOL_GPL(free_loradev);
 
+static void devm_lora_unregister(struct device *dev, void *res)
+{
+	free_loradev(*(struct net_device **)res);

Suggest to use a variable.

drivers/net/lora/dev.c void free_loradev(struct net_device *dev) · line 92

Quoted 3 lines
drivers/net/lora/dev.c +3 -0
+}
+
+int devm_lora_register_netdev(struct device *dev, struct net_device *net)

Nice idea, but why a separate registration function? Suggest to instead introduce devm_alloc_loradev(). If you then reorder those two patches to the front of the series, I'll queue them immediately.

Thanks,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#16 lore raw

PATCH 3/10 net: lora: sx1301: convert to devm registration of netdev

In reply to Ben Whitten

From: Ben Whitten <[email protected]>

We allow the devres framework handle the clean removal of resources on teardown of the device, in this case the SPI device, saving lengthy unwind code and improving clarity.

---
 drivers/net/lora/sx1301.c | 87 +++++++++++++++++------------------------------
 1 file changed, 31 insertions(+), 56 deletions(-)
drivers/net/lora/sx1301.c +31 -56
index 49958f0..54bfc31 100644
@@ -869,9 +869,13 @@ static int sx1301_probe(struct spi_device *spi)
 	spi_setup(spi);
 
 	netdev = alloc_loradev(sizeof(*priv));
-	if (!netdev) {
-		ret = -ENOMEM;
-		goto err_alloc_loradev;
+	if (!netdev)
+		return -ENOMEM;
+
+	ret = devm_lora_register_netdev(&spi->dev, netdev);
+	if (ret) {
+		free_loradev(netdev);
+		return ret;
 	}
 
 	priv = netdev_priv(netdev);
@@ -885,7 +889,7 @@ static int sx1301_probe(struct spi_device *spi)
 	if (IS_ERR(priv->regmap)) {
 		ret = PTR_ERR(priv->regmap);
 		dev_err(&spi->dev, "Regmap allocation failed: %d\n", ret);
-		return err_regmap;
+		return ret;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(sx1301_reg_fields); i++) {
@@ -897,38 +901,37 @@ static int sx1301_probe(struct spi_device *spi)
 		if (IS_ERR(priv->regmap_fields[i])) {
 			ret = PTR_ERR(priv->regmap_fields[i]);
 			dev_err(&spi->dev, "Cannot allocate regmap field: %d\n", ret);
-			goto err_regmap;
+			return ret;
 		}
 	}
 
 	ret = regmap_read(priv->regmap, SX1301_VER, &ver);
 	if (ret) {
 		dev_err(&spi->dev, "version read failed\n");
-		goto err_version;
+		return ret;
 	}
 
 	if (ver != 103) {
 		dev_err(&spi->dev, "unexpected version: %u\n", ver);
-		ret = -ENXIO;
-		goto err_version;
+		return -ENXIO;
 	}
 
 	ret = sx1301_write(spi, REG_PAGE_RESET, 0);
 	if (ret) {
 		dev_err(&spi->dev, "page/reset write failed\n");
-		goto err_init_page;
+		return ret;
 	}
 
 	ret = sx1301_soft_reset(spi);
 	if (ret) {
 		dev_err(&spi->dev, "soft reset failed\n");
-		goto err_soft_reset;
+		return ret;
 	}
 
 	ret = sx1301_read(spi, 16, &val);
 	if (ret) {
 		dev_err(&spi->dev, "16 read failed\n");
-		goto err_read_global_en_0;
+		return ret;
 	}
 
 	val &= ~REG_16_GLOBAL_EN;
@@ -936,13 +939,13 @@ static int sx1301_probe(struct spi_device *spi)
 	ret = sx1301_write(spi, 16, val);
 	if (ret) {
 		dev_err(&spi->dev, "16 write failed\n");
-		goto err_write_global_en_0;
+		return ret;
 	}
 
 	ret = sx1301_read(spi, 17, &val);
 	if (ret) {
 		dev_err(&spi->dev, "17 read failed\n");
-		goto err_read_clk32m_0;
+		return ret;
 	}
 
 	val &= ~REG_17_CLK32M_EN;
@@ -950,7 +953,7 @@ static int sx1301_probe(struct spi_device *spi)
 	ret = sx1301_write(spi, 17, val);
 	if (ret) {
 		dev_err(&spi->dev, "17 write failed\n");
-		goto err_write_clk32m_0;
+		return ret;
 	}
 
 	ret = sx1301_page_read(spi, 2, 43, &val);
@@ -1003,8 +1006,7 @@ static int sx1301_probe(struct spi_device *spi)
 
 	priv->radio_a_ctrl = spi_alloc_master(&spi->dev, sizeof(*radio));
 	if (!priv->radio_a_ctrl) {
-		ret = -ENOMEM;
-		goto err_radio_a_alloc;
+		return -ENOMEM;
 	}
 
 	sx1301_radio_setup(priv->radio_a_ctrl);
@@ -1019,15 +1021,14 @@ static int sx1301_probe(struct spi_device *spi)
 	if (ret) {
 		dev_err(&spi->dev, "radio A SPI register failed\n");
 		spi_controller_put(priv->radio_a_ctrl);
-		goto err_radio_a_register;
+		return ret;
 	}
 
 	/* radio B */
 
 	priv->radio_b_ctrl = spi_alloc_master(&spi->dev, sizeof(*radio));
 	if (!priv->radio_b_ctrl) {
-		ret = -ENOMEM;
-		goto err_radio_b_alloc;
+		return -ENOMEM;
 	}
 
 	sx1301_radio_setup(priv->radio_b_ctrl);
@@ -1042,7 +1043,7 @@ static int sx1301_probe(struct spi_device *spi)
 	if (ret) {
 		dev_err(&spi->dev, "radio B SPI register failed\n");
 		spi_controller_put(priv->radio_b_ctrl);
-		goto err_radio_b_register;
+		return ret;
 	}
 
 	/* GPIO */
@@ -1050,7 +1051,7 @@ static int sx1301_probe(struct spi_device *spi)
 	ret = sx1301_read(spi, REG_GPIO_MODE, &val);
 	if (ret) {
 		dev_err(&spi->dev, "GPIO mode read failed\n");
-		goto err_read_gpio_mode;
+		return ret;
 	}
 
 	val |= GENMASK(4, 0);
@@ -1058,13 +1059,13 @@ static int sx1301_probe(struct spi_device *spi)
 	ret = sx1301_write(spi, REG_GPIO_MODE, val);
 	if (ret) {
 		dev_err(&spi->dev, "GPIO mode write failed\n");
-		goto err_write_gpio_mode;
+		return ret;
 	}
 
 	ret = sx1301_read(spi, REG_GPIO_SELECT_OUTPUT, &val);
 	if (ret) {
 		dev_err(&spi->dev, "GPIO select output read failed\n");
-		goto err_read_gpio_select_output;
+		return ret;
 	}
 
 	val &= ~GENMASK(3, 0);
@@ -1073,7 +1074,7 @@ static int sx1301_probe(struct spi_device *spi)
 	ret = sx1301_write(spi, REG_GPIO_SELECT_OUTPUT, val);
 	if (ret) {
 		dev_err(&spi->dev, "GPIO select output write failed\n");
-		goto err_write_gpio_select_output;
+		return ret;
 	}
 
 	/* TODO LBT */
@@ -1081,7 +1082,7 @@ static int sx1301_probe(struct spi_device *spi)
 	ret = sx1301_read(spi, 16, &val);
 	if (ret) {
 		dev_err(&spi->dev, "16 read (1) failed\n");
-		goto err_read_global_en_1;
+		return ret;
 	}
 
 	val |= REG_16_GLOBAL_EN;
@@ -1089,13 +1090,13 @@ static int sx1301_probe(struct spi_device *spi)
 	ret = sx1301_write(spi, 16, val);
 	if (ret) {
 		dev_err(&spi->dev, "16 write (1) failed\n");
-		goto err_write_global_en_1;
+		return ret;
 	}
 
 	ret = sx1301_read(spi, 17, &val);
 	if (ret) {
 		dev_err(&spi->dev, "17 read (1) failed\n");
-		goto err_read_clk32m_1;
+		return ret;
 	}
 
 	val |= REG_17_CLK32M_EN;
@@ -1103,50 +1104,24 @@ static int sx1301_probe(struct spi_device *spi)
 	ret = sx1301_write(spi, 17, val);
 	if (ret) {
 		dev_err(&spi->dev, "17 write (1) failed\n");
-		goto err_write_clk32m_1;
+		return ret;
 	}
 
 	/* calibration */
 
 	ret = sx1301_agc_calibrate(spi);
 	if (ret)
-		goto err_agc_calibrate;
+		return ret;
 
 	/* TODO */
 
 	ret = sx1301_load_all_firmware(spi);
 	if (ret)
-		goto err_load_firmware;
+		return ret;
 
 	dev_info(&spi->dev, "SX1301 module probed\n");
 
 	return 0;
-
-err_load_firmware:
-err_agc_calibrate:
-err_write_clk32m_1:
-err_read_clk32m_1:
-err_write_global_en_1:
-err_read_global_en_1:
-err_write_gpio_select_output:
-err_read_gpio_select_output:
-err_write_gpio_mode:
-err_read_gpio_mode:
-err_radio_b_register:
-err_radio_b_alloc:
-err_radio_a_register:
-err_radio_a_alloc:
-err_write_clk32m_0:
-err_read_clk32m_0:
-err_write_global_en_0:
-err_read_global_en_0:
-err_soft_reset:
-err_init_page:
-err_version:
-err_regmap:
-	free_loradev(netdev);
-err_alloc_loradev:
-	return ret;
 }
 
 static int sx1301_remove(struct spi_device *spi)
Signature · 1 line
2.7.4
#17 lore raw

Re: [PATCH lora-next 03/10] net: lora: sx1301: convert to devm registration of netdev

Am 07.08.2018 um 19:32 schrieb Ben Whitten:

Quoted 4 lines
drivers/net/lora/sx1301.c +0 -0
index 49958f0..54bfc31 100644

[...]

drivers/net/lora/sx1301.c static int sx1301_probe(struct spi_device *spi) · line 888

Quoted 5 lines
drivers/net/lora/sx1301.c +0 -1
@@ -885,7 +889,7 @@ static int sx1301_probe(struct spi_device *spi)
 	if (IS_ERR(priv->regmap)) {
 		ret = PTR_ERR(priv->regmap);
 		dev_err(&spi->dev, "Regmap allocation failed: %d\n", ret);
-		return err_regmap;

That hints at a build error in an earlier patch.

Regards,
Andreas

Quoted 10 lines
drivers/net/lora/sx1301.c +2 -1
+		return ret;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(sx1301_reg_fields); i++) {
@@ -897,38 +901,37 @@ static int sx1301_probe(struct spi_device *spi)
 		if (IS_ERR(priv->regmap_fields[i])) {
 			ret = PTR_ERR(priv->regmap_fields[i]);
 			dev_err(&spi->dev, "Cannot allocate regmap field: %d\n", ret);
-			goto err_regmap;
+			return ret;

[snip]

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#18 lore raw

PATCH 4/10 net: lora: sx1301: convert probe function to regmap access

In reply to Ben Whitten

From: Ben Whitten <[email protected]>

Keeping change sets small we convert the probe function over to regmap and regmap_field access for initialisation.

---
 drivers/net/lora/sx1301.c | 155 +++++++++-------------------------------------
 drivers/net/lora/sx1301.h |   7 +++
 2 files changed, 35 insertions(+), 127 deletions(-)
drivers/net/lora/sx1301.c +28 -127
index 54bfc31..118e8d8 100644
@@ -379,11 +379,6 @@ static int sx1301_page_write(struct spi_device *spi, u8 page, u8 reg, u8 val)
 	return sx1301_write(spi, reg, val);
 }
 
-static int sx1301_soft_reset(struct spi_device *spi)
-{
-	return sx1301_write(spi, REG_PAGE_RESET, REG_PAGE_RESET_SOFT_RESET);
-}
-
 #define REG_RADIO_X_DATA		0
 #define REG_RADIO_X_DATA_READBACK	1
 #define REG_RADIO_X_ADDR		2
@@ -854,7 +849,6 @@ static int sx1301_probe(struct spi_device *spi)
 	int ret;
 	int i;
 	unsigned int ver;
-	u8 val;
 
 	rst = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_LOW);
 	if (IS_ERR(rst))
@@ -911,96 +905,46 @@ static int sx1301_probe(struct spi_device *spi)
 		return ret;
 	}
 
-	if (ver != 103) {
+	if (ver != SX1301_CHIP_VERSION) {
 		dev_err(&spi->dev, "unexpected version: %u\n", ver);
 		return -ENXIO;
 	}
 
-	ret = sx1301_write(spi, REG_PAGE_RESET, 0);
+	ret = regmap_write(priv->regmap, SX1301_PAGE, 0);
 	if (ret) {
 		dev_err(&spi->dev, "page/reset write failed\n");
 		return ret;
 	}
 
-	ret = sx1301_soft_reset(spi);
+	ret = sx1301_field_write(priv, F_SOFT_RESET, 1);
 	if (ret) {
 		dev_err(&spi->dev, "soft reset failed\n");
 		return ret;
 	}
 
-	ret = sx1301_read(spi, 16, &val);
-	if (ret) {
-		dev_err(&spi->dev, "16 read failed\n");
-		return ret;
-	}
-
-	val &= ~REG_16_GLOBAL_EN;
-
-	ret = sx1301_write(spi, 16, val);
-	if (ret) {
-		dev_err(&spi->dev, "16 write failed\n");
-		return ret;
-	}
-
-	ret = sx1301_read(spi, 17, &val);
-	if (ret) {
-		dev_err(&spi->dev, "17 read failed\n");
-		return ret;
-	}
-
-	val &= ~REG_17_CLK32M_EN;
-
-	ret = sx1301_write(spi, 17, val);
-	if (ret) {
-		dev_err(&spi->dev, "17 write failed\n");
-		return ret;
-	}
-
-	ret = sx1301_page_read(spi, 2, 43, &val);
-	if (ret) {
-		dev_err(&spi->dev, "2|43 read failed\n");
+	/* gate clocks */
+	ret = sx1301_field_write(priv, F_GLOBAL_EN, 0);
+	if (ret)
 		return ret;
-	}
-
-	val |= REG_2_43_RADIO_B_EN | REG_2_43_RADIO_A_EN;
-
-	ret = sx1301_page_write(spi, 2, 43, val);
-	if (ret) {
-		dev_err(&spi->dev, "2|43 write failed\n");
+	ret = sx1301_field_write(priv, F_CLK32M_EN, 0);
+	if (ret)
 		return ret;
-	}
 
-	msleep(500);
-
-	ret = sx1301_page_read(spi, 2, 43, &val);
-	if (ret) {
-		dev_err(&spi->dev, "2|43 read failed\n");
+	/* switch on and reset the radios (also starts the 32 MHz XTAL) */
+	ret = sx1301_field_write(priv, F_RADIO_A_EN, 1);
+	if (ret)
 		return ret;
-	}
-
-	val |= REG_2_43_RADIO_RST;
-
-	ret = sx1301_page_write(spi, 2, 43, val);
-	if (ret) {
-		dev_err(&spi->dev, "2|43 write failed\n");
+	ret = sx1301_field_write(priv, F_RADIO_B_EN, 1);
+	if (ret)
 		return ret;
-	}
-
-	msleep(5);
-
-	ret = sx1301_page_read(spi, 2, 43, &val);
-	if (ret) {
-		dev_err(&spi->dev, "2|43 read failed\n");
+	mdelay(500);
+	ret = sx1301_field_write(priv, F_RADIO_RST, 1);
+	if (ret)
 		return ret;
-	}
-
-	val &= ~REG_2_43_RADIO_RST;
-
-	ret = sx1301_page_write(spi, 2, 43, val);
-	if (ret) {
-		dev_err(&spi->dev, "2|43 write failed\n");
+	mdelay(5);
+	ret = sx1301_field_write(priv, F_RADIO_RST, 0);
+	if (ret)
 		return ret;
-	}
 
 	/* radio A */
 
@@ -1047,65 +991,22 @@ static int sx1301_probe(struct spi_device *spi)
 	}
 
 	/* GPIO */
-
-	ret = sx1301_read(spi, REG_GPIO_MODE, &val);
-	if (ret) {
-		dev_err(&spi->dev, "GPIO mode read failed\n");
-		return ret;
-	}
-
-	val |= GENMASK(4, 0);
-
-	ret = sx1301_write(spi, REG_GPIO_MODE, val);
-	if (ret) {
-		dev_err(&spi->dev, "GPIO mode write failed\n");
-		return ret;
-	}
-
-	ret = sx1301_read(spi, REG_GPIO_SELECT_OUTPUT, &val);
-	if (ret) {
-		dev_err(&spi->dev, "GPIO select output read failed\n");
+	ret = regmap_write(priv->regmap, SX1301_GPMODE, 0x1F);
+	if (ret)
 		return ret;
-	}
-
-	val &= ~GENMASK(3, 0);
-	val |= 2;
-
-	ret = sx1301_write(spi, REG_GPIO_SELECT_OUTPUT, val);
-	if (ret) {
-		dev_err(&spi->dev, "GPIO select output write failed\n");
+	ret = regmap_write(priv->regmap, SX1301_GPSO, 0x2);
+	if (ret)
 		return ret;
-	}
 
 	/* TODO LBT */
 
-	ret = sx1301_read(spi, 16, &val);
-	if (ret) {
-		dev_err(&spi->dev, "16 read (1) failed\n");
-		return ret;
-	}
-
-	val |= REG_16_GLOBAL_EN;
-
-	ret = sx1301_write(spi, 16, val);
-	if (ret) {
-		dev_err(&spi->dev, "16 write (1) failed\n");
-		return ret;
-	}
-
-	ret = sx1301_read(spi, 17, &val);
-	if (ret) {
-		dev_err(&spi->dev, "17 read (1) failed\n");
+	/* start clocks */
+	ret = sx1301_field_write(priv, F_GLOBAL_EN, 1);
+	if (ret)
 		return ret;
-	}
-
-	val |= REG_17_CLK32M_EN;
-
-	ret = sx1301_write(spi, 17, val);
-	if (ret) {
-		dev_err(&spi->dev, "17 write (1) failed\n");
+	ret = sx1301_field_write(priv, F_CLK32M_EN, 1);
+	if (ret)
 		return ret;
-	}
 
 	/* calibration */
 
drivers/net/lora/sx1301.h +7 -0
index abfb7b5..0e84703 100644
@@ -8,6 +8,13 @@
 #ifndef _SX1301_
 #define _SX1301_
 
+#define SX1301_CHIP_VERSION 103
+
+#define SX1301_MCU_FW_BYTE 8192
+#define SX1301_MCU_ARB_FW_VERSION 1
+#define SX1301_MCU_AGC_FW_VERSION 4
+#define SX1301_MCU_AGC_CAL_FW_VERSION 2
+
 /* Page independent */
 #define SX1301_PAGE     0x00
 #define SX1301_VER      0x01
Signature · 1 line
2.7.4
#19 lore raw

PATCH 5/10 net: lora: sx1301: add device to private data and convert ram reads

In reply to Ben Whitten

From: Ben Whitten <[email protected]>

It's easier to simply add a pointer to our device to our private data, this way we can avoid passing around our spi_device and instead just pass private data which is more flexible.

As its a two line change its coupled with converting AGC and ARB ram reads to regmap functions.

---
 drivers/net/lora/sx1301.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/net/lora/sx1301.c +21 -13
index 118e8d8..249551b 100644
@@ -283,6 +283,7 @@ struct spi_sx1301 {
 };
 
 struct sx1301_priv {
+	struct device *dev;
 	struct lora_priv lora;
 	struct gpio_desc *rst_gpio;
 	u8 cur_page;
@@ -472,40 +473,44 @@ static int sx1301_radio_spi_transfer_one(struct spi_controller *ctrl,
 	return 0;
 }
 
-static int sx1301_agc_ram_read(struct spi_device *spi, u8 addr, u8 *val)
+static int sx1301_agc_ram_read(struct sx1301_priv *priv, u8 addr, u8 *val)
 {
 	int ret;
+	unsigned int read;
 
-	ret = sx1301_page_write(spi, 2, REG_2_DBG_AGC_MCU_RAM_ADDR, addr);
+	ret = regmap_write(priv->regmap, SX1301_DBG_AGC_MCU_RAM_ADDR, addr);
 	if (ret) {
-		dev_err(&spi->dev, "AGC RAM addr write failed\n");
+		dev_err(priv->dev, "AGC RAM addr write failed\n");
 		return ret;
 	}
 
-	ret = sx1301_page_read(spi, 2, REG_2_DBG_AGC_MCU_RAM_DATA, val);
+	ret = regmap_read(priv->regmap, SX1301_DBG_AGC_MCU_RAM_DATA, &read);
 	if (ret) {
-		dev_err(&spi->dev, "AGC RAM data read failed\n");
+		dev_err(priv->dev, "AGC RAM data read failed\n");
 		return ret;
 	}
+	*val = read;
 
 	return 0;
 }
 
-static int sx1301_arb_ram_read(struct spi_device *spi, u8 addr, u8 *val)
+static int sx1301_arb_ram_read(struct sx1301_priv *priv, u8 addr, u8 *val)
 {
 	int ret;
+	unsigned int read;
 
-	ret = sx1301_page_write(spi, 2, REG_2_DBG_ARB_MCU_RAM_ADDR, addr);
+	ret = regmap_write(priv->regmap, SX1301_DBG_ARB_MCU_RAM_ADDR, addr);
 	if (ret) {
-		dev_err(&spi->dev, "ARB RAM addr write failed\n");
+		dev_err(priv->dev, "ARB RAM addr write failed\n");
 		return ret;
 	}
 
-	ret = sx1301_page_read(spi, 2, REG_2_DBG_ARB_MCU_RAM_DATA, val);
+	ret = regmap_read(priv->regmap, SX1301_DBG_ARB_MCU_RAM_DATA, &read);
 	if (ret) {
-		dev_err(&spi->dev, "ARB RAM data read failed\n");
+		dev_err(priv->dev, "ARB RAM data read failed\n");
 		return ret;
 	}
+	*val = read;
 
 	return 0;
 }
@@ -604,6 +609,7 @@ static int sx1301_load_firmware(struct spi_device *spi, int mcu, const u8 *data,
 static int sx1301_agc_calibrate(struct spi_device *spi)
 {
 	const struct firmware *fw;
+	struct sx1301_priv *priv = spi_get_drvdata(spi);
 	u8 val;
 	int ret;
 
@@ -663,7 +669,7 @@ static int sx1301_agc_calibrate(struct spi_device *spi)
 		return ret;
 	}
 
-	ret = sx1301_agc_ram_read(spi, 0x20, &val);
+	ret = sx1301_agc_ram_read(priv, 0x20, &val);
 	if (ret) {
 		dev_err(&spi->dev, "AGC RAM data read failed\n");
 		return ret;
@@ -730,6 +736,7 @@ static int sx1301_agc_calibrate(struct spi_device *spi)
 
 static int sx1301_load_all_firmware(struct spi_device *spi)
 {
+	struct sx1301_priv *priv = spi_get_drvdata(spi);
 	const struct firmware *fw;
 	u8 val;
 	int ret;
@@ -802,7 +809,7 @@ static int sx1301_load_all_firmware(struct spi_device *spi)
 		return ret;
 	}
 
-	ret = sx1301_agc_ram_read(spi, 0x20, &val);
+	ret = sx1301_agc_ram_read(priv, 0x20, &val);
 	if (ret) {
 		dev_err(&spi->dev, "AGC RAM data read failed\n");
 		return ret;
@@ -815,7 +822,7 @@ static int sx1301_load_all_firmware(struct spi_device *spi)
 		return -ENXIO;
 	}
 
-	ret = sx1301_arb_ram_read(spi, 0x20, &val);
+	ret = sx1301_arb_ram_read(priv, 0x20, &val);
 	if (ret) {
 		dev_err(&spi->dev, "ARB RAM data read failed\n");
 		return ret;
@@ -878,6 +885,7 @@ static int sx1301_probe(struct spi_device *spi)
 
 	spi_set_drvdata(spi, netdev);
 	SET_NETDEV_DEV(netdev, &spi->dev);
+	priv->dev = &spi->dev;
 
 	priv->regmap = devm_regmap_init_spi(spi, &sx1301_regmap_config);
 	if (IS_ERR(priv->regmap)) {
Signature · 1 line
2.7.4
#20 lore raw

PATCH 6/10 net: lora: sx1301: simplify firmware loading and convert

In reply to Ben Whitten

From: Ben Whitten <[email protected]>

As part of changing our internal pointers to our priv data we add the spi_device temporarily for downstream burst read/writes. Just pass firmware pointer to the load and remove redundant size checks, as both MCUs have the same program size add a define.

Use devm_kzalloc for automatic cleanup on failure to load firmware, manually cleaup this memory on success.

Convert firmware loading to regmap functions.

---
 drivers/net/lora/sx1301.c | 109 ++++++++++++++++------------------------------
 1 file changed, 37 insertions(+), 72 deletions(-)
drivers/net/lora/sx1301.c +37 -72
index 249551b..4cec524 100644
@@ -284,6 +284,7 @@ struct spi_sx1301 {
 
 struct sx1301_priv {
 	struct device *dev;
+	struct spi_device *spi;
 	struct lora_priv lora;
 	struct gpio_desc *rst_gpio;
 	u8 cur_page;
@@ -515,93 +516,73 @@ static int sx1301_arb_ram_read(struct sx1301_priv *priv, u8 addr, u8 *val)
 	return 0;
 }
 
-static int sx1301_load_firmware(struct spi_device *spi, int mcu, const u8 *data, size_t len)
+static int sx1301_load_firmware(struct sx1301_priv *priv, int mcu, const struct firmware *fw)
 {
 	u8 *buf;
-	u8 val, rst, select_mux;
 	int ret;
+	unsigned int read;
+	enum sx1301_fields rst, select_mux;
 
-	if (len > 8192)
+	if (fw->size > SX1301_MCU_FW_BYTE)
 		return -EINVAL;
 
 	switch (mcu) {
 	case 0:
-		rst = REG_0_MCU_RST_0;
-		select_mux = REG_0_MCU_SELECT_MUX_0;
+		rst = F_MCU_RST_0;
+		select_mux = F_MCU_SELECT_MUX_0;
 		break;
 	case 1:
-		rst = REG_0_MCU_RST_1;
-		select_mux = REG_0_MCU_SELECT_MUX_1;
+		rst = F_MCU_RST_1;
+		select_mux = F_MCU_SELECT_MUX_1;
 		break;
 	default:
 		return -EINVAL;
 	}
 
-	ret = sx1301_page_read(spi, 0, REG_0_MCU, &val);
-	if (ret) {
-		dev_err(&spi->dev, "MCU read failed\n");
+	ret = sx1301_field_write(priv, rst, 1);
+	if (ret)
 		return ret;
-	}
-
-	val |= rst;
-	val &= ~select_mux;
-
-	ret = sx1301_page_write(spi, 0, REG_0_MCU, val);
-	if (ret) {
-		dev_err(&spi->dev, "MCU reset / select mux write failed\n");
+	ret = sx1301_field_write(priv, select_mux, 0);
+	if (ret)
 		return ret;
-	}
 
-	ret = sx1301_write(spi, REG_MCU_PROM_ADDR, 0);
-	if (ret) {
-		dev_err(&spi->dev, "MCU prom addr write failed\n");
+	/* Load firmware into the data register */
+	ret = regmap_write(priv->regmap, SX1301_MPA, 0);
+	if (ret)
 		return ret;
-	}
 
-	ret = sx1301_write_burst(spi, REG_MCU_PROM_DATA, data, len);
+	ret = sx1301_write_burst(priv->spi, REG_MCU_PROM_DATA, fw->data, fw->size);
 	if (ret) {
-		dev_err(&spi->dev, "MCU prom data write failed\n");
+		dev_err(priv->dev, "MCU prom data write failed\n");
 		return ret;
 	}
 
-	ret = sx1301_read(spi, REG_MCU_PROM_DATA, &val);
+	ret = regmap_read(priv->regmap, SX1301_MPD, &read);
 	if (ret) {
-		dev_err(&spi->dev, "MCU prom data dummy read failed\n");
+		dev_err(priv->dev, "MCU prom data dummy read failed\n");
 		return ret;
 	}
 
-	buf = kzalloc(len, GFP_KERNEL);
+	buf = devm_kzalloc(priv->dev, fw->size, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
-	ret = sx1301_read_burst(spi, REG_MCU_PROM_DATA, buf, len);
+	ret = sx1301_read_burst(priv->spi, REG_MCU_PROM_DATA, buf, fw->size);
 	if (ret) {
-		dev_err(&spi->dev, "MCU prom data read failed\n");
-		kfree(buf);
+		dev_err(priv->dev, "MCU prom data read failed\n");
 		return ret;
 	}
 
-	if (memcmp(data, buf, len)) {
-		dev_err(&spi->dev, "MCU prom data read does not match data written\n");
-		kfree(buf);
+	if (memcmp(fw->data, buf, fw->size)) {
+		dev_err(priv->dev, "MCU prom data read does not match data written\n");
 		return -ENXIO;
 	}
 
-	kfree(buf);
+	devm_kfree(priv->dev, buf);
 
-	ret = sx1301_page_read(spi, 0, REG_0_MCU, &val);
-	if (ret) {
-		dev_err(&spi->dev, "MCU read (1) failed\n");
-		return ret;
-	}
-
-	val |= select_mux;
-
-	ret = sx1301_page_write(spi, 0, REG_0_MCU, val);
-	if (ret) {
-		dev_err(&spi->dev, "MCU reset / select mux write (1) failed\n");
+	ret = sx1301_field_write(priv, select_mux, 1);
+	if (ret)
 		return ret;
-	}
 
 	return 0;
 }
@@ -613,18 +594,13 @@ static int sx1301_agc_calibrate(struct spi_device *spi)
 	u8 val;
 	int ret;
 
-	ret = request_firmware(&fw, "sx1301_agc_calibration.bin", &spi->dev);
+	ret = request_firmware(&fw, "sx1301_agc_calibration.bin", priv->dev);
 	if (ret) {
 		dev_err(&spi->dev, "agc cal firmware file load failed\n");
 		return ret;
 	}
 
-	if (fw->size != 8192) {
-		dev_err(&spi->dev, "unexpected agc cal firmware size\n");
-		return -EINVAL;
-	}
-
-	ret = sx1301_load_firmware(spi, 1, fw->data, fw->size);
+	ret = sx1301_load_firmware(priv, 1, fw);
 	release_firmware(fw);
 	if (ret) {
 		dev_err(&spi->dev, "agc cal firmware load failed\n");
@@ -741,36 +717,24 @@ static int sx1301_load_all_firmware(struct spi_device *spi)
 	u8 val;
 	int ret;
 
-	ret = request_firmware(&fw, "sx1301_arb.bin", &spi->dev);
+	ret = request_firmware(&fw, "sx1301_arb.bin", priv->dev);
 	if (ret) {
-		dev_err(&spi->dev, "arb firmware file load failed\n");
+		dev_err(priv->dev, "arb firmware file load failed\n");
 		return ret;
 	}
 
-	if (fw->size != 8192) {
-		dev_err(&spi->dev, "unexpected arb firmware size\n");
-		release_firmware(fw);
-		return -EINVAL;
-	}
-
-	ret = sx1301_load_firmware(spi, 0, fw->data, fw->size);
+	ret = sx1301_load_firmware(priv, 0, fw);
 	release_firmware(fw);
 	if (ret)
 		return ret;
 
-	ret = request_firmware(&fw, "sx1301_agc.bin", &spi->dev);
+	ret = request_firmware(&fw, "sx1301_agc.bin", priv->dev);
 	if (ret) {
-		dev_err(&spi->dev, "agc firmware file load failed\n");
+		dev_err(priv->dev, "agc firmware file load failed\n");
 		return ret;
 	}
 
-	if (fw->size != 8192) {
-		dev_err(&spi->dev, "unexpected agc firmware size\n");
-		release_firmware(fw);
-		return -EINVAL;
-	}
-
-	ret = sx1301_load_firmware(spi, 1, fw->data, fw->size);
+	ret = sx1301_load_firmware(priv, 1, fw);
 	release_firmware(fw);
 	if (ret)
 		return ret;
@@ -886,6 +850,7 @@ static int sx1301_probe(struct spi_device *spi)
 	spi_set_drvdata(spi, netdev);
 	SET_NETDEV_DEV(netdev, &spi->dev);
 	priv->dev = &spi->dev;
+	priv->spi = spi;
 
 	priv->regmap = devm_regmap_init_spi(spi, &sx1301_regmap_config);
 	if (IS_ERR(priv->regmap)) {
Signature · 1 line
2.7.4
#21 lore raw

PATCH 7/10 net: lora: sx1301: convert read and write burst to take priv data

In reply to Ben Whitten

From: Ben Whitten <[email protected]>

As part of standardising on passing our priv data around we convert read and write burst to take the priv data, there is a small compat step needed in the old _read and _write functions and will be removed in the next step.

---
 drivers/net/lora/sx1301.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/net/lora/sx1301.c +10 -8
index 4cec524..ab45b5b 100644
@@ -312,18 +312,19 @@ static int sx1301_field_write(struct sx1301_priv *priv,
 	return regmap_field_write(priv->regmap_fields[field_id], val);
 }
 
-static int sx1301_read_burst(struct spi_device *spi, u8 reg, u8 *val, size_t len)
+static int sx1301_read_burst(struct sx1301_priv *priv, u8 reg, u8 *val, size_t len)
 {
 	u8 addr = reg & 0x7f;
-	return spi_write_then_read(spi, &addr, 1, val, len);
+	return spi_write_then_read(priv->spi, &addr, 1, val, len);
 }
 
 static int sx1301_read(struct spi_device *spi, u8 reg, u8 *val)
 {
-	return sx1301_read_burst(spi, reg, val, 1);
+	struct sx1301_priv *priv = spi_get_drvdata(spi);
+	return sx1301_read_burst(priv, reg, val, 1);
 }
 
-static int sx1301_write_burst(struct spi_device *spi, u8 reg, const u8 *val, size_t len)
+static int sx1301_write_burst(struct sx1301_priv *priv, u8 reg, const u8 *val, size_t len)
 {
 	u8 addr = reg | BIT(7);
 	struct spi_transfer xfr[2] = {
@@ -331,12 +332,13 @@ static int sx1301_write_burst(struct spi_device *spi, u8 reg, const u8 *val, siz
 		{ .tx_buf = val, .len = len },
 	};
 
-	return spi_sync_transfer(spi, xfr, 2);
+	return spi_sync_transfer(priv->spi, xfr, 2);
 }
 
 static int sx1301_write(struct spi_device *spi, u8 reg, u8 val)
 {
-	return sx1301_write_burst(spi, reg, &val, 1);
+	struct sx1301_priv *priv = spi_get_drvdata(spi);
+	return sx1301_write_burst(priv, reg, &val, 1);
 }
 
 static int sx1301_page_switch(struct spi_device *spi, u8 page)
@@ -551,7 +553,7 @@ static int sx1301_load_firmware(struct sx1301_priv *priv, int mcu, const struct
 	if (ret)
 		return ret;
 
-	ret = sx1301_write_burst(priv->spi, REG_MCU_PROM_DATA, fw->data, fw->size);
+	ret = sx1301_write_burst(priv, REG_MCU_PROM_DATA, fw->data, fw->size);
 	if (ret) {
 		dev_err(priv->dev, "MCU prom data write failed\n");
 		return ret;
@@ -567,7 +569,7 @@ static int sx1301_load_firmware(struct sx1301_priv *priv, int mcu, const struct
 	if (!buf)
 		return -ENOMEM;
 
-	ret = sx1301_read_burst(priv->spi, REG_MCU_PROM_DATA, buf, fw->size);
+	ret = sx1301_read_burst(priv, REG_MCU_PROM_DATA, buf, fw->size);
 	if (ret) {
 		dev_err(priv->dev, "MCU prom data read failed\n");
 		return ret;
Signature · 1 line
2.7.4
#22 lore raw

PATCH 8/10 net: lora: sx1301: convert read and write to priv pointer

In reply to Ben Whitten

From: Ben Whitten <[email protected]>

Convert the next layer of read and writes to our private pointer, once again compatiblity later in the page read and write layer. The page functions will be removed with this full stack as page switches are handled by regmap automatically.

---
 drivers/net/lora/sx1301.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/net/lora/sx1301.c +12 -12
index ab45b5b..f84a6ce 100644
@@ -318,9 +318,8 @@ static int sx1301_read_burst(struct sx1301_priv *priv, u8 reg, u8 *val, size_t l
 	return spi_write_then_read(priv->spi, &addr, 1, val, len);
 }
 
-static int sx1301_read(struct spi_device *spi, u8 reg, u8 *val)
+static int sx1301_read(struct sx1301_priv *priv, u8 reg, u8 *val)
 {
-	struct sx1301_priv *priv = spi_get_drvdata(spi);
 	return sx1301_read_burst(priv, reg, val, 1);
 }
 
@@ -335,9 +334,8 @@ static int sx1301_write_burst(struct sx1301_priv *priv, u8 reg, const u8 *val, s
 	return spi_sync_transfer(priv->spi, xfr, 2);
 }
 
-static int sx1301_write(struct spi_device *spi, u8 reg, u8 val)
+static int sx1301_write(struct sx1301_priv *priv, u8 reg, u8 val)
 {
-	struct sx1301_priv *priv = spi_get_drvdata(spi);
 	return sx1301_write_burst(priv, reg, &val, 1);
 }
 
@@ -350,7 +348,7 @@ static int sx1301_page_switch(struct spi_device *spi, u8 page)
 		return 0;
 
 	dev_dbg(&spi->dev, "switching to page %u\n", (unsigned)page);
-	ret = sx1301_write(spi, REG_PAGE_RESET, page & 0x3);
+	ret = sx1301_write(priv, REG_PAGE_RESET, page & 0x3);
 	if (ret) {
 		dev_err(&spi->dev, "switching to page %u failed\n", (unsigned)page);
 		return ret;
@@ -364,23 +362,25 @@ static int sx1301_page_switch(struct spi_device *spi, u8 page)
 static int sx1301_page_read(struct spi_device *spi, u8 page, u8 reg, u8 *val)
 {
 	int ret;
+	struct sx1301_priv *priv = spi_get_drvdata(spi);
 
 	ret = sx1301_page_switch(spi, page);
 	if (ret)
 		return ret;
 
-	return sx1301_read(spi, reg, val);
+	return sx1301_read(priv, reg, val);
 }
 
 static int sx1301_page_write(struct spi_device *spi, u8 page, u8 reg, u8 val)
 {
 	int ret;
+	struct sx1301_priv *priv = spi_get_drvdata(spi);
 
 	ret = sx1301_page_switch(spi, page);
 	if (ret)
 		return ret;
 
-	return sx1301_write(spi, reg, val);
+	return sx1301_write(priv, reg, val);
 }
 
 #define REG_RADIO_X_DATA		0
@@ -666,7 +666,7 @@ static int sx1301_agc_calibrate(struct spi_device *spi)
 		return ret;
 	}
 
-	ret = sx1301_read(spi, REG_EMERGENCY_FORCE, &val);
+	ret = sx1301_read(priv, REG_EMERGENCY_FORCE, &val);
 	if (ret) {
 		dev_err(&spi->dev, "emergency force read failed\n");
 		return ret;
@@ -674,7 +674,7 @@ static int sx1301_agc_calibrate(struct spi_device *spi)
 
 	val &= ~REG_EMERGENCY_FORCE_HOST_CTRL;
 
-	ret = sx1301_write(spi, REG_EMERGENCY_FORCE, val);
+	ret = sx1301_write(priv, REG_EMERGENCY_FORCE, val);
 	if (ret) {
 		dev_err(&spi->dev, "emergency force write failed\n");
 		return ret;
@@ -683,7 +683,7 @@ static int sx1301_agc_calibrate(struct spi_device *spi)
 	dev_err(&spi->dev, "starting calibration...\n");
 	msleep(2300);
 
-	ret = sx1301_read(spi, REG_EMERGENCY_FORCE, &val);
+	ret = sx1301_read(priv, REG_EMERGENCY_FORCE, &val);
 	if (ret) {
 		dev_err(&spi->dev, "emergency force read (1) failed\n");
 		return ret;
@@ -691,13 +691,13 @@ static int sx1301_agc_calibrate(struct spi_device *spi)
 
 	val |= REG_EMERGENCY_FORCE_HOST_CTRL;
 
-	ret = sx1301_write(spi, REG_EMERGENCY_FORCE, val);
+	ret = sx1301_write(priv, REG_EMERGENCY_FORCE, val);
 	if (ret) {
 		dev_err(&spi->dev, "emergency force write (1) failed\n");
 		return ret;
 	}
 
-	ret = sx1301_read(spi, REG_MCU_AGC_STATUS, &val);
+	ret = sx1301_read(priv, REG_MCU_AGC_STATUS, &val);
 	if (ret) {
 		dev_err(&spi->dev, "AGC status read failed\n");
 		return ret;
Signature · 1 line
2.7.4
#23 lore raw

PATCH 9/10 net: lora: sx1301: convert agc calibrate to regmap functions

In reply to Ben Whitten

From: Ben Whitten <[email protected]>

We convert the AGC calibration function over to regmap and take the opportunity to make the expected firmware versions defines. Switch to taking a pointer to our private data.

---
 drivers/net/lora/sx1301.c | 101 ++++++++++++----------------------------------
 1 file changed, 26 insertions(+), 75 deletions(-)
drivers/net/lora/sx1301.c +26 -75
index f84a6ce..21b3f9a 100644
@@ -589,123 +589,74 @@ static int sx1301_load_firmware(struct sx1301_priv *priv, int mcu, const struct
 	return 0;
 }
 
-static int sx1301_agc_calibrate(struct spi_device *spi)
+static int sx1301_agc_calibrate(struct sx1301_priv *priv)
 {
 	const struct firmware *fw;
-	struct sx1301_priv *priv = spi_get_drvdata(spi);
 	u8 val;
 	int ret;
+	unsigned int cal;
 
 	ret = request_firmware(&fw, "sx1301_agc_calibration.bin", priv->dev);
 	if (ret) {
-		dev_err(&spi->dev, "agc cal firmware file load failed\n");
+		dev_err(priv->dev, "AGC CAL firmware file load failed\n");
 		return ret;
 	}
 
 	ret = sx1301_load_firmware(priv, 1, fw);
 	release_firmware(fw);
 	if (ret) {
-		dev_err(&spi->dev, "agc cal firmware load failed\n");
-		return ret;
-	}
-
-	ret = sx1301_page_read(spi, 0, 105, &val);
-	if (ret) {
-		dev_err(&spi->dev, "0|105 read failed\n");
+		dev_err(priv->dev, "AGC CAL firmware load failed\n");
 		return ret;
 	}
 
-	val &= ~REG_0_105_FORCE_HOST_RADIO_CTRL;
-
-	ret = sx1301_page_write(spi, 0, 105, val);
-	if (ret) {
-		dev_err(&spi->dev, "0|105 write failed\n");
+	ret = sx1301_field_write(priv, F_FORCE_HOST_RADIO_CTRL, 0);
+	if (ret)
 		return ret;
-	}
 
 	val = BIT(4); /* with DAC gain=3 */
 	if (false)
 		val |= BIT(5); /* SX1255 */
 
-	ret = sx1301_page_write(spi, 0, REG_0_RADIO_SELECT, val);
-	if (ret) {
-		dev_err(&spi->dev, "radio select write failed\n");
-		return ret;
-	}
-
-	ret = sx1301_page_read(spi, 0, REG_0_MCU, &val);
-	if (ret) {
-		dev_err(&spi->dev, "MCU read (0) failed\n");
+	ret = regmap_write(priv->regmap, SX1301_CHRS, val);
+	if (ret)
 		return ret;
-	}
-
-	val &= ~REG_0_MCU_RST_1;
 
-	ret = sx1301_page_write(spi, 0, REG_0_MCU, val);
-	if (ret) {
-		dev_err(&spi->dev, "MCU write (0) failed\n");
+	ret = sx1301_field_write(priv, F_MCU_RST_1, 0);
+	if (ret)
 		return ret;
-	}
 
 	ret = sx1301_agc_ram_read(priv, 0x20, &val);
 	if (ret) {
-		dev_err(&spi->dev, "AGC RAM data read failed\n");
+		dev_err(priv->dev, "AGC RAM data read failed\n");
 		return ret;
 	}
 
-	dev_info(&spi->dev, "AGC calibration firmware version %u\n", (unsigned)val);
+	dev_info(priv->dev, "AGC calibration firmware version %u\n", (unsigned)val);
 
-	if (val != 2) {
-		dev_err(&spi->dev, "unexpected firmware version, expecting %u\n", 2);
+	if (val != SX1301_MCU_AGC_CAL_FW_VERSION) {
+		dev_err(priv->dev, "unexpected firmware version, expecting %u\n",
+				SX1301_MCU_AGC_CAL_FW_VERSION);
 		return -ENXIO;
 	}
 
-	ret = sx1301_page_switch(spi, 3);
-	if (ret) {
-		dev_err(&spi->dev, "page switch 3 failed\n");
-		return ret;
-	}
-
-	ret = sx1301_read(priv, REG_EMERGENCY_FORCE, &val);
-	if (ret) {
-		dev_err(&spi->dev, "emergency force read failed\n");
-		return ret;
-	}
-
-	val &= ~REG_EMERGENCY_FORCE_HOST_CTRL;
-
-	ret = sx1301_write(priv, REG_EMERGENCY_FORCE, val);
-	if (ret) {
-		dev_err(&spi->dev, "emergency force write failed\n");
+	ret = sx1301_field_write(priv, F_EMERGENCY_FORCE_HOST_CTRL, 0);
+	if (ret)
 		return ret;
-	}
 
-	dev_err(&spi->dev, "starting calibration...\n");
+	dev_err(priv->dev, "starting calibration...\n");
 	msleep(2300);
 
-	ret = sx1301_read(priv, REG_EMERGENCY_FORCE, &val);
-	if (ret) {
-		dev_err(&spi->dev, "emergency force read (1) failed\n");
-		return ret;
-	}
-
-	val |= REG_EMERGENCY_FORCE_HOST_CTRL;
-
-	ret = sx1301_write(priv, REG_EMERGENCY_FORCE, val);
-	if (ret) {
-		dev_err(&spi->dev, "emergency force write (1) failed\n");
+	ret = sx1301_field_write(priv, F_EMERGENCY_FORCE_HOST_CTRL, 1);
+	if (ret)
 		return ret;
-	}
 
-	ret = sx1301_read(priv, REG_MCU_AGC_STATUS, &val);
-	if (ret) {
-		dev_err(&spi->dev, "AGC status read failed\n");
+	ret = regmap_read(priv->regmap, SX1301_AGCSTS, &cal);
+	if (ret)
 		return ret;
-	}
 
-	dev_info(&spi->dev, "AGC status: %02x\n", (unsigned)val);
-	if ((val & (BIT(7) | BIT(0))) != (BIT(7) | BIT(0))) {
-		dev_err(&spi->dev, "AGC calibration failed\n");
+	dev_info(priv->dev, "AGC status: %02x\n", cal);
+	if ((cal & (BIT(7) | BIT(0))) != (BIT(7) | BIT(0))) {
+		dev_err(priv->dev, "AGC calibration failed\n");
 		return -ENXIO;
 	}
 
@@ -985,7 +936,7 @@ static int sx1301_probe(struct spi_device *spi)
 
 	/* calibration */
 
-	ret = sx1301_agc_calibrate(spi);
+	ret = sx1301_agc_calibrate(priv);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.7.4
#24 lore raw

PATCH 10/10 net: lora: sx1301: convert all firmware to regmap

In reply to Ben Whitten

The last stage in the probe function loads the final firmwares to the sx1301.

---
 drivers/net/lora/sx1301.c | 67 ++++++++++++++++++++---------------------------
 1 file changed, 28 insertions(+), 39 deletions(-)
drivers/net/lora/sx1301.c +28 -39
index 21b3f9a..7f4bfa1 100644
@@ -663,16 +663,15 @@ static int sx1301_agc_calibrate(struct sx1301_priv *priv)
 	return 0;
 }
 
-static int sx1301_load_all_firmware(struct spi_device *spi)
+static int sx1301_load_all_firmware(struct sx1301_priv *priv)
 {
-	struct sx1301_priv *priv = spi_get_drvdata(spi);
 	const struct firmware *fw;
 	u8 val;
 	int ret;
 
 	ret = request_firmware(&fw, "sx1301_arb.bin", priv->dev);
 	if (ret) {
-		dev_err(priv->dev, "arb firmware file load failed\n");
+		dev_err(priv->dev, "ARB firmware file load failed\n");
 		return ret;
 	}
 
@@ -683,7 +682,7 @@ static int sx1301_load_all_firmware(struct spi_device *spi)
 
 	ret = request_firmware(&fw, "sx1301_agc.bin", priv->dev);
 	if (ret) {
-		dev_err(priv->dev, "agc firmware file load failed\n");
+		dev_err(priv->dev, "AGC firmware file load failed\n");
 		return ret;
 	}
 
@@ -692,63 +691,53 @@ static int sx1301_load_all_firmware(struct spi_device *spi)
 	if (ret)
 		return ret;
 
-	ret = sx1301_page_read(spi, 0, 105, &val);
-	if (ret) {
-		dev_err(&spi->dev, "0|105 read failed\n");
+	ret = sx1301_field_write(priv, F_FORCE_HOST_RADIO_CTRL, 0);
+	if (ret)
 		return ret;
-	}
-
-	val &= ~(REG_0_105_FORCE_HOST_RADIO_CTRL | REG_0_105_FORCE_HOST_FE_CTRL | REG_0_105_FORCE_DEC_FILTER_GAIN);
-
-	ret = sx1301_page_write(spi, 0, 105, val);
-	if (ret) {
-		dev_err(&spi->dev, "0|105 write failed\n");
+	ret = sx1301_field_write(priv, F_FORCE_HOST_FE_CTRL, 0);
+	if (ret)
 		return ret;
-	}
-
-	ret = sx1301_page_write(spi, 0, REG_0_RADIO_SELECT, 0);
-	if (ret) {
-		dev_err(&spi->dev, "radio select write failed\n");
+	ret = sx1301_field_write(priv, F_FORCE_DEC_FILTER_GAIN, 0);
+	if (ret)
 		return ret;
-	}
 
-	ret = sx1301_page_read(spi, 0, REG_0_MCU, &val);
-	if (ret) {
-		dev_err(&spi->dev, "MCU read (0) failed\n");
+	ret = regmap_write(priv->regmap, SX1301_CHRS, 0);
+	if (ret)
 		return ret;
-	}
-
-	val &= ~(REG_0_MCU_RST_1 | REG_0_MCU_RST_0);
 
-	ret = sx1301_page_write(spi, 0, REG_0_MCU, val);
-	if (ret) {
-		dev_err(&spi->dev, "MCU write (0) failed\n");
+	/* Release the CPUs */
+	ret = sx1301_field_write(priv, F_MCU_RST_0, 0);
+	if (ret)
+		return ret;
+	ret = sx1301_field_write(priv, F_MCU_RST_1, 0);
+	if (ret)
 		return ret;
-	}
 
 	ret = sx1301_agc_ram_read(priv, 0x20, &val);
 	if (ret) {
-		dev_err(&spi->dev, "AGC RAM data read failed\n");
+		dev_err(priv->dev, "AGC RAM data read failed\n");
 		return ret;
 	}
 
-	dev_info(&spi->dev, "AGC firmware version %u\n", (unsigned)val);
+	dev_info(priv->dev, "AGC firmware version %u\n", (unsigned)val);
 
-	if (val != 4) {
-		dev_err(&spi->dev, "unexpected firmware version, expecting %u\n", 4);
+	if (val != SX1301_MCU_AGC_FW_VERSION) {
+		dev_err(priv->dev, "unexpected firmware version, expecting %u\n",
+				SX1301_MCU_AGC_FW_VERSION);
 		return -ENXIO;
 	}
 
 	ret = sx1301_arb_ram_read(priv, 0x20, &val);
 	if (ret) {
-		dev_err(&spi->dev, "ARB RAM data read failed\n");
+		dev_err(priv->dev, "ARB RAM data read failed\n");
 		return ret;
 	}
 
-	dev_info(&spi->dev, "ARB firmware version %u\n", (unsigned)val);
+	dev_info(priv->dev, "ARB firmware version %u\n", (unsigned)val);
 
-	if (val != 1) {
-		dev_err(&spi->dev, "unexpected firmware version, expecting %u\n", 1);
+	if (val != SX1301_MCU_ARB_FW_VERSION) {
+		dev_err(priv->dev, "unexpected firmware version, expecting %u\n",
+				SX1301_MCU_ARB_FW_VERSION);
 		return -ENXIO;
 	}
 
@@ -942,7 +931,7 @@ static int sx1301_probe(struct spi_device *spi)
 
 	/* TODO */
 
-	ret = sx1301_load_all_firmware(spi);
+	ret = sx1301_load_all_firmware(priv);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.7.4