|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright (C) 2024 Waveshare International Limited |
| 4 | + * |
| 5 | + * Based on rpi-panel-v2-regulator.c by Dave Stevenson <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/backlight.h> |
| 9 | +#include <linux/err.h> |
| 10 | +#include <linux/gpio.h> |
| 11 | +#include <linux/gpio/driver.h> |
| 12 | +#include <linux/module.h> |
| 13 | +#include <linux/regmap.h> |
| 14 | +#include <linux/regulator/driver.h> |
| 15 | +#include <linux/of.h> |
| 16 | + |
| 17 | +/* I2C registers of the microcontroller. */ |
| 18 | +#define REG_TP 0x94 |
| 19 | +#define REG_LCD 0x95 |
| 20 | +#define REG_PWM 0x96 |
| 21 | +#define REG_SIZE 0x97 |
| 22 | +#define REG_ID 0x98 |
| 23 | +#define REG_VERSION 0x99 |
| 24 | + |
| 25 | +#define NUM_GPIO 16 /* Treat BL_ENABLE, LCD_RESET, TP_RESET as GPIOs */ |
| 26 | + |
| 27 | +struct waveshare_panel_lcd { |
| 28 | + struct mutex lock; |
| 29 | + struct regmap *regmap; |
| 30 | + u16 poweron_state; |
| 31 | + u16 direction_state; |
| 32 | + |
| 33 | + struct gpio_chip gc; |
| 34 | +}; |
| 35 | + |
| 36 | +static const struct regmap_config waveshare_panel_regmap_config = { |
| 37 | + .reg_bits = 8, |
| 38 | + .val_bits = 8, |
| 39 | + .max_register = REG_PWM, |
| 40 | +}; |
| 41 | + |
| 42 | +static int waveshare_panel_gpio_direction_in(struct gpio_chip *gc, |
| 43 | + unsigned int offset) |
| 44 | +{ |
| 45 | + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); |
| 46 | + |
| 47 | + state->direction_state |= (1 << offset); |
| 48 | + |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +static int waveshare_panel_gpio_direction_out(struct gpio_chip *gc, |
| 53 | + unsigned int offset, int val) |
| 54 | +{ |
| 55 | + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); |
| 56 | + u16 last_val; |
| 57 | + |
| 58 | + state->direction_state &= ~(1 << offset); |
| 59 | + |
| 60 | + last_val = state->poweron_state; |
| 61 | + if (val) |
| 62 | + last_val |= (1 << offset); |
| 63 | + else |
| 64 | + last_val &= ~(1 << offset); |
| 65 | + |
| 66 | + state->poweron_state = last_val; |
| 67 | + |
| 68 | + regmap_write(state->regmap, REG_TP, last_val >> 8); |
| 69 | + regmap_write(state->regmap, REG_LCD, last_val & 0xff); |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +static int waveshare_panel_gpio_get_direction(struct gpio_chip *gc, |
| 75 | + unsigned int offset) |
| 76 | +{ |
| 77 | + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); |
| 78 | + |
| 79 | + if (state->direction_state & (1 << offset)) |
| 80 | + return GPIO_LINE_DIRECTION_IN; |
| 81 | + else |
| 82 | + return GPIO_LINE_DIRECTION_OUT; |
| 83 | +} |
| 84 | + |
| 85 | +static int waveshare_panel_gpio_get(struct gpio_chip *gc, unsigned int offset) |
| 86 | +{ |
| 87 | + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); |
| 88 | + |
| 89 | + if (state->poweron_state & (1 << offset)) |
| 90 | + return 1; |
| 91 | + else |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +static void waveshare_panel_gpio_set(struct gpio_chip *gc, unsigned int offset, |
| 96 | + int value) |
| 97 | +{ |
| 98 | + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); |
| 99 | + u16 last_val; |
| 100 | + |
| 101 | + if (offset >= NUM_GPIO) |
| 102 | + return; |
| 103 | + |
| 104 | + mutex_lock(&state->lock); |
| 105 | + |
| 106 | + last_val = state->poweron_state; |
| 107 | + if (value) |
| 108 | + last_val |= (1 << offset); |
| 109 | + else |
| 110 | + last_val &= ~(1 << offset); |
| 111 | + |
| 112 | + state->poweron_state = last_val; |
| 113 | + |
| 114 | + regmap_write(state->regmap, REG_TP, last_val >> 8); |
| 115 | + regmap_write(state->regmap, REG_LCD, last_val & 0xff); |
| 116 | + |
| 117 | + mutex_unlock(&state->lock); |
| 118 | +} |
| 119 | + |
| 120 | +static int waveshare_panel_update_status(struct backlight_device *bl) |
| 121 | +{ |
| 122 | + struct waveshare_panel_lcd *state = bl_get_data(bl); |
| 123 | + int brightness = bl->props.brightness; |
| 124 | + u16 last_val; |
| 125 | + |
| 126 | + if (bl->props.power != FB_BLANK_UNBLANK || |
| 127 | + bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) |
| 128 | + brightness = 0; |
| 129 | + |
| 130 | + mutex_lock(&state->lock); |
| 131 | + |
| 132 | + last_val = state->poweron_state; |
| 133 | + if (brightness) |
| 134 | + last_val |= (1 << 2); // Enable BL_EN |
| 135 | + else |
| 136 | + last_val &= ~(1 << 2); // Disable BL_EN |
| 137 | + |
| 138 | + state->poweron_state = last_val; |
| 139 | + |
| 140 | + regmap_write(state->regmap, REG_TP, last_val >> 8); |
| 141 | + regmap_write(state->regmap, REG_LCD, last_val & 0xff); |
| 142 | + |
| 143 | + mutex_unlock(&state->lock); |
| 144 | + |
| 145 | + return regmap_write(state->regmap, REG_PWM, brightness); |
| 146 | +} |
| 147 | + |
| 148 | +static const struct backlight_ops waveshare_panel_bl = { |
| 149 | + .update_status = waveshare_panel_update_status, |
| 150 | +}; |
| 151 | + |
| 152 | +static int waveshare_panel_i2c_read(struct i2c_client *client, u8 reg, |
| 153 | + unsigned int *buf) |
| 154 | +{ |
| 155 | + struct i2c_msg msgs[1]; |
| 156 | + u8 addr_buf[1] = { reg }; |
| 157 | + u8 data_buf[1] = { |
| 158 | + 0, |
| 159 | + }; |
| 160 | + int ret; |
| 161 | + |
| 162 | + /* Write register address */ |
| 163 | + msgs[0].addr = client->addr; |
| 164 | + msgs[0].flags = 0; |
| 165 | + msgs[0].len = ARRAY_SIZE(addr_buf); |
| 166 | + msgs[0].buf = addr_buf; |
| 167 | + |
| 168 | + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 169 | + if (ret != ARRAY_SIZE(msgs)) |
| 170 | + return -EIO; |
| 171 | + |
| 172 | + usleep_range(5000, 10000); |
| 173 | + |
| 174 | + /* Read data from register */ |
| 175 | + msgs[0].addr = client->addr; |
| 176 | + msgs[0].flags = I2C_M_RD; |
| 177 | + msgs[0].len = 1; |
| 178 | + msgs[0].buf = data_buf; |
| 179 | + |
| 180 | + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 181 | + if (ret != ARRAY_SIZE(msgs)) |
| 182 | + return -EIO; |
| 183 | + |
| 184 | + *buf = data_buf[0]; |
| 185 | + return 0; |
| 186 | +} |
| 187 | + |
| 188 | +/* |
| 189 | + * I2C driver interface functions |
| 190 | + */ |
| 191 | +static int waveshare_panel_i2c_probe(struct i2c_client *i2c) |
| 192 | +{ |
| 193 | + struct backlight_properties props = {}; |
| 194 | + struct backlight_device *bl; |
| 195 | + struct waveshare_panel_lcd *state; |
| 196 | + struct regmap *regmap; |
| 197 | + unsigned int data; |
| 198 | + int ret; |
| 199 | + |
| 200 | + state = devm_kzalloc(&i2c->dev, sizeof(*state), GFP_KERNEL); |
| 201 | + if (!state) |
| 202 | + return -ENOMEM; |
| 203 | + |
| 204 | + mutex_init(&state->lock); |
| 205 | + i2c_set_clientdata(i2c, state); |
| 206 | + |
| 207 | + regmap = devm_regmap_init_i2c(i2c, &waveshare_panel_regmap_config); |
| 208 | + if (IS_ERR(regmap)) { |
| 209 | + ret = PTR_ERR(regmap); |
| 210 | + dev_err(&i2c->dev, "Failed to allocate register map: %d\n", |
| 211 | + ret); |
| 212 | + goto error; |
| 213 | + } |
| 214 | + |
| 215 | + ret = waveshare_panel_i2c_read(i2c, REG_ID, &data); |
| 216 | + if (ret == 0) |
| 217 | + dev_info(&i2c->dev, "waveshare panel hw id = 0x%x\n", data); |
| 218 | + |
| 219 | + ret = waveshare_panel_i2c_read(i2c, REG_SIZE, &data); |
| 220 | + if (ret == 0) |
| 221 | + dev_info(&i2c->dev, "waveshare panel size = %d\n", data); |
| 222 | + |
| 223 | + ret = waveshare_panel_i2c_read(i2c, REG_VERSION, &data); |
| 224 | + if (ret == 0) |
| 225 | + dev_info(&i2c->dev, "waveshare panel mcu version = 0x%x\n", |
| 226 | + data); |
| 227 | + |
| 228 | + state->direction_state = 0; |
| 229 | + state->poweron_state = (1 << 9) | (1 << 8) | (1 << 4) | |
| 230 | + (1 << 0); // Enable VCC |
| 231 | + regmap_write(regmap, REG_TP, state->poweron_state >> 8); |
| 232 | + regmap_write(regmap, REG_LCD, state->poweron_state & 0xff); |
| 233 | + msleep(20); |
| 234 | + |
| 235 | + state->regmap = regmap; |
| 236 | + state->gc.parent = &i2c->dev; |
| 237 | + state->gc.label = i2c->name; |
| 238 | + state->gc.owner = THIS_MODULE; |
| 239 | + state->gc.base = -1; |
| 240 | + state->gc.ngpio = NUM_GPIO; |
| 241 | + |
| 242 | + state->gc.get = waveshare_panel_gpio_get; |
| 243 | + state->gc.set = waveshare_panel_gpio_set; |
| 244 | + state->gc.direction_input = waveshare_panel_gpio_direction_in; |
| 245 | + state->gc.direction_output = waveshare_panel_gpio_direction_out; |
| 246 | + state->gc.get_direction = waveshare_panel_gpio_get_direction; |
| 247 | + state->gc.can_sleep = true; |
| 248 | + |
| 249 | + ret = devm_gpiochip_add_data(&i2c->dev, &state->gc, state); |
| 250 | + if (ret) { |
| 251 | + dev_err(&i2c->dev, "Failed to create gpiochip: %d\n", ret); |
| 252 | + goto error; |
| 253 | + } |
| 254 | + |
| 255 | + props.type = BACKLIGHT_RAW; |
| 256 | + props.max_brightness = 255; |
| 257 | + bl = devm_backlight_device_register(&i2c->dev, dev_name(&i2c->dev), |
| 258 | + &i2c->dev, state, |
| 259 | + &waveshare_panel_bl, &props); |
| 260 | + if (IS_ERR(bl)) |
| 261 | + return PTR_ERR(bl); |
| 262 | + |
| 263 | + bl->props.brightness = 255; |
| 264 | + |
| 265 | + return 0; |
| 266 | + |
| 267 | +error: |
| 268 | + mutex_destroy(&state->lock); |
| 269 | + return ret; |
| 270 | +} |
| 271 | + |
| 272 | +static void waveshare_panel_i2c_remove(struct i2c_client *client) |
| 273 | +{ |
| 274 | + struct waveshare_panel_lcd *state = i2c_get_clientdata(client); |
| 275 | + |
| 276 | + mutex_destroy(&state->lock); |
| 277 | +} |
| 278 | + |
| 279 | +static void waveshare_panel_i2c_shutdown(struct i2c_client *client) |
| 280 | +{ |
| 281 | + struct waveshare_panel_lcd *state = i2c_get_clientdata(client); |
| 282 | + |
| 283 | + regmap_write(state->regmap, REG_PWM, 0); |
| 284 | +} |
| 285 | + |
| 286 | +static const struct of_device_id waveshare_panel_dt_ids[] = { |
| 287 | + { .compatible = "waveshare,touchscreen-panel-regulator" }, |
| 288 | + {}, |
| 289 | +}; |
| 290 | +MODULE_DEVICE_TABLE(of, waveshare_panel_dt_ids); |
| 291 | + |
| 292 | +static struct i2c_driver waveshare_panel_regulator_driver = { |
| 293 | + .driver = { |
| 294 | + .name = "waveshare_touchscreen", |
| 295 | + .of_match_table = of_match_ptr(waveshare_panel_dt_ids), |
| 296 | + }, |
| 297 | + .probe = waveshare_panel_i2c_probe, |
| 298 | + .remove = waveshare_panel_i2c_remove, |
| 299 | + .shutdown = waveshare_panel_i2c_shutdown, |
| 300 | +}; |
| 301 | + |
| 302 | +module_i2c_driver(waveshare_panel_regulator_driver); |
| 303 | + |
| 304 | +MODULE_AUTHOR( "Waveshare Team <[email protected]>"); |
| 305 | +MODULE_DESCRIPTION("Regulator device driver for Waveshare touchscreen"); |
| 306 | +MODULE_LICENSE("GPL"); |
0 commit comments