Linux Audio

Check our new training course

Loading...
v6.2
  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
 
 
 
 
 
 
 
 
 
 
 
 22static 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 */
 33struct 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 */
 45struct cros_feature_to_cells {
 46	unsigned int id;
 47	const struct mfd_cell *mfd_cells;
 48	unsigned int num_cells;
 49};
 
 
 
 
 
 
 
 
 
 50
 51static 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_SCP_C1,
 69		.name	= CROS_EC_DEV_SCP_C1_NAME,
 70		.desc	= "System Control Processor 2nd Core",
 71	},
 72	{
 73		.id	= EC_FEATURE_TOUCHPAD,
 74		.name	= CROS_EC_DEV_TP_NAME,
 75		.desc	= "Touchpad",
 76	},
 77};
 78
 79static const struct mfd_cell cros_ec_cec_cells[] = {
 80	{ .name = "cros-ec-cec", },
 81};
 
 82
 83static const struct mfd_cell cros_ec_rtc_cells[] = {
 84	{ .name = "cros-ec-rtc", },
 85};
 86
 87static const struct mfd_cell cros_ec_sensorhub_cells[] = {
 88	{ .name = "cros-ec-sensorhub", },
 89};
 
 
 
 
 
 
 
 
 
 
 
 
 90
 91static const struct mfd_cell cros_usbpd_charger_cells[] = {
 92	{ .name = "cros-usbpd-charger", },
 93	{ .name = "cros-usbpd-logger", },
 94};
 95
 96static const struct mfd_cell cros_usbpd_notify_cells[] = {
 97	{ .name = "cros-usbpd-notify", },
 98};
 99
100static const struct cros_feature_to_cells cros_subdevices[] = {
101	{
102		.id		= EC_FEATURE_CEC,
103		.mfd_cells	= cros_ec_cec_cells,
104		.num_cells	= ARRAY_SIZE(cros_ec_cec_cells),
105	},
106	{
107		.id		= EC_FEATURE_RTC,
108		.mfd_cells	= cros_ec_rtc_cells,
109		.num_cells	= ARRAY_SIZE(cros_ec_rtc_cells),
110	},
111	{
112		.id		= EC_FEATURE_USB_PD,
113		.mfd_cells	= cros_usbpd_charger_cells,
114		.num_cells	= ARRAY_SIZE(cros_usbpd_charger_cells),
115	},
116};
117
118static const struct mfd_cell cros_ec_platform_cells[] = {
119	{ .name = "cros-ec-chardev", },
120	{ .name = "cros-ec-debugfs", },
121	{ .name = "cros-ec-sysfs", },
122};
123
124static const struct mfd_cell cros_ec_pchg_cells[] = {
125	{ .name = "cros-ec-pchg", },
126};
 
 
 
 
 
 
127
128static const struct mfd_cell cros_ec_lightbar_cells[] = {
129	{ .name = "cros-ec-lightbar", }
130};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
132static const struct mfd_cell cros_ec_vbc_cells[] = {
133	{ .name = "cros-ec-vbc", }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134};
135
136static void cros_ec_class_release(struct device *dev)
137{
138	kfree(to_cros_ec_dev(dev));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139}
140
141static int ec_device_probe(struct platform_device *pdev)
142{
143	int retval = -ENOMEM;
144	struct device_node *node;
145	struct device *dev = &pdev->dev;
146	struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
147	struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
148	struct ec_response_pchg_count pchg_count;
149	int i;
150
151	if (!ec)
152		return retval;
153
154	dev_set_drvdata(dev, ec);
155	ec->ec_dev = dev_get_drvdata(dev->parent);
156	ec->dev = dev;
157	ec->cmd_offset = ec_platform->cmd_offset;
158	ec->features.flags[0] = -1U; /* Not cached yet */
159	ec->features.flags[1] = -1U; /* Not cached yet */
160	device_initialize(&ec->class_dev);
161
162	for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) {
163		/*
164		 * Check whether this is actually a dedicated MCU rather
165		 * than an standard EC.
166		 */
167		if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) {
168			dev_info(dev, "CrOS %s MCU detected\n",
169				 cros_mcu_devices[i].desc);
170			/*
171			 * Help userspace differentiating ECs from other MCU,
172			 * regardless of the probing order.
173			 */
174			ec_platform->ec_name = cros_mcu_devices[i].name;
175			break;
176		}
177	}
178
179	/*
180	 * Add the class device
 
 
181	 */
 
