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