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