Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-or-later
0002 /*
0003  * ENE KB3930 Embedded Controller Driver
0004  *
0005  * Copyright (C) 2020 Lubomir Rintel
0006  */
0007 
0008 #include <linux/delay.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/i2c.h>
0011 #include <linux/mfd/core.h>
0012 #include <linux/module.h>
0013 #include <linux/reboot.h>
0014 #include <linux/regmap.h>
0015 
0016 /* I2C registers that are multiplexing access to the EC RAM. */
0017 enum {
0018     EC_DATA_IN  = 0x00,
0019     EC_RAM_OUT  = 0x80,
0020     EC_RAM_IN   = 0x81,
0021 };
0022 
0023 /* EC RAM registers. */
0024 enum {
0025     EC_MODEL    = 0x30,
0026     EC_VERSION_MAJ  = 0x31,
0027     EC_VERSION_MIN  = 0x32,
0028 };
0029 
0030 struct kb3930 {
0031     struct i2c_client *client;
0032     struct regmap *ram_regmap;
0033     struct gpio_descs *off_gpios;
0034 };
0035 
0036 static struct kb3930 *kb3930_power_off;
0037 
0038 #define EC_GPIO_WAVE        0
0039 #define EC_GPIO_OFF_MODE    1
0040 
0041 #define EC_OFF_MODE_REBOOT  0
0042 #define EC_OFF_MODE_POWER   1
0043 
0044 static void kb3930_off(struct kb3930 *ddata, int off_mode)
0045 {
0046     gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_OFF_MODE],
0047                    off_mode);
0048 
0049     /*
0050      * This creates a 10 Hz wave on EC_GPIO_WAVE that signals a
0051      * shutdown request to the EC. Once the EC detects it, it will
0052      * proceed to turn the power off or reset the board depending on
0053      * the value of EC_GPIO_OFF_MODE.
0054      */
0055     while (1) {
0056         mdelay(50);
0057         gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_WAVE], 0);
0058         mdelay(50);
0059         gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_WAVE], 1);
0060     }
0061 }
0062 
0063 static int kb3930_restart(struct notifier_block *this,
0064               unsigned long mode, void *cmd)
0065 {
0066     kb3930_off(kb3930_power_off, EC_OFF_MODE_REBOOT);
0067     return NOTIFY_DONE;
0068 }
0069 
0070 static void kb3930_pm_power_off(void)
0071 {
0072     kb3930_off(kb3930_power_off, EC_OFF_MODE_POWER);
0073 }
0074 
0075 static struct notifier_block kb3930_restart_nb = {
0076     .notifier_call = kb3930_restart,
0077 };
0078 
0079 static const struct mfd_cell ariel_ec_cells[] = {
0080     { .name = "dell-wyse-ariel-led", },
0081     { .name = "dell-wyse-ariel-power", },
0082 };
0083 
0084 static int kb3930_ec_ram_reg_write(void *context, unsigned int reg,
0085                    unsigned int val)
0086 {
0087     struct kb3930 *ddata = context;
0088 
0089     return i2c_smbus_write_word_data(ddata->client, EC_RAM_OUT,
0090                      (val << 8) | reg);
0091 }
0092 
0093 static int kb3930_ec_ram_reg_read(void *context, unsigned int reg,
0094                   unsigned int *val)
0095 {
0096     struct kb3930 *ddata = context;
0097     int ret;
0098 
0099     ret = i2c_smbus_write_word_data(ddata->client, EC_RAM_IN, reg);
0100     if (ret < 0)
0101         return ret;
0102 
0103     ret = i2c_smbus_read_word_data(ddata->client, EC_DATA_IN);
0104     if (ret < 0)
0105         return ret;
0106 
0107     *val = ret >> 8;
0108     return 0;
0109 }
0110 
0111 static const struct regmap_config kb3930_ram_regmap_config = {
0112     .name = "ec_ram",
0113     .reg_bits = 8,
0114     .val_bits = 8,
0115     .reg_stride = 1,
0116     .max_register = 0xff,
0117     .reg_write = kb3930_ec_ram_reg_write,
0118     .reg_read = kb3930_ec_ram_reg_read,
0119     .fast_io = false,
0120 };
0121 
0122 static int kb3930_probe(struct i2c_client *client)
0123 {
0124     struct device *dev = &client->dev;
0125     struct device_node *np = dev->of_node;
0126     struct kb3930 *ddata;
0127     unsigned int model;
0128     int ret;
0129 
0130     ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
0131     if (!ddata)
0132         return -ENOMEM;
0133 
0134     kb3930_power_off = ddata;
0135     ddata->client = client;
0136     i2c_set_clientdata(client, ddata);
0137 
0138     ddata->ram_regmap = devm_regmap_init(dev, NULL, ddata,
0139                          &kb3930_ram_regmap_config);
0140     if (IS_ERR(ddata->ram_regmap))
0141         return PTR_ERR(ddata->ram_regmap);
0142 
0143     ret = regmap_read(ddata->ram_regmap, EC_MODEL, &model);
0144     if (ret < 0)
0145         return ret;
0146 
0147     /* Currently we only support the cells present on Dell Ariel model. */
0148     if (model != 'J') {
0149         dev_err(dev, "unknown board model: %02x\n", model);
0150         return -ENODEV;
0151     }
0152 
0153     ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
0154                    ariel_ec_cells,
0155                    ARRAY_SIZE(ariel_ec_cells),
0156                    NULL, 0, NULL);
0157     if (ret)
0158         return ret;
0159 
0160     if (of_property_read_bool(np, "system-power-controller")) {
0161         ddata->off_gpios =
0162             devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
0163         if (IS_ERR(ddata->off_gpios))
0164             return PTR_ERR(ddata->off_gpios);
0165         if (ddata->off_gpios->ndescs < 2) {
0166             dev_err(dev, "invalid off-gpios property\n");
0167             return -EINVAL;
0168         }
0169     }
0170 
0171     if (ddata->off_gpios) {
0172         register_restart_handler(&kb3930_restart_nb);
0173         if (!pm_power_off)
0174             pm_power_off = kb3930_pm_power_off;
0175     }
0176 
0177     return 0;
0178 }
0179 
0180 static int kb3930_remove(struct i2c_client *client)
0181 {
0182     struct kb3930 *ddata = i2c_get_clientdata(client);
0183 
0184     if (ddata->off_gpios) {
0185         if (pm_power_off == kb3930_pm_power_off)
0186             pm_power_off = NULL;
0187         unregister_restart_handler(&kb3930_restart_nb);
0188     }
0189     kb3930_power_off = NULL;
0190 
0191     return 0;
0192 }
0193 
0194 static const struct of_device_id kb3930_dt_ids[] = {
0195     { .compatible = "ene,kb3930" },
0196     { }
0197 };
0198 MODULE_DEVICE_TABLE(of, kb3930_dt_ids);
0199 
0200 static struct i2c_driver kb3930_driver = {
0201     .probe_new = kb3930_probe,
0202     .remove = kb3930_remove,
0203     .driver = {
0204         .name = "ene-kb3930",
0205         .of_match_table = kb3930_dt_ids,
0206     },
0207 };
0208 module_i2c_driver(kb3930_driver);
0209 
0210 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
0211 MODULE_DESCRIPTION("ENE KB3930 Embedded Controller Driver");
0212 MODULE_LICENSE("Dual BSD/GPL");