182	ec->class_dev.class = &cros_class;
183	ec->class_dev.parent = dev;
184	ec->class_dev.release = cros_ec_class_release;
185
186	retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
187	if (retval) {
188		dev_err(dev, "dev_set_name failed => %d\n", retval);
189		goto failed;
190	}
191
192	retval = device_add(&ec->class_dev);
193	if (retval)
194		goto failed;
195
196	/* check whether this EC is a sensor hub. */
197	if (cros_ec_get_sensor_count(ec) > 0) {
198		retval = mfd_add_hotplug_devices(ec->dev,
199				cros_ec_sensorhub_cells,
200				ARRAY_SIZE(cros_ec_sensorhub_cells));
201		if (retval)
202			dev_err(ec->dev, "failed to add %s subdevice: %d\n",
203				cros_ec_sensorhub_cells->name, retval);
204	}
205
206	/*
207	 * The following subdevices can be detected by sending the
208	 * EC_FEATURE_GET_CMD Embedded Controller device.
209	 */
210	for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) {
211		if (cros_ec_check_features(ec, cros_subdevices[i].id)) {
212			retval = mfd_add_hotplug_devices(ec->dev,
213						cros_subdevices[i].mfd_cells,
214						cros_subdevices[i].num_cells);
215			if (retval)
216				dev_err(ec->dev,
217					"failed to add %s subdevice: %d\n",
218					cros_subdevices[i].mfd_cells->name,
219					retval);
220		}
221	}
222
223	/*
224	 * Lightbar is a special case. Newer devices support autodetection,
225	 * but older ones do not.
226	 */
227	if (cros_ec_check_features(ec, EC_FEATURE_LIGHTBAR) ||
228	    dmi_match(DMI_PRODUCT_NAME, "Link")) {
229		retval = mfd_add_hotplug_devices(ec->dev,
230					cros_ec_lightbar_cells,
231					ARRAY_SIZE(cros_ec_lightbar_cells));
232		if (retval)
233			dev_warn(ec->dev, "failed to add lightbar: %d\n",
234				 retval);
235	}
236
237	/*
238	 * The PD notifier driver cell is separate since it only needs to be
239	 * explicitly added on platforms that don't have the PD notifier ACPI
240	 * device entry defined.
241	 */
242	if (IS_ENABLED(CONFIG_OF) && ec->ec_dev->dev->of_node) {
243		if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
244			retval = mfd_add_hotplug_devices(ec->dev,
245					cros_usbpd_notify_cells,
246					ARRAY_SIZE(cros_usbpd_notify_cells));
247			if (retval)
248				dev_err(ec->dev,
249					"failed to add PD notify devices: %d\n",
250					retval);
251		}
252	}
253
254	/*
255	 * The PCHG device cannot be detected by sending EC_FEATURE_GET_CMD, but
256	 * it can be detected by querying the number of peripheral chargers.
257	 */
258	retval = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_PCHG_COUNT, NULL, 0,
259			     &pchg_count, sizeof(pchg_count));
260	if (retval >= 0 && pchg_count.port_count) {
261		retval = mfd_add_hotplug_devices(ec->dev,
262					cros_ec_pchg_cells,
263					ARRAY_SIZE(cros_ec_pchg_cells));
264		if (retval)
265			dev_warn(ec->dev, "failed to add pchg: %d\n",
266				 retval);
267	}
268
269	/*
270	 * The following subdevices cannot be detected by sending the
271	 * EC_FEATURE_GET_CMD to the Embedded Controller device.
272	 */
273	retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells,
274					 ARRAY_SIZE(cros_ec_platform_cells));
275	if (retval)
276		dev_warn(ec->dev,
277			 "failed to add cros-ec platform devices: %d\n",
278			 retval);
279
280	/* Check whether this EC instance has a VBC NVRAM */
281	node = ec->ec_dev->dev->of_node;
282	if (of_property_read_bool(node, "google,has-vbc-nvram")) {
283		retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells,
284						ARRAY_SIZE(cros_ec_vbc_cells));
285		if (retval)
286			dev_warn(ec->dev, "failed to add VBC devices: %d\n",
287				 retval);
288	}
289
290	return 0;
291
292failed:
293	put_device(&ec->class_dev);
294	return retval;
295}
296
297static int ec_device_remove(struct platform_device *pdev)
298{
299	struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
300
301	mfd_remove_devices(ec->dev);
 
 
 
 
 
302	device_unregister(&ec->class_dev);
303	return 0;
304}
305
306static const struct platform_device_id cros_ec_id[] = {
307	{ DRV_NAME, 0 },
308	{ /* sentinel */ }
309};
310MODULE_DEVICE_TABLE(platform, cros_ec_id);
311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312static struct platform_driver cros_ec_dev_driver = {
313	.driver = {
314		.name = DRV_NAME,
 
315	},
316	.id_table = cros_ec_id,
317	.probe = ec_device_probe,
318	.remove = ec_device_remove,
319};
320
321static int __init cros_ec_dev_init(void)
322{
323	int ret;
 
324
325	ret  = class_register(&cros_class);
326	if (ret) {
327		pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
328		return ret;
329	}
330
 
 
 
 
 
 
 
 
331	/* Register the driver */
332	ret = platform_driver_register(&cros_ec_dev_driver);
333	if (ret < 0) {
334		pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
335		goto failed_devreg;
336	}
337	return 0;
338
339failed_devreg:
 
 
340	class_unregister(&cros_class);
341	return ret;
342}
343
344static void __exit cros_ec_dev_exit(void)
345{
346	platform_driver_unregister(&cros_ec_dev_driver);
 
347	class_unregister(&cros_class);
348}
349
350module_init(cros_ec_dev_init);
351module_exit(cros_ec_dev_exit);
352
 
353MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
354MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
355MODULE_VERSION("1.0");
356MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space
  3 *
  4 * Copyright (C) 2014 Google, Inc.
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include <linux/fs.h>
 
 21#include <linux/mfd/core.h>
 22#include <linux/module.h>
 
 
 23#include <linux/platform_device.h>
 24#include <linux/pm.h>
 
 
 25#include <linux/slab.h>
 26#include <linux/uaccess.h>
 27
 28#include "cros_ec_dev.h"
 29
 30#define DRV_NAME "cros-ec-dev"
 31
 32/* Device variables */
 33#define CROS_MAX_DEV 128
 34static int ec_major;
 35
 36static const struct attribute_group *cros_ec_groups[] = {
 37	&cros_ec_attr_group,
 38	&cros_ec_lightbar_attr_group,
 39	&cros_ec_vbc_attr_group,
 40	NULL,
 41};
 42
 43static struct class cros_class = {
 44	.owner          = THIS_MODULE,
 45	.name           = "chromeos",
 46	.dev_groups     = cros_ec_groups,
 47};
 48
 49/* Basic communication */
 50static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
 51{
 52	struct ec_response_get_version *resp;
 53	static const char * const current_image_name[] = {
 54		"unknown", "read-only", "read-write", "invalid",
 55	};
 56	struct cros_ec_command *msg;
 57	int ret;
 
 
 58
 59	msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
 60	if (!msg)
 61		return -ENOMEM;
 62
 63	msg->version = 0;
 64	msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
 65	msg->insize = sizeof(*resp);
 66	msg->outsize = 0;
 67
 68	ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
 69	if (ret < 0)
 70		goto exit;
 71
 72	if (msg->result != EC_RES_SUCCESS) {
 73		snprintf(str, maxlen,
 74			 "%s\nUnknown EC version: EC returned %d\n",
 75			 CROS_EC_DEV_VERSION, msg->result);
 76		ret = -EINVAL;
 77		goto exit;
 78	}
 79
 80	resp = (struct ec_response_get_version *)msg->data;
 81	if (resp->current_image >= ARRAY_SIZE(current_image_name))
 82		resp->current_image = 3; /* invalid */
 83
 84	snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
 85		 resp->version_string_ro, resp->version_string_rw,
 86		 current_image_name[resp->current_image]);
 87
 88	ret = 0;
 89exit:
 90	kfree(msg);
 91	return ret;
 92}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 93
 94static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
 95{
 96	struct cros_ec_command *msg;
 97	int ret;
 98
 99	if (ec->features[0] == -1U && ec->features[1] == -1U) {
100		/* features bitmap not read yet */
 
101
102		msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
103		if (!msg)
104			return -ENOMEM;
105
106		msg->version = 0;
107		msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
108		msg->insize = sizeof(ec->features);
109		msg->outsize = 0;
110
111		ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
112		if (ret < 0 || msg->result != EC_RES_SUCCESS) {
113			dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
114				 ret, msg->result);
115			memset(ec->features, 0, sizeof(ec->features));
116		}
117
118		memcpy(ec->features, msg->data, sizeof(ec->features));
 
 
 
119
120		dev_dbg(ec->dev, "EC features %08x %08x\n",
121			ec->features[0], ec->features[1]);
 
122
123		kfree(msg);
124	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
126	return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
127}
 
 
 
