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
)
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
51 regmap_write(rdev
->regmap
, REG_PORTA
, BIT(2));
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);
64 static int attiny_lcd_power_is_enabled(struct regulator_dev
*rdev
)
69 ret
= regmap_read(rdev
->regmap
, REG_POWERON
, &data
);
76 ret
= regmap_read(rdev
->regmap
, REG_PORTB
, &data
);
83 static const struct regulator_init_data attiny_regulator_default
= {
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
,
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
)
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
);
119 ret
= regmap_read(regmap
, REG_PWM
, &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
;
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",
153 ret
= regmap_read(regmap
, REG_ID
, &data
);
155 dev_err(&i2c
->dev
, "Failed to read REG_ID reg: %d\n", ret
);
160 case 0xde: /* ver 1 */
161 case 0xc3: /* ver 2 */
164 dev_err(&i2c
->dev
, "Unknown Atmel firmware revision: 0x%02x\n", data
);
168 regmap_write(regmap
, REG_POWERON
, 0);
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
);
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
,
191 bl
->props
.brightness
= 0xff;
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
= {
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");