acpi.patch 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
  2. index 566644bb496a..081105a9cfca 100644
  3. --- a/drivers/platform/x86/Kconfig
  4. +++ b/drivers/platform/x86/Kconfig
  5. @@ -1158,6 +1158,15 @@ config SURFACE_3_BUTTON
  6. ---help---
  7. This driver handles the power/home/volume buttons on the Microsoft Surface 3 tablet.
  8. +config ACPI_SURFACE
  9. + tristate "Microsoft Surface Extras"
  10. + depends on ACPI
  11. + depends on ACPI_WMI
  12. + depends on INPUT
  13. + ---help---
  14. + This driver adds support for access to certain system events
  15. + on Microsoft Surface devices.
  16. +
  17. config INTEL_PUNIT_IPC
  18. tristate "Intel P-Unit IPC Driver"
  19. ---help---
  20. diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
  21. index 2ba6cb795338..8fd5b93bb20d 100644
  22. --- a/drivers/platform/x86/Makefile
  23. +++ b/drivers/platform/x86/Makefile
  24. @@ -81,6 +81,9 @@ obj-$(CONFIG_INTEL_PMC_IPC) += intel_pmc_ipc.o
  25. obj-$(CONFIG_SILEAD_DMI) += silead_dmi.o
  26. obj-$(CONFIG_SURFACE_PRO3_BUTTON) += surfacepro3_button.o
  27. obj-$(CONFIG_SURFACE_3_BUTTON) += surface3_button.o
  28. +obj-$(CONFIG_ACPI_SURFACE) += surface_acpi.o
  29. +obj-$(CONFIG_ACPI_SURFACE) += surface_i2c.o
  30. +obj-$(CONFIG_ACPI_SURFACE) += surface_platform.o
  31. obj-$(CONFIG_INTEL_PUNIT_IPC) += intel_punit_ipc.o
  32. obj-$(CONFIG_INTEL_BXTWC_PMIC_TMU) += intel_bxtwc_tmu.o
  33. obj-$(CONFIG_INTEL_TELEMETRY) += intel_telemetry_core.o \
  34. diff --git a/drivers/platform/x86/surface_acpi.c b/drivers/platform/x86/surface_acpi.c
  35. new file mode 100644
  36. index 000000000000..c969bda99464
  37. --- /dev/null
  38. +++ b/drivers/platform/x86/surface_acpi.c
  39. @@ -0,0 +1,485 @@
  40. +/*
  41. + * surface_acpi.c - Microsoft Surface ACPI Driver
  42. + *
  43. + * This program is free software; you can redistribute it and/or modify
  44. + * it under the terms of the GNU General Public License as published by
  45. + * the Free Software Foundation; either version 2 of the License, or
  46. + * (at your option) any later version.
  47. + *
  48. + * This program is distributed in the hope that it will be useful,
  49. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  50. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  51. + * GNU General Public License for more details.
  52. + *
  53. + * The full GNU General Public License is included in this distribution in
  54. + * the file called "COPYING".
  55. + */
  56. +
  57. +#define SURFACE_ACPI_VERSION "0.1"
  58. +#define SURFACE_GEN_VERSION 0x08
  59. +#define PROC_SURFACE "surface"
  60. +
  61. +#include <linux/kernel.h>
  62. +#include <linux/module.h>
  63. +#include <linux/init.h>
  64. +#include <linux/types.h>
  65. +#include <linux/acpi.h>
  66. +#include <linux/power_supply.h>
  67. +#include <linux/thermal.h>
  68. +#include <linux/dmi.h>
  69. +#include <linux/seq_file.h>
  70. +#include <acpi/acpi_bus.h>
  71. +#include <acpi/acpi_drivers.h>
  72. +
  73. +#include "surface_acpi.h"
  74. +
  75. +#define SUR_METHOD_DSM "_DSM"
  76. +#define SUR_METHOD_REG "_REG"
  77. +#define SUR_METHOD_STA "_STA"
  78. +#define SUR_METHOD_INI "_INI"
  79. +#define SUR_METHOD_CRS "_CRS"
  80. +
  81. +#define SUR_QUERY_DEVICE 0x00
  82. +#define SUR_SET_DVER 0x01
  83. +#define SUR_GET_BOARD_REVID 0x02
  84. +#define SUR_BAT1_STATE_CHANGE 0x03
  85. +#define SUR_BAT1_INFO_CHANGE 0x04
  86. +#define SUR_PSU_STATE_CHANGE 0x05
  87. +#define SUR_PSU_INFO_CHANGE 0x06
  88. +#define SUR_BAT2_STATE_CHANGE 0x07
  89. +#define SUR_BAT2_INFO_CHANGE 0x08
  90. +#define SUR_SENSOR_TRIP_POINT 0x09
  91. +
  92. +#define REG_AVAILABLE 0x01
  93. +#define REG_INIT 0x09
  94. +
  95. +static char SURFACE_EVENT_GUID[] = "93b666c5-70c6-469f-a215-3d487c91ab3c";
  96. +static char SUR_SAN_RQST[] = "\\_SB._SAN.RQST";
  97. +static char SUR_SAN_RQSX[] = "\\_SB._SAN.RQSX";
  98. +
  99. +struct surface_acpi_dev {
  100. + acpi_handle handle;
  101. + acpi_handle rqst_handle;
  102. + acpi_handle rqsx_handle;
  103. +
  104. + struct acpi_device *san_dev;
  105. + struct acpi_device *ssh_dev;
  106. + struct acpi_device *bat1_dev;
  107. + struct acpi_device *bat2_dev;
  108. + struct acpi_device *psu_dev;
  109. +
  110. + unsigned int bat1_attached:1;
  111. + unsigned int bat2_attached:1;
  112. + unsigned int psu_registered:1;
  113. +};
  114. +
  115. +static struct surface_acpi_dev *surface_acpi;
  116. +
  117. +static struct proc_dir_entry *surface_proc_dir;
  118. +
  119. +static acpi_status surface_acpi_check_status(struct acpi_device *dev)
  120. +{
  121. + unsigned long long value;
  122. + acpi_status status;
  123. +
  124. + if (acpi_has_method(dev->handle, SUR_METHOD_STA)) {
  125. + status = acpi_evaluate_integer(dev->handle,
  126. + SUR_METHOD_STA, NULL, &value);
  127. +
  128. + if (ACPI_FAILURE(status)) {
  129. + pr_err("surface_acpi: ACPI event failure status %s\n",
  130. + acpi_format_exception(status));
  131. + return AE_ERROR;
  132. + }
  133. + }
  134. + else
  135. + return AE_NOT_FOUND;
  136. +
  137. + return AE_OK;
  138. +}
  139. +
  140. +static acpi_status surface_acpi_san_reg(void)
  141. +{
  142. + union acpi_object in_objs[2], out_objs[1];
  143. + struct acpi_object_list params;
  144. + struct acpi_buffer results;
  145. + acpi_status status;
  146. +
  147. + params.count = ARRAY_SIZE(in_objs);
  148. + params.pointer = in_objs;
  149. + in_objs[0].type = ACPI_TYPE_INTEGER;
  150. + in_objs[0].integer.value = REG_INIT;
  151. + in_objs[1].type = ACPI_TYPE_INTEGER;
  152. + in_objs[1].integer.value = REG_AVAILABLE;
  153. + results.length = sizeof(out_objs);
  154. + results.pointer = out_objs;
  155. +
  156. + if (acpi_has_method(surface_acpi->handle, SUR_METHOD_REG)) {
  157. + status = acpi_evaluate_object(surface_acpi->handle,
  158. + SUR_METHOD_REG, &params, &results);
  159. +
  160. + if (ACPI_FAILURE(status)) {
  161. + pr_err("surface_acpi: ACPI event failure status %s\n",
  162. + acpi_format_exception(status));
  163. + return AE_ERROR;
  164. + }
  165. + }
  166. + else
  167. + return AE_NOT_FOUND;
  168. +
  169. + return AE_OK;
  170. +}
  171. +
  172. +acpi_status surface_acpi_event_handler(u32 event)
  173. +{
  174. + union acpi_object in_objs[4], out_objs[5];
  175. + struct acpi_object_list params;
  176. + struct acpi_buffer results;
  177. + acpi_status status;
  178. +
  179. + params.count = ARRAY_SIZE(in_objs);
  180. + params.pointer = in_objs;
  181. + in_objs[0].type = ACPI_TYPE_BUFFER;
  182. + in_objs[0].buffer.length = sizeof(SURFACE_EVENT_GUID);
  183. + in_objs[0].buffer.pointer = SURFACE_EVENT_GUID;
  184. + in_objs[1].type = ACPI_TYPE_INTEGER;
  185. + in_objs[1].integer.value = SUR_QUERY_DEVICE;
  186. + in_objs[2].type = ACPI_TYPE_INTEGER;
  187. + in_objs[2].integer.value = event;
  188. + in_objs[3].type = ACPI_TYPE_PACKAGE;
  189. + in_objs[3].package.count = 0;
  190. + in_objs[3].package.elements = SURFACE_GEN_VERSION;
  191. + results.length = sizeof(out_objs);
  192. + results.pointer = out_objs;
  193. +
  194. + if (acpi_has_method(surface_acpi->handle, SUR_METHOD_DSM)) {
  195. + status = acpi_evaluate_object(surface_acpi->handle,
  196. + SUR_METHOD_DSM, &params, &results);
  197. +
  198. + if (ACPI_FAILURE(status)) {
  199. + pr_err("surface_acpi: ACPI event failure status %s\n",
  200. + acpi_format_exception(status));
  201. + return AE_ERROR;
  202. + }
  203. + }
  204. + else
  205. + return AE_NOT_FOUND;
  206. +
  207. + return AE_OK;
  208. +}
  209. +EXPORT_SYMBOL(surface_acpi_event_handler);
  210. +
  211. +static void surface_acpi_san_load(void)
  212. +{
  213. + acpi_status ret;
  214. +
  215. + ret = surface_acpi_event_handler(SUR_SET_DVER);
  216. + if (ACPI_FAILURE(ret))
  217. + pr_err("surface_acpi: Error setting Driver Version\n");
  218. +
  219. + ret = surface_acpi_event_handler(SUR_SENSOR_TRIP_POINT);
  220. + if (ACPI_FAILURE(ret))
  221. + pr_err("surface_acpi: Error setting Sensor Trip Point\n");
  222. +
  223. + ret = surface_acpi_event_handler(SUR_BAT1_INFO_CHANGE);
  224. + if (ACPI_FAILURE(ret))
  225. + pr_err("surface_acpi: Error attaching BAT1\n");
  226. + else
  227. + surface_acpi->bat1_attached = 1;
  228. +
  229. + ret = surface_acpi_event_handler(SUR_BAT2_INFO_CHANGE);
  230. + if (ACPI_FAILURE(ret))
  231. + pr_err("surface_acpi: Error attaching BAT2\n");
  232. + else
  233. + surface_acpi->bat2_attached = 1;
  234. +
  235. + ret = surface_acpi_event_handler(SUR_PSU_INFO_CHANGE);
  236. + if (ACPI_FAILURE(ret))
  237. + pr_err("surface_acpi: Error registering PSU\n");
  238. + else
  239. + surface_acpi->psu_registered = 1;
  240. +}
  241. +
  242. +static acpi_status surface_acpi_ssh_initialize(void)
  243. +{
  244. + acpi_status status;
  245. +
  246. + if (acpi_has_method(surface_acpi->ssh_dev->handle, SUR_METHOD_INI)) {
  247. + status = acpi_evaluate_object(surface_acpi->ssh_dev->handle,
  248. + SUR_METHOD_INI, NULL, NULL);
  249. +
  250. + if (ACPI_FAILURE(status)) {
  251. + pr_err("surface_acpi: ACPI event failure status %s\n",
  252. + acpi_format_exception(status));
  253. + return AE_ERROR;
  254. + }
  255. + }
  256. + else
  257. + return AE_NOT_FOUND;
  258. +
  259. + return AE_OK;
  260. +}
  261. +
  262. +static int bat1_proc_show(struct seq_file *m, void *v)
  263. +{
  264. + seq_printf(m, "attached: %d\n", surface_acpi->bat1_attached);
  265. + return 0;
  266. +}
  267. +
  268. +static int bat1_proc_open(struct inode *inode, struct file *file)
  269. +{
  270. + return single_open(file, bat1_proc_show, PDE_DATA(inode));
  271. +}
  272. +
  273. +static const struct file_operations bat1_proc_fops = {
  274. + .owner = THIS_MODULE,
  275. + .open = bat1_proc_open,
  276. + .read = seq_read,
  277. + .llseek = seq_lseek,
  278. + .release = single_release,
  279. +};
  280. +
  281. +static int bat2_proc_show(struct seq_file *m, void *v)
  282. +{
  283. + seq_printf(m, "attached: %d\n", surface_acpi->bat2_attached);
  284. + return 0;
  285. +}
  286. +
  287. +static int bat2_proc_open(struct inode *inode, struct file *file)
  288. +{
  289. + return single_open(file, bat2_proc_show, PDE_DATA(inode));
  290. +}
  291. +
  292. +static const struct file_operations bat2_proc_fops = {
  293. + .owner = THIS_MODULE,
  294. + .open = bat2_proc_open,
  295. + .read = seq_read,
  296. + .llseek = seq_lseek,
  297. + .release = single_release,
  298. +};
  299. +
  300. +static int psu_proc_show(struct seq_file *m, void *v)
  301. +{
  302. + seq_printf(m, "registered: %d\n", surface_acpi->psu_registered);
  303. + return 0;
  304. +}
  305. +
  306. +static int psu_proc_open(struct inode *inode, struct file *file)
  307. +{
  308. + return single_open(file, psu_proc_show, PDE_DATA(inode));
  309. +}
  310. +
  311. +static const struct file_operations psu_proc_fops = {
  312. + .owner = THIS_MODULE,
  313. + .open = psu_proc_open,
  314. + .read = seq_read,
  315. + .llseek = seq_lseek,
  316. + .release = single_release,
  317. +};
  318. +
  319. +static int version_proc_show(struct seq_file *m, void *v)
  320. +{
  321. + seq_printf(m, "driver: %s\n", SURFACE_ACPI_VERSION);
  322. + return 0;
  323. +}
  324. +
  325. +static int version_proc_open(struct inode *inode, struct file *file)
  326. +{
  327. + return single_open(file, version_proc_show, PDE_DATA(inode));
  328. +}
  329. +
  330. +static const struct file_operations version_proc_fops = {
  331. + .owner = THIS_MODULE,
  332. + .open = version_proc_open,
  333. + .read = seq_read,
  334. + .llseek = seq_lseek,
  335. + .release = single_release,
  336. +};
  337. +
  338. +static void create_surface_proc_entries(void)
  339. +{
  340. + proc_create_data("BAT1", 0, surface_proc_dir,
  341. + &bat1_proc_fops, surface_acpi->bat1_attached);
  342. + proc_create_data("BAT2", 0, surface_proc_dir,
  343. + &bat2_proc_fops, surface_acpi->bat2_attached);
  344. + proc_create_data("ADP1", 0, surface_proc_dir,
  345. + &psu_proc_fops, surface_acpi->psu_registered);
  346. + proc_create_data("version", 0, surface_proc_dir,
  347. + &version_proc_fops, SURFACE_ACPI_VERSION);
  348. +}
  349. +
  350. +static void remove_surface_proc_entries(void)
  351. +{
  352. + remove_proc_entry("BAT1", surface_proc_dir);
  353. + remove_proc_entry("BAT2", surface_proc_dir);
  354. + remove_proc_entry("ADP1", surface_proc_dir);
  355. + remove_proc_entry("version", surface_proc_dir);
  356. +}
  357. +
  358. +static void surface_acpi_notify(struct acpi_device *dev, u32 event)
  359. +{
  360. + pr_info("surface_acpi: Event received %x\n", event);
  361. +}
  362. +
  363. +static void surface_acpi_register_rqst_handler(void)
  364. +{
  365. + acpi_status status;
  366. +
  367. + status = acpi_get_handle(NULL, SUR_SAN_RQST, &surface_acpi->rqst_handle);
  368. + if (ACPI_FAILURE(status)) {
  369. + pr_err("surface_acpi: ACPI event failure status %s\n",
  370. + acpi_format_exception(status));
  371. + }
  372. +}
  373. +
  374. +static void surface_acpi_register_rqsx_handler(void)
  375. +{
  376. + acpi_status status;
  377. +
  378. + status = acpi_get_handle(NULL, SUR_SAN_RQSX, &surface_acpi->rqsx_handle);
  379. + if (ACPI_FAILURE(status)) {
  380. + pr_err("surface_acpi: ACPI event failure status %s\n",
  381. + acpi_format_exception(status));
  382. + }
  383. +}
  384. +
  385. +static acpi_status surface_acpi_walk_callback(acpi_handle handle, u32 level,
  386. + void *context, void **return_value)
  387. +{
  388. + struct acpi_device_info *info;
  389. +
  390. + if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
  391. + pr_warn("method: name: %4.4s, args %X\n",
  392. + (char *)&info->name, info->param_count);
  393. +
  394. + kfree(info);
  395. + }
  396. +
  397. + return AE_OK;
  398. +}
  399. +
  400. +static void surface_acpi_walk_namespace(struct acpi_device *dev)
  401. +{
  402. + acpi_status status;
  403. +
  404. + status = acpi_walk_namespace(ACPI_TYPE_METHOD,
  405. + dev->handle, 1, surface_acpi_walk_callback,
  406. + NULL, NULL, NULL);
  407. + if (ACPI_FAILURE(status))
  408. + pr_warn("surface_acpi: Unable to walk acpi resources\n");
  409. +}
  410. +
  411. +static int surface_acpi_add(struct acpi_device *dev)
  412. +{
  413. + if (!surface_acpi)
  414. + {
  415. + surface_acpi = kzalloc(sizeof(*surface_acpi), GFP_KERNEL);
  416. + if (!surface_acpi)
  417. + return AE_NO_MEMORY;
  418. + }
  419. +
  420. + if (acpi_has_method(dev->handle, SUR_METHOD_DSM))
  421. + {
  422. + pr_info("surface_acpi: Attaching device MSHW0091\n");
  423. +
  424. + surface_acpi->san_dev = dev;
  425. + surface_acpi->handle = dev->handle;
  426. +
  427. + surface_acpi_walk_namespace(surface_acpi->san_dev);
  428. + surface_acpi_check_status(surface_acpi->san_dev);
  429. +
  430. + surface_acpi_register_rqst_handler();
  431. + surface_acpi_register_rqsx_handler();
  432. +
  433. + surface_acpi_san_reg();
  434. + surface_acpi_san_load();
  435. +
  436. + create_surface_proc_entries();
  437. + }
  438. + else if (acpi_has_method(dev->handle, SUR_METHOD_CRS))
  439. + {
  440. + pr_info("surface_acpi: Attaching device MSHW0084\n");
  441. +
  442. + surface_acpi->ssh_dev = dev;
  443. +
  444. + surface_acpi_walk_namespace(surface_acpi->ssh_dev);
  445. + surface_acpi_check_status(surface_acpi->ssh_dev);
  446. +
  447. + surface_acpi_ssh_initialize();
  448. + //surface_acpi_ssh_load();
  449. + }
  450. + else
  451. + {
  452. + pr_info("surface_acpi: Attaching device\n");
  453. + }
  454. +
  455. + device_init_wakeup(&dev->dev, true);
  456. +
  457. + return AE_OK;
  458. +}
  459. +
  460. +static int surface_acpi_remove(struct acpi_device *dev)
  461. +{
  462. + remove_surface_proc_entries();
  463. +
  464. + return AE_OK;
  465. +}
  466. +
  467. +static const struct acpi_device_id surface_device_ids[] = {
  468. + {"MSHW0084", 0},
  469. + {"MSHW0091", 0},
  470. + {"MSHW0124", 0},
  471. + {"INT3403", 0},
  472. + {"LNXTHERM", 0},
  473. + {"PNP0C0A", 0},
  474. + {"", 0},
  475. +};
  476. +MODULE_DEVICE_TABLE(acpi, surface_device_ids);
  477. +
  478. +static struct acpi_driver surface_acpi_driver = {
  479. + .name = "surface_acpi",
  480. + .owner = THIS_MODULE,
  481. + .ids = surface_device_ids,
  482. + .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  483. + .ops = {
  484. + .add = surface_acpi_add,
  485. + .remove = surface_acpi_remove,
  486. + .notify = surface_acpi_notify,
  487. + },
  488. +};
  489. +
  490. +static int __init surface_acpi_init(void)
  491. +{
  492. + int ret;
  493. +
  494. + pr_info("surface_acpi: Microsoft Surface ACPI Driver version %s\n",
  495. + SURFACE_ACPI_VERSION);
  496. +
  497. + surface_proc_dir = proc_mkdir(PROC_SURFACE, acpi_root_dir);
  498. + if (!surface_proc_dir) {
  499. + pr_err("surface_acpi: Unable to create proc dir " PROC_SURFACE "\n");
  500. + return -ENODEV;
  501. + }
  502. +
  503. + ret = acpi_bus_register_driver(&surface_acpi_driver);
  504. + if (ret) {
  505. + pr_err("surface_acpi: Failed to register ACPI driver: %d\n", ret);
  506. + remove_proc_entry(PROC_SURFACE, acpi_root_dir);
  507. + }
  508. +
  509. + return ret;
  510. +}
  511. +
  512. +static void __exit surface_acpi_exit(void)
  513. +{
  514. + acpi_bus_unregister_driver(&surface_acpi_driver);
  515. + if (surface_proc_dir)
  516. + remove_proc_entry(PROC_SURFACE, acpi_root_dir);
  517. +}
  518. +
  519. +module_init(surface_acpi_init);
  520. +module_exit(surface_acpi_exit);
  521. +
  522. +MODULE_AUTHOR("Jake Day");
  523. +MODULE_DESCRIPTION("Microsoft Surface ACPI Driver");
  524. +MODULE_LICENSE("GPL");
  525. diff --git a/drivers/platform/x86/surface_acpi.h b/drivers/platform/x86/surface_acpi.h
  526. new file mode 100644
  527. index 000000000000..5b6627c4d6f1
  528. --- /dev/null
  529. +++ b/drivers/platform/x86/surface_acpi.h
  530. @@ -0,0 +1,18 @@
  531. +/*
  532. + * surface_acpi.h - Microsoft Surface ACPI Driver
  533. + *
  534. + * This program is free software; you can redistribute it and/or modify
  535. + * it under the terms of the GNU General Public License as published by
  536. + * the Free Software Foundation; either version 2 of the License, or
  537. + * (at your option) any later version.
  538. + *
  539. + * This program is distributed in the hope that it will be useful,
  540. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  541. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  542. + * GNU General Public License for more details.
  543. + *
  544. + * The full GNU General Public License is included in this distribution in
  545. + * the file called "COPYING".
  546. + */
  547. +
  548. +acpi_status surface_acpi_event_handler(u32 event);
  549. diff --git a/drivers/platform/x86/surface_i2c.c b/drivers/platform/x86/surface_i2c.c
  550. new file mode 100644
  551. index 000000000000..fb2cf0cae72f
  552. --- /dev/null
  553. +++ b/drivers/platform/x86/surface_i2c.c
  554. @@ -0,0 +1,696 @@
  555. +/*
  556. + * surface_i2c.c - Microsoft Surface I2C Driver
  557. + *
  558. + * This program is free software; you can redistribute it and/or modify
  559. + * it under the terms of the GNU General Public License as published by
  560. + * the Free Software Foundation; either version 2 of the License, or
  561. + * (at your option) any later version.
  562. + *
  563. + * This program is distributed in the hope that it will be useful,
  564. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  565. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  566. + * GNU General Public License for more details.
  567. + *
  568. + * The full GNU General Public License is included in this distribution in
  569. + * the file called "COPYING".
  570. + */
  571. +
  572. +#include <linux/kernel.h>
  573. +#include <linux/i2c.h>
  574. +#include <linux/slab.h>
  575. +#include <linux/acpi.h>
  576. +#include <linux/module.h>
  577. +#include <linux/uuid.h>
  578. +#include <linux/kthread.h>
  579. +#include <linux/freezer.h>
  580. +#include <asm/unaligned.h>
  581. +
  582. +#include "surface_acpi.h"
  583. +
  584. +#define POLL_INTERVAL (HZ * 2)
  585. +
  586. +struct surface_i2c_data {
  587. + struct i2c_client *adp1;
  588. + struct i2c_client *bat0;
  589. + unsigned short notify_version;
  590. + struct task_struct *poll_task;
  591. + bool kthread_running;
  592. + bool charging;
  593. + bool bat_charging;
  594. + u8 trip_point;
  595. + s32 full_capacity;
  596. +};
  597. +
  598. +struct surface_i2c_lookup {
  599. + struct surface_i2c_data *cdata;
  600. + unsigned int n;
  601. + unsigned int index;
  602. + int addr;
  603. +};
  604. +
  605. +struct surface_i2c_handler_data {
  606. + struct acpi_connection_info info;
  607. + struct i2c_client *client;
  608. +};
  609. +
  610. +struct bix {
  611. + u32 revision;
  612. + u32 power_unit;
  613. + u32 design_capacity;
  614. + u32 last_full_charg_capacity;
  615. + u32 battery_technology;
  616. + u32 design_voltage;
  617. + u32 design_capacity_of_warning;
  618. + u32 design_capacity_of_low;
  619. + u32 cycle_count;
  620. + u32 measurement_accuracy;
  621. + u32 max_sampling_time;
  622. + u32 min_sampling_time;
  623. + u32 max_average_interval;
  624. + u32 min_average_interval;
  625. + u32 battery_capacity_granularity_1;
  626. + u32 battery_capacity_granularity_2;
  627. + char model[10];
  628. + char serial[10];
  629. + char type[10];
  630. + char OEM[10];
  631. +} __packed;
  632. +
  633. +struct bst {
  634. + u32 battery_state;
  635. + s32 battery_present_rate;
  636. + u32 battery_remaining_capacity;
  637. + u32 battery_present_voltage;
  638. +} __packed;
  639. +
  640. +struct gsb_command {
  641. + u8 arg0;
  642. + u8 arg1;
  643. + u8 arg2;
  644. +} __packed;
  645. +
  646. +struct gsb_buffer {
  647. + u8 status;
  648. + u8 len;
  649. + u8 ret;
  650. + union {
  651. + struct gsb_command cmd;
  652. + struct bst bst;
  653. + struct bix bix;
  654. + } __packed;
  655. +} __packed;
  656. +
  657. +#define ACPI_BATTERY_STATE_DISCHARGING 0x1
  658. +#define ACPI_BATTERY_STATE_CHARGING 0x2
  659. +#define ACPI_BATTERY_STATE_CRITICAL 0x4
  660. +
  661. +#define surface_i2c_CMD_DEST_BAT0 0x01
  662. +#define surface_i2c_CMD_DEST_ADP1 0x03
  663. +
  664. +#define surface_i2c_CMD_BAT0_STA 0x01
  665. +#define surface_i2c_CMD_BAT0_BIX 0x02
  666. +#define surface_i2c_CMD_BAT0_BCT 0x03
  667. +#define surface_i2c_CMD_BAT0_BTM 0x04
  668. +#define surface_i2c_CMD_BAT0_BST 0x05
  669. +#define surface_i2c_CMD_BAT0_BTP 0x06
  670. +#define surface_i2c_CMD_ADP1_PSR 0x07
  671. +#define surface_i2c_CMD_BAT0_PSOC 0x09
  672. +#define surface_i2c_CMD_BAT0_PMAX 0x0A
  673. +#define surface_i2c_CMD_BAT0_PSRC 0x0B
  674. +#define surface_i2c_CMD_BAT0_CHGI 0x0C
  675. +#define surface_i2c_CMD_BAT0_ARTG 0x0D
  676. +
  677. +#define surface_i2c_NOTIFY_GET_VERSION 0x00
  678. +#define surface_i2c_NOTIFY_ADP1 0x01
  679. +#define surface_i2c_NOTIFY_BAT0_BST 0x02
  680. +#define surface_i2c_NOTIFY_BAT0_BIX 0x05
  681. +
  682. +#define surface_i2c_ADP1_REG_PSR 0x03
  683. +
  684. +#define surface_i2c_BAT0_REG_CAPACITY 0x0c
  685. +#define surface_i2c_BAT0_REG_FULL_CHG_CAPACITY 0x0e
  686. +#define surface_i2c_BAT0_REG_DESIGN_CAPACITY 0x40
  687. +#define surface_i2c_BAT0_REG_VOLTAGE 0x08
  688. +#define surface_i2c_BAT0_REG_RATE 0x14
  689. +#define surface_i2c_BAT0_REG_OEM 0x45
  690. +#define surface_i2c_BAT0_REG_TYPE 0x4e
  691. +#define surface_i2c_BAT0_REG_SERIAL_NO 0x56
  692. +#define surface_i2c_BAT0_REG_CYCLE_CNT 0x6e
  693. +
  694. +#define surface_i2c_EV_2_5 0x1ff
  695. +
  696. +static int surface_i2c_read_block(struct i2c_client *client, u8 reg, u8 *buf,
  697. + int len)
  698. +{
  699. + int status, i;
  700. +
  701. + for (i = 0; i < len; i++) {
  702. + status = i2c_smbus_read_byte_data(client, reg + i);
  703. + if (status < 0) {
  704. + buf[i] = 0xff;
  705. + continue;
  706. + }
  707. +
  708. + buf[i] = (u8)status;
  709. + }
  710. +
  711. + return 0;
  712. +}
  713. +
  714. +static int
  715. +surface_i2c_notify(struct surface_i2c_data *cdata, u8 arg1, u8 arg2,
  716. + unsigned int *ret_value)
  717. +{
  718. + /*static const guid_t surface_i2c_guid =
  719. + GUID_INIT(0x93b666c5, 0x70c6, 0x469f,
  720. + 0xa2, 0x15, 0x3d, 0x48, 0x7c, 0x91, 0xab, 0x3c);*/
  721. +
  722. + struct acpi_device *adev;
  723. + acpi_handle handle;
  724. + acpi_status status;
  725. +
  726. + handle = ACPI_HANDLE(&cdata->adp1->dev);
  727. + if (!handle || acpi_bus_get_device(handle, &adev))
  728. + return -ENODEV;
  729. +
  730. + *ret_value = 0;
  731. +
  732. + status = surface_acpi_event_handler(arg2);
  733. + if (ACPI_FAILURE(status)) {
  734. + pr_err("surface_i2c: ACPI event failure status %s\n",
  735. + acpi_format_exception(status));
  736. + }
  737. +
  738. + return 0;
  739. +}
  740. +
  741. +static const struct bix default_bix = {
  742. + .revision = 0x00,
  743. + .power_unit = 0x00,
  744. + .design_capacity = 0x1734,
  745. + .last_full_charg_capacity = 0x1734,
  746. + .battery_technology = 0x01,
  747. + .design_voltage = 0x1d92,
  748. + .design_capacity_of_warning = 0xc8,
  749. + .design_capacity_of_low = 0xc8,
  750. + .battery_capacity_granularity_1 = 0x45,
  751. + .battery_capacity_granularity_2 = 0x11,
  752. + .cycle_count = 0x01,
  753. + .measurement_accuracy = 0x00015F90,
  754. + .max_sampling_time = 0x03E8,
  755. + .min_sampling_time = 0x03E8,
  756. + .max_average_interval = 0x03E8,
  757. + .min_average_interval = 0x03E8,
  758. + .model = "PNP0C0A",
  759. + .serial = "1234567890",
  760. + .type = "SDS-BAT",
  761. + .OEM = "MICROSOFT",
  762. +};
  763. +
  764. +static int surface_i2c_bix(struct surface_i2c_data *cdata, struct bix *bix)
  765. +{
  766. + struct i2c_client *client = cdata->bat0;
  767. + int ret;
  768. + char buf[10];
  769. +
  770. + *bix = default_bix;
  771. +
  772. + /* get design capacity */
  773. + ret = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_DESIGN_CAPACITY);
  774. + if (ret < 0) {
  775. + dev_err(&client->dev, "Error reading design capacity: %d\n", ret);
  776. + return ret;
  777. + }
  778. + bix->design_capacity = le16_to_cpu(ret);
  779. +
  780. + /* get last full charge capacity */
  781. + ret = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_FULL_CHG_CAPACITY);
  782. + if (ret < 0) {
  783. + dev_err(&client->dev, "Error reading last full charge capacity: %d\n", ret);
  784. + return ret;
  785. + }
  786. + bix->last_full_charg_capacity = le16_to_cpu(ret);
  787. +
  788. + /* get serial number */
  789. + ret = surface_i2c_read_block(client, surface_i2c_BAT0_REG_SERIAL_NO,
  790. + buf, 10);
  791. + if (ret) {
  792. + dev_err(&client->dev, "Error reading serial no: %d\n", ret);
  793. + return ret;
  794. + }
  795. + memcpy(bix->serial, buf + 7, 3);
  796. + memcpy(bix->serial + 3, buf, 6);
  797. + bix->serial[9] = '\0';
  798. +
  799. + /* get cycle count */
  800. + ret = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_CYCLE_CNT);
  801. + if (ret < 0) {
  802. + dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
  803. + return ret;
  804. + }
  805. + bix->cycle_count = le16_to_cpu(ret);
  806. +
  807. + /* get OEM name */
  808. + ret = surface_i2c_read_block(client, surface_i2c_BAT0_REG_OEM, buf, 4);
  809. + if (ret) {
  810. + dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
  811. + return ret;
  812. + }
  813. + memcpy(bix->OEM, buf, 3);
  814. + bix->OEM[4] = '\0';
  815. +
  816. + return 0;
  817. +}
  818. +
  819. +static int surface_i2c_bst(struct surface_i2c_data *cdata, struct bst *bst)
  820. +{
  821. + struct i2c_client *client = cdata->bat0;
  822. + int rate, capacity, voltage, state;
  823. + s16 tmp;
  824. +
  825. + rate = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_RATE);
  826. + if (rate < 0)
  827. + return rate;
  828. +
  829. + capacity = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_CAPACITY);
  830. + if (capacity < 0)
  831. + return capacity;
  832. +
  833. + voltage = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_VOLTAGE);
  834. + if (voltage < 0)
  835. + return voltage;
  836. +
  837. + tmp = le16_to_cpu(rate);
  838. + bst->battery_present_rate = abs((s32)tmp);
  839. +
  840. + state = 0;
  841. + if ((s32) tmp > 0)
  842. + state |= ACPI_BATTERY_STATE_CHARGING;
  843. + else if ((s32) tmp < 0)
  844. + state |= ACPI_BATTERY_STATE_DISCHARGING;
  845. + bst->battery_state = state;
  846. +
  847. + bst->battery_remaining_capacity = le16_to_cpu(capacity);
  848. + bst->battery_present_voltage = le16_to_cpu(voltage);
  849. +
  850. + return 0;
  851. +}
  852. +
  853. +static int surface_i2c_adp_psr(struct surface_i2c_data *cdata)
  854. +{
  855. + struct i2c_client *client = cdata->adp1;
  856. + int ret;
  857. +
  858. + ret = i2c_smbus_read_byte_data(client, surface_i2c_ADP1_REG_PSR);
  859. + if (ret < 0)
  860. + return ret;
  861. +
  862. + return ret;
  863. +}
  864. +
  865. +static int surface_i2c_isr(struct surface_i2c_data *cdata)
  866. +{
  867. + struct bst bst;
  868. + struct bix bix;
  869. + int ret;
  870. + bool status, bat_status;
  871. +
  872. + ret = surface_i2c_adp_psr(cdata);
  873. + if (ret < 0)
  874. + return ret;
  875. +
  876. + status = ret;
  877. +
  878. + if (status != cdata->charging)
  879. + surface_i2c_notify(cdata, cdata->notify_version,
  880. + surface_i2c_NOTIFY_ADP1, &ret);
  881. +
  882. + cdata->charging = status;
  883. +
  884. + ret = surface_i2c_bst(cdata, &bst);
  885. + if (ret < 0)
  886. + return ret;
  887. +
  888. + bat_status = bst.battery_state;
  889. +
  890. + if (bat_status != cdata->bat_charging)
  891. + surface_i2c_notify(cdata, cdata->notify_version,
  892. + surface_i2c_NOTIFY_BAT0_BST, &ret);
  893. +
  894. + cdata->bat_charging = bat_status;
  895. +
  896. + ret = surface_i2c_bix(cdata, &bix);
  897. + if (ret < 0)
  898. + return ret;
  899. + if (bix.last_full_charg_capacity != cdata->full_capacity)
  900. + surface_i2c_notify(cdata, cdata->notify_version,
  901. + surface_i2c_NOTIFY_BAT0_BIX, &ret);
  902. +
  903. + cdata->full_capacity = bix.last_full_charg_capacity;
  904. +
  905. + return 0;
  906. +}
  907. +
  908. +static int surface_i2c_poll_task(void *data)
  909. +{
  910. + struct surface_i2c_data *cdata = data;
  911. + int ret = 0;
  912. +
  913. + cdata->kthread_running = true;
  914. +
  915. + set_freezable();
  916. +
  917. + while (!kthread_should_stop()) {
  918. + schedule_timeout_interruptible(POLL_INTERVAL);
  919. + try_to_freeze();
  920. + ret = surface_i2c_isr(data);
  921. + if (ret)
  922. + goto out;
  923. + }
  924. +
  925. +out:
  926. + cdata->kthread_running = false;
  927. + return ret;
  928. +}
  929. +
  930. +static acpi_status
  931. +surface_i2c_space_handler(u32 function, acpi_physical_address command,
  932. + u32 bits, u64 *value64,
  933. + void *handler_context, void *region_context)
  934. +{
  935. + struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
  936. + struct surface_i2c_handler_data *data = handler_context;
  937. + struct acpi_connection_info *info = &data->info;
  938. + struct acpi_resource_i2c_serialbus *sb;
  939. + struct i2c_client *client = data->client;
  940. + struct surface_i2c_data *cdata = i2c_get_clientdata(client);
  941. + struct acpi_resource *ares;
  942. + u32 accessor_type = function >> 16;
  943. + acpi_status ret;
  944. + int status = 1;
  945. +
  946. + ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
  947. + if (ACPI_FAILURE(ret))
  948. + return ret;
  949. +
  950. + if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  951. + ret = AE_BAD_PARAMETER;
  952. + goto err;
  953. + }
  954. +
  955. + sb = &ares->data.i2c_serial_bus;
  956. + if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  957. + ret = AE_BAD_PARAMETER;
  958. + goto err;
  959. + }
  960. +
  961. + if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
  962. + ret = AE_BAD_PARAMETER;
  963. + goto err;
  964. + }
  965. +
  966. + if (gsb->cmd.arg0 == surface_i2c_CMD_DEST_ADP1 &&
  967. + gsb->cmd.arg1 == surface_i2c_CMD_ADP1_PSR) {
  968. + ret = surface_i2c_adp_psr(cdata);
  969. + if (ret >= 0) {
  970. + status = ret;
  971. + ret = 0;
  972. + }
  973. + goto out;
  974. + }
  975. +
  976. + if (gsb->cmd.arg0 != surface_i2c_CMD_DEST_BAT0) {
  977. + ret = AE_BAD_PARAMETER;
  978. + goto err;
  979. + }
  980. +
  981. + switch (gsb->cmd.arg1) {
  982. + case surface_i2c_CMD_BAT0_STA:
  983. + status = 1;
  984. + ret = 0;
  985. + break;
  986. + case surface_i2c_CMD_BAT0_BIX:
  987. + status = 1;
  988. + ret = surface_i2c_bix(cdata, &gsb->bix);
  989. + break;
  990. + case surface_i2c_CMD_BAT0_BTP:
  991. + status = 1;
  992. + ret = 0;
  993. + cdata->trip_point = gsb->cmd.arg2;
  994. + break;
  995. + case surface_i2c_CMD_BAT0_BST:
  996. + status = 1;
  997. + ret = surface_i2c_bst(cdata, &gsb->bst);
  998. + break;
  999. + default:
  1000. + pr_info("command(0x%02x) is not supported.\n", gsb->cmd.arg1);
  1001. + ret = AE_BAD_PARAMETER;
  1002. + goto err;
  1003. + }
  1004. +
  1005. + out:
  1006. + gsb->ret = status;
  1007. + gsb->status = 0;
  1008. +
  1009. + err:
  1010. + ACPI_FREE(ares);
  1011. + return ret;
  1012. +}
  1013. +
  1014. +static int surface_i2c_install_space_handler(struct i2c_client *client)
  1015. +{
  1016. + acpi_handle handle;
  1017. + struct surface_i2c_handler_data *data;
  1018. + acpi_status status;
  1019. +
  1020. + handle = ACPI_HANDLE(&client->dev);
  1021. +
  1022. + if (!handle)
  1023. + return -ENODEV;
  1024. +
  1025. + data = kzalloc(sizeof(struct surface_i2c_handler_data),
  1026. + GFP_KERNEL);
  1027. + if (!data)
  1028. + return -ENOMEM;
  1029. +
  1030. + data->client = client;
  1031. + status = acpi_bus_attach_private_data(handle, (void *)data);
  1032. + if (ACPI_FAILURE(status)) {
  1033. + kfree(data);
  1034. + return -ENOMEM;
  1035. + }
  1036. +
  1037. + status = acpi_install_address_space_handler(handle,
  1038. + ACPI_ADR_SPACE_GSBUS,
  1039. + &surface_i2c_space_handler,
  1040. + NULL,
  1041. + data);
  1042. + if (ACPI_FAILURE(status)) {
  1043. + dev_err(&client->dev, "Error installing i2c space handler\n");
  1044. + acpi_bus_detach_private_data(handle);
  1045. + kfree(data);
  1046. + return -ENOMEM;
  1047. + }
  1048. +
  1049. + acpi_walk_dep_device_list(handle);
  1050. + return 0;
  1051. +}
  1052. +
  1053. +static void surface_i2c_remove_space_handler(struct i2c_client *client)
  1054. +{
  1055. + acpi_handle handle;
  1056. + struct surface_i2c_handler_data *data;
  1057. + acpi_status status;
  1058. +
  1059. + handle = ACPI_HANDLE(&client->dev);
  1060. +
  1061. + if (!handle)
  1062. + return;
  1063. +
  1064. + acpi_remove_address_space_handler(handle,
  1065. + ACPI_ADR_SPACE_GSBUS,
  1066. + &surface_i2c_space_handler);
  1067. +
  1068. + status = acpi_bus_get_private_data(handle, (void **)&data);
  1069. + if (ACPI_SUCCESS(status))
  1070. + kfree(data);
  1071. +
  1072. + acpi_bus_detach_private_data(handle);
  1073. +}
  1074. +
  1075. +static int acpi_find_i2c(struct acpi_resource *ares, void *data)
  1076. +{
  1077. + struct surface_i2c_lookup *lookup = data;
  1078. +
  1079. + if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  1080. + return 1;
  1081. +
  1082. + if (lookup->n++ == lookup->index && !lookup->addr)
  1083. + lookup->addr = ares->data.i2c_serial_bus.slave_address;
  1084. +
  1085. + return 1;
  1086. +}
  1087. +
  1088. +static int surface_i2c_resource_lookup(struct surface_i2c_data *cdata,
  1089. + unsigned int index)
  1090. +{
  1091. + struct i2c_client *client = cdata->adp1;
  1092. + struct acpi_device *adev = ACPI_COMPANION(&client->dev);
  1093. + struct surface_i2c_lookup lookup = {
  1094. + .cdata = cdata,
  1095. + .index = index,
  1096. + };
  1097. + struct list_head res_list;
  1098. + int ret;
  1099. +
  1100. + INIT_LIST_HEAD(&res_list);
  1101. +
  1102. + ret = acpi_dev_get_resources(adev, &res_list, acpi_find_i2c, &lookup);
  1103. + if (ret < 0)
  1104. + return ret;
  1105. +
  1106. + acpi_dev_free_resource_list(&res_list);
  1107. +
  1108. + if (!lookup.addr)
  1109. + return -ENOENT;
  1110. +
  1111. + return lookup.addr;
  1112. +}
  1113. +
  1114. +static void surface_i2c_dump_registers(struct i2c_client *client,
  1115. + struct i2c_client *bat0)
  1116. +{
  1117. + char rd_buf[60];
  1118. + int error, i, c;
  1119. + char buff[17 * 3 * 2] = {0};
  1120. +
  1121. + dev_info(&client->dev, "dumping registers 0x00 to 0x7F:\n");
  1122. +
  1123. + for (i = 0; i < 0x80; i += 0x20) {
  1124. + memset(rd_buf, 0, sizeof(rd_buf));
  1125. + error = surface_i2c_read_block(bat0, i, rd_buf, 0x20);
  1126. + dev_info(&client->dev, " read 0x%02x: %*ph|%*ph\n",
  1127. + i,
  1128. + 0x10, rd_buf,
  1129. + 0x10, rd_buf + 0x10);
  1130. + for (c = 0; c < 0x20; c++) {
  1131. + if (rd_buf[c] >= 0x20 && rd_buf[c] <= 0x7e) {
  1132. + buff[c * 3 + 0] = ' ';
  1133. + buff[c * 3 + 1] = rd_buf[c];
  1134. + } else {
  1135. + buff[c * 3 + 0] = '-';
  1136. + buff[c * 3 + 1] = '-';
  1137. + }
  1138. + buff[c * 3 + 2] = (c + 1) % 0x10 ? ' ' : '|';
  1139. + }
  1140. + buff[0x1f * 3 + 2] = '\0';
  1141. + dev_info(&client->dev, "ascii 0x%02x: %s\n", i, buff);
  1142. + }
  1143. +}
  1144. +
  1145. +static int surface_i2c_probe(struct i2c_client *client)
  1146. +{
  1147. + struct device *dev = &client->dev;
  1148. + struct i2c_client *bat0;
  1149. + struct surface_i2c_data *data;
  1150. + int error, version, addr;
  1151. +
  1152. + pr_info("surface_i2c: Probing for surface i2c device...\n");
  1153. +
  1154. + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  1155. + if (!data)
  1156. + return -ENOMEM;
  1157. +
  1158. + data->adp1 = client;
  1159. + i2c_set_clientdata(client, data);
  1160. +
  1161. + addr = surface_i2c_resource_lookup(data, 1);
  1162. + if (addr < 0)
  1163. + return addr;
  1164. +
  1165. + bat0 = i2c_new_dummy(client->adapter, addr);
  1166. + if (!bat0)
  1167. + return -ENOMEM;
  1168. +
  1169. + data->bat0 = bat0;
  1170. + i2c_set_clientdata(bat0, data);
  1171. +
  1172. + // debugging
  1173. + surface_i2c_dump_registers(client, bat0);
  1174. +
  1175. + pr_info("surface_i2c: Attaching device MSHW0124...");
  1176. +
  1177. + error = surface_i2c_notify(data, 1, surface_i2c_NOTIFY_GET_VERSION, &version);
  1178. + if (error)
  1179. + goto out_err;
  1180. +
  1181. + data->notify_version = version == surface_i2c_EV_2_5;
  1182. +
  1183. + data->poll_task = kthread_run(surface_i2c_poll_task, data, "surface_i2c_adp");
  1184. + if (IS_ERR(data->poll_task)) {
  1185. + error = PTR_ERR(data->poll_task);
  1186. + dev_err(&client->dev, "Unable to run kthread err %d\n", error);
  1187. + goto out_err;
  1188. + }
  1189. +
  1190. + //error = surface_i2c_install_space_handler(client);
  1191. + //if (error)
  1192. + // goto out_err;
  1193. +
  1194. + return 0;
  1195. +
  1196. +out_err:
  1197. + if (data->kthread_running)
  1198. + kthread_stop(data->poll_task);
  1199. + i2c_unregister_device(data->bat0);
  1200. + return error;
  1201. +}
  1202. +
  1203. +static int surface_i2c_remove(struct i2c_client *client)
  1204. +{
  1205. + struct surface_i2c_data *cdata = i2c_get_clientdata(client);
  1206. +
  1207. + surface_i2c_remove_space_handler(client);
  1208. +
  1209. + if (cdata->kthread_running)
  1210. + kthread_stop(cdata->poll_task);
  1211. +
  1212. + i2c_unregister_device(cdata->bat0);
  1213. +
  1214. + return 0;
  1215. +}
  1216. +
  1217. +int surface_i2c_detect(struct i2c_client* client, struct i2c_board_info* board_info)
  1218. +{
  1219. + pr_info("surface_i2c: Detecting surface_i2c device...");
  1220. + return 0;
  1221. +}
  1222. +
  1223. +static const struct acpi_device_id surface_i2c_acpi_match[] = {
  1224. + { "MSHW0124", 0 },
  1225. + { "", 0 }
  1226. +};
  1227. +MODULE_DEVICE_TABLE(acpi, surface_i2c_acpi_match);
  1228. +
  1229. +static const struct i2c_device_id surface_i2c_id[] = {
  1230. + { "MSHW0124:00", 0 },
  1231. + { "MSHW0124:01", 0 },
  1232. + { "", 0 }
  1233. +};
  1234. +MODULE_DEVICE_TABLE(i2c, surface_i2c_id);
  1235. +
  1236. +static struct i2c_driver surface_i2c_driver = {
  1237. + .probe_new = surface_i2c_probe,
  1238. + .remove = surface_i2c_remove,
  1239. + .detect = surface_i2c_detect,
  1240. + .id_table = surface_i2c_id,
  1241. + .driver = {
  1242. + .name = "surface_i2c",
  1243. + .acpi_match_table = ACPI_PTR(surface_i2c_acpi_match),
  1244. + },
  1245. +};
  1246. +module_i2c_driver(surface_i2c_driver);
  1247. +
  1248. +MODULE_AUTHOR("Jake Day");
  1249. +MODULE_DESCRIPTION("Microsoft Surface I2C Driver");
  1250. +MODULE_LICENSE("GPL");
  1251. diff --git a/drivers/platform/x86/surface_platform.c b/drivers/platform/x86/surface_platform.c
  1252. new file mode 100644
  1253. index 000000000000..7a84340b04bb
  1254. --- /dev/null
  1255. +++ b/drivers/platform/x86/surface_platform.c
  1256. @@ -0,0 +1,67 @@
  1257. +/*
  1258. + * surface_platform.c - Microsoft Surface Platform Driver
  1259. + *
  1260. + * This program is free software; you can redistribute it and/or modify
  1261. + * it under the terms of the GNU General Public License as published by
  1262. + * the Free Software Foundation; either version 2 of the License, or
  1263. + * (at your option) any later version.
  1264. + *
  1265. + * This program is distributed in the hope that it will be useful,
  1266. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1267. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1268. + * GNU General Public License for more details.
  1269. + *
  1270. + * The full GNU General Public License is included in this distribution in
  1271. + * the file called "COPYING".
  1272. + */
  1273. +
  1274. +#include <linux/acpi.h>
  1275. +#include <linux/gpio/consumer.h>
  1276. +#include <linux/kernel.h>
  1277. +#include <linux/module.h>
  1278. +#include <linux/moduleparam.h>
  1279. +#include <linux/platform_device.h>
  1280. +#include <linux/power_supply.h>
  1281. +#include <linux/thermal.h>
  1282. +#include <linux/uuid.h>
  1283. +#include <linux/workqueue.h>
  1284. +
  1285. +#include <asm/unaligned.h>
  1286. +
  1287. +struct surface_platform_data {
  1288. + struct device *dev;
  1289. +};
  1290. +
  1291. +static int surface_platform_probe(struct platform_device *pdev)
  1292. +{
  1293. + struct surface_platform_data *pdata;
  1294. +
  1295. + platform_set_drvdata(pdev, pdata);
  1296. + return 0;
  1297. +}
  1298. +
  1299. +static int surface_platform_remove(struct platform_device *pdev)
  1300. +{
  1301. + struct surface_platform_data *pdata = platform_get_drvdata(pdev);
  1302. +}
  1303. +
  1304. +static const struct acpi_device_id surface_platform_acpi_match[] = {
  1305. + { "MSHW0091", 0 },
  1306. + { "INT3403", 0 },
  1307. + { "", 0 }
  1308. +};
  1309. +MODULE_DEVICE_TABLE(acpi, surface_platform_acpi_match);
  1310. +
  1311. +static struct platform_driver surface_platform_driver = {
  1312. + .probe = surface_platform_probe,
  1313. + .remove = surface_platform_remove,
  1314. + .driver = {
  1315. + .name = "surface_platform",
  1316. + .acpi_match_table = ACPI_PTR(surface_platform_acpi_match),
  1317. + },
  1318. +};
  1319. +module_platform_driver(surface_platform_driver);
  1320. +
  1321. +MODULE_AUTHOR("Jake Day");
  1322. +MODULE_DESCRIPTION("Microsoft Surface Platform Driver");
  1323. +MODULE_LICENSE("GPL");