128
129/* Device file ops */
130static int ec_device_open(struct inode *inode, struct file *filp)
131{
132	struct cros_ec_dev *ec = container_of(inode->i_cdev,
133					      struct cros_ec_dev, cdev);
134	filp->private_data = ec;
135	nonseekable_open(inode, filp);
136	return 0;
137}
138
139static int ec_device_release(struct inode *inode, struct file *filp)
140{
141	return 0;
142}
143
144static ssize_t ec_device_read(struct file *filp, char __user *buffer,
145			      size_t length, loff_t *offset)
146{
147	struct cros_ec_dev *ec = filp->private_data;
148	char msg[sizeof(struct ec_response_get_version) +
149		 sizeof(CROS_EC_DEV_VERSION)];
150	size_t count;
151	int ret;
152
153	if (*offset != 0)
154		return 0;
155
156	ret = ec_get_version(ec, msg, sizeof(msg));
157	if (ret)
158		return ret;
159
160	count = min(length, strlen(msg));
161
162	if (copy_to_user(buffer, msg, count))
163		return -EFAULT;
164
165	*offset = count;
166	return count;
167}
168
169/* Ioctls */
170static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
171{
172	long ret;
173	struct cros_ec_command u_cmd;
174	struct cros_ec_command *s_cmd;
175
176	if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
177		return -EFAULT;
178
179	if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
180	    (u_cmd.insize > EC_MAX_MSG_BYTES))
181		return -EINVAL;
182
183	s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
184			GFP_KERNEL);
185	if (!s_cmd)
186		return -ENOMEM;
187
188	if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
189		ret = -EFAULT;
190		goto exit;
191	}
192
193	if (u_cmd.outsize != s_cmd->outsize ||
194	    u_cmd.insize != s_cmd->insize) {
195		ret = -EINVAL;
196		goto exit;
197	}
198
199	s_cmd->command += ec->cmd_offset;
200	ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
201	/* Only copy data to userland if data was received. */
202	if (ret < 0)
203		goto exit;
204
205	if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
206		ret = -EFAULT;
207exit:
208	kfree(s_cmd);
209	return ret;
210}
211
212static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
213{
214	struct cros_ec_device *ec_dev = ec->ec_dev;
215	struct cros_ec_readmem s_mem = { };
216	long num;
217
218	/* Not every platform supports direct reads */
219	if (!ec_dev->cmd_readmem)
220		return -ENOTTY;
221
222	if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
223		return -EFAULT;
224
225	num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
226				  s_mem.buffer);
227	if (num <= 0)
228		return num;
229
230	if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
231		return -EFAULT;
232
233	return 0;
234}
235
236static long ec_device_ioctl(struct file *filp, unsigned int cmd,
237			    unsigned long arg)
238{
239	struct cros_ec_dev *ec = filp->private_data;
240
241	if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
242		return -ENOTTY;
243
244	switch (cmd) {
245	case CROS_EC_DEV_IOCXCMD:
246		return ec_device_ioctl_xcmd(ec, (void __user *)arg);
247	case CROS_EC_DEV_IOCRDMEM:
248		return ec_device_ioctl_readmem(ec, (void __user *)arg);
249	}
250
251	return -ENOTTY;
252}
253
254/* Module initialization */
255static const struct file_operations fops = {
256	.open = ec_device_open,
257	.release = ec_device_release,
258	.read = ec_device_read,
259	.unlocked_ioctl = ec_device_ioctl,
260#ifdef CONFIG_COMPAT
261	.compat_ioctl = ec_device_ioctl,
262#endif
263};
264
265static void __remove(struct device *dev)
266{
267	struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
268					      class_dev);
269	kfree(ec);
270}
271
272static void cros_ec_sensors_register(struct cros_ec_dev *ec)
273{
274	/*
275	 * Issue a command to get the number of sensor reported.
276	 * Build an array of sensors driver and register them all.
277	 */
278	int ret, i, id, sensor_num;
279	struct mfd_cell *sensor_cells;
280	struct cros_ec_sensor_platform *sensor_platforms;
281	int sensor_type[MOTIONSENSE_TYPE_MAX];
282	struct ec_params_motion_sense *params;
283	struct ec_response_motion_sense *resp;
284	struct cros_ec_command *msg;
285
286	msg = kzalloc(sizeof(struct cros_ec_command) +
287		      max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
288	if (msg == NULL)
289		return;
290
291	msg->version = 2;
292	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
293	msg->outsize = sizeof(*params);
294	msg->insize = sizeof(*resp);
295
296	params = (struct ec_params_motion_sense *)msg->data;
297	params->cmd = MOTIONSENSE_CMD_DUMP;
298
299	ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
300	if (ret < 0 || msg->result != EC_RES_SUCCESS) {
301		dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
302			 ret, msg->result);
303		goto error;
304	}
305
306	resp = (struct ec_response_motion_sense *)msg->data;
307	sensor_num = resp->dump.sensor_count;
308	/* Allocate 1 extra sensors in FIFO are needed */
309	sensor_cells = kzalloc(sizeof(struct mfd_cell) * (sensor_num + 1),
310			       GFP_KERNEL);
311	if (sensor_cells == NULL)
312		goto error;
313
314	sensor_platforms = kzalloc(sizeof(struct cros_ec_sensor_platform) *
315		  (sensor_num + 1), GFP_KERNEL);
316	if (sensor_platforms == NULL)
317		goto error_platforms;
318
319	memset(sensor_type, 0, sizeof(sensor_type));
320	id = 0;
321	for (i = 0; i < sensor_num; i++) {
322		params->cmd = MOTIONSENSE_CMD_INFO;
323		params->info.sensor_num = i;
324		ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
325		if (ret < 0 || msg->result != EC_RES_SUCCESS) {
326			dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
327				 i, ret, msg->result);
328			continue;
329		}
330		switch (resp->info.type) {
331		case MOTIONSENSE_TYPE_ACCEL:
332			sensor_cells[id].name = "cros-ec-accel";
333			break;
334		case MOTIONSENSE_TYPE_BARO:
335			sensor_cells[id].name = "cros-ec-baro";
336			break;
337		case MOTIONSENSE_TYPE_GYRO:
338			sensor_cells[id].name = "cros-ec-gyro";
339			break;
340		case MOTIONSENSE_TYPE_MAG:
341			sensor_cells[id].name = "cros-ec-mag";
342			break;
343		case MOTIONSENSE_TYPE_PROX:
344			sensor_cells[id].name = "cros-ec-prox";
345			break;
346		case MOTIONSENSE_TYPE_LIGHT:
347			sensor_cells[id].name = "cros-ec-light";
348			break;
349		case MOTIONSENSE_TYPE_ACTIVITY:
350			sensor_cells[id].name = "cros-ec-activity";
351			break;
352		default:
353			dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
354			continue;
355		}
356		sensor_platforms[id].sensor_num = i;
357		sensor_cells[id].id = sensor_type[resp->info.type];
358		sensor_cells[id].platform_data = &sensor_platforms[id];
359		sensor_cells[id].pdata_size =
360			sizeof(struct cros_ec_sensor_platform);
361
362		sensor_type[resp->info.type]++;
363		id++;
364	}
365
366	if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
367		ec->has_kb_wake_angle = true;
368
369	if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
370		sensor_cells[id].name = "cros-ec-ring";
371		id++;
372	}
373
374	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
375			      NULL, 0, NULL);
376	if (ret)
377		dev_err(ec->dev, "failed to add EC sensors\n");
378
379	kfree(sensor_platforms);
380error_platforms:
381	kfree(sensor_cells);
382error:
383	kfree(msg);
384}
385
386static int ec_device_probe(struct platform_device *pdev)
387{
388	int retval = -ENOMEM;
 
389	struct device *dev = &pdev->dev;
390	struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
391	struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
 
 
392
393	if (!ec)
394		return retval;
395
396	dev_set_drvdata(dev, ec);
397	ec->ec_dev = dev_get_drvdata(dev->parent);
398	ec->dev = dev;
399	ec->cmd_offset = ec_platform->cmd_offset;
400	ec->features[0] = -1U; /* Not cached yet */
401	ec->features[1] = -1U; /* Not cached yet */
402	device_initialize(&ec->class_dev);
403	cdev_init(&ec->cdev, &fops);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404
405	/*
406	 * Add the class device
407	 * Link to the character device for creating the /dev entry
408	 * in devtmpfs.
409	 */
410	ec->class_dev.devt = MKDEV(ec_major, pdev->id);
411	ec->class_dev.class = &cros_class;
412	ec->class_dev.parent = dev;
413	ec->class_dev.release = __remove;
414
415	retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
416	if (retval) {
417		dev_err(dev, "dev_set_name failed => %d\n", retval);
418		goto failed;
419	}
420
 
 
 
 
421	/* check whether this EC is a sensor hub. */
422	if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
423		cros_ec_sensors_register(ec);
 
 
 
 
 
 
424
425	/* Take control of the lightbar from the EC. */
426	lb_manual_suspend_ctrl(ec, 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427
428	/* We can now add the sysfs class, we know which parameter to show */
429	retval = cdev_device_add(&ec->cdev, &ec->class_dev);
430	if (retval) {
431		dev_err(dev, "cdev_device_add failed => %d\n", retval);
432		goto failed;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
433	}
434
435	if (cros_ec_debugfs_init(ec))
436		dev_warn(dev, "failed to create debugfs directory\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
437
438	return 0;
439
440failed:
441	put_device(&ec->class_dev);
442	return retval;
443}
444
445static int ec_device_remove(struct platform_device *pdev)
446{
447	struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
448
449	/* Let the EC take over the lightbar again. */
450	lb_manual_suspend_ctrl(ec, 0);
451
452	cros_ec_debugfs_remove(ec);
453
454	cdev_del(&ec->cdev);
455	device_unregister(&ec->class_dev);
456	return 0;
457}
458
459static const struct platform_device_id cros_ec_id[] = {
460	{ DRV_NAME, 0 },
461	{ /* sentinel */ },
462};
463MODULE_DEVICE_TABLE(platform, cros_ec_id);
464
465static __maybe_unused int ec_device_suspend(struct device *dev)
466{
467	struct cros_ec_dev *ec = dev_get_drvdata(dev);
468
469	lb_suspend(ec);
470
471	return 0;
472}
473
474static __maybe_unused int ec_device_resume(struct device *dev)
475{
476	struct cros_ec_dev *ec = dev_get_drvdata(dev);
477
478	lb_resume(ec);
479
480	return 0;
481}
482
483static const struct dev_pm_ops cros_ec_dev_pm_ops = {
484#ifdef CONFIG_PM_SLEEP
485	.suspend = ec_device_suspend,
486	.resume = ec_device_resume,
487#endif
488};
489
490static struct platform_driver cros_ec_dev_driver = {
491	.driver = {
492		.name = DRV_NAME,
493		.pm = &cros_ec_dev_pm_ops,
494	},
 
495	.probe = ec_device_probe,
496	.remove = ec_device_remove,
497};
498
499static int __init cros_ec_dev_init(void)
500{
501	int ret;
502	dev_t dev = 0;
503
504	ret  = class_register(&cros_class);
505	if (ret) {
506		pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
507		return ret;
508	}
509
510	/* Get a range of minor numbers (starting with 0) to work with */
511	ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
512	if (ret < 0) {
513		pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
514		goto failed_chrdevreg;
515	}
516	ec_major = MAJOR(dev);
517
518	/* Register the driver */
519	ret = platform_driver_register(&cros_ec_dev_driver);
520	if (ret < 0) {
521		pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
522		goto failed_devreg;
523	}
524	return 0;
525
526failed_devreg:
527	unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
528failed_chrdevreg:
529	class_unregister(&cros_class);
530	return ret;
531}
532
533static void __exit cros_ec_dev_exit(void)
534{
535	platform_driver_unregister(&cros_ec_dev_driver);
536	unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
537	class_unregister(&cros_class);
538}
539
540module_init(cros_ec_dev_init);
541module_exit(cros_ec_dev_exit);
542
543MODULE_ALIAS("platform:" DRV_NAME);
544MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
545MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
546MODULE_VERSION("1.0");
547MODULE_LICENSE("GPL");