2 * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space
4 * Copyright (C) 2014 Google, Inc.
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.
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.
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/>.
21 #include <linux/mfd/core.h>
22 #include <linux/module.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
29 #include "cros_ec_dev.h"
31 #define DRV_NAME "cros-ec-dev"
33 /* Device variables */
34 #define CROS_MAX_DEV 128
37 static const struct attribute_group
*cros_ec_groups
[] = {
39 &cros_ec_lightbar_attr_group
,
40 &cros_ec_vbc_attr_group
,
44 static struct class cros_class
= {
47 .dev_groups
= cros_ec_groups
,
50 /* Basic communication */
51 static int ec_get_version(struct cros_ec_dev
*ec
, char *str
, int maxlen
)
53 struct ec_response_get_version
*resp
;
54 static const char * const current_image_name
[] = {
55 "unknown", "read-only", "read-write", "invalid",
57 struct cros_ec_command
*msg
;
60 msg
= kmalloc(sizeof(*msg
) + sizeof(*resp
), GFP_KERNEL
);
65 msg
->command
= EC_CMD_GET_VERSION
+ ec
->cmd_offset
;
66 msg
->insize
= sizeof(*resp
);
69 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
73 if (msg
->result
!= EC_RES_SUCCESS
) {
75 "%s\nUnknown EC version: EC returned %d\n",
76 CROS_EC_DEV_VERSION
, msg
->result
);
81 resp
= (struct ec_response_get_version
*)msg
->data
;
82 if (resp
->current_image
>= ARRAY_SIZE(current_image_name
))
83 resp
->current_image
= 3; /* invalid */
85 snprintf(str
, maxlen
, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION
,
86 resp
->version_string_ro
, resp
->version_string_rw
,
87 current_image_name
[resp
->current_image
]);
95 static int cros_ec_check_features(struct cros_ec_dev
*ec
, int feature
)
97 struct cros_ec_command
*msg
;
100 if (ec
->features
[0] == -1U && ec
->features
[1] == -1U) {
101 /* features bitmap not read yet */
103 msg
= kmalloc(sizeof(*msg
) + sizeof(ec
->features
), GFP_KERNEL
);
108 msg
->command
= EC_CMD_GET_FEATURES
+ ec
->cmd_offset
;
109 msg
->insize
= sizeof(ec
->features
);
112 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
113 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
114 dev_warn(ec
->dev
, "cannot get EC features: %d/%d\n",
116 memset(ec
->features
, 0, sizeof(ec
->features
));
118 memcpy(ec
->features
, msg
->data
, sizeof(ec
->features
));
121 dev_dbg(ec
->dev
, "EC features %08x %08x\n",
122 ec
->features
[0], ec
->features
[1]);
127 return ec
->features
[feature
/ 32] & EC_FEATURE_MASK_0(feature
);
130 /* Device file ops */
131 static int ec_device_open(struct inode
*inode
, struct file
*filp
)
133 struct cros_ec_dev
*ec
= container_of(inode
->i_cdev
,
134 struct cros_ec_dev
, cdev
);
135 filp
->private_data
= ec
;
136 nonseekable_open(inode
, filp
);
140 static int ec_device_release(struct inode
*inode
, struct file
*filp
)
145 static ssize_t
ec_device_read(struct file
*filp
, char __user
*buffer
,
146 size_t length
, loff_t
*offset
)
148 struct cros_ec_dev
*ec
= filp
->private_data
;
149 char msg
[sizeof(struct ec_response_get_version
) +
150 sizeof(CROS_EC_DEV_VERSION
)];
157 ret
= ec_get_version(ec
, msg
, sizeof(msg
));
161 count
= min(length
, strlen(msg
));
163 if (copy_to_user(buffer
, msg
, count
))
171 static long ec_device_ioctl_xcmd(struct cros_ec_dev
*ec
, void __user
*arg
)
174 struct cros_ec_command u_cmd
;
175 struct cros_ec_command
*s_cmd
;
177 if (copy_from_user(&u_cmd
, arg
, sizeof(u_cmd
)))
180 if ((u_cmd
.outsize
> EC_MAX_MSG_BYTES
) ||
181 (u_cmd
.insize
> EC_MAX_MSG_BYTES
))
184 s_cmd
= kmalloc(sizeof(*s_cmd
) + max(u_cmd
.outsize
, u_cmd
.insize
),
189 if (copy_from_user(s_cmd
, arg
, sizeof(*s_cmd
) + u_cmd
.outsize
)) {
194 if (u_cmd
.outsize
!= s_cmd
->outsize
||
195 u_cmd
.insize
!= s_cmd
->insize
) {
200 s_cmd
->command
+= ec
->cmd_offset
;
201 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, s_cmd
);
202 /* Only copy data to userland if data was received. */
206 if (copy_to_user(arg
, s_cmd
, sizeof(*s_cmd
) + s_cmd
->insize
))
213 static long ec_device_ioctl_readmem(struct cros_ec_dev
*ec
, void __user
*arg
)
215 struct cros_ec_device
*ec_dev
= ec
->ec_dev
;
216 struct cros_ec_readmem s_mem
= { };
219 /* Not every platform supports direct reads */
220 if (!ec_dev
->cmd_readmem
)
223 if (copy_from_user(&s_mem
, arg
, sizeof(s_mem
)))
226 num
= ec_dev
->cmd_readmem(ec_dev
, s_mem
.offset
, s_mem
.bytes
,
231 if (copy_to_user((void __user
*)arg
, &s_mem
, sizeof(s_mem
)))
237 static long ec_device_ioctl(struct file
*filp
, unsigned int cmd
,
240 struct cros_ec_dev
*ec
= filp
->private_data
;
242 if (_IOC_TYPE(cmd
) != CROS_EC_DEV_IOC
)
246 case CROS_EC_DEV_IOCXCMD
:
247 return ec_device_ioctl_xcmd(ec
, (void __user
*)arg
);
248 case CROS_EC_DEV_IOCRDMEM
:
249 return ec_device_ioctl_readmem(ec
, (void __user
*)arg
);
255 /* Module initialization */
256 static const struct file_operations fops
= {
257 .open
= ec_device_open
,
258 .release
= ec_device_release
,
259 .read
= ec_device_read
,
260 .unlocked_ioctl
= ec_device_ioctl
,
262 .compat_ioctl
= ec_device_ioctl
,
266 static void cros_ec_sensors_register(struct cros_ec_dev
*ec
)
269 * Issue a command to get the number of sensor reported.
270 * Build an array of sensors driver and register them all.
272 int ret
, i
, id
, sensor_num
;
273 struct mfd_cell
*sensor_cells
;
274 struct cros_ec_sensor_platform
*sensor_platforms
;
275 int sensor_type
[MOTIONSENSE_TYPE_MAX
];
276 struct ec_params_motion_sense
*params
;
277 struct ec_response_motion_sense
*resp
;
278 struct cros_ec_command
*msg
;
280 msg
= kzalloc(sizeof(struct cros_ec_command
) +
281 max(sizeof(*params
), sizeof(*resp
)), GFP_KERNEL
);
286 msg
->command
= EC_CMD_MOTION_SENSE_CMD
+ ec
->cmd_offset
;
287 msg
->outsize
= sizeof(*params
);
288 msg
->insize
= sizeof(*resp
);
290 params
= (struct ec_params_motion_sense
*)msg
->data
;
291 params
->cmd
= MOTIONSENSE_CMD_DUMP
;
293 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
294 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
295 dev_warn(ec
->dev
, "cannot get EC sensor information: %d/%d\n",
300 resp
= (struct ec_response_motion_sense
*)msg
->data
;
301 sensor_num
= resp
->dump
.sensor_count
;
302 /* Allocate 1 extra sensors in FIFO are needed */
303 sensor_cells
= kcalloc(sensor_num
+ 1, sizeof(struct mfd_cell
),
305 if (sensor_cells
== NULL
)
308 sensor_platforms
= kcalloc(sensor_num
+ 1,
309 sizeof(struct cros_ec_sensor_platform
),
311 if (sensor_platforms
== NULL
)
312 goto error_platforms
;
314 memset(sensor_type
, 0, sizeof(sensor_type
));
316 for (i
= 0; i
< sensor_num
; i
++) {
317 params
->cmd
= MOTIONSENSE_CMD_INFO
;
318 params
->info
.sensor_num
= i
;
319 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
320 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
321 dev_warn(ec
->dev
, "no info for EC sensor %d : %d/%d\n",
322 i
, ret
, msg
->result
);
325 switch (resp
->info
.type
) {
326 case MOTIONSENSE_TYPE_ACCEL
:
327 sensor_cells
[id
].name
= "cros-ec-accel";
329 case MOTIONSENSE_TYPE_BARO
:
330 sensor_cells
[id
].name
= "cros-ec-baro";
332 case MOTIONSENSE_TYPE_GYRO
:
333 sensor_cells
[id
].name
= "cros-ec-gyro";
335 case MOTIONSENSE_TYPE_MAG
:
336 sensor_cells
[id
].name
= "cros-ec-mag";
338 case MOTIONSENSE_TYPE_PROX
:
339 sensor_cells
[id
].name
= "cros-ec-prox";
341 case MOTIONSENSE_TYPE_LIGHT
:
342 sensor_cells
[id
].name
= "cros-ec-light";
344 case MOTIONSENSE_TYPE_ACTIVITY
:
345 sensor_cells
[id
].name
= "cros-ec-activity";
348 dev_warn(ec
->dev
, "unknown type %d\n", resp
->info
.type
);
351 sensor_platforms
[id
].sensor_num
= i
;
352 sensor_cells
[id
].id
= sensor_type
[resp
->info
.type
];
353 sensor_cells
[id
].platform_data
= &sensor_platforms
[id
];
354 sensor_cells
[id
].pdata_size
=
355 sizeof(struct cros_ec_sensor_platform
);
357 sensor_type
[resp
->info
.type
]++;
361 if (sensor_type
[MOTIONSENSE_TYPE_ACCEL
] >= 2)
362 ec
->has_kb_wake_angle
= true;
364 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE_FIFO
)) {
365 sensor_cells
[id
].name
= "cros-ec-ring";
369 ret
= mfd_add_devices(ec
->dev
, 0, sensor_cells
, id
,
372 dev_err(ec
->dev
, "failed to add EC sensors\n");
374 kfree(sensor_platforms
);
381 static const struct mfd_cell cros_ec_cec_cells
[] = {
382 { .name
= "cros-ec-cec" }
385 static const struct mfd_cell cros_ec_rtc_cells
[] = {
386 { .name
= "cros-ec-rtc" }
389 static const struct mfd_cell cros_usbpd_charger_cells
[] = {
390 { .name
= "cros-usbpd-charger" }
393 static int ec_device_probe(struct platform_device
*pdev
)
395 int retval
= -ENOMEM
;
396 struct device
*dev
= &pdev
->dev
;
397 struct cros_ec_platform
*ec_platform
= dev_get_platdata(dev
);
398 struct cros_ec_dev
*ec
= devm_kzalloc(dev
, sizeof(*ec
), GFP_KERNEL
);
403 dev_set_drvdata(dev
, ec
);
404 ec
->ec_dev
= dev_get_drvdata(dev
->parent
);
406 ec
->cmd_offset
= ec_platform
->cmd_offset
;
407 ec
->features
[0] = -1U; /* Not cached yet */
408 ec
->features
[1] = -1U; /* Not cached yet */
409 device_initialize(&ec
->class_dev
);
410 cdev_init(&ec
->cdev
, &fops
);
413 * Add the class device
414 * Link to the character device for creating the /dev entry
417 ec
->class_dev
.devt
= MKDEV(ec_major
, pdev
->id
);
418 ec
->class_dev
.class = &cros_class
;
419 ec
->class_dev
.parent
= dev
;
421 retval
= dev_set_name(&ec
->class_dev
, "%s", ec_platform
->ec_name
);
423 dev_err(dev
, "dev_set_name failed => %d\n", retval
);
427 /* check whether this EC is a sensor hub. */
428 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE
))
429 cros_ec_sensors_register(ec
);
431 /* Check whether this EC instance has CEC host command support */
432 if (cros_ec_check_features(ec
, EC_FEATURE_CEC
)) {
433 retval
= mfd_add_devices(ec
->dev
, PLATFORM_DEVID_AUTO
,
435 ARRAY_SIZE(cros_ec_cec_cells
),
439 "failed to add cros-ec-cec device: %d\n",
443 /* Check whether this EC instance has RTC host command support */
444 if (cros_ec_check_features(ec
, EC_FEATURE_RTC
)) {
445 retval
= mfd_add_devices(ec
->dev
, PLATFORM_DEVID_AUTO
,
447 ARRAY_SIZE(cros_ec_rtc_cells
),
451 "failed to add cros-ec-rtc device: %d\n",
455 /* Check whether this EC instance has the PD charge manager */
456 if (cros_ec_check_features(ec
, EC_FEATURE_USB_PD
)) {
457 retval
= mfd_add_devices(ec
->dev
, PLATFORM_DEVID_AUTO
,
458 cros_usbpd_charger_cells
,
459 ARRAY_SIZE(cros_usbpd_charger_cells
),
463 "failed to add cros-usbpd-charger device: %d\n",
467 /* Take control of the lightbar from the EC. */
468 lb_manual_suspend_ctrl(ec
, 1);
470 /* We can now add the sysfs class, we know which parameter to show */
471 retval
= cdev_device_add(&ec
->cdev
, &ec
->class_dev
);
473 dev_err(dev
, "cdev_device_add failed => %d\n", retval
);
477 if (cros_ec_debugfs_init(ec
))
478 dev_warn(dev
, "failed to create debugfs directory\n");
483 put_device(&ec
->class_dev
);
487 static int ec_device_remove(struct platform_device
*pdev
)
489 struct cros_ec_dev
*ec
= dev_get_drvdata(&pdev
->dev
);
491 /* Let the EC take over the lightbar again. */
492 lb_manual_suspend_ctrl(ec
, 0);
494 cros_ec_debugfs_remove(ec
);
497 device_unregister(&ec
->class_dev
);
501 static void ec_device_shutdown(struct platform_device
*pdev
)
503 struct cros_ec_dev
*ec
= dev_get_drvdata(&pdev
->dev
);
505 /* Be sure to clear up debugfs delayed works */
506 cros_ec_debugfs_remove(ec
);
509 static const struct platform_device_id cros_ec_id
[] = {
513 MODULE_DEVICE_TABLE(platform
, cros_ec_id
);
515 static __maybe_unused
int ec_device_suspend(struct device
*dev
)
517 struct cros_ec_dev
*ec
= dev_get_drvdata(dev
);
519 cros_ec_debugfs_suspend(ec
);
526 static __maybe_unused
int ec_device_resume(struct device
*dev
)
528 struct cros_ec_dev
*ec
= dev_get_drvdata(dev
);
530 cros_ec_debugfs_resume(ec
);
537 static const struct dev_pm_ops cros_ec_dev_pm_ops
= {
538 #ifdef CONFIG_PM_SLEEP
539 .suspend
= ec_device_suspend
,
540 .resume
= ec_device_resume
,
544 static struct platform_driver cros_ec_dev_driver
= {
547 .pm
= &cros_ec_dev_pm_ops
,
549 .id_table
= cros_ec_id
,
550 .probe
= ec_device_probe
,
551 .remove
= ec_device_remove
,
552 .shutdown
= ec_device_shutdown
,
555 static int __init
cros_ec_dev_init(void)
560 ret
= class_register(&cros_class
);
562 pr_err(CROS_EC_DEV_NAME
": failed to register device class\n");
566 /* Get a range of minor numbers (starting with 0) to work with */
567 ret
= alloc_chrdev_region(&dev
, 0, CROS_MAX_DEV
, CROS_EC_DEV_NAME
);
569 pr_err(CROS_EC_DEV_NAME
": alloc_chrdev_region() failed\n");
570 goto failed_chrdevreg
;
572 ec_major
= MAJOR(dev
);
574 /* Register the driver */
575 ret
= platform_driver_register(&cros_ec_dev_driver
);
577 pr_warn(CROS_EC_DEV_NAME
": can't register driver: %d\n", ret
);
583 unregister_chrdev_region(MKDEV(ec_major
, 0), CROS_MAX_DEV
);
585 class_unregister(&cros_class
);
589 static void __exit
cros_ec_dev_exit(void)
591 platform_driver_unregister(&cros_ec_dev_driver
);
592 unregister_chrdev(ec_major
, CROS_EC_DEV_NAME
);
593 class_unregister(&cros_class
);
596 module_init(cros_ec_dev_init
);
597 module_exit(cros_ec_dev_exit
);
599 MODULE_ALIAS("platform:" DRV_NAME
);
600 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
601 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
602 MODULE_VERSION("1.0");
603 MODULE_LICENSE("GPL");