brintos

brintos / linux-shallow public Read only

0
0
Text · 9.5 KiB · f3dc812 Raw
383 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * ChromeOS Embedded Controller4 *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 22static 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 */32struct 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 */44struct cros_feature_to_cells {45	unsigned int id;46	const struct mfd_cell *mfd_cells;47	unsigned int num_cells;48};49 50static 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 73static const struct mfd_cell cros_ec_cec_cells[] = {74	{ .name = "cros-ec-cec", },75};76 77static const struct mfd_cell cros_ec_gpio_cells[] = {78	{ .name = "cros-ec-gpio", },79};80 81static const struct mfd_cell cros_ec_rtc_cells[] = {82	{ .name = "cros-ec-rtc", },83};84 85static const struct mfd_cell cros_ec_sensorhub_cells[] = {86	{ .name = "cros-ec-sensorhub", },87};88 89static const struct mfd_cell cros_usbpd_charger_cells[] = {90	{ .name = "cros-charge-control", },91	{ .name = "cros-usbpd-charger", },92	{ .name = "cros-usbpd-logger", },93};94 95static const struct mfd_cell cros_usbpd_notify_cells[] = {96	{ .name = "cros-usbpd-notify", },97};98 99static const struct mfd_cell cros_ec_wdt_cells[] = {100	{ .name = "cros-ec-wdt", }101};102 103static const struct mfd_cell cros_ec_led_cells[] = {104	{ .name = "cros-ec-led", },105};106 107static const struct mfd_cell cros_ec_keyboard_leds_cells[] = {108	{ .name = "cros-keyboard-leds", },109};110 111static const struct cros_feature_to_cells cros_subdevices[] = {112	{113		.id		= EC_FEATURE_CEC,114		.mfd_cells	= cros_ec_cec_cells,115		.num_cells	= ARRAY_SIZE(cros_ec_cec_cells),116	},117	{118		.id		= EC_FEATURE_GPIO,119		.mfd_cells	= cros_ec_gpio_cells,120		.num_cells	= ARRAY_SIZE(cros_ec_gpio_cells),121	},122	{123		.id		= EC_FEATURE_RTC,124		.mfd_cells	= cros_ec_rtc_cells,125		.num_cells	= ARRAY_SIZE(cros_ec_rtc_cells),126	},127	{128		.id		= EC_FEATURE_USB_PD,129		.mfd_cells	= cros_usbpd_charger_cells,130		.num_cells	= ARRAY_SIZE(cros_usbpd_charger_cells),131	},132	{133		.id		= EC_FEATURE_HANG_DETECT,134		.mfd_cells	= cros_ec_wdt_cells,135		.num_cells	= ARRAY_SIZE(cros_ec_wdt_cells),136	},137	{138		.id		= EC_FEATURE_LED,139		.mfd_cells	= cros_ec_led_cells,140		.num_cells	= ARRAY_SIZE(cros_ec_led_cells),141	},142	{143		.id		= EC_FEATURE_PWM_KEYB,144		.mfd_cells	= cros_ec_keyboard_leds_cells,145		.num_cells	= ARRAY_SIZE(cros_ec_keyboard_leds_cells),146	},147};148 149static const struct mfd_cell cros_ec_platform_cells[] = {150	{ .name = "cros-ec-chardev", },151	{ .name = "cros-ec-debugfs", },152	{ .name = "cros-ec-hwmon", },153	{ .name = "cros-ec-sysfs", },154};155 156static const struct mfd_cell cros_ec_pchg_cells[] = {157	{ .name = "cros-ec-pchg", },158};159 160static const struct mfd_cell cros_ec_lightbar_cells[] = {161	{ .name = "cros-ec-lightbar", }162};163 164static const struct mfd_cell cros_ec_vbc_cells[] = {165	{ .name = "cros-ec-vbc", }166};167 168static void cros_ec_class_release(struct device *dev)169{170	kfree(to_cros_ec_dev(dev));171}172 173static int ec_device_probe(struct platform_device *pdev)174{175	int retval = -ENOMEM;176	struct device_node *node;177	struct device *dev = &pdev->dev;178	struct cros_ec_platform *ec_platform = dev_get_platdata(dev);179	struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);180	struct ec_response_pchg_count pchg_count;181	int i;182 183	if (!ec)184		return retval;185 186	dev_set_drvdata(dev, ec);187	ec->ec_dev = dev_get_drvdata(dev->parent);188	ec->dev = dev;189	ec->cmd_offset = ec_platform->cmd_offset;190	ec->features.flags[0] = -1U; /* Not cached yet */191	ec->features.flags[1] = -1U; /* Not cached yet */192	device_initialize(&ec->class_dev);193 194	for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) {195		/*196		 * Check whether this is actually a dedicated MCU rather197		 * than an standard EC.198		 */199		if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) {200			dev_info(dev, "CrOS %s MCU detected\n",201				 cros_mcu_devices[i].desc);202			/*203			 * Help userspace differentiating ECs from other MCU,204			 * regardless of the probing order.205			 */206			ec_platform->ec_name = cros_mcu_devices[i].name;207			break;208		}209	}210 211	/*212	 * Add the class device213	 */214	ec->class_dev.class = &cros_class;215	ec->class_dev.parent = dev;216	ec->class_dev.release = cros_ec_class_release;217 218	retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);219	if (retval) {220		dev_err(dev, "dev_set_name failed => %d\n", retval);221		goto failed;222	}223 224	retval = device_add(&ec->class_dev);225	if (retval)226		goto failed;227 228	/* check whether this EC is a sensor hub. */229	if (cros_ec_get_sensor_count(ec) > 0) {230		retval = mfd_add_hotplug_devices(ec->dev,231				cros_ec_sensorhub_cells,232				ARRAY_SIZE(cros_ec_sensorhub_cells));233		if (retval)234			dev_err(ec->dev, "failed to add %s subdevice: %d\n",235				cros_ec_sensorhub_cells->name, retval);236	}237 238	/*239	 * The following subdevices can be detected by sending the240	 * EC_FEATURE_GET_CMD Embedded Controller device.241	 */242	for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) {243		if (cros_ec_check_features(ec, cros_subdevices[i].id)) {244			retval = mfd_add_hotplug_devices(ec->dev,245						cros_subdevices[i].mfd_cells,246						cros_subdevices[i].num_cells);247			if (retval)248				dev_err(ec->dev,249					"failed to add %s subdevice: %d\n",250					cros_subdevices[i].mfd_cells->name,251					retval);252		}253	}254 255	/*256	 * Lightbar is a special case. Newer devices support autodetection,257	 * but older ones do not.258	 */259	if (cros_ec_check_features(ec, EC_FEATURE_LIGHTBAR) ||260	    dmi_match(DMI_PRODUCT_NAME, "Link")) {261		retval = mfd_add_hotplug_devices(ec->dev,262					cros_ec_lightbar_cells,263					ARRAY_SIZE(cros_ec_lightbar_cells));264		if (retval)265			dev_warn(ec->dev, "failed to add lightbar: %d\n",266				 retval);267	}268 269	/*270	 * The PD notifier driver cell is separate since it only needs to be271	 * explicitly added on platforms that don't have the PD notifier ACPI272	 * device entry defined.273	 */274	if (IS_ENABLED(CONFIG_OF) && ec->ec_dev->dev->of_node) {275		if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {276			retval = mfd_add_hotplug_devices(ec->dev,277					cros_usbpd_notify_cells,278					ARRAY_SIZE(cros_usbpd_notify_cells));279			if (retval)280				dev_err(ec->dev,281					"failed to add PD notify devices: %d\n",282					retval);283		}284	}285 286	/*287	 * The PCHG device cannot be detected by sending EC_FEATURE_GET_CMD, but288	 * it can be detected by querying the number of peripheral chargers.289	 */290	retval = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_PCHG_COUNT, NULL, 0,291			     &pchg_count, sizeof(pchg_count));292	if (retval >= 0 && pchg_count.port_count) {293		retval = mfd_add_hotplug_devices(ec->dev,294					cros_ec_pchg_cells,295					ARRAY_SIZE(cros_ec_pchg_cells));296		if (retval)297			dev_warn(ec->dev, "failed to add pchg: %d\n",298				 retval);299	}300 301	/*302	 * The following subdevices cannot be detected by sending the303	 * EC_FEATURE_GET_CMD to the Embedded Controller device.304	 */305	retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells,306					 ARRAY_SIZE(cros_ec_platform_cells));307	if (retval)308		dev_warn(ec->dev,309			 "failed to add cros-ec platform devices: %d\n",310			 retval);311 312	/* Check whether this EC instance has a VBC NVRAM */313	node = ec->ec_dev->dev->of_node;314	if (of_property_read_bool(node, "google,has-vbc-nvram")) {315		retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells,316						ARRAY_SIZE(cros_ec_vbc_cells));317		if (retval)318			dev_warn(ec->dev, "failed to add VBC devices: %d\n",319				 retval);320	}321 322	return 0;323 324failed:325	put_device(&ec->class_dev);326	return retval;327}328 329static void ec_device_remove(struct platform_device *pdev)330{331	struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);332 333	mfd_remove_devices(ec->dev);334	device_unregister(&ec->class_dev);335}336 337static const struct platform_device_id cros_ec_id[] = {338	{ DRV_NAME, 0 },339	{ /* sentinel */ }340};341MODULE_DEVICE_TABLE(platform, cros_ec_id);342 343static struct platform_driver cros_ec_dev_driver = {344	.driver = {345		.name = DRV_NAME,346	},347	.id_table = cros_ec_id,348	.probe = ec_device_probe,349	.remove_new = ec_device_remove,350};351 352static int __init cros_ec_dev_init(void)353{354	int ret;355 356	ret = class_register(&cros_class);357	if (ret) {358		pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");359		return ret;360	}361 362	ret = platform_driver_register(&cros_ec_dev_driver);363	if (ret) {364		pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);365		class_unregister(&cros_class);366	}367	return ret;368}369 370static void __exit cros_ec_dev_exit(void)371{372	platform_driver_unregister(&cros_ec_dev_driver);373	class_unregister(&cros_class);374}375 376module_init(cros_ec_dev_init);377module_exit(cros_ec_dev_exit);378 379MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");380MODULE_DESCRIPTION("ChromeOS Embedded Controller");381MODULE_VERSION("1.0");382MODULE_LICENSE("GPL");383