1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ChromeOS Embedded Controller 4 * 5 * Copyright (C) 2014 Google, Inc. 6 */ 7 8 #include <linux/acpi.h> 9 #include <linux/dmi.h> 10 #include <linux/kconfig.h> 11 #include <linux/mfd/core.h> 12 #include <linux/module.h> 13 #include <linux/mod_devicetable.h> 14 #include <linux/of.h> 15 #include <linux/platform_device.h> 16 #include <linux/platform_data/cros_ec_chardev.h> 17 #include <linux/platform_data/cros_ec_commands.h> 18 #include <linux/platform_data/cros_ec_proto.h> 19 #include <linux/slab.h> 20 21 #define DRV_NAME "cros-ec-dev" 22 23 static struct class cros_class = { 24 .name = "chromeos", 25 }; 26 27 /** 28 * struct cros_feature_to_name - CrOS feature id to name/short description. 29 * @id: The feature identifier. 30 * @name: Device name associated with the feature id. 31 * @desc: Short name that will be displayed. 32 */ 33 struct cros_feature_to_name { 34 unsigned int id; 35 const char *name; 36 const char *desc; 37 }; 38 39 /** 40 * struct cros_feature_to_cells - CrOS feature id to mfd cells association. 41 * @id: The feature identifier. 42 * @mfd_cells: Pointer to the array of mfd cells that needs to be added. 43 * @num_cells: Number of mfd cells into the array. 44 */ 45 struct cros_feature_to_cells { 46 unsigned int id; 47 const struct mfd_cell *mfd_cells; 48 unsigned int num_cells; 49 }; 50 51 static const struct cros_feature_to_name cros_mcu_devices[] = { 52 { 53 .id = EC_FEATURE_FINGERPRINT, 54 .name = CROS_EC_DEV_FP_NAME, 55 .desc = "Fingerprint", 56 }, 57 { 58 .id = EC_FEATURE_ISH, 59 .name = CROS_EC_DEV_ISH_NAME, 60 .desc = "Integrated Sensor Hub", 61 }, 62 { 63 .id = EC_FEATURE_SCP, 64 .name = CROS_EC_DEV_SCP_NAME, 65 .desc = "System Control Processor", 66 }, 67 { 68 .id = EC_FEATURE_TOUCHPAD, 69 .name = CROS_EC_DEV_TP_NAME, 70 .desc = "Touchpad", 71 }, 72 }; 73 74 static const struct mfd_cell cros_ec_cec_cells[] = { 75 { .name = "cros-ec-cec", }, 76 }; 77 78 static const struct mfd_cell cros_ec_gpio_cells[] = { 79 { .name = "cros-ec-gpio", }, 80 }; 81 82 static const struct mfd_cell cros_ec_rtc_cells[] = { 83 { .name = "cros-ec-rtc", }, 84 }; 85 86 static const struct mfd_cell cros_ec_sensorhub_cells[] = { 87 { .name = "cros-ec-sensorhub", }, 88 }; 89 90 static const struct mfd_cell cros_usbpd_charger_cells[] = { 91 { .name = "cros-usbpd-charger", }, 92 { .name = "cros-usbpd-logger", }, 93 }; 94 95 static const struct mfd_cell cros_usbpd_notify_cells[] = { 96 { .name = "cros-usbpd-notify", }, 97 }; 98 99 static const struct mfd_cell cros_ec_wdt_cells[] = { 100 { .name = "cros-ec-wdt", } 101 }; 102 103 static const struct mfd_cell cros_ec_led_cells[] = { 104 { .name = "cros-ec-led", }, 105 }; 106 107 static const struct mfd_cell cros_ec_keyboard_leds_cells[] = { 108 { .name = "cros-keyboard-leds", }, 109 }; 110 111 static const struct mfd_cell cros_ec_ucsi_cells[] = { 112 { .name = "cros_ec_ucsi", }, 113 }; 114 115 static const struct mfd_cell cros_ec_charge_control_cells[] = { 116 { .name = "cros-charge-control", }, 117 }; 118 119 static const struct cros_feature_to_cells cros_subdevices[] = { 120 { 121 .id = EC_FEATURE_CEC, 122 .mfd_cells = cros_ec_cec_cells, 123 .num_cells = ARRAY_SIZE(cros_ec_cec_cells), 124 }, 125 { 126 .id = EC_FEATURE_GPIO, 127 .mfd_cells = cros_ec_gpio_cells, 128 .num_cells = ARRAY_SIZE(cros_ec_gpio_cells), 129 }, 130 { 131 .id = EC_FEATURE_RTC, 132 .mfd_cells = cros_ec_rtc_cells, 133 .num_cells = ARRAY_SIZE(cros_ec_rtc_cells), 134 }, 135 { 136 .id = EC_FEATURE_HANG_DETECT, 137 .mfd_cells = cros_ec_wdt_cells, 138 .num_cells = ARRAY_SIZE(cros_ec_wdt_cells), 139 }, 140 { 141 .id = EC_FEATURE_LED, 142 .mfd_cells = cros_ec_led_cells, 143 .num_cells = ARRAY_SIZE(cros_ec_led_cells), 144 }, 145 { 146 .id = EC_FEATURE_PWM_KEYB, 147 .mfd_cells = cros_ec_keyboard_leds_cells, 148 .num_cells = ARRAY_SIZE(cros_ec_keyboard_leds_cells), 149 }, 150 { 151 .id = EC_FEATURE_CHARGER, 152 .mfd_cells = cros_ec_charge_control_cells, 153 .num_cells = ARRAY_SIZE(cros_ec_charge_control_cells), 154 }, 155 }; 156 157 static const struct mfd_cell cros_ec_platform_cells[] = { 158 { .name = "cros-ec-chardev", }, 159 { .name = "cros-ec-debugfs", }, 160 { .name = "cros-ec-hwmon", }, 161 { .name = "cros-ec-sysfs", }, 162 }; 163 164 static const struct mfd_cell cros_ec_pchg_cells[] = { 165 { .name = "cros-ec-pchg", }, 166 }; 167 168 static const struct mfd_cell cros_ec_lightbar_cells[] = { 169 { .name = "cros-ec-lightbar", } 170 }; 171 172 static const struct mfd_cell cros_ec_vbc_cells[] = { 173 { .name = "cros-ec-vbc", } 174 }; 175 176 static void cros_ec_class_release(struct device *dev) 177 { 178 kfree(to_cros_ec_dev(dev)); 179 } 180 181 static int ec_device_probe(struct platform_device *pdev) 182 { 183 int retval = -ENOMEM; 184 struct device_node *node; 185 struct device *dev = &pdev->dev; 186 struct cros_ec_platform *ec_platform = dev_get_platdata(dev); 187 struct cros_ec_dev *ec = kzalloc_obj(*ec); 188 struct ec_response_pchg_count pchg_count; 189 int i; 190 191 if (!ec) 192 return retval; 193 194 ec->ec_dev = dev_get_drvdata(dev->parent); 195 ec->dev = dev; 196 ec->cmd_offset = ec_platform->cmd_offset; 197 ec->features.flags[0] = -1U; /* Not cached yet */ 198 ec->features.flags[1] = -1U; /* Not cached yet */ 199 device_initialize(&ec->class_dev); 200 201 for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) { 202 /* 203 * Check whether this is actually a dedicated MCU rather 204 * than an standard EC. 205 */ 206 if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) { 207 dev_info(dev, "CrOS %s MCU detected\n", 208 cros_mcu_devices[i].desc); 209 /* 210 * Help userspace differentiating ECs from other MCU, 211 * regardless of the probing order. 212 */ 213 ec_platform->ec_name = cros_mcu_devices[i].name; 214 break; 215 } 216 } 217 218 /* 219 * Add the class device 220 */ 221 ec->class_dev.class = &cros_class; 222 ec->class_dev.parent = dev; 223 ec->class_dev.release = cros_ec_class_release; 224 225 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name); 226 if (retval) { 227 dev_err(dev, "dev_set_name failed => %d\n", retval); 228 goto failed; 229 } 230 231 retval = device_add(&ec->class_dev); 232 if (retval) 233 goto failed; 234 235 dev_set_drvdata(dev, ec); 236 237 /* check whether this EC is a sensor hub. */ 238 if (cros_ec_get_sensor_count(ec) > 0) { 239 retval = mfd_add_hotplug_devices(ec->dev, 240 cros_ec_sensorhub_cells, 241 ARRAY_SIZE(cros_ec_sensorhub_cells)); 242 if (retval) 243 dev_err(ec->dev, "failed to add %s subdevice: %d\n", 244 cros_ec_sensorhub_cells->name, retval); 245 } 246 247 /* 248 * The following subdevices can be detected by sending the 249 * EC_FEATURE_GET_CMD Embedded Controller device. 250 */ 251 for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) { 252 if (cros_ec_check_features(ec, cros_subdevices[i].id)) { 253 retval = mfd_add_hotplug_devices(ec->dev, 254 cros_subdevices[i].mfd_cells, 255 cros_subdevices[i].num_cells); 256 if (retval) 257 dev_err(ec->dev, 258 "failed to add %s subdevice: %d\n", 259 cros_subdevices[i].mfd_cells->name, 260 retval); 261 } 262 } 263 264 /* 265 * FW nodes can load cros_ec_ucsi, but early PDC devices did not define 266 * the required nodes. On PDC systems without FW nodes for cros_ec_ucsi, 267 * the driver should be added as an mfd subdevice. 268 */ 269 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD) && 270 cros_ec_check_features(ec, EC_FEATURE_UCSI_PPM) && 271 !acpi_dev_found("GOOG0021") && 272 !of_find_compatible_node(NULL, NULL, "google,cros-ec-ucsi")) { 273 retval = mfd_add_hotplug_devices(ec->dev, 274 cros_ec_ucsi_cells, 275 ARRAY_SIZE(cros_ec_ucsi_cells)); 276 277 if (retval) 278 dev_warn(ec->dev, "failed to add cros_ec_ucsi: %d\n", retval); 279 } 280 281 /* 282 * UCSI provides power supply information so we don't need to separately 283 * load the cros_usbpd_charger driver. 284 */ 285 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD) && 286 !cros_ec_check_features(ec, EC_FEATURE_UCSI_PPM)) { 287 retval = mfd_add_hotplug_devices(ec->dev, 288 cros_usbpd_charger_cells, 289 ARRAY_SIZE(cros_usbpd_charger_cells)); 290 291 if (retval) 292 dev_warn(ec->dev, "failed to add usbpd-charger: %d\n", 293 retval); 294 } 295 296 /* 297 * Lightbar is a special case. Newer devices support autodetection, 298 * but older ones do not. 299 */ 300 if (cros_ec_check_features(ec, EC_FEATURE_LIGHTBAR) || 301 dmi_match(DMI_PRODUCT_NAME, "Link")) { 302 retval = mfd_add_hotplug_devices(ec->dev, 303 cros_ec_lightbar_cells, 304 ARRAY_SIZE(cros_ec_lightbar_cells)); 305 if (retval) 306 dev_warn(ec->dev, "failed to add lightbar: %d\n", 307 retval); 308 } 309 310 /* 311 * The PD notifier driver cell is separate since it only needs to be 312 * explicitly added on platforms that don't have the PD notifier ACPI 313 * device entry defined. 314 */ 315 if (IS_ENABLED(CONFIG_OF) && ec->ec_dev->dev->of_node) { 316 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) { 317 retval = mfd_add_hotplug_devices(ec->dev, 318 cros_usbpd_notify_cells, 319 ARRAY_SIZE(cros_usbpd_notify_cells)); 320 if (retval) 321 dev_err(ec->dev, 322 "failed to add PD notify devices: %d\n", 323 retval); 324 } 325 } 326 327 /* 328 * The PCHG device cannot be detected by sending EC_FEATURE_GET_CMD, but 329 * it can be detected by querying the number of peripheral chargers. 330 */ 331 retval = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_PCHG_COUNT, NULL, 0, 332 &pchg_count, sizeof(pchg_count)); 333 if (retval >= 0 && pchg_count.port_count) { 334 retval = mfd_add_hotplug_devices(ec->dev, 335 cros_ec_pchg_cells, 336 ARRAY_SIZE(cros_ec_pchg_cells)); 337 if (retval) 338 dev_warn(ec->dev, "failed to add pchg: %d\n", 339 retval); 340 } 341 342 /* 343 * The following subdevices cannot be detected by sending the 344 * EC_FEATURE_GET_CMD to the Embedded Controller device. 345 */ 346 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells, 347 ARRAY_SIZE(cros_ec_platform_cells)); 348 if (retval) 349 dev_warn(ec->dev, 350 "failed to add cros-ec platform devices: %d\n", 351 retval); 352 353 /* Check whether this EC instance has a VBC NVRAM */ 354 node = ec->ec_dev->dev->of_node; 355 if (of_property_read_bool(node, "google,has-vbc-nvram")) { 356 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells, 357 ARRAY_SIZE(cros_ec_vbc_cells)); 358 if (retval) 359 dev_warn(ec->dev, "failed to add VBC devices: %d\n", 360 retval); 361 } 362 363 return 0; 364 365 failed: 366 put_device(&ec->class_dev); 367 return retval; 368 } 369 370 static void ec_device_remove(struct platform_device *pdev) 371 { 372 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev); 373 374 mfd_remove_devices(ec->dev); 375 device_unregister(&ec->class_dev); 376 } 377 378 static const struct platform_device_id cros_ec_id[] = { 379 { DRV_NAME, 0 }, 380 { /* sentinel */ } 381 }; 382 MODULE_DEVICE_TABLE(platform, cros_ec_id); 383 384 static struct platform_driver cros_ec_dev_driver = { 385 .driver = { 386 .name = DRV_NAME, 387 }, 388 .id_table = cros_ec_id, 389 .probe = ec_device_probe, 390 .remove = ec_device_remove, 391 }; 392 393 static int __init cros_ec_dev_init(void) 394 { 395 int ret; 396 397 ret = class_register(&cros_class); 398 if (ret) { 399 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n"); 400 return ret; 401 } 402 403 ret = platform_driver_register(&cros_ec_dev_driver); 404 if (ret) { 405 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret); 406 class_unregister(&cros_class); 407 } 408 return ret; 409 } 410 411 static void __exit cros_ec_dev_exit(void) 412 { 413 platform_driver_unregister(&cros_ec_dev_driver); 414 class_unregister(&cros_class); 415 } 416 417 module_init(cros_ec_dev_init); 418 module_exit(cros_ec_dev_exit); 419 420 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>"); 421 MODULE_DESCRIPTION("ChromeOS Embedded Controller"); 422 MODULE_VERSION("1.0"); 423 MODULE_LICENSE("GPL"); 424