surface3_power.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 <[email protected]>
  7. * (C) Copyright 2016 Stephen Just <[email protected]>
  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. acpi_handle handle;
  145. unsigned int i;
  146. handle = ACPI_HANDLE(&cdata->adp1->dev);
  147. if (!handle)
  148. return -ENODEV;
  149. obj = acpi_evaluate_dsm_typed(handle, &mshw0011_guid, arg1, arg2, NULL,
  150. ACPI_TYPE_BUFFER);
  151. if (!obj) {
  152. dev_err(&cdata->adp1->dev, "device _DSM execution failed\n");
  153. return -ENODEV;
  154. }
  155. *ret_value = 0;
  156. for (i = 0; i < obj->buffer.length; i++)
  157. *ret_value |= obj->buffer.pointer[i] << (i * 8);
  158. ACPI_FREE(obj);
  159. return 0;
  160. }
  161. static const struct bix default_bix = {
  162. .revision = 0x00,
  163. .power_unit = 0x01,
  164. .design_capacity = 0x1dca,
  165. .last_full_charg_capacity = 0x1dca,
  166. .battery_technology = 0x01,
  167. .design_voltage = 0x10df,
  168. .design_capacity_of_warning = 0x8f,
  169. .design_capacity_of_low = 0x47,
  170. .cycle_count = 0xffffffff,
  171. .measurement_accuracy = 0x00015f90,
  172. .max_sampling_time = 0x03e8,
  173. .min_sampling_time = 0x03e8,
  174. .max_average_interval = 0x03e8,
  175. .min_average_interval = 0x03e8,
  176. .battery_capacity_granularity_1 = 0x45,
  177. .battery_capacity_granularity_2 = 0x11,
  178. .model = "P11G8M",
  179. .serial = "",
  180. .type = "LION",
  181. .OEM = "",
  182. };
  183. static int mshw0011_bix(struct mshw0011_data *cdata, struct bix *bix)
  184. {
  185. struct i2c_client *client = cdata->bat0;
  186. char buf[SURFACE_3_STRLEN];
  187. int ret;
  188. *bix = default_bix;
  189. /* get design capacity */
  190. ret = i2c_smbus_read_word_data(client,
  191. MSHW0011_BAT0_REG_DESIGN_CAPACITY);
  192. if (ret < 0) {
  193. dev_err(&client->dev, "Error reading design capacity: %d\n",
  194. ret);
  195. return ret;
  196. }
  197. bix->design_capacity = ret;
  198. /* get last full charge capacity */
  199. ret = i2c_smbus_read_word_data(client,
  200. MSHW0011_BAT0_REG_FULL_CHG_CAPACITY);
  201. if (ret < 0) {
  202. dev_err(&client->dev,
  203. "Error reading last full charge capacity: %d\n", ret);
  204. return ret;
  205. }
  206. bix->last_full_charg_capacity = ret;
  207. /*
  208. * Get serial number, on some devices (with unofficial replacement
  209. * battery?) reading any of the serial number range addresses gets
  210. * nacked in this case just leave the serial number empty.
  211. */
  212. ret = i2c_smbus_read_i2c_block_data(client, MSHW0011_BAT0_REG_SERIAL_NO,
  213. sizeof(buf), buf);
  214. if (ret == -EREMOTEIO) {
  215. /* no serial number available */
  216. } else if (ret != sizeof(buf)) {
  217. dev_err(&client->dev, "Error reading serial no: %d\n", ret);
  218. return ret;
  219. } else {
  220. snprintf(bix->serial, ARRAY_SIZE(bix->serial), "%3pE%6pE", buf + 7, buf);
  221. }
  222. /* get cycle count */
  223. ret = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_CYCLE_CNT);
  224. if (ret < 0) {
  225. dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
  226. return ret;
  227. }
  228. bix->cycle_count = ret;
  229. /* get OEM name */
  230. ret = i2c_smbus_read_i2c_block_data(client, MSHW0011_BAT0_REG_OEM,
  231. 4, buf);
  232. if (ret != 4) {
  233. dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
  234. return ret;
  235. }
  236. snprintf(bix->OEM, ARRAY_SIZE(bix->OEM), "%3pE", buf);
  237. return 0;
  238. }
  239. static int mshw0011_bst(struct mshw0011_data *cdata, struct bst *bst)
  240. {
  241. struct i2c_client *client = cdata->bat0;
  242. int rate, capacity, voltage, state;
  243. s16 tmp;
  244. rate = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_RATE);
  245. if (rate < 0)
  246. return rate;
  247. capacity = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_CAPACITY);
  248. if (capacity < 0)
  249. return capacity;
  250. voltage = i2c_smbus_read_word_data(client, MSHW0011_BAT0_REG_VOLTAGE);
  251. if (voltage < 0)
  252. return voltage;
  253. tmp = rate;
  254. bst->battery_present_rate = abs((s32)tmp);
  255. state = 0;
  256. if ((s32) tmp > 0)
  257. state |= ACPI_BATTERY_STATE_CHARGING;
  258. else if ((s32) tmp < 0)
  259. state |= ACPI_BATTERY_STATE_DISCHARGING;
  260. bst->battery_state = state;
  261. bst->battery_remaining_capacity = capacity;
  262. bst->battery_present_voltage = voltage;
  263. return 0;
  264. }
  265. static int mshw0011_adp_psr(struct mshw0011_data *cdata)
  266. {
  267. return i2c_smbus_read_byte_data(cdata->adp1, MSHW0011_ADP1_REG_PSR);
  268. }
  269. static int mshw0011_isr(struct mshw0011_data *cdata)
  270. {
  271. struct bst bst;
  272. struct bix bix;
  273. int ret;
  274. bool status, bat_status;
  275. ret = mshw0011_adp_psr(cdata);
  276. if (ret < 0)
  277. return ret;
  278. status = ret;
  279. if (status != cdata->charging)
  280. mshw0011_notify(cdata, cdata->notify_mask,
  281. MSHW0011_NOTIFY_ADP1, &ret);
  282. cdata->charging = status;
  283. ret = mshw0011_bst(cdata, &bst);
  284. if (ret < 0)
  285. return ret;
  286. bat_status = bst.battery_state;
  287. if (bat_status != cdata->bat_charging)
  288. mshw0011_notify(cdata, cdata->notify_mask,
  289. MSHW0011_NOTIFY_BAT0_BST, &ret);
  290. cdata->bat_charging = bat_status;
  291. ret = mshw0011_bix(cdata, &bix);
  292. if (ret < 0)
  293. return ret;
  294. if (bix.last_full_charg_capacity != cdata->full_capacity)
  295. mshw0011_notify(cdata, cdata->notify_mask,
  296. MSHW0011_NOTIFY_BAT0_BIX, &ret);
  297. cdata->full_capacity = bix.last_full_charg_capacity;
  298. return 0;
  299. }
  300. static int mshw0011_poll_task(void *data)
  301. {
  302. struct mshw0011_data *cdata = data;
  303. int ret = 0;
  304. cdata->kthread_running = true;
  305. set_freezable();
  306. while (!kthread_should_stop()) {
  307. schedule_timeout_interruptible(SURFACE_3_POLL_INTERVAL);
  308. try_to_freeze();
  309. ret = mshw0011_isr(data);
  310. if (ret)
  311. break;
  312. }
  313. cdata->kthread_running = false;
  314. return ret;
  315. }
  316. static acpi_status
  317. mshw0011_space_handler(u32 function, acpi_physical_address command,
  318. u32 bits, u64 *value64,
  319. void *handler_context, void *region_context)
  320. {
  321. struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
  322. struct mshw0011_handler_data *data = handler_context;
  323. struct acpi_connection_info *info = &data->info;
  324. struct acpi_resource_i2c_serialbus *sb;
  325. struct i2c_client *client = data->client;
  326. struct mshw0011_data *cdata = i2c_get_clientdata(client);
  327. struct acpi_resource *ares;
  328. u32 accessor_type = function >> 16;
  329. acpi_status ret;
  330. int status = 1;
  331. ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
  332. if (ACPI_FAILURE(ret))
  333. return ret;
  334. if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) {
  335. ret = AE_BAD_PARAMETER;
  336. goto err;
  337. }
  338. if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
  339. ret = AE_BAD_PARAMETER;
  340. goto err;
  341. }
  342. if (gsb->cmd.arg0 == MSHW0011_CMD_DEST_ADP1 &&
  343. gsb->cmd.arg1 == MSHW0011_CMD_ADP1_PSR) {
  344. status = mshw0011_adp_psr(cdata);
  345. if (status >= 0) {
  346. ret = AE_OK;
  347. goto out;
  348. } else {
  349. ret = AE_ERROR;
  350. goto err;
  351. }
  352. }
  353. if (gsb->cmd.arg0 != MSHW0011_CMD_DEST_BAT0) {
  354. ret = AE_BAD_PARAMETER;
  355. goto err;
  356. }
  357. switch (gsb->cmd.arg1) {
  358. case MSHW0011_CMD_BAT0_STA:
  359. break;
  360. case MSHW0011_CMD_BAT0_BIX:
  361. ret = mshw0011_bix(cdata, &gsb->bix);
  362. break;
  363. case MSHW0011_CMD_BAT0_BTP:
  364. cdata->trip_point = gsb->cmd.arg2;
  365. break;
  366. case MSHW0011_CMD_BAT0_BST:
  367. ret = mshw0011_bst(cdata, &gsb->bst);
  368. break;
  369. default:
  370. dev_info(&cdata->bat0->dev, "command(0x%02x) is not supported.\n", gsb->cmd.arg1);
  371. ret = AE_BAD_PARAMETER;
  372. goto err;
  373. }
  374. out:
  375. gsb->ret = status;
  376. gsb->status = 0;
  377. err:
  378. ACPI_FREE(ares);
  379. return ret;
  380. }
  381. static int mshw0011_install_space_handler(struct i2c_client *client)
  382. {
  383. struct acpi_device *adev;
  384. struct mshw0011_handler_data *data;
  385. acpi_status status;
  386. adev = ACPI_COMPANION(&client->dev);
  387. if (!adev)
  388. return -ENODEV;
  389. data = kzalloc(sizeof(struct mshw0011_handler_data),
  390. GFP_KERNEL);
  391. if (!data)
  392. return -ENOMEM;
  393. data->client = client;
  394. status = acpi_bus_attach_private_data(adev->handle, (void *)data);
  395. if (ACPI_FAILURE(status)) {
  396. kfree(data);
  397. return -ENOMEM;
  398. }
  399. status = acpi_install_address_space_handler(adev->handle,
  400. ACPI_ADR_SPACE_GSBUS,
  401. &mshw0011_space_handler,
  402. NULL,
  403. data);
  404. if (ACPI_FAILURE(status)) {
  405. dev_err(&client->dev, "Error installing i2c space handler\n");
  406. acpi_bus_detach_private_data(adev->handle);
  407. kfree(data);
  408. return -ENOMEM;
  409. }
  410. acpi_dev_clear_dependencies(adev);
  411. return 0;
  412. }
  413. static void mshw0011_remove_space_handler(struct i2c_client *client)
  414. {
  415. struct mshw0011_handler_data *data;
  416. acpi_handle handle;
  417. acpi_status status;
  418. handle = ACPI_HANDLE(&client->dev);
  419. if (!handle)
  420. return;
  421. acpi_remove_address_space_handler(handle,
  422. ACPI_ADR_SPACE_GSBUS,
  423. &mshw0011_space_handler);
  424. status = acpi_bus_get_private_data(handle, (void **)&data);
  425. if (ACPI_SUCCESS(status))
  426. kfree(data);
  427. acpi_bus_detach_private_data(handle);
  428. }
  429. static int mshw0011_probe(struct i2c_client *client)
  430. {
  431. struct i2c_board_info board_info;
  432. struct device *dev = &client->dev;
  433. struct i2c_client *bat0;
  434. struct mshw0011_data *data;
  435. int error, mask;
  436. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  437. if (!data)
  438. return -ENOMEM;
  439. data->adp1 = client;
  440. i2c_set_clientdata(client, data);
  441. memset(&board_info, 0, sizeof(board_info));
  442. strscpy(board_info.type, "MSHW0011-bat0", I2C_NAME_SIZE);
  443. bat0 = i2c_acpi_new_device(dev, 1, &board_info);
  444. if (IS_ERR(bat0))
  445. return PTR_ERR(bat0);
  446. data->bat0 = bat0;
  447. i2c_set_clientdata(bat0, data);
  448. error = mshw0011_notify(data, 1, MSHW0011_NOTIFY_GET_VERSION, &mask);
  449. if (error)
  450. goto out_err;
  451. data->notify_mask = mask == MSHW0011_EV_2_5_MASK;
  452. data->poll_task = kthread_run(mshw0011_poll_task, data, "mshw0011_adp");
  453. if (IS_ERR(data->poll_task)) {
  454. error = PTR_ERR(data->poll_task);
  455. dev_err(&client->dev, "Unable to run kthread err %d\n", error);
  456. goto out_err;
  457. }
  458. error = mshw0011_install_space_handler(client);
  459. if (error)
  460. goto out_err;
  461. return 0;
  462. out_err:
  463. if (data->kthread_running)
  464. kthread_stop(data->poll_task);
  465. i2c_unregister_device(data->bat0);
  466. return error;
  467. }
  468. static void mshw0011_remove(struct i2c_client *client)
  469. {
  470. struct mshw0011_data *cdata = i2c_get_clientdata(client);
  471. mshw0011_remove_space_handler(client);
  472. if (cdata->kthread_running)
  473. kthread_stop(cdata->poll_task);
  474. i2c_unregister_device(cdata->bat0);
  475. }
  476. static const struct acpi_device_id mshw0011_acpi_match[] = {
  477. { "MSHW0011", 0 },
  478. { }
  479. };
  480. MODULE_DEVICE_TABLE(acpi, mshw0011_acpi_match);
  481. static struct i2c_driver mshw0011_driver = {
  482. .probe_new = mshw0011_probe,
  483. .remove = mshw0011_remove,
  484. .driver = {
  485. .name = "mshw0011",
  486. .acpi_match_table = mshw0011_acpi_match,
  487. },
  488. };
  489. module_i2c_driver(mshw0011_driver);
  490. MODULE_AUTHOR("Benjamin Tissoires <[email protected]>");
  491. MODULE_DESCRIPTION("mshw0011 driver");
  492. MODULE_LICENSE("GPL v2");