Skip to contentSkip to conversation

platform/x86: surface3_power: MSHW0011 rev-eng implementation

PATCH In review Benjamin Tissoires 2 participants 2 messages Aug 31, 2018 – Sep 26, 2018 x86 platform drivers

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 platform/x86: surface3_power: MSHW0011 rev-eng implementation

The MSHW0011 device is a chip that replaces the battery firmware by using ACPI operation regions on the Surface 3.
It is unclear whether or not the chip will be reused somewhere else (under Windows, the chip is called "Surface Platform Power Driver" and the driver is provided by Microsoft).

The values have been obtained by reverse engineering, and are subject to errors. Looks like it works on overall pretty well.

I couldn't manage to get the IRQ correctly triggered, so I am using a good old polling thread to check for changes. This is something to be fixed in a later version.

---

changes in v2:
- moved to drivers/acpi/ instead of power
- use uuid_le
- fix uper/lower case
- print_hex_dump() used in mshw0011_dump_registers() instead of custom dump
- removed "MSHW0011:00" in mshw0011_id

changes in v3:
- remove le16_to_cpu() after i2c_smbus_read_word_data() as it already handles it properly
- use i2c_acpi_new_device() to remove a bunch of duplicated code from I2C core.
- move the driver in platform/x86
- add depends ACPI && I2C in Kconfig
- remove the dump registers facility, as it's debugging only
- use of BIT() in MSHW0011 defs
- use of guid_t
- remove useless ret = 0
- use probe_new()
- remove empty .id_table
- removed mshw0011_i2c_read_block() and use directly i2c_smbus_read_i2c_block_data()
- use snprintf() instead of custom memcpy()
- renamed the 'version' as 'mask'
- use SPDX license identifier

---
 drivers/platform/x86/Kconfig          |   7 +
 drivers/platform/x86/Makefile         |   1 +
 drivers/platform/x86/surface3_power.c | 604 ++++++++++++++++++++++++++
 3 files changed, 612 insertions(+)
 create mode 100644 drivers/platform/x86/surface3_power.c
drivers/platform/x86/Kconfig +7 -0
index ac4d48830415..c254f7aadbe1 100644
@@ -1158,6 +1158,13 @@ config SURFACE_3_BUTTON
 	---help---
 	  This driver handles the power/home/volume buttons on the Microsoft Surface 3 tablet.
 
+config SURFACE_3_POWER_OPREGION
+	tristate "Surface 3 battery platform operation region support"
+	depends on ACPI && I2C
+	help
+	  Select this option to enable support for ACPI operation
+	  region of the Surface 3 battery platform driver.
+
 config INTEL_PUNIT_IPC
 	tristate "Intel P-Unit IPC Driver"
 	---help---
drivers/platform/x86/Makefile +1 -0
index 2ba6cb795338..2f683597af9f 100644
@@ -81,6 +81,7 @@ obj-$(CONFIG_INTEL_PMC_IPC)	+= intel_pmc_ipc.o
 obj-$(CONFIG_SILEAD_DMI)	+= silead_dmi.o
 obj-$(CONFIG_SURFACE_PRO3_BUTTON)	+= surfacepro3_button.o
 obj-$(CONFIG_SURFACE_3_BUTTON)	+= surface3_button.o
+obj-$(CONFIG_SURFACE_3_POWER_OPREGION) += surface3_power.o
 obj-$(CONFIG_INTEL_PUNIT_IPC)  += intel_punit_ipc.o
 obj-$(CONFIG_INTEL_BXTWC_PMIC_TMU)	+= intel_bxtwc_tmu.o
 obj-$(CONFIG_INTEL_TELEMETRY)	+= intel_telemetry_core.o \
drivers/platform/x86/surface3_power.c new file +604 -0

604 lines not on this page · raw message

Signature · 1 line
2.17.0
#1 lore raw
Quoted 7 lines · Benjamin Tissoires

The MSHW0011 device is a chip that replaces the battery firmware by using ACPI operation regions on the Surface 3.
It is unclear whether or not the chip will be reused somewhere else (under Windows, the chip is called "Surface Platform Power Driver" and the driver is provided by Microsoft).

Quoted 6 lines

The values have been obtained by reverse engineering, and are subject to errors. Looks like it works on overall pretty well.

I couldn't manage to get the IRQ correctly triggered, so I am using a good old polling thread to check for changes. This is something to be fixed in a later version.

I have pushed to my review and testing queue for bots playing with it! But below my review.

Quoted 2 lines

Remove this blank line.

drivers/platform/x86/surface3_power.c line 2

Quoted 3 lines
drivers/platform/x86/surface3_power.c new file +2 -0
+// SPDX-License-Identifier: GPL-2.0+
 
+

No need for this blank line either.

Quoted 8 lines

+/*
+ * Supports for the power IC on the Surface 3 tablet.
+ *
+ * (C) Copyright 2016-2018 Red Hat, Inc
+ * (C) Copyright 2016-2018 Benjamin Tissoires <[email protected]>
+ * (C) Copyright 2016 Stephen Just <[email protected]>

+ *

...and for this.

Quoted 3 lines

+ */

+#include <asm/unaligned.h>

I'm pretty sure this should go after linux/*.h ones

drivers/platform/x86/surface3_power.c line 94

Quoted 13 lines
drivers/platform/x86/surface3_power.c new file +11 -0
+#include <linux/acpi.h>
+#include <linux/freezer.h>
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/kthread.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
 
 
+       char    model[10];
+       char    serial[10];
+       char    type[10];
+       char    OEM[10];

These 10:s is something worth to define:

#define SURFACE_3_STRLEN 10

or alike.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+

One blank line is enough

Quoted 1 line

+#define MSHW0011_EV_2_5 0x1ff

Is it mask? Value? Some threshold?

drivers/platform/x86/surface3_power.c line 167

Quoted 3 lines
drivers/platform/x86/surface3_power.c new file +3 -0
+       static const guid_t mshw0011_guid =
+               GUID_INIT(0x3F99E367, 0x6220, 0x4955,
+                         0x8B, 0x0F, 0x06, 0xEF, 0x2A, 0xE7, 0x94, 0x12);

Can you move this outside of the function?

Quoted 1 line

+ char buf[10];

Same definition as I mentioned above?

drivers/platform/x86/surface3_power.c line 245

Quoted 3 lines
drivers/platform/x86/surface3_power.c new file +3 -0
+       /* get serial number */
+       ret = i2c_smbus_read_i2c_block_data(client, MSHW0011_BAT0_REG_SERIAL_NO,
+                                           10, buf);

10 -> sizeof() ?

Quoted 1 line

+ if (ret != 10) {

Ditto.

drivers/platform/x86/surface3_power.c line 251

Quoted 6 lines
drivers/platform/x86/surface3_power.c new file +5 -0
+               dev_err(&client->dev, "Error reading serial no: %d\n", ret);
+               return ret;
+       }
 
+       snprintf(bix->serial, ARRAY_SIZE(bix->serial),
+                "%*pE%*pE", 3, buf + 7, 6, buf);

It would be one line with format string as "%3pE%6pE" followed by two pointers.

drivers/platform/x86/surface3_power.c line 264

Quoted 4 lines
drivers/platform/x86/surface3_power.c new file +4 -0
+       /* get OEM name */
+       ret = i2c_smbus_read_i2c_block_data(client, MSHW0011_BAT0_REG_OEM,
+                                           4, buf);
+       if (ret != 4) {

4 to be defined? Or separate buffer?

drivers/platform/x86/surface3_power.c line 268

Quoted 4 lines
drivers/platform/x86/surface3_power.c new file +4 -0
+               dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
+               return ret;
+       }
+       snprintf(bix->OEM, ARRAY_SIZE(bix->OEM), "%*pE", 3, buf);

Depending on the above it might be either "%3pE" or '...sizeof(buf) - 1, buf...'.

drivers/platform/x86/surface3_power.c line 331

Quoted 3 lines
drivers/platform/x86/surface3_power.c new file +2 -0
+       status = ret;
 
+

No need to have this blank line.

drivers/platform/x86/surface3_power.c line 343

Quoted 7 lines
drivers/platform/x86/surface3_power.c new file +5 -0
+       if (status != cdata->charging)
+               mshw0011_notify(cdata, cdata->notify_mask,
+                               MSHW0011_NOTIFY_ADP1, &ret);
 
+       bat_status = bst.battery_state;
 
+

Ditto.

drivers/platform/x86/surface3_power.c line 352

Quoted 7 lines
drivers/platform/x86/surface3_power.c new file +6 -0
+       if (bat_status != cdata->bat_charging)
+               mshw0011_notify(cdata, cdata->notify_mask,
+                               MSHW0011_NOTIFY_BAT0_BST, &ret);
 
+       ret = mshw0011_bix(cdata, &bix);
+       if (ret < 0)
+               return ret;

Unlike here, would be nice to add a blank line.

Quoted 4 lines
drivers/platform/x86/surface3_power.c new file +3 -0
+       if (bix.last_full_charg_capacity != cdata->full_capacity)
+               mshw0011_notify(cdata, cdata->notify_mask,
+                               MSHW0011_NOTIFY_BAT0_BIX, &ret);
 

drivers/platform/x86/surface3_power.c line 412

Quoted 10 lines
drivers/platform/x86/surface3_power.c new file +10 -0
+       if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
+               ret = AE_BAD_PARAMETER;
+               goto err;
+       }
+
+       sb = &ares->data.i2c_serial_bus;
+       if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
+               ret = AE_BAD_PARAMETER;
+               goto err;
+       }

Side note: It would be a good candidate to use my (not yet upstreamed) helper.

drivers/platform/x86/surface3_power.c line 468

Quoted 3 lines
drivers/platform/x86/surface3_power.c new file +2 -0
+       handle = ACPI_HANDLE(&client->dev);
 
+

Remove this blank line.

drivers/platform/x86/surface3_power.c line 505

Quoted 9 lines
drivers/platform/x86/surface3_power.c new file +8 -0
+       if (!handle)
+               return -ENODEV;
 
+static void mshw0011_remove_space_handler(struct i2c_client *client)
+{
+       acpi_handle handle = ACPI_HANDLE(&client->dev);
+       struct mshw0011_handler_data *data;
+       acpi_status status;
+

I would prefer to see handle assignment here followed by immediate check.

drivers/platform/x86/surface3_power.c line 525

Quoted 8 lines
drivers/platform/x86/surface3_power.c new file +6 -0
+       if (!handle)
+               return;
 
+       struct i2c_board_info board_info;
+       struct device *dev = &client->dev;
+       struct i2c_client *bat0;
 
+

Redundant.

drivers/platform/x86/surface3_power.c line 550

Quoted 4 lines
drivers/platform/x86/surface3_power.c new file +3 -0
+       struct mshw0011_data *data;
+       int error, mask;
 
+       data->notify_mask = mask == MSHW0011_EV_2_5;

Since it's a mask, append _MASK and use GENMASK() above.

Signature · 2 lines
With Best Regards,
Andy Shevchenko