xref: /openbmc/linux/drivers/mfd/cros_ec_dev.c (revision 7ec6b431)
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 
8 #include <linux/mfd/core.h>
9 #include <linux/mfd/cros_ec.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 
19 #define DRV_NAME "cros-ec-dev"
20 
21 static struct class cros_class = {
22 	.owner          = THIS_MODULE,
23 	.name           = "chromeos",
24 };
25 
26 /**
27  * 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  * 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_rtc_cells[] = {
78 	{ .name = "cros-ec-rtc", },
79 };
80 
81 static const struct mfd_cell cros_ec_sensorhub_cells[] = {
82 	{ .name = "cros-ec-sensorhub", },
83 };
84 
85 static const struct mfd_cell cros_usbpd_charger_cells[] = {
86 	{ .name = "cros-usbpd-charger", },
87 	{ .name = "cros-usbpd-logger", },
88 };
89 
90 static const struct cros_feature_to_cells cros_subdevices[] = {
91 	{
92 		.id		= EC_FEATURE_CEC,
93 		.mfd_cells	= cros_ec_cec_cells,
94 		.num_cells	= ARRAY_SIZE(cros_ec_cec_cells),
95 	},
96 	{
97 		.id		= EC_FEATURE_RTC,
98 		.mfd_cells	= cros_ec_rtc_cells,
99 		.num_cells	= ARRAY_SIZE(cros_ec_rtc_cells),
100 	},
101 	{
102 		.id		= EC_FEATURE_USB_PD,
103 		.mfd_cells	= cros_usbpd_charger_cells,
104 		.num_cells	= ARRAY_SIZE(cros_usbpd_charger_cells),
105 	},
106 };
107 
108 static const struct mfd_cell cros_ec_platform_cells[] = {
109 	{ .name = "cros-ec-chardev", },
110 	{ .name = "cros-ec-debugfs", },
111 	{ .name = "cros-ec-lightbar", },
112 	{ .name = "cros-ec-sysfs", },
113 };
114 
115 static const struct mfd_cell cros_ec_vbc_cells[] = {
116 	{ .name = "cros-ec-vbc", }
117 };
118 
119 static void cros_ec_class_release(struct device *dev)
120 {
121 	kfree(to_cros_ec_dev(dev));
122 }
123 
124 static int ec_device_probe(struct platform_device *pdev)
125 {
126 	int retval = -ENOMEM;
127 	struct device_node *node;
128 	struct device *dev = &pdev->dev;
129 	struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
130 	struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
131 	int i;
132 
133 	if (!ec)
134 		return retval;
135 
136 	dev_set_drvdata(dev, ec);
137 	ec->ec_dev = dev_get_drvdata(dev->parent);
138 	ec->dev = dev;
139 	ec->cmd_offset = ec_platform->cmd_offset;
140 	ec->features[0] = -1U; /* Not cached yet */
141 	ec->features[1] = -1U; /* Not cached yet */
142 	device_initialize(&ec->class_dev);
143 
144 	for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) {
145 		/*
146 		 * Check whether this is actually a dedicated MCU rather
147 		 * than an standard EC.
148 		 */
149 		if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) {
150 			dev_info(dev, "CrOS %s MCU detected\n",
151 				 cros_mcu_devices[i].desc);
152 			/*
153 			 * Help userspace differentiating ECs from other MCU,
154 			 * regardless of the probing order.
155 			 */
156 			ec_platform->ec_name = cros_mcu_devices[i].name;
157 			break;
158 		}
159 	}
160 
161 	/*
162 	 * Add the class device
163 	 */
164 	ec->class_dev.class = &cros_class;
165 	ec->class_dev.parent = dev;
166 	ec->class_dev.release = cros_ec_class_release;
167 
168 	retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
169 	if (retval) {
170 		dev_err(dev, "dev_set_name failed => %d\n", retval);
171 		goto failed;
172 	}
173 
174 	retval = device_add(&ec->class_dev);
175 	if (retval)
176 		goto failed;
177 
178 	/* check whether this EC is a sensor hub. */
179 	if (cros_ec_get_sensor_count(ec) > 0) {
180 		retval = mfd_add_hotplug_devices(ec->dev,
181 				cros_ec_sensorhub_cells,
182 				ARRAY_SIZE(cros_ec_sensorhub_cells));
183 		if (retval)
184 			dev_err(ec->dev, "failed to add %s subdevice: %d\n",
185 				cros_ec_sensorhub_cells->name, retval);
186 	}
187 
188 	/*
189 	 * The following subdevices can be detected by sending the
190 	 * EC_FEATURE_GET_CMD Embedded Controller device.
191 	 */
192 	for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) {
193 		if (cros_ec_check_features(ec, cros_subdevices[i].id)) {
194 			retval = mfd_add_hotplug_devices(ec->dev,
195 						cros_subdevices[i].mfd_cells,
196 						cros_subdevices[i].num_cells);
197 			if (retval)
198 				dev_err(ec->dev,
199 					"failed to add %s subdevice: %d\n",
200 					cros_subdevices[i].mfd_cells->name,
201 					retval);
202 		}
203 	}
204 
205 	/*
206 	 * The following subdevices cannot be detected by sending the
207 	 * EC_FEATURE_GET_CMD to the Embedded Controller device.
208 	 */
209 	retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells,
210 					 ARRAY_SIZE(cros_ec_platform_cells));
211 	if (retval)
212 		dev_warn(ec->dev,
213 			 "failed to add cros-ec platform devices: %d\n",
214 			 retval);
215 
216 	/* Check whether this EC instance has a VBC NVRAM */
217 	node = ec->ec_dev->dev->of_node;
218 	if (of_property_read_bool(node, "google,has-vbc-nvram")) {
219 		retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells,
220 						ARRAY_SIZE(cros_ec_vbc_cells));
221 		if (retval)
222 			dev_warn(ec->dev, "failed to add VBC devices: %d\n",
223 				 retval);
224 	}
225 
226 	return 0;
227 
228 failed:
229 	put_device(&ec->class_dev);
230 	return retval;
231 }
232 
233 static int ec_device_remove(struct platform_device *pdev)
234 {
235 	struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
236 
237 	mfd_remove_devices(ec->dev);
238 	device_unregister(&ec->class_dev);
239 	return 0;
240 }
241 
242 static const struct platform_device_id cros_ec_id[] = {
243 	{ DRV_NAME, 0 },
244 	{ /* sentinel */ }
245 };
246 MODULE_DEVICE_TABLE(platform, cros_ec_id);
247 
248 static struct platform_driver cros_ec_dev_driver = {
249 	.driver = {
250 		.name = DRV_NAME,
251 	},
252 	.id_table = cros_ec_id,
253 	.probe = ec_device_probe,
254 	.remove = ec_device_remove,
255 };
256 
257 static int __init cros_ec_dev_init(void)
258 {
259 	int ret;
260 
261 	ret  = class_register(&cros_class);
262 	if (ret) {
263 		pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
264 		return ret;
265 	}
266 
267 	/* Register the driver */
268 	ret = platform_driver_register(&cros_ec_dev_driver);
269 	if (ret < 0) {
270 		pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
271 		goto failed_devreg;
272 	}
273 	return 0;
274 
275 failed_devreg:
276 	class_unregister(&cros_class);
277 	return ret;
278 }
279 
280 static void __exit cros_ec_dev_exit(void)
281 {
282 	platform_driver_unregister(&cros_ec_dev_driver);
283 	class_unregister(&cros_class);
284 }
285 
286 module_init(cros_ec_dev_init);
287 module_exit(cros_ec_dev_exit);
288 
289 MODULE_ALIAS("platform:" DRV_NAME);
290 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
291 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
292 MODULE_VERSION("1.0");
293 MODULE_LICENSE("GPL");
294