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