1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Marek Vasut <marex@denx.de>
5 * Based on rpi_touchscreen.c by Eric Anholt <eric@anholt.net>
8 #include <linux/backlight.h>
10 #include <linux/gpio.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
18 #include <linux/regulator/of_regulator.h>
19 #include <linux/slab.h>
21 /* I2C registers of the Atmel microcontroller. */
23 #define REG_PORTA 0x81
24 #define REG_PORTA_HF BIT(2)
25 #define REG_PORTA_VF BIT(3)
26 #define REG_PORTB 0x82
27 #define REG_POWERON 0x85
30 static const struct regmap_config attiny_regmap_config = {
33 .max_register = REG_PWM,
34 .cache_type = REGCACHE_NONE,
37 static int attiny_lcd_power_enable(struct regulator_dev *rdev)
42 regmap_write(rdev->regmap, REG_POWERON, 1);
45 /* Wait for nPWRDWN to go low to indicate poweron is done. */
46 for (i = 0; i < 20; i++) {
47 ret = regmap_read(rdev->regmap, REG_PORTB, &data);
52 usleep_range(10000, 12000);
54 usleep_range(10000, 12000);
57 pr_err("%s: regmap_read_poll_timeout failed %d\n", __func__, ret);
59 /* Default to the same orientation as the closed source
60 * firmware used for the panel. Runtime rotation
61 * configuration will be supported using VC4's plane
64 regmap_write(rdev->regmap, REG_PORTA, BIT(2));
69 static int attiny_lcd_power_disable(struct regulator_dev *rdev)
71 regmap_write(rdev->regmap, REG_PWM, 0);
72 regmap_write(rdev->regmap, REG_POWERON, 0);
77 static int attiny_lcd_power_is_enabled(struct regulator_dev *rdev)
82 for (i = 0; i < 10; i++) {
83 ret = regmap_read(rdev->regmap, REG_POWERON, &data);
86 usleep_range(10000, 12000);
94 for (i = 0; i < 10; i++) {
95 ret = regmap_read(rdev->regmap, REG_PORTB, &data);
98 usleep_range(10000, 12000);
104 return data & BIT(0);
107 static const struct regulator_init_data attiny_regulator_default = {
109 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
113 static const struct regulator_ops attiny_regulator_ops = {
114 .enable = attiny_lcd_power_enable,
115 .disable = attiny_lcd_power_disable,
116 .is_enabled = attiny_lcd_power_is_enabled,
119 static const struct regulator_desc attiny_regulator = {
120 .name = "tc358762-power",
121 .ops = &attiny_regulator_ops,
122 .type = REGULATOR_VOLTAGE,
123 .owner = THIS_MODULE,
126 static int attiny_update_status(struct backlight_device *bl)
128 struct regmap *regmap = bl_get_data(bl);
129 int brightness = bl->props.brightness;
132 if (bl->props.power != FB_BLANK_UNBLANK ||
133 bl->props.fb_blank != FB_BLANK_UNBLANK)
136 for (i = 0; i < 10; i++) {
137 ret = regmap_write(regmap, REG_PWM, brightness);
145 static int attiny_get_brightness(struct backlight_device *bl)
147 struct regmap *regmap = bl_get_data(bl);
148 int ret, brightness, i;
150 for (i = 0; i < 10; i++) {
151 ret = regmap_read(regmap, REG_PWM, &brightness);
162 static const struct backlight_ops attiny_bl = {
163 .update_status = attiny_update_status,
164 .get_brightness = attiny_get_brightness,
168 * I2C driver interface functions
170 static int attiny_i2c_probe(struct i2c_client *i2c,
171 const struct i2c_device_id *id)
173 struct backlight_properties props = { };
174 struct regulator_config config = { };
175 struct backlight_device *bl;
176 struct regulator_dev *rdev;
177 struct regmap *regmap;
181 regmap = devm_regmap_init_i2c(i2c, &attiny_regmap_config);
182 if (IS_ERR(regmap)) {
183 ret = PTR_ERR(regmap);
184 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
189 ret = regmap_read(regmap, REG_ID, &data);
191 dev_err(&i2c->dev, "Failed to read REG_ID reg: %d\n", ret);
196 case 0xde: /* ver 1 */
197 case 0xc3: /* ver 2 */
200 dev_err(&i2c->dev, "Unknown Atmel firmware revision: 0x%02x\n", data);
204 regmap_write(regmap, REG_POWERON, 0);
207 config.dev = &i2c->dev;
208 config.regmap = regmap;
209 config.of_node = i2c->dev.of_node;
210 config.init_data = &attiny_regulator_default;
212 rdev = devm_regulator_register(&i2c->dev, &attiny_regulator, &config);
214 dev_err(&i2c->dev, "Failed to register ATTINY regulator\n");
215 return PTR_ERR(rdev);
218 props.type = BACKLIGHT_RAW;
219 props.max_brightness = 0xff;
220 bl = devm_backlight_device_register(&i2c->dev,
221 "7inch-touchscreen-panel-bl",
222 &i2c->dev, regmap, &attiny_bl,
227 bl->props.brightness = 0xff;
232 static const struct of_device_id attiny_dt_ids[] = {
233 { .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
236 MODULE_DEVICE_TABLE(of, attiny_dt_ids);
238 static struct i2c_driver attiny_regulator_driver = {
240 .name = "rpi_touchscreen_attiny",
241 .of_match_table = of_match_ptr(attiny_dt_ids),
243 .probe = attiny_i2c_probe,
246 module_i2c_driver(attiny_regulator_driver);
248 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
249 MODULE_DESCRIPTION("Regulator device driver for Raspberry Pi 7-inch touchscreen");
250 MODULE_LICENSE("GPL v2");