bmc150-accel-i2c.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * 3-axis accelerometer driver supporting many I2C Bosch-Sensortec chips
  4. * Copyright (c) 2014, Intel Corporation.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/i2c.h>
  9. #include <linux/module.h>
  10. #include <linux/acpi.h>
  11. #include <linux/regmap.h>
  12. #include "bmc150-accel.h"
  13. #ifdef CONFIG_ACPI
  14. static const struct acpi_device_id bmc150_acpi_dual_accel_ids[] = {
  15. {"BOSC0200"},
  16. {"DUAL250E"},
  17. { }
  18. };
  19. /*
  20. * The DUAL250E ACPI device for 360° hinges type 2-in-1s with 1 accelerometer
  21. * in the display and 1 in the hinge has an ACPI-method (DSM) to tell the
  22. * ACPI code about the angle between the 2 halves. This will make the ACPI
  23. * code enable/disable the keyboard and touchpad. We need to call this to avoid
  24. * the keyboard being disabled when the 2-in-1 is turned-on or resumed while
  25. * fully folded into tablet mode (which gets detected with a HALL-sensor).
  26. * If we don't call this then the keyboard won't work even when the 2-in-1 is
  27. * changed to be used in laptop mode after the power-on / resume.
  28. *
  29. * This DSM takes 2 angles, selected by setting aux0 to 0 or 1, these presumably
  30. * define the angle between the gravity vector measured by the accelerometer in
  31. * the display (aux0=0) resp. the base (aux0=1) and some reference vector.
  32. * The 2 angles get subtracted from each other so the reference vector does
  33. * not matter and we can simply leave the second angle at 0.
  34. */
  35. #define BMC150_DSM_GUID "7681541e-8827-4239-8d9d-36be7fe12542"
  36. #define DUAL250E_SET_ANGLE_FN_INDEX 3
  37. struct dual250e_set_angle_args {
  38. u32 aux0;
  39. u32 ang0;
  40. u32 rawx;
  41. u32 rawy;
  42. u32 rawz;
  43. } __packed;
  44. static bool bmc150_acpi_set_angle_dsm(struct i2c_client *client, u32 aux0, u32 ang0)
  45. {
  46. struct acpi_device *adev = ACPI_COMPANION(&client->dev);
  47. struct dual250e_set_angle_args args = {
  48. .aux0 = aux0,
  49. .ang0 = ang0,
  50. };
  51. union acpi_object args_obj, *obj;
  52. guid_t guid;
  53. if (!acpi_dev_hid_uid_match(adev, "DUAL250E", NULL))
  54. return false;
  55. guid_parse(BMC150_DSM_GUID, &guid);
  56. if (!acpi_check_dsm(adev->handle, &guid, 0, BIT(DUAL250E_SET_ANGLE_FN_INDEX)))
  57. return false;
  58. /*
  59. * Note this triggers the following warning:
  60. * "ACPI Warning: \_SB.PCI0.I2C2.ACC1._DSM: Argument #4 type mismatch -
  61. * Found [Buffer], ACPI requires [Package]"
  62. * This is unavoidable since the _DSM implementation expects a "naked"
  63. * buffer, so wrapping it in a package will _not_ work.
  64. */
  65. args_obj.type = ACPI_TYPE_BUFFER;
  66. args_obj.buffer.length = sizeof(args);
  67. args_obj.buffer.pointer = (u8 *)&args;
  68. obj = acpi_evaluate_dsm(adev->handle, &guid, 0, DUAL250E_SET_ANGLE_FN_INDEX, &args_obj);
  69. if (!obj) {
  70. dev_err(&client->dev, "Failed to call DSM to enable keyboard and touchpad\n");
  71. return false;
  72. }
  73. ACPI_FREE(obj);
  74. return true;
  75. }
  76. static bool bmc150_acpi_enable_keyboard(struct i2c_client *client)
  77. {
  78. /*
  79. * The EC must see a change for it to re-enable the kbd, so first
  80. * set the angle to 270° (tent/stand mode) and then change it to
  81. * 90° (laptop mode).
  82. */
  83. if (!bmc150_acpi_set_angle_dsm(client, 0, 270))
  84. return false;
  85. /* The EC needs some time to notice the angle being changed */
  86. msleep(100);
  87. return bmc150_acpi_set_angle_dsm(client, 0, 90);
  88. }
  89. static void bmc150_acpi_resume_work(struct work_struct *work)
  90. {
  91. struct bmc150_accel_data *data =
  92. container_of(work, struct bmc150_accel_data, resume_work.work);
  93. bmc150_acpi_enable_keyboard(data->second_device);
  94. }
  95. static void bmc150_acpi_resume_handler(struct device *dev)
  96. {
  97. struct bmc150_accel_data *data = iio_priv(dev_get_drvdata(dev));
  98. /*
  99. * Delay the bmc150_acpi_enable_keyboard() call till after the system
  100. * resume has completed, otherwise it will not work.
  101. */
  102. schedule_delayed_work(&data->resume_work, msecs_to_jiffies(1000));
  103. }
  104. /*
  105. * Some acpi_devices describe 2 accelerometers in a single ACPI device,
  106. * try instantiating a second i2c_client for an I2cSerialBusV2 ACPI resource
  107. * with index 1.
  108. */
  109. static void bmc150_acpi_dual_accel_probe(struct i2c_client *client)
  110. {
  111. struct bmc150_accel_data *data = iio_priv(i2c_get_clientdata(client));
  112. struct acpi_device *adev = ACPI_COMPANION(&client->dev);
  113. char dev_name[16];
  114. struct i2c_board_info board_info = {
  115. .type = "bmc150_accel",
  116. .dev_name = dev_name,
  117. .fwnode = client->dev.fwnode,
  118. };
  119. if (acpi_match_device_ids(adev, bmc150_acpi_dual_accel_ids))
  120. return;
  121. /*
  122. * The 2nd accel sits in the base of 2-in-1s. The suffix is static, as
  123. * there should never be more then 1 ACPI node with 2 accelerometers.
  124. */
  125. snprintf(dev_name, sizeof(dev_name), "%s:base", acpi_device_hid(adev));
  126. board_info.irq = acpi_dev_gpio_irq_get(adev, 1);
  127. data->second_device = i2c_acpi_new_device(&client->dev, 1, &board_info);
  128. if (!IS_ERR(data->second_device) && bmc150_acpi_enable_keyboard(data->second_device)) {
  129. INIT_DELAYED_WORK(&data->resume_work, bmc150_acpi_resume_work);
  130. data->resume_callback = bmc150_acpi_resume_handler;
  131. }
  132. }
  133. static void bmc150_acpi_dual_accel_remove(struct i2c_client *client)
  134. {
  135. struct bmc150_accel_data *data = iio_priv(i2c_get_clientdata(client));
  136. if (data->resume_callback)
  137. cancel_delayed_work_sync(&data->resume_work);
  138. i2c_unregister_device(data->second_device);
  139. }
  140. #else
  141. static void bmc150_acpi_dual_accel_probe(struct i2c_client *client) {}
  142. static void bmc150_acpi_dual_accel_remove(struct i2c_client *client) {}
  143. #endif
  144. static int bmc150_accel_probe(struct i2c_client *client,
  145. const struct i2c_device_id *id)
  146. {
  147. struct regmap *regmap;
  148. const char *name = NULL;
  149. enum bmc150_type type = BOSCH_UNKNOWN;
  150. bool block_supported =
  151. i2c_check_functionality(client->adapter, I2C_FUNC_I2C) ||
  152. i2c_check_functionality(client->adapter,
  153. I2C_FUNC_SMBUS_READ_I2C_BLOCK);
  154. int ret;
  155. regmap = devm_regmap_init_i2c(client, &bmc150_regmap_conf);
  156. if (IS_ERR(regmap)) {
  157. dev_err(&client->dev, "Failed to initialize i2c regmap\n");
  158. return PTR_ERR(regmap);
  159. }
  160. if (id) {
  161. name = id->name;
  162. type = id->driver_data;
  163. }
  164. ret = bmc150_accel_core_probe(&client->dev, regmap, client->irq,
  165. type, name, block_supported);
  166. if (ret)
  167. return ret;
  168. /*
  169. * The !id check avoids recursion when probe() gets called
  170. * for the second client.
  171. */
  172. if (!id && has_acpi_companion(&client->dev))
  173. bmc150_acpi_dual_accel_probe(client);
  174. return 0;
  175. }
  176. static void bmc150_accel_remove(struct i2c_client *client)
  177. {
  178. bmc150_acpi_dual_accel_remove(client);
  179. bmc150_accel_core_remove(&client->dev);
  180. }
  181. static const struct acpi_device_id bmc150_accel_acpi_match[] = {
  182. {"BMA0255"},
  183. {"BMA0280"},
  184. {"BMA222"},
  185. {"BMA222E"},
  186. {"BMA250E"},
  187. {"BMC150A"},
  188. {"BMI055A"},
  189. {"BOSC0200"},
  190. {"BSBA0150"},
  191. {"DUAL250E"},
  192. { },
  193. };
  194. MODULE_DEVICE_TABLE(acpi, bmc150_accel_acpi_match);
  195. static const struct i2c_device_id bmc150_accel_id[] = {
  196. {"bma222"},
  197. {"bma222e"},
  198. {"bma250e"},
  199. {"bma253"},
  200. {"bma254"},
  201. {"bma255"},
  202. {"bma280"},
  203. {"bmc150_accel"},
  204. {"bmc156_accel", BOSCH_BMC156},
  205. {"bmi055_accel"},
  206. {}
  207. };
  208. MODULE_DEVICE_TABLE(i2c, bmc150_accel_id);
  209. static const struct of_device_id bmc150_accel_of_match[] = {
  210. { .compatible = "bosch,bma222" },
  211. { .compatible = "bosch,bma222e" },
  212. { .compatible = "bosch,bma250e" },
  213. { .compatible = "bosch,bma253" },
  214. { .compatible = "bosch,bma254" },
  215. { .compatible = "bosch,bma255" },
  216. { .compatible = "bosch,bma280" },
  217. { .compatible = "bosch,bmc150_accel" },
  218. { .compatible = "bosch,bmc156_accel" },
  219. { .compatible = "bosch,bmi055_accel" },
  220. { },
  221. };
  222. MODULE_DEVICE_TABLE(of, bmc150_accel_of_match);
  223. static struct i2c_driver bmc150_accel_driver = {
  224. .driver = {
  225. .name = "bmc150_accel_i2c",
  226. .of_match_table = bmc150_accel_of_match,
  227. .acpi_match_table = ACPI_PTR(bmc150_accel_acpi_match),
  228. .pm = &bmc150_accel_pm_ops,
  229. },
  230. .probe = bmc150_accel_probe,
  231. .remove = bmc150_accel_remove,
  232. .id_table = bmc150_accel_id,
  233. };
  234. module_i2c_driver(bmc150_accel_driver);
  235. MODULE_AUTHOR("Srinivas Pandruvada <[email protected]>");
  236. MODULE_LICENSE("GPL v2");
  237. MODULE_DESCRIPTION("BMC150 I2C accelerometer driver");
  238. MODULE_IMPORT_NS(IIO_BMC150);