1 // SPDX-License-Identifier: GPL-2.0+
3 * Acer Iconia Tab A500 Embedded Controller Driver
5 * Copyright 2020 GRATE-driver project
8 #include <linux/delay.h>
10 #include <linux/mfd/core.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/reboot.h>
14 #include <linux/regmap.h>
16 #define A500_EC_I2C_ERR_TIMEOUT 500
17 #define A500_EC_POWER_CMD_TIMEOUT 1000
20 * Controller's firmware expects specific command opcodes to be used for the
21 * corresponding registers. Unsupported commands are skipped by the firmware.
23 #define CMD_SHUTDOWN 0x0
24 #define CMD_WARM_REBOOT 0x0
25 #define CMD_COLD_REBOOT 0x1
28 REG_CURRENT_NOW = 0x03,
30 REG_WARM_REBOOT = 0x54,
31 REG_COLD_REBOOT = 0x55,
34 static struct i2c_client *a500_ec_client_pm_off;
36 static int a500_ec_read(void *context, const void *reg_buf, size_t reg_size,
37 void *val_buf, size_t val_sizel)
39 struct i2c_client *client = context;
40 unsigned int reg, retries = 5;
41 u16 *ret_val = val_buf;
46 while (retries-- > 0) {
47 ret = i2c_smbus_read_word_data(client, reg);
51 msleep(A500_EC_I2C_ERR_TIMEOUT);
55 dev_err(&client->dev, "read 0x%x failed: %d\n", reg, ret);
61 if (reg == REG_CURRENT_NOW)
67 static int a500_ec_write(void *context, const void *data, size_t count)
69 struct i2c_client *client = context;
70 unsigned int reg, val, retries = 5;
73 reg = *(u8 *)(data + 0);
74 val = *(u16 *)(data + 1);
76 while (retries-- > 0) {
77 ret = i2c_smbus_write_word_data(client, reg, val);
81 msleep(A500_EC_I2C_ERR_TIMEOUT);
85 dev_err(&client->dev, "write 0x%x failed: %d\n", reg, ret);
92 static const struct regmap_config a500_ec_regmap_config = {
99 static const struct regmap_bus a500_ec_regmap_bus = {
100 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
101 .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
102 .write = a500_ec_write,
103 .read = a500_ec_read,
107 static void a500_ec_poweroff(void)
109 i2c_smbus_write_word_data(a500_ec_client_pm_off,
110 REG_SHUTDOWN, CMD_SHUTDOWN);
112 mdelay(A500_EC_POWER_CMD_TIMEOUT);
115 static int a500_ec_restart_notify(struct notifier_block *this,
116 unsigned long reboot_mode, void *data)
118 if (reboot_mode == REBOOT_WARM)
119 i2c_smbus_write_word_data(a500_ec_client_pm_off,
120 REG_WARM_REBOOT, CMD_WARM_REBOOT);
122 i2c_smbus_write_word_data(a500_ec_client_pm_off,
123 REG_COLD_REBOOT, CMD_COLD_REBOOT);
125 mdelay(A500_EC_POWER_CMD_TIMEOUT);
130 static struct notifier_block a500_ec_restart_handler = {
131 .notifier_call = a500_ec_restart_notify,
135 static const struct mfd_cell a500_ec_cells[] = {
136 { .name = "acer-a500-iconia-battery", },
137 { .name = "acer-a500-iconia-leds", },
140 static int a500_ec_probe(struct i2c_client *client)
142 struct regmap *regmap;
145 regmap = devm_regmap_init(&client->dev, &a500_ec_regmap_bus,
146 client, &a500_ec_regmap_config);
148 return PTR_ERR(regmap);
150 err = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_AUTO,
151 a500_ec_cells, ARRAY_SIZE(a500_ec_cells),
154 dev_err(&client->dev, "failed to add sub-devices: %d\n", err);
158 if (of_device_is_system_power_controller(client->dev.of_node)) {
159 a500_ec_client_pm_off = client;
161 err = register_restart_handler(&a500_ec_restart_handler);
166 pm_power_off = a500_ec_poweroff;
172 static void a500_ec_remove(struct i2c_client *client)
174 if (of_device_is_system_power_controller(client->dev.of_node)) {
175 if (pm_power_off == a500_ec_poweroff)
178 unregister_restart_handler(&a500_ec_restart_handler);
182 static const struct of_device_id a500_ec_match[] = {
183 { .compatible = "acer,a500-iconia-ec" },
186 MODULE_DEVICE_TABLE(of, a500_ec_match);
188 static struct i2c_driver a500_ec_driver = {
190 .name = "acer-a500-embedded-controller",
191 .of_match_table = a500_ec_match,
193 .probe_new = a500_ec_probe,
194 .remove = a500_ec_remove,
196 module_i2c_driver(a500_ec_driver);
198 MODULE_DESCRIPTION("Acer Iconia Tab A500 Embedded Controller driver");
199 MODULE_AUTHOR("Dmitry Osipenko <digetx@gmail.com>");
200 MODULE_LICENSE("GPL");