linux/drivers/regulator/rpi-panel-attiny-regulator.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (C) 2020 Marek Vasut <marex@denx.de>
   4 *
   5 * Based on rpi_touchscreen.c by Eric Anholt <eric@anholt.net>
   6 */
   7
   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>
  20
  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
  29
  30static 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,
  35};
  36
  37static int attiny_lcd_power_enable(struct regulator_dev *rdev)
  38{
  39        unsigned int data;
  40
  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);
  45
  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.
  50         */
  51        regmap_write(rdev->regmap, REG_PORTA, BIT(2));
  52
  53        return 0;
  54}
  55
  56static int attiny_lcd_power_disable(struct regulator_dev *rdev)
  57{
  58        regmap_write(rdev->regmap, REG_PWM, 0);
  59        regmap_write(rdev->regmap, REG_POWERON, 0);
  60        udelay(1);
  61        return 0;
  62}
  63
  64static int attiny_lcd_power_is_enabled(struct regulator_dev *rdev)
  65{
  66        unsigned int data;
  67        int ret;
  68
  69        ret = regmap_read(rdev->regmap, REG_POWERON, &data);
  70        if (ret < 0)
  71                return ret;
  72
  73        if (!(data & BIT(0)))
  74                return 0;
  75
  76        ret = regmap_read(rdev->regmap, REG_PORTB, &data);
  77        if (ret < 0)
  78                return ret;
  79
  80        return data & BIT(0);
  81}
  82
  83static const struct regulator_init_data attiny_regulator_default = {
  84        .constraints = {
  85                .valid_ops_mask = REGULATOR_CHANGE_STATUS,
  86        },
  87};
  88
  89static 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,
  93};
  94
  95static const struct regulator_desc attiny_regulator = {
  96        .name   = "tc358762-power",
  97        .ops    = &attiny_regulator_ops,
  98        .type   = REGULATOR_VOLTAGE,
  99        .owner  = THIS_MODULE,
 100};
 101
 102static int attiny_update_status(struct backlight_device *bl)
 103{
 104        struct regmap *regmap = bl_get_data(bl);
 105        int brightness = bl->props.brightness;
 106
 107        if (bl->props.power != FB_BLANK_UNBLANK ||
 108            bl->props.fb_blank != FB_BLANK_UNBLANK)
 109                brightness = 0;
 110
 111        return regmap_write(regmap, REG_PWM, brightness);
 112}
 113
 114static int attiny_get_brightness(struct backlight_device *bl)
 115{
 116        struct regmap *regmap = bl_get_data(bl);
 117        int ret, brightness;
 118
 119        ret = regmap_read(regmap, REG_PWM, &brightness);
 120        if (ret)
 121                return ret;
 122
 123        return brightness;
 124}
 125
 126static const struct backlight_ops attiny_bl = {
 127        .update_status  = attiny_update_status,
 128        .get_brightness = attiny_get_brightness,
 129};
 130
 131/*
 132 * I2C driver interface functions
 133 */
 134static int attiny_i2c_probe(struct i2c_client *i2c,
 135                const struct i2c_device_id *id)
 136{
 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;
 144
 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;
 151        }
 152
 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;
 157        }
 158
 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;
 166        }
 167
 168        regmap_write(regmap, REG_POWERON, 0);
 169        mdelay(1);
 170
 171        config.dev = &i2c->dev;
 172        config.regmap = regmap;
 173        config.of_node = i2c->dev.of_node;
 174        config.init_data = &attiny_regulator_default;
 175
 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);
 180        }
 181
 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);
 190
 191        bl->props.brightness = 0xff;
 192
 193        return 0;
 194}
 195
 196static const struct of_device_id attiny_dt_ids[] = {
 197        { .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
 198        {},
 199};
 200MODULE_DEVICE_TABLE(of, attiny_dt_ids);
 201
 202static struct i2c_driver attiny_regulator_driver = {
 203        .driver = {
 204                .name = "rpi_touchscreen_attiny",
 205                .of_match_table = of_match_ptr(attiny_dt_ids),
 206        },
 207        .probe = attiny_i2c_probe,
 208};
 209
 210module_i2c_driver(attiny_regulator_driver);
 211
 212MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
 213MODULE_DESCRIPTION("Regulator device driver for Raspberry Pi 7-inch touchscreen");
 214MODULE_LICENSE("GPL v2");
 215