Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / regulator / rpi-panel-attiny-regulator.c
blobee46bfbf5eee7e25380208372c5ac0a47ae8293f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2020 Marek Vasut <marex@denx.de>
5 * Based on rpi_touchscreen.c by Eric Anholt <eric@anholt.net>
6 */
8 #include <linux/backlight.h>
9 #include <linux/err.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. */
22 #define REG_ID 0x80
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
28 #define REG_PWM 0x86
30 static const struct regmap_config attiny_regmap_config = {
31 .reg_bits = 8,
32 .val_bits = 8,
33 .max_register = REG_PWM,
34 .cache_type = REGCACHE_NONE,
37 static int attiny_lcd_power_enable(struct regulator_dev *rdev)
39 unsigned int data;
41 regmap_write(rdev->regmap, REG_POWERON, 1);
42 /* Wait for nPWRDWN to go low to indicate poweron is done. */
43 regmap_read_poll_timeout(rdev->regmap, REG_PORTB, data,
44 data & BIT(0), 10, 1000000);
46 /* Default to the same orientation as the closed source
47 * firmware used for the panel. Runtime rotation
48 * configuration will be supported using VC4's plane
49 * orientation bits.
51 regmap_write(rdev->regmap, REG_PORTA, BIT(2));
53 return 0;
56 static int attiny_lcd_power_disable(struct regulator_dev *rdev)
58 regmap_write(rdev->regmap, REG_PWM, 0);
59 regmap_write(rdev->regmap, REG_POWERON, 0);
60 udelay(1);
61 return 0;
64 static int attiny_lcd_power_is_enabled(struct regulator_dev *rdev)
66 unsigned int data;
67 int ret;
69 ret = regmap_read(rdev->regmap, REG_POWERON, &data);
70 if (ret < 0)
71 return ret;
73 if (!(data & BIT(0)))
74 return 0;
76 ret = regmap_read(rdev->regmap, REG_PORTB, &data);
77 if (ret < 0)
78 return ret;
80 return data & BIT(0);
83 static const struct regulator_init_data attiny_regulator_default = {
84 .constraints = {
85 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
89 static const struct regulator_ops attiny_regulator_ops = {
90 .enable = attiny_lcd_power_enable,
91 .disable = attiny_lcd_power_disable,
92 .is_enabled = attiny_lcd_power_is_enabled,
95 static const struct regulator_desc attiny_regulator = {
96 .name = "tc358762-power",
97 .ops = &attiny_regulator_ops,
98 .type = REGULATOR_VOLTAGE,
99 .owner = THIS_MODULE,
102 static int attiny_update_status(struct backlight_device *bl)
104 struct regmap *regmap = bl_get_data(bl);
105 int brightness = bl->props.brightness;
107 if (bl->props.power != FB_BLANK_UNBLANK ||
108 bl->props.fb_blank != FB_BLANK_UNBLANK)
109 brightness = 0;
111 return regmap_write(regmap, REG_PWM, brightness);
114 static int attiny_get_brightness(struct backlight_device *bl)
116 struct regmap *regmap = bl_get_data(bl);
117 int ret, brightness;
119 ret = regmap_read(regmap, REG_PWM, &brightness);
120 if (ret)
121 return ret;
123 return brightness;
126 static const struct backlight_ops attiny_bl = {
127 .update_status = attiny_update_status,
128 .get_brightness = attiny_get_brightness,
132 * I2C driver interface functions
134 static int attiny_i2c_probe(struct i2c_client *i2c,
135 const struct i2c_device_id *id)
137 struct backlight_properties props = { };
138 struct regulator_config config = { };
139 struct backlight_device *bl;
140 struct regulator_dev *rdev;
141 struct regmap *regmap;
142 unsigned int data;
143 int ret;
145 regmap = devm_regmap_init_i2c(i2c, &attiny_regmap_config);
146 if (IS_ERR(regmap)) {
147 ret = PTR_ERR(regmap);
148 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
149 ret);
150 return ret;
153 ret = regmap_read(regmap, REG_ID, &data);
154 if (ret < 0) {
155 dev_err(&i2c->dev, "Failed to read REG_ID reg: %d\n", ret);
156 return ret;
159 switch (data) {
160 case 0xde: /* ver 1 */
161 case 0xc3: /* ver 2 */
162 break;
163 default:
164 dev_err(&i2c->dev, "Unknown Atmel firmware revision: 0x%02x\n", data);
165 return -ENODEV;
168 regmap_write(regmap, REG_POWERON, 0);
169 mdelay(1);
171 config.dev = &i2c->dev;
172 config.regmap = regmap;
173 config.of_node = i2c->dev.of_node;
174 config.init_data = &attiny_regulator_default;
176 rdev = devm_regulator_register(&i2c->dev, &attiny_regulator, &config);
177 if (IS_ERR(rdev)) {
178 dev_err(&i2c->dev, "Failed to register ATTINY regulator\n");
179 return PTR_ERR(rdev);
182 props.type = BACKLIGHT_RAW;
183 props.max_brightness = 0xff;
184 bl = devm_backlight_device_register(&i2c->dev,
185 "7inch-touchscreen-panel-bl",
186 &i2c->dev, regmap, &attiny_bl,
187 &props);
188 if (IS_ERR(bl))
189 return PTR_ERR(bl);
191 bl->props.brightness = 0xff;
193 return 0;
196 static const struct of_device_id attiny_dt_ids[] = {
197 { .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
200 MODULE_DEVICE_TABLE(of, attiny_dt_ids);
202 static struct i2c_driver attiny_regulator_driver = {
203 .driver = {
204 .name = "rpi_touchscreen_attiny",
205 .of_match_table = of_match_ptr(attiny_dt_ids),
207 .probe = attiny_i2c_probe,
210 module_i2c_driver(attiny_regulator_driver);
212 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
213 MODULE_DESCRIPTION("Regulator device driver for Raspberry Pi 7-inch touchscreen");
214 MODULE_LICENSE("GPL v2");