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/dmi.h> 9 #include <linux/kconfig.h> 10 #include <linux/mfd/core.h> 11 #include <linux/module.h> 12 #include <linux/mod_devicetable.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_UCSI_PPM, 136 .mfd_cells = cros_ec_ucsi_cells, 137 .num_cells = ARRAY_SIZE(cros_ec_ucsi_cells), 138 }, 139 { 140 .id = EC_FEATURE_HANG_DETECT, 141 .mfd_cells = cros_ec_wdt_cells, 142 .num_cells = ARRAY_SIZE(cros_ec_wdt_cells), 143 }, 144 { 145 .id = EC_FEATURE_LED, 146 .mfd_cells = cros_ec_led_cells, 147 .num_cells = ARRAY_SIZE(cros_ec_led_cells), 148 }, 149 { 150 .id = EC_FEATURE_PWM_KEYB, 151 .mfd_cells = cros_ec_keyboard_leds_cells, 152 .num_cells = ARRAY_SIZE(cros_ec_keyboard_leds_cells), 153 }, 154 { 155 .id = EC_FEATURE_CHARGER, 156 .mfd_cells = cros_ec_charge_control_cells, 157 .num_cells = ARRAY_SIZE(cros_ec_charge_control_cells), 158 }, 159 }; 160 161 static const struct mfd_cell cros_ec_platform_cells[] = { 162 { .name = "cros-ec-chardev", }, 163 { .name = "cros-ec-debugfs", }, 164 { .name = "cros-ec-hwmon", }, 165 { .name = "cros-ec-sysfs", }, 166 }; 167 168 static const struct mfd_cell cros_ec_pchg_cells[] = { 169 { .name = "cros-ec-pchg", }, 170 }; 171 172 static const struct mfd_cell cros_ec_lightbar_cells[] = { 173 { .name = "cros-ec-lightbar", } 174 }; 175 176 static const struct mfd_cell cros_ec_vbc_cells[] = { 177 { .name = "cros-ec-vbc", } 178 }; 179 180 static void cros_ec_class_release(struct device *dev) 181 { 182 kfree(to_cros_ec_dev(dev)); 183 } 184 185 static int ec_device_probe(struct platform_device *pdev) 186 { 187 int retval = -ENOMEM; 188 struct device_node *node; 189 struct device *dev = &pdev->dev; 190 struct cros_ec_platform *ec_platform = dev_get_platdata(dev); 191 struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL); 192 struct ec_response_pchg_count pchg_count; 193 int i; 194 195 if (!ec) 196 return retval; 197 198 dev_set_drvdata(dev, ec); 199 ec->ec_dev = dev_get_drvdata(dev->parent); 200 ec->dev = dev; 201 ec->cmd_offset = ec_platform->cmd_offset; 202 ec->features.flags[0] = -1U; /* Not cached yet */ 203 ec->features.flags[1] = -1U; /* Not cached yet */ 204 device_initialize(&ec->class_dev); 205 206 for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) { 207 /* 208 * Check whether this is actually a dedicated MCU rather 209 * than an standard EC. 210 */ 211 if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) { 212 dev_info(dev, "CrOS %s MCU detected\n", 213 cros_mcu_devices[i].desc); 214 /* 215 * Help userspace differentiating ECs from other MCU, 216 * regardless of the probing order. 217 */ 218 ec_platform->ec_name = cros_mcu_devices[i].name; 219 break; 220 } 221 } 222 223 /* 224 * Add the class device 225 */ 226 ec->class_dev.class = &cros_class; 227 ec->class_dev.parent = dev; 228 ec->class_dev.release = cros_ec_class_release; 229 230 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name); 231 if (retval) { 232 dev_err(dev, "dev_set_name failed => %d\n", retval); 233 goto failed; 234 } 235 236 retval = device_add(&ec->class_dev); 237 if (retval) 238 goto failed; 239 240 /* check whether this EC is a sensor hub. */ 241 if (cros_ec_get_sensor_count(ec) > 0) { 242 retval = mfd_add_hotplug_devices(ec->dev, 243 cros_ec_sensorhub_cells, 244 ARRAY_SIZE(cros_ec_sensorhub_cells)); 245 if (retval) 246 dev_err(ec->dev, "failed to add %s subdevice: %d\n", 247 cros_ec_sensorhub_cells->name, retval); 248 } 249 250 /* 251 * The following subdevices can be detected by sending the 252 * EC_FEATURE_GET_CMD Embedded Controller device. 253 */ 254 for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) { 255 if (cros_ec_check_features(ec, cros_subdevices[i].id)) { 256 retval = mfd_add_hotplug_devices(ec->dev, 257 cros_subdevices[i].mfd_cells, 258 cros_subdevices[i].num_cells); 259 if (retval) 260 dev_err(ec->dev, 261 "failed to add %s subdevice: %d\n", 262 cros_subdevices[i].mfd_cells->name, 263 retval); 264 } 265 } 266 267 /* 268 * UCSI provides power supply information so we don't need to separately 269 * load the cros_usbpd_charger driver. 270 */ 271 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD) && 272 !cros_ec_check_features(ec, EC_FEATURE_UCSI_PPM)) { 273 retval = mfd_add_hotplug_devices(ec->dev, 274 cros_usbpd_charger_cells, 275 ARRAY_SIZE(cros_usbpd_charger_cells)); 276 277 if (retval) 278 dev_warn(ec->dev, "failed to add usbpd-charger: %d\n", 279 retval); 280 } 281 282 /* 283 * Lightbar is a special case. Newer devices support autodetection, 284 * but older ones do not. 285 */ 286 if (cros_ec_check_features(ec, EC_FEATURE_LIGHTBAR) || 287 dmi_match(DMI_PRODUCT_NAME, "Link")) { 288 retval = mfd_add_hotplug_devices(ec->dev, 289 cros_ec_lightbar_cells, 290 ARRAY_SIZE(cros_ec_lightbar_cells)); 291 if (retval) 292 dev_warn(ec->dev, "failed to add lightbar: %d\n", 293 retval); 294 } 295 296 /* 297 * The PD notifier driver cell is separate since it only needs to be 298 * explicitly added on platforms that don't have the PD notifier ACPI 299 * device entry defined. 300 */ 301 if (IS_ENABLED(CONFIG_OF) && ec->ec_dev->dev->of_node) { 302 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) { 303 retval = mfd_add_hotplug_devices(ec->dev, 304 cros_usbpd_notify_cells, 305 ARRAY_SIZE(cros_usbpd_notify_cells)); 306 if (retval) 307 dev_err(ec->dev, 308 "failed to add PD notify devices: %d\n", 309 retval); 310 } 311 } 312 313 /* 314 * The PCHG device cannot be detected by sending EC_FEATURE_GET_CMD, but 315 * it can be detected by querying the number of peripheral chargers. 316 */ 317 retval = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_PCHG_COUNT, NULL, 0, 318 &pchg_count, sizeof(pchg_count)); 319 if (retval >= 0 && pchg_count.port_count) { 320 retval = mfd_add_hotplug_devices(ec->dev, 321 cros_ec_pchg_cells, 322 ARRAY_SIZE(cros_ec_pchg_cells)); 323 if (retval) 324 dev_warn(ec->dev, "failed to add pchg: %d\n", 325 retval); 326 } 327 328 /* 329 * The following subdevices cannot be detected by sending the 330 * EC_FEATURE_GET_CMD to the Embedded Controller device. 331 */ 332 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells, 333 ARRAY_SIZE(cros_ec_platform_cells)); 334 if (retval) 335 dev_warn(ec->dev, 336 "failed to add cros-ec platform devices: %d\n", 337 retval); 338 339 /* Check whether this EC instance has a VBC NVRAM */ 340 node = ec->ec_dev->dev->of_node; 341 if (of_property_read_bool(node, "google,has-vbc-nvram")) { 342 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells, 343 ARRAY_SIZE(cros_ec_vbc_cells)); 344 if (retval) 345 dev_warn(ec->dev, "failed to add VBC devices: %d\n", 346 retval); 347 } 348 349 return 0; 350 351 failed: 352 put_device(&ec->class_dev); 353 return retval; 354 } 355 356 static void ec_device_remove(struct platform_device *pdev) 357 { 358 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev); 359 360 mfd_remove_devices(ec->dev); 361 device_unregister(&ec->class_dev); 362 } 363 364 static const struct platform_device_id cros_ec_id[] = { 365 { DRV_NAME, 0 }, 366 { /* sentinel */ } 367 }; 368 MODULE_DEVICE_TABLE(platform, cros_ec_id); 369 370 static struct platform_driver cros_ec_dev_driver = { 371 .driver = { 372 .name = DRV_NAME, 373 }, 374 .id_table = cros_ec_id, 375 .probe = ec_device_probe, 376 .remove = ec_device_remove, 377 }; 378 379 static int __init cros_ec_dev_init(void) 380 { 381 int ret; 382 383 ret = class_register(&cros_class); 384 if (ret) { 385 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n"); 386 return ret; 387 } 388 389 ret = platform_driver_register(&cros_ec_dev_driver); 390 if (ret) { 391 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret); 392 class_unregister(&cros_class); 393 } 394 return ret; 395 } 396 397 static void __exit cros_ec_dev_exit(void) 398 { 399 platform_driver_unregister(&cros_ec_dev_driver); 400 class_unregister(&cros_class); 401 } 402 403 module_init(cros_ec_dev_init); 404 module_exit(cros_ec_dev_exit); 405 406 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>"); 407 MODULE_DESCRIPTION("ChromeOS Embedded Controller"); 408 MODULE_VERSION("1.0"); 409 MODULE_LICENSE("GPL"); 410