cros_ec_dev.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space
  4. *
  5. * Copyright (C) 2014 Google, Inc.
  6. */
  7. #include <linux/dmi.h>
  8. #include <linux/kconfig.h>
  9. #include <linux/mfd/core.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/platform_data/cros_ec_chardev.h>
  15. #include <linux/platform_data/cros_ec_commands.h>
  16. #include <linux/platform_data/cros_ec_proto.h>
  17. #include <linux/slab.h>
  18. #define DRV_NAME "cros-ec-dev"
  19. static struct class cros_class = {
  20. .owner = THIS_MODULE,
  21. .name = "chromeos",
  22. };
  23. /**
  24. * struct cros_feature_to_name - CrOS feature id to name/short description.
  25. * @id: The feature identifier.
  26. * @name: Device name associated with the feature id.
  27. * @desc: Short name that will be displayed.
  28. */
  29. struct cros_feature_to_name {
  30. unsigned int id;
  31. const char *name;
  32. const char *desc;
  33. };
  34. /**
  35. * struct cros_feature_to_cells - CrOS feature id to mfd cells association.
  36. * @id: The feature identifier.
  37. * @mfd_cells: Pointer to the array of mfd cells that needs to be added.
  38. * @num_cells: Number of mfd cells into the array.
  39. */
  40. struct cros_feature_to_cells {
  41. unsigned int id;
  42. const struct mfd_cell *mfd_cells;
  43. unsigned int num_cells;
  44. };
  45. static const struct cros_feature_to_name cros_mcu_devices[] = {
  46. {
  47. .id = EC_FEATURE_FINGERPRINT,
  48. .name = CROS_EC_DEV_FP_NAME,
  49. .desc = "Fingerprint",
  50. },
  51. {
  52. .id = EC_FEATURE_ISH,
  53. .name = CROS_EC_DEV_ISH_NAME,
  54. .desc = "Integrated Sensor Hub",
  55. },
  56. {
  57. .id = EC_FEATURE_SCP,
  58. .name = CROS_EC_DEV_SCP_NAME,
  59. .desc = "System Control Processor",
  60. },
  61. {
  62. .id = EC_FEATURE_SCP_C1,
  63. .name = CROS_EC_DEV_SCP_C1_NAME,
  64. .desc = "System Control Processor 2nd Core",
  65. },
  66. {
  67. .id = EC_FEATURE_TOUCHPAD,
  68. .name = CROS_EC_DEV_TP_NAME,
  69. .desc = "Touchpad",
  70. },
  71. };
  72. static const struct mfd_cell cros_ec_cec_cells[] = {
  73. { .name = "cros-ec-cec", },
  74. };
  75. static const struct mfd_cell cros_ec_rtc_cells[] = {
  76. { .name = "cros-ec-rtc", },
  77. };
  78. static const struct mfd_cell cros_ec_sensorhub_cells[] = {
  79. { .name = "cros-ec-sensorhub", },
  80. };
  81. static const struct mfd_cell cros_usbpd_charger_cells[] = {
  82. { .name = "cros-usbpd-charger", },
  83. { .name = "cros-usbpd-logger", },
  84. };
  85. static const struct mfd_cell cros_usbpd_notify_cells[] = {
  86. { .name = "cros-usbpd-notify", },
  87. };
  88. static const struct cros_feature_to_cells cros_subdevices[] = {
  89. {
  90. .id = EC_FEATURE_CEC,
  91. .mfd_cells = cros_ec_cec_cells,
  92. .num_cells = ARRAY_SIZE(cros_ec_cec_cells),
  93. },
  94. {
  95. .id = EC_FEATURE_RTC,
  96. .mfd_cells = cros_ec_rtc_cells,
  97. .num_cells = ARRAY_SIZE(cros_ec_rtc_cells),
  98. },
  99. {
  100. .id = EC_FEATURE_USB_PD,
  101. .mfd_cells = cros_usbpd_charger_cells,
  102. .num_cells = ARRAY_SIZE(cros_usbpd_charger_cells),
  103. },
  104. };
  105. static const struct mfd_cell cros_ec_platform_cells[] = {
  106. { .name = "cros-ec-chardev", },
  107. { .name = "cros-ec-debugfs", },
  108. { .name = "cros-ec-sysfs", },
  109. };
  110. static const struct mfd_cell cros_ec_pchg_cells[] = {
  111. { .name = "cros-ec-pchg", },
  112. };
  113. static const struct mfd_cell cros_ec_lightbar_cells[] = {
  114. { .name = "cros-ec-lightbar", }
  115. };
  116. static const struct mfd_cell cros_ec_vbc_cells[] = {
  117. { .name = "cros-ec-vbc", }
  118. };
  119. static void cros_ec_class_release(struct device *dev)
  120. {
  121. kfree(to_cros_ec_dev(dev));
  122. }
  123. static int ec_device_probe(struct platform_device *pdev)
  124. {
  125. int retval = -ENOMEM;
  126. struct device_node *node;
  127. struct device *dev = &pdev->dev;
  128. struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
  129. struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  130. struct ec_response_pchg_count pchg_count;
  131. int i;
  132. if (!ec)
  133. return retval;
  134. dev_set_drvdata(dev, ec);
  135. ec->ec_dev = dev_get_drvdata(dev->parent);
  136. ec->dev = dev;
  137. ec->cmd_offset = ec_platform->cmd_offset;
  138. ec->features.flags[0] = -1U; /* Not cached yet */
  139. ec->features.flags[1] = -1U; /* Not cached yet */
  140. device_initialize(&ec->class_dev);
  141. for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) {
  142. /*
  143. * Check whether this is actually a dedicated MCU rather
  144. * than an standard EC.
  145. */
  146. if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) {
  147. dev_info(dev, "CrOS %s MCU detected\n",
  148. cros_mcu_devices[i].desc);
  149. /*
  150. * Help userspace differentiating ECs from other MCU,
  151. * regardless of the probing order.
  152. */
  153. ec_platform->ec_name = cros_mcu_devices[i].name;
  154. break;
  155. }
  156. }
  157. /*
  158. * Add the class device
  159. */
  160. ec->class_dev.class = &cros_class;
  161. ec->class_dev.parent = dev;
  162. ec->class_dev.release = cros_ec_class_release;
  163. retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
  164. if (retval) {
  165. dev_err(dev, "dev_set_name failed => %d\n", retval);
  166. goto failed;
  167. }
  168. retval = device_add(&ec->class_dev);
  169. if (retval)
  170. goto failed;
  171. /* check whether this EC is a sensor hub. */
  172. if (cros_ec_get_sensor_count(ec) > 0) {
  173. retval = mfd_add_hotplug_devices(ec->dev,
  174. cros_ec_sensorhub_cells,
  175. ARRAY_SIZE(cros_ec_sensorhub_cells));
  176. if (retval)
  177. dev_err(ec->dev, "failed to add %s subdevice: %d\n",
  178. cros_ec_sensorhub_cells->name, retval);
  179. }
  180. /*
  181. * The following subdevices can be detected by sending the
  182. * EC_FEATURE_GET_CMD Embedded Controller device.
  183. */
  184. for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) {
  185. if (cros_ec_check_features(ec, cros_subdevices[i].id)) {
  186. retval = mfd_add_hotplug_devices(ec->dev,
  187. cros_subdevices[i].mfd_cells,
  188. cros_subdevices[i].num_cells);
  189. if (retval)
  190. dev_err(ec->dev,
  191. "failed to add %s subdevice: %d\n",
  192. cros_subdevices[i].mfd_cells->name,
  193. retval);
  194. }
  195. }
  196. /*
  197. * Lightbar is a special case. Newer devices support autodetection,
  198. * but older ones do not.
  199. */
  200. if (cros_ec_check_features(ec, EC_FEATURE_LIGHTBAR) ||
  201. dmi_match(DMI_PRODUCT_NAME, "Link")) {
  202. retval = mfd_add_hotplug_devices(ec->dev,
  203. cros_ec_lightbar_cells,
  204. ARRAY_SIZE(cros_ec_lightbar_cells));
  205. if (retval)
  206. dev_warn(ec->dev, "failed to add lightbar: %d\n",
  207. retval);
  208. }
  209. /*
  210. * The PD notifier driver cell is separate since it only needs to be
  211. * explicitly added on platforms that don't have the PD notifier ACPI
  212. * device entry defined.
  213. */
  214. if (IS_ENABLED(CONFIG_OF) && ec->ec_dev->dev->of_node) {
  215. if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
  216. retval = mfd_add_hotplug_devices(ec->dev,
  217. cros_usbpd_notify_cells,
  218. ARRAY_SIZE(cros_usbpd_notify_cells));
  219. if (retval)
  220. dev_err(ec->dev,
  221. "failed to add PD notify devices: %d\n",
  222. retval);
  223. }
  224. }
  225. /*
  226. * The PCHG device cannot be detected by sending EC_FEATURE_GET_CMD, but
  227. * it can be detected by querying the number of peripheral chargers.
  228. */
  229. retval = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_PCHG_COUNT, NULL, 0,
  230. &pchg_count, sizeof(pchg_count));
  231. if (retval >= 0 && pchg_count.port_count) {
  232. retval = mfd_add_hotplug_devices(ec->dev,
  233. cros_ec_pchg_cells,
  234. ARRAY_SIZE(cros_ec_pchg_cells));
  235. if (retval)
  236. dev_warn(ec->dev, "failed to add pchg: %d\n",
  237. retval);
  238. }
  239. /*
  240. * The following subdevices cannot be detected by sending the
  241. * EC_FEATURE_GET_CMD to the Embedded Controller device.
  242. */
  243. retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells,
  244. ARRAY_SIZE(cros_ec_platform_cells));
  245. if (retval)
  246. dev_warn(ec->dev,
  247. "failed to add cros-ec platform devices: %d\n",
  248. retval);
  249. /* Check whether this EC instance has a VBC NVRAM */
  250. node = ec->ec_dev->dev->of_node;
  251. if (of_property_read_bool(node, "google,has-vbc-nvram")) {
  252. retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells,
  253. ARRAY_SIZE(cros_ec_vbc_cells));
  254. if (retval)
  255. dev_warn(ec->dev, "failed to add VBC devices: %d\n",
  256. retval);
  257. }
  258. return 0;
  259. failed:
  260. put_device(&ec->class_dev);
  261. return retval;
  262. }
  263. static int ec_device_remove(struct platform_device *pdev)
  264. {
  265. struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
  266. mfd_remove_devices(ec->dev);
  267. device_unregister(&ec->class_dev);
  268. return 0;
  269. }
  270. static const struct platform_device_id cros_ec_id[] = {
  271. { DRV_NAME, 0 },
  272. { /* sentinel */ }
  273. };
  274. MODULE_DEVICE_TABLE(platform, cros_ec_id);
  275. static struct platform_driver cros_ec_dev_driver = {
  276. .driver = {
  277. .name = DRV_NAME,
  278. },
  279. .id_table = cros_ec_id,
  280. .probe = ec_device_probe,
  281. .remove = ec_device_remove,
  282. };
  283. static int __init cros_ec_dev_init(void)
  284. {
  285. int ret;
  286. ret = class_register(&cros_class);
  287. if (ret) {
  288. pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
  289. return ret;
  290. }
  291. /* Register the driver */
  292. ret = platform_driver_register(&cros_ec_dev_driver);
  293. if (ret < 0) {
  294. pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
  295. goto failed_devreg;
  296. }
  297. return 0;
  298. failed_devreg:
  299. class_unregister(&cros_class);
  300. return ret;
  301. }
  302. static void __exit cros_ec_dev_exit(void)
  303. {
  304. platform_driver_unregister(&cros_ec_dev_driver);
  305. class_unregister(&cros_class);
  306. }
  307. module_init(cros_ec_dev_init);
  308. module_exit(cros_ec_dev_exit);
  309. MODULE_AUTHOR("Bill Richardson <[email protected]>");
  310. MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
  311. MODULE_VERSION("1.0");
  312. MODULE_LICENSE("GPL");