1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2024, Nikita Travkin <nikita@trvn.ru> */
4 #include <linux/unaligned.h>
5 #include <drm/drm_bridge.h>
6 #include <linux/bits.h>
7 #include <linux/delay.h>
9 #include <linux/input.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/power_supply.h>
13 #include <linux/usb/typec_mux.h>
14 #include <linux/workqueue_types.h>
16 #define MILLI_TO_MICRO 1000
18 #define ASPIRE_EC_EVENT 0x05
20 #define ASPIRE_EC_EVENT_WATCHDOG 0x20
21 #define ASPIRE_EC_EVENT_KBD_BKL_ON 0x57
22 #define ASPIRE_EC_EVENT_KBD_BKL_OFF 0x58
23 #define ASPIRE_EC_EVENT_LID_CLOSE 0x9b
24 #define ASPIRE_EC_EVENT_LID_OPEN 0x9c
25 #define ASPIRE_EC_EVENT_BKL_UNBLANKED 0x9d
26 #define ASPIRE_EC_EVENT_BKL_BLANKED 0x9e
27 #define ASPIRE_EC_EVENT_FG_INF_CHG 0x85
28 #define ASPIRE_EC_EVENT_FG_STA_CHG 0xc6
29 #define ASPIRE_EC_EVENT_HPD_DIS 0xa3
30 #define ASPIRE_EC_EVENT_HPD_CON 0xa4
32 #define ASPIRE_EC_FG_DYNAMIC 0x07
33 #define ASPIRE_EC_FG_STATIC 0x08
35 #define ASPIRE_EC_FG_FLAG_PRESENT BIT(0)
36 #define ASPIRE_EC_FG_FLAG_FULL BIT(1)
37 #define ASPIRE_EC_FG_FLAG_DISCHARGING BIT(2)
38 #define ASPIRE_EC_FG_FLAG_CHARGING BIT(3)
40 #define ASPIRE_EC_RAM_READ 0x20
41 #define ASPIRE_EC_RAM_WRITE 0x21
43 #define ASPIRE_EC_RAM_WATCHDOG 0x19
44 #define ASPIRE_EC_WATCHDOG_BIT BIT(6)
46 #define ASPIRE_EC_RAM_KBD_MODE 0x43
48 #define ASPIRE_EC_RAM_KBD_FN_EN BIT(0)
49 #define ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP BIT(5)
50 #define ASPIRE_EC_RAM_KBD_ALWAYS_SET BIT(6)
51 #define ASPIRE_EC_RAM_KBD_NUM_LAYER_EN BIT(7)
53 #define ASPIRE_EC_RAM_KBD_MODE_2 0x60
55 #define ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY BIT(3)
57 #define ASPIRE_EC_RAM_HPD_STATUS 0xf4
58 #define ASPIRE_EC_HPD_CONNECTED 0x03
60 #define ASPIRE_EC_RAM_LID_STATUS 0x4c
61 #define ASPIRE_EC_LID_OPEN BIT(6)
63 #define ASPIRE_EC_RAM_ADP 0x40
64 #define ASPIRE_EC_AC_STATUS BIT(0)
67 struct i2c_client
*client
;
68 struct power_supply
*bat_psy
;
69 struct power_supply
*adp_psy
;
70 struct input_dev
*idev
;
72 bool bridge_configured
;
73 struct drm_bridge bridge
;
74 struct work_struct work
;
77 static int aspire_ec_ram_read(struct i2c_client
*client
, u8 off
, u8
*data
, u8 data_len
)
79 i2c_smbus_write_byte_data(client
, ASPIRE_EC_RAM_READ
, off
);
80 i2c_smbus_read_i2c_block_data(client
, ASPIRE_EC_RAM_READ
, data_len
, data
);
84 static int aspire_ec_ram_write(struct i2c_client
*client
, u8 off
, u8 data
)
86 u8 tmp
[2] = {off
, data
};
88 i2c_smbus_write_i2c_block_data(client
, ASPIRE_EC_RAM_WRITE
, sizeof(tmp
), tmp
);
92 static irqreturn_t
aspire_ec_irq_handler(int irq
, void *data
)
94 struct aspire_ec
*ec
= data
;
99 * The original ACPI firmware actually has a small sleep in the handler.
101 * It seems like in most cases it's not needed but when the device
102 * just exits suspend, our i2c driver has a brief time where data
103 * transfer is not possible yet. So this delay allows us to suppress
104 * quite a bunch of spurious error messages in dmesg. Thus it's kept.
106 usleep_range(15000, 30000);
108 id
= i2c_smbus_read_byte_data(ec
->client
, ASPIRE_EC_EVENT
);
110 dev_err(&ec
->client
->dev
, "Failed to read event id: %pe\n", ERR_PTR(id
));
115 case 0x0: /* No event */
118 case ASPIRE_EC_EVENT_WATCHDOG
:
120 * Here acpi responds to the event and clears some bit.
121 * Notify (\_SB.I2C3.BAT1, 0x81) // Information Change
122 * Notify (\_SB.I2C3.ADP1, 0x80) // Status Change
124 aspire_ec_ram_read(ec
->client
, ASPIRE_EC_RAM_WATCHDOG
, &tmp
, sizeof(tmp
));
125 tmp
&= ~ASPIRE_EC_WATCHDOG_BIT
;
126 aspire_ec_ram_write(ec
->client
, ASPIRE_EC_RAM_WATCHDOG
, tmp
);
129 case ASPIRE_EC_EVENT_LID_CLOSE
:
130 /* Notify (\_SB.LID0, 0x80) // Status Change */
131 input_report_switch(ec
->idev
, SW_LID
, 1);
132 input_sync(ec
->idev
);
135 case ASPIRE_EC_EVENT_LID_OPEN
:
136 /* Notify (\_SB.LID0, 0x80) // Status Change */
137 input_report_switch(ec
->idev
, SW_LID
, 0);
138 input_sync(ec
->idev
);
141 case ASPIRE_EC_EVENT_FG_INF_CHG
:
142 /* Notify (\_SB.I2C3.BAT1, 0x81) // Information Change */
144 case ASPIRE_EC_EVENT_FG_STA_CHG
:
145 /* Notify (\_SB.I2C3.BAT1, 0x80) // Status Change */
146 power_supply_changed(ec
->bat_psy
);
147 power_supply_changed(ec
->adp_psy
);
150 case ASPIRE_EC_EVENT_HPD_DIS
:
151 if (ec
->bridge_configured
)
152 drm_bridge_hpd_notify(&ec
->bridge
, connector_status_disconnected
);
155 case ASPIRE_EC_EVENT_HPD_CON
:
156 if (ec
->bridge_configured
)
157 drm_bridge_hpd_notify(&ec
->bridge
, connector_status_connected
);
160 case ASPIRE_EC_EVENT_BKL_BLANKED
:
161 case ASPIRE_EC_EVENT_BKL_UNBLANKED
:
162 /* Display backlight blanked on FN+F6. No action needed. */
165 case ASPIRE_EC_EVENT_KBD_BKL_ON
:
166 case ASPIRE_EC_EVENT_KBD_BKL_OFF
:
168 * There is a keyboard backlight connector on Aspire 1 that is
169 * controlled by FN+F8. There is no kb backlight on the device though.
170 * Seems like this is used on other devices like Acer Spin 7.
176 dev_warn(&ec
->client
->dev
, "Unknown event id=0x%x\n", id
);
186 struct aspire_ec_bat_psy_static_data
{
190 __le16 voltage_design
;
191 __le16 capacity_full
;
198 static const char * const aspire_ec_bat_psy_battery_model
[] = {
206 static const char * const aspire_ec_bat_psy_battery_vendor
[] = {
219 struct aspire_ec_bat_psy_dynamic_data
{
230 static int aspire_ec_bat_psy_get_property(struct power_supply
*psy
,
231 enum power_supply_property psp
,
232 union power_supply_propval
*val
)
234 struct aspire_ec
*ec
= power_supply_get_drvdata(psy
);
235 struct aspire_ec_bat_psy_static_data sdat
;
236 struct aspire_ec_bat_psy_dynamic_data ddat
;
239 i2c_smbus_read_i2c_block_data(ec
->client
, ASPIRE_EC_FG_STATIC
, sizeof(sdat
), (u8
*)&sdat
);
240 i2c_smbus_read_i2c_block_data(ec
->client
, ASPIRE_EC_FG_DYNAMIC
, sizeof(ddat
), (u8
*)&ddat
);
243 case POWER_SUPPLY_PROP_STATUS
:
244 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
245 if (ddat
.flags
& ASPIRE_EC_FG_FLAG_CHARGING
)
246 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
247 else if (ddat
.flags
& ASPIRE_EC_FG_FLAG_DISCHARGING
)
248 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
249 else if (ddat
.flags
& ASPIRE_EC_FG_FLAG_FULL
)
250 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
253 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
254 val
->intval
= get_unaligned_le16(&ddat
.voltage_now
) * MILLI_TO_MICRO
;
257 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
258 val
->intval
= le16_to_cpu(sdat
.voltage_design
) * MILLI_TO_MICRO
;
261 case POWER_SUPPLY_PROP_CHARGE_NOW
:
262 val
->intval
= get_unaligned_le16(&ddat
.capacity_now
) * MILLI_TO_MICRO
;
265 case POWER_SUPPLY_PROP_CHARGE_FULL
:
266 val
->intval
= le16_to_cpu(sdat
.capacity_full
) * MILLI_TO_MICRO
;
269 case POWER_SUPPLY_PROP_CAPACITY
:
270 val
->intval
= get_unaligned_le16(&ddat
.capacity_now
) * 100;
271 val
->intval
/= le16_to_cpu(sdat
.capacity_full
);
274 case POWER_SUPPLY_PROP_CURRENT_NOW
:
275 val
->intval
= (s16
)get_unaligned_le16(&ddat
.current_now
) * MILLI_TO_MICRO
;
278 case POWER_SUPPLY_PROP_PRESENT
:
279 val
->intval
= !!(ddat
.flags
& ASPIRE_EC_FG_FLAG_PRESENT
);
282 case POWER_SUPPLY_PROP_SCOPE
:
283 val
->intval
= POWER_SUPPLY_SCOPE_SYSTEM
;
286 case POWER_SUPPLY_PROP_MODEL_NAME
:
287 str_index
= sdat
.model_id
- 1;
289 if (str_index
>= 0 && str_index
< ARRAY_SIZE(aspire_ec_bat_psy_battery_model
))
290 val
->strval
= aspire_ec_bat_psy_battery_model
[str_index
];
292 val
->strval
= "Unknown";
295 case POWER_SUPPLY_PROP_MANUFACTURER
:
296 str_index
= sdat
.vendor_id
- 3; /* ACPI uses 3 as an offset here. */
298 if (str_index
>= 0 && str_index
< ARRAY_SIZE(aspire_ec_bat_psy_battery_vendor
))
299 val
->strval
= aspire_ec_bat_psy_battery_vendor
[str_index
];
301 val
->strval
= "Unknown";
311 static enum power_supply_property aspire_ec_bat_psy_props
[] = {
312 POWER_SUPPLY_PROP_STATUS
,
313 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
314 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
315 POWER_SUPPLY_PROP_CHARGE_NOW
,
316 POWER_SUPPLY_PROP_CHARGE_FULL
,
317 POWER_SUPPLY_PROP_CAPACITY
,
318 POWER_SUPPLY_PROP_CURRENT_NOW
,
319 POWER_SUPPLY_PROP_PRESENT
,
320 POWER_SUPPLY_PROP_SCOPE
,
321 POWER_SUPPLY_PROP_MODEL_NAME
,
322 POWER_SUPPLY_PROP_MANUFACTURER
,
325 static const struct power_supply_desc aspire_ec_bat_psy_desc
= {
326 .name
= "aspire-ec-bat",
327 .type
= POWER_SUPPLY_TYPE_BATTERY
,
328 .get_property
= aspire_ec_bat_psy_get_property
,
329 .properties
= aspire_ec_bat_psy_props
,
330 .num_properties
= ARRAY_SIZE(aspire_ec_bat_psy_props
),
333 static int aspire_ec_adp_psy_get_property(struct power_supply
*psy
,
334 enum power_supply_property psp
,
335 union power_supply_propval
*val
)
337 struct aspire_ec
*ec
= power_supply_get_drvdata(psy
);
341 case POWER_SUPPLY_PROP_ONLINE
:
342 aspire_ec_ram_read(ec
->client
, ASPIRE_EC_RAM_ADP
, &tmp
, sizeof(tmp
));
343 val
->intval
= !!(tmp
& ASPIRE_EC_AC_STATUS
);
353 static enum power_supply_property aspire_ec_adp_psy_props
[] = {
354 POWER_SUPPLY_PROP_ONLINE
,
357 static const struct power_supply_desc aspire_ec_adp_psy_desc
= {
358 .name
= "aspire-ec-adp",
359 .type
= POWER_SUPPLY_TYPE_MAINS
,
360 .get_property
= aspire_ec_adp_psy_get_property
,
361 .properties
= aspire_ec_adp_psy_props
,
362 .num_properties
= ARRAY_SIZE(aspire_ec_adp_psy_props
),
366 * USB-C DP Alt mode HPD.
369 static int aspire_ec_bridge_attach(struct drm_bridge
*bridge
, enum drm_bridge_attach_flags flags
)
371 return flags
& DRM_BRIDGE_ATTACH_NO_CONNECTOR
? 0 : -EINVAL
;
374 static void aspire_ec_bridge_update_hpd_work(struct work_struct
*work
)
376 struct aspire_ec
*ec
= container_of(work
, struct aspire_ec
, work
);
379 aspire_ec_ram_read(ec
->client
, ASPIRE_EC_RAM_HPD_STATUS
, &tmp
, sizeof(tmp
));
380 if (tmp
== ASPIRE_EC_HPD_CONNECTED
)
381 drm_bridge_hpd_notify(&ec
->bridge
, connector_status_connected
);
383 drm_bridge_hpd_notify(&ec
->bridge
, connector_status_disconnected
);
386 static void aspire_ec_bridge_hpd_enable(struct drm_bridge
*bridge
)
388 struct aspire_ec
*ec
= container_of(bridge
, struct aspire_ec
, bridge
);
390 schedule_work(&ec
->work
);
393 static const struct drm_bridge_funcs aspire_ec_bridge_funcs
= {
394 .hpd_enable
= aspire_ec_bridge_hpd_enable
,
395 .attach
= aspire_ec_bridge_attach
,
402 static ssize_t
fn_lock_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
404 struct aspire_ec
*ec
= i2c_get_clientdata(to_i2c_client(dev
));
407 aspire_ec_ram_read(ec
->client
, ASPIRE_EC_RAM_KBD_MODE
, &tmp
, sizeof(tmp
));
409 return sysfs_emit(buf
, "%u\n", !(tmp
& ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP
));
412 static ssize_t
fn_lock_store(struct device
*dev
, struct device_attribute
*attr
,
413 const char *buf
, size_t count
)
415 struct aspire_ec
*ec
= i2c_get_clientdata(to_i2c_client(dev
));
421 ret
= kstrtobool(buf
, &state
);
425 aspire_ec_ram_read(ec
->client
, ASPIRE_EC_RAM_KBD_MODE
, &tmp
, sizeof(tmp
));
428 tmp
&= ~ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP
;
430 tmp
|= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP
;
432 aspire_ec_ram_write(ec
->client
, ASPIRE_EC_RAM_KBD_MODE
, tmp
);
437 static DEVICE_ATTR_RW(fn_lock
);
439 static struct attribute
*aspire_ec_attrs
[] = {
440 &dev_attr_fn_lock
.attr
,
443 ATTRIBUTE_GROUPS(aspire_ec
);
445 static int aspire_ec_probe(struct i2c_client
*client
)
447 struct power_supply_config psy_cfg
= {0};
448 struct device
*dev
= &client
->dev
;
449 struct fwnode_handle
*fwnode
;
450 struct aspire_ec
*ec
;
454 ec
= devm_kzalloc(dev
, sizeof(*ec
), GFP_KERNEL
);
459 i2c_set_clientdata(client
, ec
);
461 /* Battery status reports */
462 psy_cfg
.drv_data
= ec
;
463 ec
->bat_psy
= devm_power_supply_register(dev
, &aspire_ec_bat_psy_desc
, &psy_cfg
);
464 if (IS_ERR(ec
->bat_psy
))
465 return dev_err_probe(dev
, PTR_ERR(ec
->bat_psy
),
466 "Failed to register battery power supply\n");
468 ec
->adp_psy
= devm_power_supply_register(dev
, &aspire_ec_adp_psy_desc
, &psy_cfg
);
469 if (IS_ERR(ec
->adp_psy
))
470 return dev_err_probe(dev
, PTR_ERR(ec
->adp_psy
),
471 "Failed to register AC power supply\n");
474 ec
->idev
= devm_input_allocate_device(dev
);
478 ec
->idev
->name
= "aspire-ec";
479 ec
->idev
->phys
= "aspire-ec/input0";
480 input_set_capability(ec
->idev
, EV_SW
, SW_LID
);
482 ret
= input_register_device(ec
->idev
);
484 return dev_err_probe(dev
, ret
, "Input device register failed\n");
486 /* Enable the keyboard fn keys */
487 tmp
= ASPIRE_EC_RAM_KBD_FN_EN
| ASPIRE_EC_RAM_KBD_ALWAYS_SET
;
488 tmp
|= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP
;
489 aspire_ec_ram_write(client
, ASPIRE_EC_RAM_KBD_MODE
, tmp
);
491 aspire_ec_ram_read(client
, ASPIRE_EC_RAM_KBD_MODE_2
, &tmp
, sizeof(tmp
));
492 tmp
|= ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY
;
493 aspire_ec_ram_write(client
, ASPIRE_EC_RAM_KBD_MODE_2
, tmp
);
495 /* External Type-C display attach reports */
496 fwnode
= device_get_named_child_node(dev
, "connector");
498 INIT_WORK(&ec
->work
, aspire_ec_bridge_update_hpd_work
);
499 ec
->bridge
.funcs
= &aspire_ec_bridge_funcs
;
500 ec
->bridge
.of_node
= to_of_node(fwnode
);
501 ec
->bridge
.ops
= DRM_BRIDGE_OP_HPD
;
502 ec
->bridge
.type
= DRM_MODE_CONNECTOR_USB
;
504 ret
= devm_drm_bridge_add(dev
, &ec
->bridge
);
506 fwnode_handle_put(fwnode
);
507 return dev_err_probe(dev
, ret
, "Failed to register drm bridge\n");
510 ec
->bridge_configured
= true;
513 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
514 aspire_ec_irq_handler
, IRQF_ONESHOT
,
517 return dev_err_probe(dev
, ret
, "Failed to request irq\n");
522 static int aspire_ec_resume(struct device
*dev
)
524 struct aspire_ec
*ec
= i2c_get_clientdata(to_i2c_client(dev
));
527 aspire_ec_ram_read(ec
->client
, ASPIRE_EC_RAM_LID_STATUS
, &tmp
, sizeof(tmp
));
528 input_report_switch(ec
->idev
, SW_LID
, !!(tmp
& ASPIRE_EC_LID_OPEN
));
529 input_sync(ec
->idev
);
534 static const struct i2c_device_id aspire_ec_id
[] = {
538 MODULE_DEVICE_TABLE(i2c
, aspire_ec_id
);
540 static const struct of_device_id aspire_ec_of_match
[] = {
541 { .compatible
= "acer,aspire1-ec", },
544 MODULE_DEVICE_TABLE(of
, aspire_ec_of_match
);
546 static DEFINE_SIMPLE_DEV_PM_OPS(aspire_ec_pm_ops
, NULL
, aspire_ec_resume
);
548 static struct i2c_driver aspire_ec_driver
= {
551 .of_match_table
= aspire_ec_of_match
,
552 .pm
= pm_sleep_ptr(&aspire_ec_pm_ops
),
553 .dev_groups
= aspire_ec_groups
,
555 .probe
= aspire_ec_probe
,
556 .id_table
= aspire_ec_id
,
558 module_i2c_driver(aspire_ec_driver
);
560 MODULE_DESCRIPTION("Acer Aspire 1 embedded controller");
561 MODULE_AUTHOR("Nikita Travkin <nikita@trvn.ru>");
562 MODULE_LICENSE("GPL");