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