2019-12-11 14:22:10

by Lorenzo Bianconi

[permalink] [raw]
Subject: [PATCH] mt76: mt7615: introduce LED support

Initialize brightness_set and blink_set callbacks to
mt7615_led_set_brightness and mt7615_led_set_blink in order to enable
LED support in mt7615 driver

Tested-by: Deng Qingfang <[email protected]>
Signed-off-by: Lorenzo Bianconi <[email protected]>
---
.../net/wireless/mediatek/mt76/mt7615/init.c | 58 +++++++++++++++++++
.../net/wireless/mediatek/mt76/mt7615/regs.h | 27 +++++++++
2 files changed, 85 insertions(+)

diff --git a/drivers/net/wireless/mediatek/mt76/mt7615/init.c b/drivers/net/wireless/mediatek/mt76/mt7615/init.c
index eb7c6b9e3d4d..c25ba5fc3cdc 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7615/init.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7615/init.c
@@ -196,6 +196,58 @@ static const struct ieee80211_iface_combination if_comb[] = {
}
};

+static void
+mt7615_led_set_config(struct led_classdev *led_cdev,
+ u8 delay_on, u8 delay_off)
+{
+ struct mt7615_dev *dev;
+ struct mt76_dev *mt76;
+ u32 val, addr;
+
+ mt76 = container_of(led_cdev, struct mt76_dev, led_cdev);
+ dev = container_of(mt76, struct mt7615_dev, mt76);
+ val = MT_LED_STATUS_DURATION(0xffff) |
+ MT_LED_STATUS_OFF(delay_off) |
+ MT_LED_STATUS_ON(delay_on);
+
+ addr = mt7615_reg_map(dev, MT_LED_STATUS_0(mt76->led_pin));
+ mt76_wr(dev, addr, val);
+ addr = mt7615_reg_map(dev, MT_LED_STATUS_1(mt76->led_pin));
+ mt76_wr(dev, addr, val);
+
+ val = MT_LED_CTRL_REPLAY(mt76->led_pin) |
+ MT_LED_CTRL_KICK(mt76->led_pin);
+ if (mt76->led_al)
+ val |= MT_LED_CTRL_POLARITY(mt76->led_pin);
+ addr = mt7615_reg_map(dev, MT_LED_CTRL);
+ mt76_wr(dev, addr, val);
+}
+
+static int
+mt7615_led_set_blink(struct led_classdev *led_cdev,
+ unsigned long *delay_on,
+ unsigned long *delay_off)
+{
+ u8 delta_on, delta_off;
+
+ delta_off = max_t(u8, *delay_off / 10, 1);
+ delta_on = max_t(u8, *delay_on / 10, 1);
+
+ mt7615_led_set_config(led_cdev, delta_on, delta_off);
+
+ return 0;
+}
+
+static void
+mt7615_led_set_brightness(struct led_classdev *led_cdev,
+ enum led_brightness brightness)
+{
+ if (!brightness)
+ mt7615_led_set_config(led_cdev, 0, 0xff);
+ else
+ mt7615_led_set_config(led_cdev, 0xff, 0);
+}
+
static void
mt7615_init_txpower(struct mt7615_dev *dev,
struct ieee80211_supported_band *sband)
@@ -383,6 +435,12 @@ int mt7615_register_device(struct mt7615_dev *dev)
mt7615_cap_dbdc_disable(dev);
dev->phy.dfs_state = -1;

+ /* init led callbacks */
+ if (IS_ENABLED(CONFIG_MT76_LEDS)) {
+ dev->mt76.led_cdev.brightness_set = mt7615_led_set_brightness;
+ dev->mt76.led_cdev.blink_set = mt7615_led_set_blink;
+ }
+
ret = mt76_register_device(&dev->mt76, true, mt7615_rates,
ARRAY_SIZE(mt7615_rates));
if (ret)
diff --git a/drivers/net/wireless/mediatek/mt76/mt7615/regs.h b/drivers/net/wireless/mediatek/mt76/mt7615/regs.h
index 26d121646787..3234ae0761b8 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7615/regs.h
+++ b/drivers/net/wireless/mediatek/mt76/mt7615/regs.h
@@ -345,6 +345,33 @@

#define MT_TX_AGG_CNT(n) MT_WF_MIB(0xa8 + ((n) << 2))

