surface3_power.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Supports for the power IC on the Surface 3 tablet.
  4. *
  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>
  8. *
  9. * This driver has been reverse-engineered by parsing the DSDT of the Surface 3
  10. * and looking at the registers of the chips.
  11. *
  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
  16. * this driver
  17. * - the DSM 3f99e367-6220-4955-8b0f-06ef2ae79412 allows to trigger ACPI
  18. * events to BAT0 (the code is all available in the DSDT).
  19. *
  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
  29. * read/write
  30. * . the various registers semantic as been deduced by observing the register
  31. * dumps.
  32. */
  33. #include <linux/acpi.h>
  34. #include <linux/bits.h>
  35. #include <linux/freezer.h>
  36. #include <linux/i2c.h>
  37. #include <linux/kernel.h>
  38. #include <linux/kthread.h>
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <linux/uuid.h>
  42. #include <asm/unaligned.h>
  43. #define SURFACE_3_POLL_INTERVAL (2 * HZ)
  44. #define SURFACE_3_STRLEN 10
  45. struct mshw0011_data {
  46. struct i2c_client *adp1;
  47. struct i2c_client *bat0;
  48. unsigned short notify_mask;
  49. struct task_struct *poll_task;
  50. bool kthread_running;
  51. bool charging;
  52. bool bat_charging;
  53. u8 trip_point;
  54. s32 full_capacity;
  55. };
  56. struct mshw0011_handler_data {
  57. struct acpi_connection_info info;
  58. struct i2c_client *client;
  59. };
  60. struct bix {
  61. u32 revision;
  62. u32 power_unit;
  63. u32 design_capacity;
  64. u32 last_full_charg_capacity;
  65. u32 battery_technology;
  66. u32 design_voltage;
  67. u32 design_capacity_of_warning;
  68. u32 design_capacity_of_low;
  69. u32 cycle_count;
  70. u32 measurement_accuracy;
  71. u32 max_sampling_time;
  72. u32 min_sampling_time;
  73. u32 max_average_interval;
  74. u32 min_average_interval;
  75. u32 battery_capacity_granularity_1;
  76. u32 battery_capacity_granularity_2;
  77. char model[SURFACE_3_STRLEN];
  78. char serial[SURFACE_3_STRLEN];
  79. char type[SURFACE_3_STRLEN];
  80. char OEM[SURFACE_3_STRLEN];
  81. } __packed;
  82. struct bst {
  83. u32 battery_state;
  84. s32 battery_present_rate;
  85. u32 battery_remaining_capacity;
  86. u32 battery_present_voltage;
  87. } __packed;
  88. struct gsb_command {
  89. u8 arg0;
  90. u8 arg1;
  91. u8 arg2;
  92. } __packed;
  93. struct gsb_buffer {
  94. u8 status;
  95. u8 len;
  96. u8 ret;
  97. union {
  98. struct gsb_command cmd;
  99. struct bst bst;
  100. struct bix bix;
  101. } __packed;
  102. } __packed;
  103. #define ACPI_BATTERY_STATE_DISCHARGING BIT(0)
  104. #define ACPI_BATTERY_STATE_CHARGING BIT(1)
  105. #define ACPI_BATTERY_STATE_CRITICAL BIT(2)
  106. #define MSHW0011_CMD_DEST_BAT0 0x01
  107. #define MSHW0011_CMD_DEST_ADP1 0x03
  108. #define MSHW0011_CMD_BAT0_STA 0x01
  109. #define MSHW0011_CMD_BAT0_BIX 0x02
  110. #define MSHW0011_CMD_BAT0_BCT 0x03
  111. #define MSHW0011_CMD_BAT0_BTM 0x04
  112. #define MSHW0011_CMD_BAT0_BST 0x05
  113. #define MSHW0011_CMD_BAT0_BTP 0x06
  114. #define MSHW0011_CMD_ADP1_PSR 0x07
  115. #define MSHW0011_CMD_BAT0_PSOC 0x09
  116. #define MSHW0011_CMD_BAT0_PMAX 0x0a
  117. #define MSHW0011_CMD_BAT0_PSRC 0x0b
  118. #define MSHW0011_CMD_BAT0_CHGI 0x0c
  119. #define MSHW0011_CMD_BAT0_ARTG 0x0d
  120. #define MSHW0011_NOTIFY_GET_VERSION 0x00
  121. #define MSHW0011_NOTIFY_ADP1 0x01
  122. #define MSHW0011_NOTIFY_BAT0_BST 0x02
  123. #define MSHW0011_NOTIFY_BAT0_BIX 0x05
  124. #define MSHW0011_ADP1_REG_PSR 0x04
  125. #define MSHW0011_BAT0_REG_CAPACITY 0x0c
  126. #define MSHW0011_BAT0_REG_FULL_CHG_CAPACITY 0x0e
  127. #define MSHW0011_BAT0_REG_DESIGN_CAPACITY 0x40
  128. #define MSHW0011_BAT0_REG_VOLTAGE 0x08
  129. #define MSHW0011_BAT0_REG_RATE 0x14
  130. #define MSHW0011_BAT0_REG_OEM 0x45
  131. #define MSHW0011_BAT0_REG_TYPE 0x4e
  132. #define MSHW0011_BAT0_REG_SERIAL_NO 0x56
  133. #define MSHW0011_BAT0_REG_CYCLE_CNT 0x6e
  134. #define MSHW0011_EV_2_5_MASK GENMASK(8, 0)
  135. /* 3f99e367-6220-4955-8b0f-06ef2ae79412 */
  136. static const guid_t mshw0011_guid =
  137. GUID_INIT(0x3F99E367, 0x6220, 0x4955, 0x8B, 0x0F, 0x06, 0xEF,
  138. 0x2A, 0xE7, 0x94, 0x12);
  139. static int
  140. mshw0011_notify(struct mshw0011_data *cdata, u8 arg1, u8 arg2,
  141. unsigned int *ret_value)
  142. {
  143. union acpi_object *obj;
  144. struct acpi_device *adev;
  145. acpi_handle handle;
  146. unsigned int i;
  147. handle = ACPI_HANDLE(&cdata->adp1->dev);
  148. if (!handle || acpi_bus_get_device(handle, &adev))
  149. return -ENODEV;
  150. obj = acpi_evaluate_dsm_typed(handle, &mshw0011_guid, arg1, arg2, NULL,
  151. ACPI_TYPE_BUFFER);
  152. if (!obj) {
  153. dev_err(&cdata->adp1->dev, "device _DSM execution failed\n");
  154. return -ENODEV;
  155. }
  156. *ret_value = 0;
  157. for (i = 0; i < obj->buffer.length; i++)
  158. *ret_value |= obj->buffer.pointer[i] << (i * 8);
  159. ACPI_FREE(obj);
  160. return 0;
  161. }
  162. static const struct bix default_bix = {
  163. .revision = 0x00,
  164. .power_unit = 0x01,
  165. .design_capacity = 0x1dca,
  166. .last_full_charg_capacity = 0x1dca,
  167. .battery_technology = 0x01,
  168. .design_voltage = 0x10df,
  169. .design_capacity_of_warning = 0x8f,
  170. .design_capacity_of_low = 0x47,
  171. .cycle_count = 0xffffffff,
  172. .measurement_accuracy = 0x00015f90,
  173. .max_sampling_time = 0x03e8,
  174. .min_sampling_time = 0x03e8,
  175. .max_average_interval = 0x03e8,
  176. .min_average_interval = 0x03e8,
  177. .battery_capacity_granularity_1 = 0x45,
  178. .battery_capacity_granularity_2 = 0x11,
  179. .model = "P11G8M",
  180. .serial = "",
  181. .type = "LION",
  182. .OEM = "",
  183. };
  184. static int mshw0011_bix(struct mshw0011_data *cdata, struct bix *bix)
  185. {
  186. struct i2c_client *client = cdata->bat0;
  187. char buf[SURFACE_3_STRLEN];
  188. int ret;
  189. *bix = default_bix;
  190. /* get design capacity */
  191. ret = i2c_smbus_read_word_data(client,
  192. MSHW0011_BAT0_REG_DESIGN_CAPACITY);
  193. if (ret < 0) {
  194. dev_err(&client->dev, "Error reading design capacity: %d\n",
  195. ret);
  196. return ret;
  197. }
  198. bix->design_capacity = ret;
  199. /* get last full charge capacity */
  200. ret = i2c_smbus_read_word_data(client,
  201. MSHW0011_BAT0_REG_FULL_CHG_CAPACITY);
  202. if (ret < 0) {
  203. dev_err(&client->dev,
  204. "Error reading last full charge capacity: %d\n", ret);
  205. return ret;
  206. }
  207. bix->last_full_charg_capacity = ret;
  208. /*
  209. * Get serial number, on some devices (with unofficial replacement
  210. * battery?) reading any of the serial number range addresses gets
  211. * nacked in this case just leave the serial number empty.
  212. */
  213. ret = i2c_smbus_read_i2c_block_data(client, MSHW0011_BAT0_REG_SERIAL_NO,
  214. sizeof(buf), buf);
  215. if (ret == -EREMOTEIO) {
  216. /* no serial number available */
  217. } else if (ret != sizeof(buf)) {
  218. dev_err(&client->dev, "Error reading serial no: %d\n", ret);
  219. return ret;
  220. } else {
  221. snprintf(bix->serial, ARRAY_SIZE(bix->serial), "%3pE%6pE", buf + 7, buf);
  222. }
  223. /* get cycle count */
  224. ret = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_CYCLE_CNT);
  225. if (ret < 0) {
  226. dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
  227. return ret;
  228. }
  229. bix->cycle_count = ret;
  230. /* get OEM name */
  231. ret = i2c_smbus_read_i2c_block_data(client, MSHW0011_BAT0_REG_OEM,
  232. 4, buf);
  233. if (ret != 4) {
  234. dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
  235. return ret;
  236. }
  237. snprintf(bix->OEM, ARRAY_SIZE(bix->OEM), "%3pE", buf);
  238. return 0;
  239. }
  240. static int mshw0011_bst(struct mshw0011_data *cdata, struct bst *bst)
  241. {
  242. struct i2c_client *client = cdata->bat0;
  243. int rate, capacity, voltage, state;
  244. s16 tmp;
  245. rate = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_RATE);
  246. if (rate < 0)
  247. return rate;
  248. capacity = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_CAPACITY);
  249. if (capacity < 0)
  250. return capacity;
  251. voltage = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_VOLTAGE);
  252. if (voltage < 0)
  253. return voltage;
  254. tmp = rate;
  255. bst->battery_present_rate = abs((s32)tmp);
  256. state = 0;
  257. if ((s32) tmp > 0)
  258. state |= ACPI_BATTERY_STATE_CHARGING;
  259. else if ((s32) tmp < 0)
  260. state |= ACPI_BATTERY_STATE_DISCHARGING;
  261. bst->battery_state = state;
  262. bst->battery_remaining_capacity = capacity;
  263. bst->battery_present_voltage = voltage;
  264. return 0;
  265. }
  266. static int mshw0011_adp_psr(struct mshw0011_data *cdata)
  267. {
  268. return i2c_smbus_read_byte_data(cdata->adp1, MSHW0011_ADP1_REG_PSR);
  269. }
  270. static int mshw0011_isr(struct mshw0011_data *cdata)
  271. {
  272. struct bst bst;
  273. struct bix bix;
  274. int ret;
  275. bool status, bat_status;
  276. ret = mshw0011_adp_psr(cdata);
  277. if (ret < 0)
  278. return ret;
  279. status = ret;
  280. if (status != cdata->charging)
  281. mshw0011_notify(cdata, cdata->notify_mask,
  282. MSHW0011_NOTIFY_ADP1, &ret);
  283. cdata->charging = status;
  284. ret = mshw0011_bst(cdata, &bst);
  285. if (ret < 0)
  286. return ret;
  287. bat_status = bst.battery_state;
  288. if (bat_status != cdata->bat_charging)
  289. mshw0011_notify(cdata, cdata->notify_mask,
  290. MSHW0011_NOTIFY_BAT0_BST, &ret);
  291. cdata->bat_charging = bat_status;
  292. ret = mshw0011_bix(cdata, &bix);
  293. if (ret < 0)
  294. return ret;
  295. if (bix.last_full_charg_capacity != cdata->full_capacity)
  296. mshw0011_notify(cdata, cdata->notify_mask,
  297. MSHW0011_NOTIFY_BAT0_BIX, &ret);
  298. cdata->full_capacity = bix.last_full_charg_capacity;
  299. return 0;
  300. }
  301. static int mshw0011_poll_task(void *data)
  302. {
  303. struct mshw0011_data *cdata = data;
  304. int ret = 0;
  305. cdata->kthread_running = true;
  306. set_freezable();
  307. while (!kthread_should_stop()) {
  308. schedule_timeout_interruptible(SURFACE_3_POLL_INTERVAL);
  309. try_to_freeze();
  310. ret = mshw0011_isr(data);
  311. if (ret)
  312. break;
  313. }
  314. cdata->kthread_running = false;
  315. return ret;
  316. }
  317. static acpi_status
  318. mshw0011_space_handler(u32 function, acpi_physical_address command,
  319. u32 bits, u64 *value64,
  320. void *handler_context, void *region_context)
  321. {
  322. struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
  323. struct mshw0011_handler_data *data = handler_context;
  324. struct acpi_connection_info *info = &data->info;
  325. struct acpi_resource_i2c_serialbus *sb;
  326. struct i2c_client *client = data->client;
  327. struct mshw0011_data *cdata = i2c_get_clientdata(client);
  328. struct acpi_resource *ares;
  329. u32 accessor_type = function >> 16;
  330. acpi_status ret;
  331. int status = 1;
  332. ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
  333. if (ACPI_FAILURE(ret))
  334. return ret;
  335. if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  336. ret = AE_BAD_PARAMETER;
  337. goto err;
  338. }
  339. sb = &ares->data.i2c_serial_bus;
  340. if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  341. ret = AE_BAD_PARAMETER;
  342. goto err;
  343. }
  344. if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
  345. ret = AE_BAD_PARAMETER;
  346. goto err;
  347. }
  348. if (gsb->cmd.arg0 == MSHW0011_CMD_DEST_ADP1 &&
  349. gsb->cmd.arg1 == MSHW0011_CMD_ADP1_PSR) {
  350. status = mshw0011_adp_psr(cdata);
  351. if (status >= 0) {
  352. ret = AE_OK;
  353. goto out;
  354. } else {
  355. ret = AE_ERROR;
  356. goto err;
  357. }
  358. }
  359. if (gsb->cmd.arg0 != MSHW0011_CMD_DEST_BAT0) {
  360. ret = AE_BAD_PARAMETER;
  361. goto err;
  362. }
  363. switch (gsb->cmd.arg1) {
  364. case MSHW0011_CMD_BAT0_STA:
  365. break;
  366. case MSHW0011_CMD_BAT0_BIX:
  367. ret = mshw0011_bix(cdata, &gsb->bix);
  368. break;
  369. case MSHW0011_CMD_BAT0_BTP:
  370. cdata->trip_point = gsb->cmd.arg2;
  371. break;
  372. case MSHW0011_CMD_BAT0_BST:
  373. ret = mshw0011_bst(cdata, &gsb->bst);
  374. break;
  375. default:
  376. dev_info(&cdata->bat0->dev, "command(0x%02x) is not supported.\n", gsb->cmd.arg1);
  377. ret = AE_BAD_PARAMETER;
  378. goto err;
  379. }
  380. out:
  381. gsb->ret = status;
  382. gsb->status = 0;
  383. err:
  384. ACPI_FREE(ares);
  385. return ret;
  386. }
  387. static int mshw0011_install_space_handler(struct i2c_client *client)
  388. {
  389. acpi_handle handle;
  390. struct mshw0011_handler_data *data;
  391. acpi_status status;
  392. handle = ACPI_HANDLE(&client->dev);
  393. if (!handle)
  394. return -ENODEV;
  395. data = kzalloc(sizeof(struct mshw0011_handler_data),
  396. GFP_KERNEL);
  397. if (!data)
  398. return -ENOMEM;
  399. data->client = client;
  400. status = acpi_bus_attach_private_data(handle, (void *)data);
  401. if (ACPI_FAILURE(status)) {
  402. kfree(data);
  403. return -ENOMEM;
  404. }
  405. status = acpi_install_address_space_handler(handle,
  406. ACPI_ADR_SPACE_GSBUS,
  407. &mshw0011_space_handler,
  408. NULL,
  409. data);
  410. if (ACPI_FAILURE(status)) {
  411. dev_err(&client->dev, "Error installing i2c space handler\n");
  412. acpi_bus_detach_private_data(handle);
  413. kfree(data);
  414. return -ENOMEM;
  415. }
  416. acpi_walk_dep_device_list(handle);
  417. return 0;
  418. }
  419. static void mshw0011_remove_space_handler(struct i2c_client *client)
  420. {
  421. struct mshw0011_handler_data *data;
  422. acpi_handle handle;
  423. acpi_status status;
  424. handle = ACPI_HANDLE(&client->dev);
  425. if (!handle)
  426. return;
  427. acpi_remove_address_space_handler(handle,
  428. ACPI_ADR_SPACE_GSBUS,
  429. &mshw0011_space_handler);
  430. status = acpi_bus_get_private_data(handle, (void **)&data);
  431. if (ACPI_SUCCESS(status))
  432. kfree(data);
  433. acpi_bus_detach_private_data(handle);
  434. }
  435. static int mshw0011_probe(struct i2c_client *client)
  436. {
  437. struct i2c_board_info board_info;
  438. struct device *dev = &client->dev;
  439. struct i2c_client *bat0;
  440. struct mshw0011_data *data;
  441. int error, mask;
  442. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  443. if (!data)
  444. return -ENOMEM;
  445. data->adp1 = client;
  446. i2c_set_clientdata(client, data);
  447. memset(&board_info, 0, sizeof(board_info));
  448. strlcpy(board_info.type, "MSHW0011-bat0", I2C_NAME_SIZE);
  449. bat0 = i2c_acpi_new_device(dev, 1, &board_info);
  450. if (IS_ERR(bat0))
  451. return PTR_ERR(bat0);
  452. data->bat0 = bat0;
  453. i2c_set_clientdata(bat0, data);
  454. error = mshw0011_notify(data, 1, MSHW0011_NOTIFY_GET_VERSION, &mask);
  455. if (error)
  456. goto out_err;
  457. data->notify_mask = mask == MSHW0011_EV_2_5_MASK;
  458. data->poll_task = kthread_run(mshw0011_poll_task, data, "mshw0011_adp");
  459. if (IS_ERR(data->poll_task)) {
  460. error = PTR_ERR(data->poll_task);
  461. dev_err(&client->dev, "Unable to run kthread err %d\n", error);
  462. goto out_err;
  463. }
  464. error = mshw0011_install_space_handler(client);
  465. if (error)
  466. goto out_err;
  467. return 0;
  468. out_err:
  469. if (data->kthread_running)
  470. kthread_stop(data->poll_task);
  471. i2c_unregister_device(data->bat0);
  472. return error;
  473. }
  474. static int mshw0011_remove(struct i2c_client *client)
  475. {
  476. struct mshw0011_data *cdata = i2c_get_clientdata(client);
  477. mshw0011_remove_space_handler(client);
  478. if (cdata->kthread_running)
  479. kthread_stop(cdata->poll_task);
  480. i2c_unregister_device(cdata->bat0);
  481. return 0;
  482. }
  483. static const struct acpi_device_id mshw0011_acpi_match[] = {
  484. { "MSHW0011", 0 },
  485. { }
  486. };
  487. MODULE_DEVICE_TABLE(acpi, mshw0011_acpi_match);
  488. static struct i2c_driver mshw0011_driver = {
  489. .probe_new = mshw0011_probe,
  490. .remove = mshw0011_remove,
  491. .driver = {
  492. .name = "mshw0011",
  493. .acpi_match_table = mshw0011_acpi_match,
  494. },
  495. };
  496. module_i2c_driver(mshw0011_driver);
  497. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  498. MODULE_DESCRIPTION("mshw0011 driver");
  499. MODULE_LICENSE("GPL v2");