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