192eba680SDmitry Osipenko // SPDX-License-Identifier: GPL-2.0+
292eba680SDmitry Osipenko /*
392eba680SDmitry Osipenko * Acer Iconia Tab A500 Embedded Controller Driver
492eba680SDmitry Osipenko *
592eba680SDmitry Osipenko * Copyright 2020 GRATE-driver project
692eba680SDmitry Osipenko */
792eba680SDmitry Osipenko
892eba680SDmitry Osipenko #include <linux/delay.h>
992eba680SDmitry Osipenko #include <linux/i2c.h>
1092eba680SDmitry Osipenko #include <linux/mfd/core.h>
1192eba680SDmitry Osipenko #include <linux/module.h>
12*dc0c386eSRob Herring #include <linux/of.h>
1392eba680SDmitry Osipenko #include <linux/reboot.h>
1492eba680SDmitry Osipenko #include <linux/regmap.h>
1592eba680SDmitry Osipenko
1692eba680SDmitry Osipenko #define A500_EC_I2C_ERR_TIMEOUT 500
1792eba680SDmitry Osipenko #define A500_EC_POWER_CMD_TIMEOUT 1000
1892eba680SDmitry Osipenko
1992eba680SDmitry Osipenko /*
2092eba680SDmitry Osipenko * Controller's firmware expects specific command opcodes to be used for the
2192eba680SDmitry Osipenko * corresponding registers. Unsupported commands are skipped by the firmware.
2292eba680SDmitry Osipenko */
2392eba680SDmitry Osipenko #define CMD_SHUTDOWN 0x0
2492eba680SDmitry Osipenko #define CMD_WARM_REBOOT 0x0
2592eba680SDmitry Osipenko #define CMD_COLD_REBOOT 0x1
2692eba680SDmitry Osipenko
2792eba680SDmitry Osipenko enum {
2892eba680SDmitry Osipenko REG_CURRENT_NOW = 0x03,
2992eba680SDmitry Osipenko REG_SHUTDOWN = 0x52,
3092eba680SDmitry Osipenko REG_WARM_REBOOT = 0x54,
3192eba680SDmitry Osipenko REG_COLD_REBOOT = 0x55,
3292eba680SDmitry Osipenko };
3392eba680SDmitry Osipenko
3492eba680SDmitry Osipenko static struct i2c_client *a500_ec_client_pm_off;
3592eba680SDmitry Osipenko
a500_ec_read(void * context,const void * reg_buf,size_t reg_size,void * val_buf,size_t val_sizel)3692eba680SDmitry Osipenko static int a500_ec_read(void *context, const void *reg_buf, size_t reg_size,
3792eba680SDmitry Osipenko void *val_buf, size_t val_sizel)
3892eba680SDmitry Osipenko {
3992eba680SDmitry Osipenko struct i2c_client *client = context;
4092eba680SDmitry Osipenko unsigned int reg, retries = 5;
4192eba680SDmitry Osipenko u16 *ret_val = val_buf;
4292eba680SDmitry Osipenko s32 ret = 0;
4392eba680SDmitry Osipenko
4492eba680SDmitry Osipenko reg = *(u8 *)reg_buf;
4592eba680SDmitry Osipenko
4692eba680SDmitry Osipenko while (retries-- > 0) {
4792eba680SDmitry Osipenko ret = i2c_smbus_read_word_data(client, reg);
4892eba680SDmitry Osipenko if (ret >= 0)
4992eba680SDmitry Osipenko break;
5092eba680SDmitry Osipenko
5192eba680SDmitry Osipenko msleep(A500_EC_I2C_ERR_TIMEOUT);
5292eba680SDmitry Osipenko }
5392eba680SDmitry Osipenko
5492eba680SDmitry Osipenko if (ret < 0) {
5592eba680SDmitry Osipenko dev_err(&client->dev, "read 0x%x failed: %d\n", reg, ret);
5692eba680SDmitry Osipenko return ret;
5792eba680SDmitry Osipenko }
5892eba680SDmitry Osipenko
5992eba680SDmitry Osipenko *ret_val = ret;
6092eba680SDmitry Osipenko
6192eba680SDmitry Osipenko if (reg == REG_CURRENT_NOW)
6292eba680SDmitry Osipenko fsleep(10000);
6392eba680SDmitry Osipenko
6492eba680SDmitry Osipenko return 0;
6592eba680SDmitry Osipenko }
6692eba680SDmitry Osipenko
a500_ec_write(void * context,const void * data,size_t count)6792eba680SDmitry Osipenko static int a500_ec_write(void *context, const void *data, size_t count)
6892eba680SDmitry Osipenko {
6992eba680SDmitry Osipenko struct i2c_client *client = context;
7092eba680SDmitry Osipenko unsigned int reg, val, retries = 5;
7192eba680SDmitry Osipenko s32 ret = 0;
7292eba680SDmitry Osipenko
7392eba680SDmitry Osipenko reg = *(u8 *)(data + 0);
7492eba680SDmitry Osipenko val = *(u16 *)(data + 1);
7592eba680SDmitry Osipenko
7692eba680SDmitry Osipenko while (retries-- > 0) {
7792eba680SDmitry Osipenko ret = i2c_smbus_write_word_data(client, reg, val);
7892eba680SDmitry Osipenko if (ret >= 0)
7992eba680SDmitry Osipenko break;
8092eba680SDmitry Osipenko
8192eba680SDmitry Osipenko msleep(A500_EC_I2C_ERR_TIMEOUT);
8292eba680SDmitry Osipenko }
8392eba680SDmitry Osipenko
8492eba680SDmitry Osipenko if (ret < 0) {
8592eba680SDmitry Osipenko dev_err(&client->dev, "write 0x%x failed: %d\n", reg, ret);
8692eba680SDmitry Osipenko return ret;
8792eba680SDmitry Osipenko }
8892eba680SDmitry Osipenko
8992eba680SDmitry Osipenko return 0;
9092eba680SDmitry Osipenko }
9192eba680SDmitry Osipenko
9292eba680SDmitry Osipenko static const struct regmap_config a500_ec_regmap_config = {
9392eba680SDmitry Osipenko .name = "KB930",
9492eba680SDmitry Osipenko .reg_bits = 8,
9592eba680SDmitry Osipenko .val_bits = 16,
9692eba680SDmitry Osipenko .max_register = 0xff,
9792eba680SDmitry Osipenko };
9892eba680SDmitry Osipenko
9992eba680SDmitry Osipenko static const struct regmap_bus a500_ec_regmap_bus = {
10092eba680SDmitry Osipenko .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
10192eba680SDmitry Osipenko .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
10292eba680SDmitry Osipenko .write = a500_ec_write,
10392eba680SDmitry Osipenko .read = a500_ec_read,
10492eba680SDmitry Osipenko .max_raw_read = 2,
10592eba680SDmitry Osipenko };
10692eba680SDmitry Osipenko
a500_ec_poweroff(void)10792eba680SDmitry Osipenko static void a500_ec_poweroff(void)
10892eba680SDmitry Osipenko {
10992eba680SDmitry Osipenko i2c_smbus_write_word_data(a500_ec_client_pm_off,
11092eba680SDmitry Osipenko REG_SHUTDOWN, CMD_SHUTDOWN);
11192eba680SDmitry Osipenko
11292eba680SDmitry Osipenko mdelay(A500_EC_POWER_CMD_TIMEOUT);
11392eba680SDmitry Osipenko }
11492eba680SDmitry Osipenko
a500_ec_restart_notify(struct notifier_block * this,unsigned long reboot_mode,void * data)11592eba680SDmitry Osipenko static int a500_ec_restart_notify(struct notifier_block *this,
11692eba680SDmitry Osipenko unsigned long reboot_mode, void *data)
11792eba680SDmitry Osipenko {
11892eba680SDmitry Osipenko if (reboot_mode == REBOOT_WARM)
11992eba680SDmitry Osipenko i2c_smbus_write_word_data(a500_ec_client_pm_off,
12092eba680SDmitry Osipenko REG_WARM_REBOOT, CMD_WARM_REBOOT);
12192eba680SDmitry Osipenko else
12292eba680SDmitry Osipenko i2c_smbus_write_word_data(a500_ec_client_pm_off,
12392eba680SDmitry Osipenko REG_COLD_REBOOT, CMD_COLD_REBOOT);
12492eba680SDmitry Osipenko
12592eba680SDmitry Osipenko mdelay(A500_EC_POWER_CMD_TIMEOUT);
12692eba680SDmitry Osipenko
12792eba680SDmitry Osipenko return NOTIFY_DONE;
12892eba680SDmitry Osipenko }
12992eba680SDmitry Osipenko
13092eba680SDmitry Osipenko static struct notifier_block a500_ec_restart_handler = {
13192eba680SDmitry Osipenko .notifier_call = a500_ec_restart_notify,
13292eba680SDmitry Osipenko .priority = 200,
13392eba680SDmitry Osipenko };
13492eba680SDmitry Osipenko
13592eba680SDmitry Osipenko static const struct mfd_cell a500_ec_cells[] = {
13692eba680SDmitry Osipenko { .name = "acer-a500-iconia-battery", },
13792eba680SDmitry Osipenko { .name = "acer-a500-iconia-leds", },
13892eba680SDmitry Osipenko };
13992eba680SDmitry Osipenko
a500_ec_probe(struct i2c_client * client)14092eba680SDmitry Osipenko static int a500_ec_probe(struct i2c_client *client)
14192eba680SDmitry Osipenko {
14292eba680SDmitry Osipenko struct regmap *regmap;
14392eba680SDmitry Osipenko int err;
14492eba680SDmitry Osipenko
14592eba680SDmitry Osipenko regmap = devm_regmap_init(&client->dev, &a500_ec_regmap_bus,
14692eba680SDmitry Osipenko client, &a500_ec_regmap_config);
14792eba680SDmitry Osipenko if (IS_ERR(regmap))
14892eba680SDmitry Osipenko return PTR_ERR(regmap);
14992eba680SDmitry Osipenko
15092eba680SDmitry Osipenko err = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_AUTO,
15192eba680SDmitry Osipenko a500_ec_cells, ARRAY_SIZE(a500_ec_cells),
15292eba680SDmitry Osipenko NULL, 0, NULL);
15392eba680SDmitry Osipenko if (err) {
15492eba680SDmitry Osipenko dev_err(&client->dev, "failed to add sub-devices: %d\n", err);
15592eba680SDmitry Osipenko return err;
15692eba680SDmitry Osipenko }
15792eba680SDmitry Osipenko
15892eba680SDmitry Osipenko if (of_device_is_system_power_controller(client->dev.of_node)) {
15992eba680SDmitry Osipenko a500_ec_client_pm_off = client;
16092eba680SDmitry Osipenko
16192eba680SDmitry Osipenko err = register_restart_handler(&a500_ec_restart_handler);
16292eba680SDmitry Osipenko if (err)
16392eba680SDmitry Osipenko return err;
16492eba680SDmitry Osipenko
16592eba680SDmitry Osipenko if (!pm_power_off)
16692eba680SDmitry Osipenko pm_power_off = a500_ec_poweroff;
16792eba680SDmitry Osipenko }
16892eba680SDmitry Osipenko
16992eba680SDmitry Osipenko return 0;
17092eba680SDmitry Osipenko }
17192eba680SDmitry Osipenko
a500_ec_remove(struct i2c_client * client)172ed5c2f5fSUwe Kleine-König static void a500_ec_remove(struct i2c_client *client)
17392eba680SDmitry Osipenko {
17492eba680SDmitry Osipenko if (of_device_is_system_power_controller(client->dev.of_node)) {
17592eba680SDmitry Osipenko if (pm_power_off == a500_ec_poweroff)
17692eba680SDmitry Osipenko pm_power_off = NULL;
17792eba680SDmitry Osipenko
17892eba680SDmitry Osipenko unregister_restart_handler(&a500_ec_restart_handler);
17992eba680SDmitry Osipenko }
18092eba680SDmitry Osipenko }
18192eba680SDmitry Osipenko
18292eba680SDmitry Osipenko static const struct of_device_id a500_ec_match[] = {
18392eba680SDmitry Osipenko { .compatible = "acer,a500-iconia-ec" },
18492eba680SDmitry Osipenko { }
18592eba680SDmitry Osipenko };
18692eba680SDmitry Osipenko MODULE_DEVICE_TABLE(of, a500_ec_match);
18792eba680SDmitry Osipenko
18892eba680SDmitry Osipenko static struct i2c_driver a500_ec_driver = {
18992eba680SDmitry Osipenko .driver = {
19092eba680SDmitry Osipenko .name = "acer-a500-embedded-controller",
19192eba680SDmitry Osipenko .of_match_table = a500_ec_match,
19292eba680SDmitry Osipenko },
1939816d859SUwe Kleine-König .probe = a500_ec_probe,
19492eba680SDmitry Osipenko .remove = a500_ec_remove,
19592eba680SDmitry Osipenko };
19692eba680SDmitry Osipenko module_i2c_driver(a500_ec_driver);
19792eba680SDmitry Osipenko
19892eba680SDmitry Osipenko MODULE_DESCRIPTION("Acer Iconia Tab A500 Embedded Controller driver");
19992eba680SDmitry Osipenko MODULE_AUTHOR("Dmitry Osipenko <digetx@gmail.com>");
20092eba680SDmitry Osipenko MODULE_LICENSE("GPL");
201