1 // SPDX-License-Identifier: GPL-2.0+
3 * Supports for the power IC on the Surface 3 tablet.
5 * (C) Copyright 2016-2018 Red Hat, Inc
6 * (C) Copyright 2016-2018 Benjamin Tissoires <benjamin.tissoires@gmail.com>
7 * (C) Copyright 2016 Stephen Just <stephenjust@gmail.com>
9 * This driver has been reverse-engineered by parsing the DSDT of the Surface 3
10 * and looking at the registers of the chips.
12 * The DSDT allowed to find out that:
13 * - the driver is required for the ACPI BAT0 device to communicate to the chip
14 * through an operation region.
15 * - the various defines for the operation region functions to communicate with
17 * - the DSM 3f99e367-6220-4955-8b0f-06ef2ae79412 allows to trigger ACPI
18 * events to BAT0 (the code is all available in the DSDT).
20 * Further findings regarding the 2 chips declared in the MSHW0011 are:
21 * - there are 2 chips declared:
22 * . 0x22 seems to control the ADP1 line status (and probably the charger)
23 * . 0x55 controls the battery directly
24 * - the battery chip uses a SMBus protocol (using plain SMBus allows non
25 * destructive commands):
26 * . the commands/registers used are in the range 0x00..0x7F
27 * . if bit 8 (0x80) is set in the SMBus command, the returned value is the
28 * same as when it is not set. There is a high chance this bit is the
30 * . the various registers semantic as been deduced by observing the register
34 #include <linux/acpi.h>
35 #include <linux/bits.h>
36 #include <linux/freezer.h>
37 #include <linux/i2c.h>
38 #include <linux/kernel.h>
39 #include <linux/kthread.h>
40 #include <linux/slab.h>
41 #include <linux/types.h>
42 #include <linux/uuid.h>
43 #include <asm/unaligned.h>
45 #define SURFACE_3_POLL_INTERVAL (2 * HZ)
46 #define SURFACE_3_STRLEN 10
48 struct mshw0011_data {
49 struct i2c_client *adp1;
50 struct i2c_client *bat0;
51 unsigned short notify_mask;
52 struct task_struct *poll_task;
61 struct mshw0011_handler_data {
62 struct acpi_connection_info info;
63 struct i2c_client *client;
70 u32 last_full_charg_capacity;
71 u32 battery_technology;
73 u32 design_capacity_of_warning;
74 u32 design_capacity_of_low;
76 u32 measurement_accuracy;
77 u32 max_sampling_time;
78 u32 min_sampling_time;
79 u32 max_average_interval;
80 u32 min_average_interval;
81 u32 battery_capacity_granularity_1;
82 u32 battery_capacity_granularity_2;
83 char model[SURFACE_3_STRLEN];
84 char serial[SURFACE_3_STRLEN];
85 char type[SURFACE_3_STRLEN];
86 char OEM[SURFACE_3_STRLEN];
91 s32 battery_present_rate;
92 u32 battery_remaining_capacity;
93 u32 battery_present_voltage;
107 struct gsb_command cmd;
113 #define ACPI_BATTERY_STATE_DISCHARGING BIT(0)
114 #define ACPI_BATTERY_STATE_CHARGING BIT(1)
115 #define ACPI_BATTERY_STATE_CRITICAL BIT(2)
117 #define MSHW0011_CMD_DEST_BAT0 0x01
118 #define MSHW0011_CMD_DEST_ADP1 0x03
120 #define MSHW0011_CMD_BAT0_STA 0x01
121 #define MSHW0011_CMD_BAT0_BIX 0x02
122 #define MSHW0011_CMD_BAT0_BCT 0x03
123 #define MSHW0011_CMD_BAT0_BTM 0x04
124 #define MSHW0011_CMD_BAT0_BST 0x05
125 #define MSHW0011_CMD_BAT0_BTP 0x06
126 #define MSHW0011_CMD_ADP1_PSR 0x07
127 #define MSHW0011_CMD_BAT0_PSOC 0x09
128 #define MSHW0011_CMD_BAT0_PMAX 0x0a
129 #define MSHW0011_CMD_BAT0_PSRC 0x0b
130 #define MSHW0011_CMD_BAT0_CHGI 0x0c
131 #define MSHW0011_CMD_BAT0_ARTG 0x0d
133 #define MSHW0011_NOTIFY_GET_VERSION 0x00
134 #define MSHW0011_NOTIFY_ADP1 0x01
135 #define MSHW0011_NOTIFY_BAT0_BST 0x02
136 #define MSHW0011_NOTIFY_BAT0_BIX 0x05
138 #define MSHW0011_ADP1_REG_PSR 0x04
140 #define MSHW0011_BAT0_REG_CAPACITY 0x0c
141 #define MSHW0011_BAT0_REG_FULL_CHG_CAPACITY 0x0e
142 #define MSHW0011_BAT0_REG_DESIGN_CAPACITY 0x40
143 #define MSHW0011_BAT0_REG_VOLTAGE 0x08
144 #define MSHW0011_BAT0_REG_RATE 0x14
145 #define MSHW0011_BAT0_REG_OEM 0x45
146 #define MSHW0011_BAT0_REG_TYPE 0x4e
147 #define MSHW0011_BAT0_REG_SERIAL_NO 0x56
148 #define MSHW0011_BAT0_REG_CYCLE_CNT 0x6e
150 #define MSHW0011_EV_2_5_MASK GENMASK(8, 0)
152 /* 3f99e367-6220-4955-8b0f-06ef2ae79412 */
153 static const guid_t mshw0011_guid =
154 GUID_INIT(0x3F99E367, 0x6220, 0x4955, 0x8B, 0x0F, 0x06, 0xEF,
155 0x2A, 0xE7, 0x94, 0x12);
158 mshw0011_notify(struct mshw0011_data *cdata, u8 arg1, u8 arg2,
159 unsigned int *ret_value)
161 union acpi_object *obj;
162 struct acpi_device *adev;
166 handle = ACPI_HANDLE(&cdata->adp1->dev);
167 if (!handle || acpi_bus_get_device(handle, &adev))
170 obj = acpi_evaluate_dsm_typed(handle, &mshw0011_guid, arg1, arg2, NULL,
173 dev_err(&cdata->adp1->dev, "device _DSM execution failed\n");
178 for (i = 0; i < obj->buffer.length; i++)
179 *ret_value |= obj->buffer.pointer[i] << (i * 8);
185 static const struct bix default_bix = {
188 .design_capacity = 0x1dca,
189 .last_full_charg_capacity = 0x1dca,
190 .battery_technology = 0x01,
191 .design_voltage = 0x10df,
192 .design_capacity_of_warning = 0x8f,
193 .design_capacity_of_low = 0x47,
194 .cycle_count = 0xffffffff,
195 .measurement_accuracy = 0x00015f90,
196 .max_sampling_time = 0x03e8,
197 .min_sampling_time = 0x03e8,
198 .max_average_interval = 0x03e8,
199 .min_average_interval = 0x03e8,
200 .battery_capacity_granularity_1 = 0x45,
201 .battery_capacity_granularity_2 = 0x11,
208 static int mshw0011_bix(struct mshw0011_data *cdata, struct bix *bix)
210 struct i2c_client *client = cdata->bat0;
211 char buf[SURFACE_3_STRLEN];
216 /* get design capacity */
217 ret = i2c_smbus_read_word_data(client,
218 MSHW0011_BAT0_REG_DESIGN_CAPACITY);
220 dev_err(&client->dev, "Error reading design capacity: %d\n",
224 bix->design_capacity = ret;
226 /* get last full charge capacity */
227 ret = i2c_smbus_read_word_data(client,
228 MSHW0011_BAT0_REG_FULL_CHG_CAPACITY);
230 dev_err(&client->dev,
231 "Error reading last full charge capacity: %d\n", ret);
234 bix->last_full_charg_capacity = ret;
237 * Get serial number, on some devices (with unofficial replacement
238 * battery?) reading any of the serial number range addresses gets
239 * nacked in this case just leave the serial number empty.
241 ret = i2c_smbus_read_i2c_block_data(client, MSHW0011_BAT0_REG_SERIAL_NO,
243 if (ret == -EREMOTEIO) {
244 /* no serial number available */
245 } else if (ret != sizeof(buf)) {
246 dev_err(&client->dev, "Error reading serial no: %d\n", ret);
249 snprintf(bix->serial, ARRAY_SIZE(bix->serial), "%3pE%6pE", buf + 7, buf);
252 /* get cycle count */
253 ret = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_CYCLE_CNT);
255 dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
258 bix->cycle_count = ret;
261 ret = i2c_smbus_read_i2c_block_data(client, MSHW0011_BAT0_REG_OEM,
264 dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
267 snprintf(bix->OEM, ARRAY_SIZE(bix->OEM), "%3pE", buf);
272 static int mshw0011_bst(struct mshw0011_data *cdata, struct bst *bst)
274 struct i2c_client *client = cdata->bat0;
275 int rate, capacity, voltage, state;
278 rate = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_RATE);
282 capacity = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_CAPACITY);
286 voltage = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_VOLTAGE);
291 bst->battery_present_rate = abs((s32)tmp);
295 state |= ACPI_BATTERY_STATE_CHARGING;
296 else if ((s32) tmp < 0)
297 state |= ACPI_BATTERY_STATE_DISCHARGING;
298 bst->battery_state = state;
300 bst->battery_remaining_capacity = capacity;
301 bst->battery_present_voltage = voltage;
306 static int mshw0011_adp_psr(struct mshw0011_data *cdata)
308 return i2c_smbus_read_byte_data(cdata->adp1, MSHW0011_ADP1_REG_PSR);
311 static int mshw0011_isr(struct mshw0011_data *cdata)
316 bool status, bat_status;
318 ret = mshw0011_adp_psr(cdata);
323 if (status != cdata->charging)
324 mshw0011_notify(cdata, cdata->notify_mask,
325 MSHW0011_NOTIFY_ADP1, &ret);
327 cdata->charging = status;
329 ret = mshw0011_bst(cdata, &bst);
333 bat_status = bst.battery_state;
334 if (bat_status != cdata->bat_charging)
335 mshw0011_notify(cdata, cdata->notify_mask,
336 MSHW0011_NOTIFY_BAT0_BST, &ret);
338 cdata->bat_charging = bat_status;
340 ret = mshw0011_bix(cdata, &bix);
344 if (bix.last_full_charg_capacity != cdata->full_capacity)
345 mshw0011_notify(cdata, cdata->notify_mask,
346 MSHW0011_NOTIFY_BAT0_BIX, &ret);
348 cdata->full_capacity = bix.last_full_charg_capacity;
353 static int mshw0011_poll_task(void *data)
355 struct mshw0011_data *cdata = data;
358 cdata->kthread_running = true;
362 while (!kthread_should_stop()) {
363 schedule_timeout_interruptible(SURFACE_3_POLL_INTERVAL);
365 ret = mshw0011_isr(data);
370 cdata->kthread_running = false;
375 mshw0011_space_handler(u32 function, acpi_physical_address command,
376 u32 bits, u64 *value64,
377 void *handler_context, void *region_context)
379 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
380 struct mshw0011_handler_data *data = handler_context;
381 struct acpi_connection_info *info = &data->info;
382 struct acpi_resource_i2c_serialbus *sb;
383 struct i2c_client *client = data->client;
384 struct mshw0011_data *cdata = i2c_get_clientdata(client);
385 struct acpi_resource *ares;
386 u32 accessor_type = function >> 16;
390 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
391 if (ACPI_FAILURE(ret))
394 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
395 ret = AE_BAD_PARAMETER;
399 sb = &ares->data.i2c_serial_bus;
400 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
401 ret = AE_BAD_PARAMETER;
405 if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
406 ret = AE_BAD_PARAMETER;
410 if (gsb->cmd.arg0 == MSHW0011_CMD_DEST_ADP1 &&
411 gsb->cmd.arg1 == MSHW0011_CMD_ADP1_PSR) {
412 status = mshw0011_adp_psr(cdata);
422 if (gsb->cmd.arg0 != MSHW0011_CMD_DEST_BAT0) {
423 ret = AE_BAD_PARAMETER;
427 switch (gsb->cmd.arg1) {
428 case MSHW0011_CMD_BAT0_STA:
430 case MSHW0011_CMD_BAT0_BIX:
431 ret = mshw0011_bix(cdata, &gsb->bix);
433 case MSHW0011_CMD_BAT0_BTP:
434 cdata->trip_point = gsb->cmd.arg2;
436 case MSHW0011_CMD_BAT0_BST:
437 ret = mshw0011_bst(cdata, &gsb->bst);
440 dev_info(&cdata->bat0->dev, "command(0x%02x) is not supported.\n", gsb->cmd.arg1);
441 ret = AE_BAD_PARAMETER;
454 static int mshw0011_install_space_handler(struct i2c_client *client)
457 struct mshw0011_handler_data *data;
460 handle = ACPI_HANDLE(&client->dev);
464 data = kzalloc(sizeof(struct mshw0011_handler_data),
469 data->client = client;
470 status = acpi_bus_attach_private_data(handle, (void *)data);
471 if (ACPI_FAILURE(status)) {
476 status = acpi_install_address_space_handler(handle,
477 ACPI_ADR_SPACE_GSBUS,
478 &mshw0011_space_handler,
481 if (ACPI_FAILURE(status)) {
482 dev_err(&client->dev, "Error installing i2c space handler\n");
483 acpi_bus_detach_private_data(handle);
488 acpi_walk_dep_device_list(handle);
492 static void mshw0011_remove_space_handler(struct i2c_client *client)
494 struct mshw0011_handler_data *data;
498 handle = ACPI_HANDLE(&client->dev);
502 acpi_remove_address_space_handler(handle,
503 ACPI_ADR_SPACE_GSBUS,
504 &mshw0011_space_handler);
506 status = acpi_bus_get_private_data(handle, (void **)&data);
507 if (ACPI_SUCCESS(status))
510 acpi_bus_detach_private_data(handle);
513 static int mshw0011_probe(struct i2c_client *client)
515 struct i2c_board_info board_info;
516 struct device *dev = &client->dev;
517 struct i2c_client *bat0;
518 struct mshw0011_data *data;
521 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
526 i2c_set_clientdata(client, data);
528 memset(&board_info, 0, sizeof(board_info));
529 strlcpy(board_info.type, "MSHW0011-bat0", I2C_NAME_SIZE);
531 bat0 = i2c_acpi_new_device(dev, 1, &board_info);
533 return PTR_ERR(bat0);
536 i2c_set_clientdata(bat0, data);
538 error = mshw0011_notify(data, 1, MSHW0011_NOTIFY_GET_VERSION, &mask);
542 data->notify_mask = mask == MSHW0011_EV_2_5_MASK;
544 data->poll_task = kthread_run(mshw0011_poll_task, data, "mshw0011_adp");
545 if (IS_ERR(data->poll_task)) {
546 error = PTR_ERR(data->poll_task);
547 dev_err(&client->dev, "Unable to run kthread err %d\n", error);
551 error = mshw0011_install_space_handler(client);
558 if (data->kthread_running)
559 kthread_stop(data->poll_task);
560 i2c_unregister_device(data->bat0);
564 static int mshw0011_remove(struct i2c_client *client)
566 struct mshw0011_data *cdata = i2c_get_clientdata(client);
568 mshw0011_remove_space_handler(client);
570 if (cdata->kthread_running)
571 kthread_stop(cdata->poll_task);
573 i2c_unregister_device(cdata->bat0);
578 static const struct acpi_device_id mshw0011_acpi_match[] = {
582 MODULE_DEVICE_TABLE(acpi, mshw0011_acpi_match);
584 static struct i2c_driver mshw0011_driver = {
585 .probe_new = mshw0011_probe,
586 .remove = mshw0011_remove,
589 .acpi_match_table = mshw0011_acpi_match,
592 module_i2c_driver(mshw0011_driver);
594 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
595 MODULE_DESCRIPTION("mshw0011 driver");
596 MODULE_LICENSE("GPL v2");