+#define MT_LED_BASE_PHYS 0x80024000
+#define MT_LED_PHYS(_n) (MT_LED_BASE_PHYS + (_n))
+
+#define MT_LED_CTRL MT_LED_PHYS(0x00)
+
+#define MT_LED_CTRL_REPLAY(_n) BIT(0 + (8 * (_n)))
+#define MT_LED_CTRL_POLARITY(_n) BIT(1 + (8 * (_n)))
+#define MT_LED_CTRL_TX_BLINK_MODE(_n) BIT(2 + (8 * (_n)))
+#define MT_LED_CTRL_TX_MANUAL_BLINK(_n) BIT(3 + (8 * (_n)))
+#define MT_LED_CTRL_TX_OVER_BLINK(_n) BIT(5 + (8 * (_n)))
+#define MT_LED_CTRL_KICK(_n) BIT(7 + (8 * (_n)))
+
+#define MT_LED_STATUS_0(_n) MT_LED_PHYS(0x10 + ((_n) * 8))
+#define MT_LED_STATUS_1(_n) MT_LED_PHYS(0x14 + ((_n) * 8))
+#define MT_LED_STATUS_OFF_MASK GENMASK(31, 24)
+#define MT_LED_STATUS_OFF(_v) (((_v) << \
+ __ffs(MT_LED_STATUS_OFF_MASK)) & \
+ MT_LED_STATUS_OFF_MASK)
+#define MT_LED_STATUS_ON_MASK GENMASK(23, 16)
+#define MT_LED_STATUS_ON(_v) (((_v) << \
+ __ffs(MT_LED_STATUS_ON_MASK)) & \
+ MT_LED_STATUS_ON_MASK)
+#define MT_LED_STATUS_DURATION_MASK GENMASK(15, 0)
+#define MT_LED_STATUS_DURATION(_v) (((_v) << \
+ __ffs(MT_LED_STATUS_DURATION_MASK)) &\
+ MT_LED_STATUS_DURATION_MASK)
+
#define MT_EFUSE_BASE 0x81070000
#define MT_EFUSE_BASE_CTRL 0x000
#define MT_EFUSE_BASE_CTRL_EMPTY BIT(30)
--
2.21.0


2019-12-13 21:53:11

by Felix Fietkau

[permalink] [raw]
Subject: Re: [PATCH] mt76: mt7615: introduce LED support

On 2019-12-11 15:21, Lorenzo Bianconi wrote:
> Initialize brightness_set and blink_set callbacks to
> mt7615_led_set_brightness and mt7615_led_set_blink in order to enable
> LED support in mt7615 driver
>
> Tested-by: Deng Qingfang <[email protected]>
> Signed-off-by: Lorenzo Bianconi <[email protected]>
> ---
> .../net/wireless/mediatek/mt76/mt7615/init.c | 58 +++++++++++++++++++
> .../net/wireless/mediatek/mt76/mt7615/regs.h | 27 +++++++++
> 2 files changed, 85 insertions(+)
>
> diff --git a/drivers/net/wireless/mediatek/mt76/mt7615/init.c b/drivers/net/wireless/mediatek/mt76/mt7615/init.c
> index eb7c6b9e3d4d..c25ba5fc3cdc 100644
> --- a/drivers/net/wireless/mediatek/mt76/mt7615/init.c
> +++ b/drivers/net/wireless/mediatek/mt76/mt7615/init.c
> @@ -196,6 +196,58 @@ static const struct ieee80211_iface_combination if_comb[] = {
> }
> };
>
> +static void
> +mt7615_led_set_config(struct led_classdev *led_cdev,
> + u8 delay_on, u8 delay_off)
> +{
> + struct mt7615_dev *dev;
> + struct mt76_dev *mt76;
> + u32 val, addr;
> +
> + mt76 = container_of(led_cdev, struct mt76_dev, led_cdev);
> + dev = container_of(mt76, struct mt7615_dev, mt76);
> + val = MT_LED_STATUS_DURATION(0xffff) |
> + MT_LED_STATUS_OFF(delay_off) |
> + MT_LED_STATUS_ON(delay_on);
> +
> + addr = mt7615_reg_map(dev, MT_LED_STATUS_0(mt76->led_pin));
> + mt76_wr(dev, addr, val);
> + addr = mt7615_reg_map(dev, MT_LED_STATUS_1(mt76->led_pin));
> + mt76_wr(dev, addr, val);
> +
> + val = MT_LED_CTRL_REPLAY(mt76->led_pin) |
> + MT_LED_CTRL_KICK(mt76->led_pin);
> + if (mt76->led_al)
> + val |= MT_LED_CTRL_POLARITY(mt76->led_pin);
> + addr = mt7615_reg_map(dev, MT_LED_CTRL);
> + mt76_wr(dev, addr, val);
> +}
> +
> [...]
> diff --git a/drivers/net/wireless/mediatek/mt76/mt7615/regs.h b/drivers/net/wireless/mediatek/mt76/mt7615/regs.h
> index 26d121646787..3234ae0761b8 100644
> --- a/drivers/net/wireless/mediatek/mt76/mt7615/regs.h
> +++ b/drivers/net/wireless/mediatek/mt76/mt7615/regs.h
> @@ -345,6 +345,33 @@
>
> #define MT_TX_AGG_CNT(n) MT_WF_MIB(0xa8 + ((n) << 2))
>
> +#define MT_LED_BASE_PHYS 0x80024000
> +#define MT_LED_PHYS(_n) (MT_LED_BASE_PHYS + (_n))
> +
> +#define MT_LED_CTRL MT_LED_PHYS(0x00)
> +
> +#define MT_LED_CTRL_REPLAY(_n) BIT(0 + (8 * (_n)))
> +#define MT_LED_CTRL_POLARITY(_n) BIT(1 + (8 * (_n)))
> +#define MT_LED_CTRL_TX_BLINK_MODE(_n) BIT(2 + (8 * (_n)))
> +#define MT_LED_CTRL_TX_MANUAL_BLINK(_n) BIT(3 + (8 * (_n)))
> +#define MT_LED_CTRL_TX_OVER_BLINK(_n) BIT(5 + (8 * (_n)))
> +#define MT_LED_CTRL_KICK(_n) BIT(7 + (8 * (_n)))
> +
> +#define MT_LED_STATUS_0(_n) MT_LED_PHYS(0x10 + ((_n) * 8))
> +#define MT_LED_STATUS_1(_n) MT_LED_PHYS(0x14 + ((_n) * 8))
> +#define MT_LED_STATUS_OFF_MASK GENMASK(31, 24)
> +#define MT_LED_STATUS_OFF(_v) (((_v) << \
> + __ffs(MT_LED_STATUS_OFF_MASK)) & \
> + MT_LED_STATUS_OFF_MASK)
> +#define MT_LED_STATUS_ON_MASK GENMASK(23, 16)
> +#define MT_LED_STATUS_ON(_v) (((_v) << \
> + __ffs(MT_LED_STATUS_ON_MASK)) & \
> + MT_LED_STATUS_ON_MASK)
> +#define MT_LED_STATUS_DURATION_MASK GENMASK(15, 0)
> +#define MT_LED_STATUS_DURATION(_v) (((_v) << \
> + __ffs(MT_LED_STATUS_DURATION_MASK)) &\
> + MT_LED_STATUS_DURATION_MASK

