^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * ENE KB3930 Embedded Controller Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2020 Lubomir Rintel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mfd/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* I2C registers that are multiplexing access to the EC RAM. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) EC_DATA_IN = 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) EC_RAM_OUT = 0x80,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) EC_RAM_IN = 0x81,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* EC RAM registers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) EC_MODEL = 0x30,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) EC_VERSION_MAJ = 0x31,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) EC_VERSION_MIN = 0x32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct kb3930 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct regmap *ram_regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct gpio_descs *off_gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct kb3930 *kb3930_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define EC_GPIO_WAVE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define EC_GPIO_OFF_MODE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define EC_OFF_MODE_REBOOT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define EC_OFF_MODE_POWER 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static void kb3930_off(struct kb3930 *ddata, int off_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_OFF_MODE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) off_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * This creates a 10 Hz wave on EC_GPIO_WAVE that signals a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * shutdown request to the EC. Once the EC detects it, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * proceed to turn the power off or reset the board depending on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * the value of EC_GPIO_OFF_MODE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) mdelay(50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_WAVE], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) mdelay(50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_WAVE], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int kb3930_restart(struct notifier_block *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned long mode, void *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) kb3930_off(kb3930_power_off, EC_OFF_MODE_REBOOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static void kb3930_pm_power_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) kb3930_off(kb3930_power_off, EC_OFF_MODE_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static struct notifier_block kb3930_restart_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .notifier_call = kb3930_restart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static const struct mfd_cell ariel_ec_cells[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) { .name = "dell-wyse-ariel-led", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) { .name = "dell-wyse-ariel-power", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int kb3930_ec_ram_reg_write(void *context, unsigned int reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct kb3930 *ddata = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return i2c_smbus_write_word_data(ddata->client, EC_RAM_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) (val << 8) | reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static int kb3930_ec_ram_reg_read(void *context, unsigned int reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct kb3930 *ddata = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ret = i2c_smbus_write_word_data(ddata->client, EC_RAM_IN, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ret = i2c_smbus_read_word_data(ddata->client, EC_DATA_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *val = ret >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static const struct regmap_config kb3930_ram_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .name = "ec_ram",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .reg_stride = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .max_register = 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .reg_write = kb3930_ec_ram_reg_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .reg_read = kb3930_ec_ram_reg_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .fast_io = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int kb3930_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct kb3930 *ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned int model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (!ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) kb3930_power_off = ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ddata->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) i2c_set_clientdata(client, ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ddata->ram_regmap = devm_regmap_init(dev, NULL, ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) &kb3930_ram_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (IS_ERR(ddata->ram_regmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return PTR_ERR(ddata->ram_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ret = regmap_read(ddata->ram_regmap, EC_MODEL, &model);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Currently we only support the cells present on Dell Ariel model. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (model != 'J') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dev_err(dev, "unknown board model: %02x\n", model);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ariel_ec_cells,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ARRAY_SIZE(ariel_ec_cells),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) NULL, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (of_property_read_bool(np, "system-power-controller")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ddata->off_gpios =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (IS_ERR(ddata->off_gpios))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return PTR_ERR(ddata->off_gpios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (ddata->off_gpios->ndescs < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) dev_err(dev, "invalid off-gpios property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (ddata->off_gpios) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) register_restart_handler(&kb3930_restart_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (!pm_power_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) pm_power_off = kb3930_pm_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int kb3930_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct kb3930 *ddata = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (ddata->off_gpios) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (pm_power_off == kb3930_pm_power_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) pm_power_off = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unregister_restart_handler(&kb3930_restart_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) kb3930_power_off = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static const struct of_device_id kb3930_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) { .compatible = "ene,kb3930" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) MODULE_DEVICE_TABLE(of, kb3930_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static struct i2c_driver kb3930_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .probe_new = kb3930_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .remove = kb3930_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) .name = "ene-kb3930",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .of_match_table = of_match_ptr(kb3930_dt_ids),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) module_i2c_driver(kb3930_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) MODULE_DESCRIPTION("ENE KB3930 Embedded Controller Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) MODULE_LICENSE("Dual BSD/GPL");