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