Why are you using separate _MASK fields and macros using __ffs in
bitshift instead of FIELD_PREP like the rest of the code?

- Felix

2019-12-14 22:03:03

by Lorenzo Bianconi

[permalink] [raw]
Subject: Re: [PATCH] mt76: mt7615: introduce LED support

>
> On 2019-12-11 15:21, Lorenzo Bianconi wrote:
> > Initialize brightness_set and blink_set callbacks to
> > mt7615_led_set_brightness and mt7615_led_set_blink in order to enable
> > LED support in mt7615 driver
> >

[...]

> > #define MT_TX_AGG_CNT(n) MT_WF_MIB(0xa8 + ((n) << 2))
> >
> > +#define MT_LED_BASE_PHYS 0x80024000
> > +#define MT_LED_PHYS(_n) (MT_LED_BASE_PHYS + (_n))
> > +
> > +#define MT_LED_CTRL MT_LED_PHYS(0x00)
> > +
> > +#define MT_LED_CTRL_REPLAY(_n) BIT(0 + (8 * (_n)))
> > +#define MT_LED_CTRL_POLARITY(_n) BIT(1 + (8 * (_n)))
> > +#define MT_LED_CTRL_TX_BLINK_MODE(_n) BIT(2 + (8 * (_n)))
> > +#define MT_LED_CTRL_TX_MANUAL_BLINK(_n) BIT(3 + (8 * (_n)))
> > +#define MT_LED_CTRL_TX_OVER_BLINK(_n) BIT(5 + (8 * (_n)))
> > +#define MT_LED_CTRL_KICK(_n) BIT(7 + (8 * (_n)))
> > +
> > +#define MT_LED_STATUS_0(_n) MT_LED_PHYS(0x10 + ((_n) * 8))
> > +#define MT_LED_STATUS_1(_n) MT_LED_PHYS(0x14 + ((_n) * 8))
> > +#define MT_LED_STATUS_OFF_MASK GENMASK(31, 24)
> > +#define MT_LED_STATUS_OFF(_v) (((_v) << \
> > + __ffs(MT_LED_STATUS_OFF_MASK)) & \
> > + MT_LED_STATUS_OFF_MASK)
> > +#define MT_LED_STATUS_ON_MASK GENMASK(23, 16)
> > +#define MT_LED_STATUS_ON(_v) (((_v) << \
> > + __ffs(MT_LED_STATUS_ON_MASK)) & \
> > + MT_LED_STATUS_ON_MASK)
> > +#define MT_LED_STATUS_DURATION_MASK GENMASK(15, 0)
> > +#define MT_LED_STATUS_DURATION(_v) (((_v) << \
> > + __ffs(MT_LED_STATUS_DURATION_MASK)) &\
> > + MT_LED_STATUS_DURATION_MASK
>
> Why are you using separate _MASK fields and macros using __ffs in
> bitshift instead of FIELD_PREP like the rest of the code?
>

ack, I will fix it in v2.

Regards,
Lorenzo

> - Felix
>