bcm27xx: add support for linux v5.15
[openwrt/staging/wigyori.git] / target / linux / bcm27xx / patches-5.15 / 950-0485-regulator-rpi-panel-Add-GPIO-control-for-panel-and-t.patch
1 From 6486bb50b96f359844b9c34f0978c69bbdcda140 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Fri, 10 Sep 2021 13:50:28 +0100
4 Subject: [PATCH] regulator: rpi-panel: Add GPIO control for panel and
5 touch resets
6
7 We need independent control of the resets for the panel&bridge,
8 vs the touch controller.
9
10 Expose the reset lines that are on the Atmel's port C via the GPIO
11 API so that they can be controlled appropriately.
12
13 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
14 ---
15 .../regulator/rpi-panel-attiny-regulator.c | 115 +++++++++++++++---
16 1 file changed, 97 insertions(+), 18 deletions(-)
17
18 --- a/drivers/regulator/rpi-panel-attiny-regulator.c
19 +++ b/drivers/regulator/rpi-panel-attiny-regulator.c
20 @@ -8,6 +8,7 @@
21 #include <linux/backlight.h>
22 #include <linux/err.h>
23 #include <linux/gpio.h>
24 +#include <linux/gpio/driver.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 @@ -44,10 +45,30 @@
29 #define PC_RST_LCD_N BIT(2)
30 #define PC_RST_BRIDGE_N BIT(3)
31
32 +enum gpio_signals {
33 + RST_BRIDGE_N, /* TC358762 bridge reset */
34 + RST_TP_N, /* Touch controller reset */
35 + NUM_GPIO
36 +};
37 +
38 +struct gpio_signal_mappings {
39 + unsigned int reg;
40 + unsigned int mask;
41 +};
42 +
43 +static const struct gpio_signal_mappings mappings[NUM_GPIO] = {
44 + [RST_BRIDGE_N] = { REG_PORTC, PC_RST_BRIDGE_N | PC_RST_LCD_N },
45 + [RST_TP_N] = { REG_PORTC, PC_RST_TP_N },
46 +};
47 +
48 struct attiny_lcd {
49 /* lock to serialise overall accesses to the Atmel */
50 struct mutex lock;
51 struct regmap *regmap;
52 + bool gpio_states[NUM_GPIO];
53 + u8 port_states[3];
54 +
55 + struct gpio_chip gc;
56 };
57
58 static const struct regmap_config attiny_regmap_config = {
59 @@ -58,6 +79,17 @@ static const struct regmap_config attiny
60 .cache_type = REGCACHE_NONE,
61 };
62
63 +static int attiny_set_port_state(struct attiny_lcd *state, int reg, u8 val)
64 +{
65 + state->port_states[reg - REG_PORTA] = val;
66 + return regmap_write(state->regmap, reg, val);
67 +};
68 +
69 +static u8 attiny_get_port_state(struct attiny_lcd *state, int reg)
70 +{
71 + return state->port_states[reg - REG_PORTA];
72 +};
73 +
74 static int attiny_lcd_power_enable(struct regulator_dev *rdev)
75 {
76 struct attiny_lcd *state = rdev_get_drvdata(rdev);
77 @@ -65,7 +97,7 @@ static int attiny_lcd_power_enable(struc
78 mutex_lock(&state->lock);
79
80 /* Ensure bridge, and tp stay in reset */
81 - regmap_write(rdev->regmap, REG_PORTC, 0);
82 + attiny_set_port_state(state, REG_PORTC, 0);
83 usleep_range(5000, 10000);
84
85 /* Default to the same orientation as the closed source
86 @@ -73,26 +105,16 @@ static int attiny_lcd_power_enable(struc
87 * configuration will be supported using VC4's plane
88 * orientation bits.
89 */
90 - regmap_write(rdev->regmap, REG_PORTA, PA_LCD_LR);
91 + attiny_set_port_state(state, REG_PORTA, PA_LCD_LR);
92 usleep_range(5000, 10000);
93 - regmap_write(rdev->regmap, REG_PORTB, PB_LCD_MAIN);
94 + /* Main regulator on, and power to the panel (LCD_VCC_N) */
95 + attiny_set_port_state(state, REG_PORTB, PB_LCD_MAIN);
96 usleep_range(5000, 10000);
97 /* Bring controllers out of reset */
98 - regmap_write(rdev->regmap, REG_PORTC,
99 - PC_LED_EN | PC_RST_BRIDGE_N | PC_RST_LCD_N | PC_RST_TP_N);
100 + attiny_set_port_state(state, REG_PORTC, PC_LED_EN);
101
102 msleep(80);
103
104 - regmap_write(rdev->regmap, REG_ADDR_H, 0x04);
105 - usleep_range(5000, 8000);
106 - regmap_write(rdev->regmap, REG_ADDR_L, 0x7c);
107 - usleep_range(5000, 8000);
108 - regmap_write(rdev->regmap, REG_WRITE_DATA_H, 0x00);
109 - usleep_range(5000, 8000);
110 - regmap_write(rdev->regmap, REG_WRITE_DATA_L, 0x00);
111 -
112 - msleep(100);
113 -
114 mutex_unlock(&state->lock);
115
116 return 0;
117 @@ -106,11 +128,12 @@ static int attiny_lcd_power_disable(stru
118
119 regmap_write(rdev->regmap, REG_PWM, 0);
120 usleep_range(5000, 10000);
121 - regmap_write(rdev->regmap, REG_PORTA, 0);
122 +
123 + attiny_set_port_state(state, REG_PORTA, 0);
124 usleep_range(5000, 10000);
125 - regmap_write(rdev->regmap, REG_PORTB, PB_LCD_VCC_N);
126 + attiny_set_port_state(state, REG_PORTB, PB_LCD_VCC_N);
127 usleep_range(5000, 10000);
128 - regmap_write(rdev->regmap, REG_PORTC, 0);
129 + attiny_set_port_state(state, REG_PORTC, 0);
130 msleep(30);
131
132 mutex_unlock(&state->lock);
133 @@ -211,6 +234,45 @@ static const struct backlight_ops attiny
134 .get_brightness = attiny_get_brightness,
135 };
136
137 +static int attiny_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
138 +{
139 + return GPIO_LINE_DIRECTION_OUT;
140 +}
141 +
142 +static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
143 +{
144 + struct attiny_lcd *state = gpiochip_get_data(gc);
145 + u8 last_val;
146 +
147 + if (off >= NUM_GPIO)
148 + return;
149 +
150 + mutex_lock(&state->lock);
151 +
152 + last_val = attiny_get_port_state(state, mappings[off].reg);
153 + if (val)
154 + last_val |= mappings[off].mask;
155 + else
156 + last_val &= ~mappings[off].mask;
157 +
158 + attiny_set_port_state(state, mappings[off].reg, last_val);
159 +
160 + if (off == RST_BRIDGE_N && val) {
161 + usleep_range(5000, 8000);
162 + regmap_write(state->regmap, REG_ADDR_H, 0x04);
163 + usleep_range(5000, 8000);
164 + regmap_write(state->regmap, REG_ADDR_L, 0x7c);
165 + usleep_range(5000, 8000);
166 + regmap_write(state->regmap, REG_WRITE_DATA_H, 0x00);
167 + usleep_range(5000, 8000);
168 + regmap_write(state->regmap, REG_WRITE_DATA_L, 0x00);
169 +
170 + msleep(100);
171 + }
172 +
173 + mutex_unlock(&state->lock);
174 +}
175 +
176 /*
177 * I2C driver interface functions
178 */
179 @@ -289,6 +351,23 @@ static int attiny_i2c_probe(struct i2c_c
180
181 bl->props.brightness = 0xff;
182
183 + state->gc.parent = &i2c->dev;
184 + state->gc.label = i2c->name;
185 + state->gc.owner = THIS_MODULE;
186 + state->gc.of_node = i2c->dev.of_node;
187 + state->gc.base = -1;
188 + state->gc.ngpio = NUM_GPIO;
189 +
190 + state->gc.set = attiny_gpio_set;
191 + state->gc.get_direction = attiny_gpio_get_direction;
192 + state->gc.can_sleep = true;
193 +
194 + ret = devm_gpiochip_add_data(&i2c->dev, &state->gc, state);
195 + if (ret) {
196 + dev_err(&i2c->dev, "Failed to create gpiochip: %d\n", ret);
197 + goto error;
198 + }
199 +
200 return 0;
201
202 error: