blob: 4199cdd4ff89e9b82782bb78bfb4e6b08825a1f8 [file] [log] [blame]
Bill Richardsone7c256f2015-02-02 12:26:25 +01001/*
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>
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +020021#include <linux/mfd/core.h>
Bill Richardsone7c256f2015-02-02 12:26:25 +010022#include <linux/module.h>
23#include <linux/platform_device.h>
Eric Caruso405c8432017-05-16 17:46:48 +020024#include <linux/pm.h>
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020025#include <linux/slab.h>
Bill Richardsone7c256f2015-02-02 12:26:25 +010026#include <linux/uaccess.h>
27
28#include "cros_ec_dev.h"
29
Thierry Escandeea01a312017-11-20 17:15:25 +010030#define DRV_NAME "cros-ec-dev"
31
Bill Richardsone7c256f2015-02-02 12:26:25 +010032/* Device variables */
33#define CROS_MAX_DEV 128
Bill Richardsone7c256f2015-02-02 12:26:25 +010034static int ec_major;
35
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020036static const struct attribute_group *cros_ec_groups[] = {
37 &cros_ec_attr_group,
38 &cros_ec_lightbar_attr_group,
Emilio López18800fc2015-09-21 10:38:22 -030039 &cros_ec_vbc_attr_group,
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020040 NULL,
41};
42
43static struct class cros_class = {
44 .owner = THIS_MODULE,
45 .name = "chromeos",
46 .dev_groups = cros_ec_groups,
47};
48
Bill Richardsone7c256f2015-02-02 12:26:25 +010049/* Basic communication */
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020050static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
Bill Richardsone7c256f2015-02-02 12:26:25 +010051{
52 struct ec_response_get_version *resp;
53 static const char * const current_image_name[] = {
54 "unknown", "read-only", "read-write", "invalid",
55 };
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020056 struct cros_ec_command *msg;
Bill Richardsone7c256f2015-02-02 12:26:25 +010057 int ret;
58
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020059 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
60 if (!msg)
61 return -ENOMEM;
Bill Richardsone7c256f2015-02-02 12:26:25 +010062
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020063 msg->version = 0;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020064 msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020065 msg->insize = sizeof(*resp);
66 msg->outsize = 0;
67
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020068 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020069 if (ret < 0)
70 goto exit;
71
72 if (msg->result != EC_RES_SUCCESS) {
Bill Richardsone7c256f2015-02-02 12:26:25 +010073 snprintf(str, maxlen,
74 "%s\nUnknown EC version: EC returned %d\n",
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020075 CROS_EC_DEV_VERSION, msg->result);
76 ret = -EINVAL;
77 goto exit;
Bill Richardsone7c256f2015-02-02 12:26:25 +010078 }
79
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020080 resp = (struct ec_response_get_version *)msg->data;
Bill Richardsone7c256f2015-02-02 12:26:25 +010081 if (resp->current_image >= ARRAY_SIZE(current_image_name))
82 resp->current_image = 3; /* invalid */
83
Olof Johanssonef59c252015-03-03 13:22:32 +010084 snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
Bill Richardsone7c256f2015-02-02 12:26:25 +010085 resp->version_string_ro, resp->version_string_rw,
86 current_image_name[resp->current_image]);
87
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020088 ret = 0;
89exit:
90 kfree(msg);
91 return ret;
Bill Richardsone7c256f2015-02-02 12:26:25 +010092}
93
Vincent Palatine4244eb2016-08-01 11:54:37 +020094static 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));
Stephen Boyddf7c3bf2018-05-30 23:23:43 -0700116 } else {
117 memcpy(ec->features, msg->data, sizeof(ec->features));
Vincent Palatine4244eb2016-08-01 11:54:37 +0200118 }
119
Vincent Palatine4244eb2016-08-01 11:54:37 +0200120 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
Bill Richardsone7c256f2015-02-02 12:26:25 +0100129/* Device file ops */
130static int ec_device_open(struct inode *inode, struct file *filp)
131{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200132 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);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100136 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{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200147 struct cros_ec_dev *ec = filp->private_data;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100148 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 */
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200170static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
Bill Richardsone7c256f2015-02-02 12:26:25 +0100171{
172 long ret;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200173 struct cros_ec_command u_cmd;
174 struct cros_ec_command *s_cmd;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100175
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200176 if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
Bill Richardsone7c256f2015-02-02 12:26:25 +0100177 return -EFAULT;
178
Gwendal Grignou5d749d02016-03-08 09:13:52 -0800179 if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
180 (u_cmd.insize > EC_MAX_MSG_BYTES))
181 return -EINVAL;
182
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200183 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
Dan Carpenter096cdc62016-06-21 16:58:46 +0300193 if (u_cmd.outsize != s_cmd->outsize ||
194 u_cmd.insize != s_cmd->insize) {
195 ret = -EINVAL;
196 goto exit;
197 }
198
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200199 s_cmd->command += ec->cmd_offset;
200 ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100201 /* Only copy data to userland if data was received. */
202 if (ret < 0)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200203 goto exit;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100204
Dan Carpenter096cdc62016-06-21 16:58:46 +0300205 if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200206 ret = -EFAULT;
207exit:
208 kfree(s_cmd);
209 return ret;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100210}
211
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200212static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
Bill Richardsone7c256f2015-02-02 12:26:25 +0100213{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200214 struct cros_ec_device *ec_dev = ec->ec_dev;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100215 struct cros_ec_readmem s_mem = { };
216 long num;
217
218 /* Not every platform supports direct reads */
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200219 if (!ec_dev->cmd_readmem)
Bill Richardsone7c256f2015-02-02 12:26:25 +0100220 return -ENOTTY;
221
222 if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
223 return -EFAULT;
224
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200225 num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
226 s_mem.buffer);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100227 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{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200239 struct cros_ec_dev *ec = filp->private_data;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100240
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,
Guenter Roeck2521ea32016-04-14 19:35:29 -0700260#ifdef CONFIG_COMPAT
261 .compat_ioctl = ec_device_ioctl,
262#endif
Bill Richardsone7c256f2015-02-02 12:26:25 +0100263};
264
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +0200265static void cros_ec_sensors_register(struct cros_ec_dev *ec)
266{
267 /*
268 * Issue a command to get the number of sensor reported.
269 * Build an array of sensors driver and register them all.
270 */
271 int ret, i, id, sensor_num;
272 struct mfd_cell *sensor_cells;
273 struct cros_ec_sensor_platform *sensor_platforms;
274 int sensor_type[MOTIONSENSE_TYPE_MAX];
275 struct ec_params_motion_sense *params;
276 struct ec_response_motion_sense *resp;
277 struct cros_ec_command *msg;
278
279 msg = kzalloc(sizeof(struct cros_ec_command) +
280 max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
281 if (msg == NULL)
282 return;
283
284 msg->version = 2;
285 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
286 msg->outsize = sizeof(*params);
287 msg->insize = sizeof(*resp);
288
289 params = (struct ec_params_motion_sense *)msg->data;
290 params->cmd = MOTIONSENSE_CMD_DUMP;
291
292 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
293 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
294 dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
295 ret, msg->result);
296 goto error;
297 }
298
299 resp = (struct ec_response_motion_sense *)msg->data;
300 sensor_num = resp->dump.sensor_count;
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100301 /* Allocate 1 extra sensors in FIFO are needed */
302 sensor_cells = kzalloc(sizeof(struct mfd_cell) * (sensor_num + 1),
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +0200303 GFP_KERNEL);
304 if (sensor_cells == NULL)
305 goto error;
306
307 sensor_platforms = kzalloc(sizeof(struct cros_ec_sensor_platform) *
308 (sensor_num + 1), GFP_KERNEL);
309 if (sensor_platforms == NULL)
310 goto error_platforms;
311
312 memset(sensor_type, 0, sizeof(sensor_type));
313 id = 0;
314 for (i = 0; i < sensor_num; i++) {
315 params->cmd = MOTIONSENSE_CMD_INFO;
316 params->info.sensor_num = i;
317 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
318 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
319 dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
320 i, ret, msg->result);
321 continue;
322 }
323 switch (resp->info.type) {
324 case MOTIONSENSE_TYPE_ACCEL:
325 sensor_cells[id].name = "cros-ec-accel";
326 break;
Gwendal Grignoud7322482017-01-24 14:41:41 +0100327 case MOTIONSENSE_TYPE_BARO:
328 sensor_cells[id].name = "cros-ec-baro";
329 break;
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +0200330 case MOTIONSENSE_TYPE_GYRO:
331 sensor_cells[id].name = "cros-ec-gyro";
332 break;
333 case MOTIONSENSE_TYPE_MAG:
334 sensor_cells[id].name = "cros-ec-mag";
335 break;
336 case MOTIONSENSE_TYPE_PROX:
337 sensor_cells[id].name = "cros-ec-prox";
338 break;
339 case MOTIONSENSE_TYPE_LIGHT:
340 sensor_cells[id].name = "cros-ec-light";
341 break;
342 case MOTIONSENSE_TYPE_ACTIVITY:
343 sensor_cells[id].name = "cros-ec-activity";
344 break;
345 default:
346 dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
347 continue;
348 }
349 sensor_platforms[id].sensor_num = i;
350 sensor_cells[id].id = sensor_type[resp->info.type];
351 sensor_cells[id].platform_data = &sensor_platforms[id];
352 sensor_cells[id].pdata_size =
353 sizeof(struct cros_ec_sensor_platform);
354
355 sensor_type[resp->info.type]++;
356 id++;
357 }
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +0200358
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100359 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
360 ec->has_kb_wake_angle = true;
361
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +0200362 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
363 sensor_cells[id].name = "cros-ec-ring";
364 id++;
365 }
366
367 ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
368 NULL, 0, NULL);
369 if (ret)
370 dev_err(ec->dev, "failed to add EC sensors\n");
371
372 kfree(sensor_platforms);
373error_platforms:
374 kfree(sensor_cells);
375error:
376 kfree(msg);
377}
378
Enric Balletbo i Serra95a4d072018-04-18 12:24:01 +0200379static const struct mfd_cell cros_ec_rtc_cells[] = {
380 { .name = "cros-ec-rtc" }
381};
382
Bill Richardsone7c256f2015-02-02 12:26:25 +0100383static int ec_device_probe(struct platform_device *pdev)
384{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200385 int retval = -ENOMEM;
386 struct device *dev = &pdev->dev;
387 struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
Gwendal Grignou3aa21772018-05-30 09:54:22 -0700388 struct cros_ec_dev *ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100389
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200390 if (!ec)
391 return retval;
392
393 dev_set_drvdata(dev, ec);
394 ec->ec_dev = dev_get_drvdata(dev->parent);
395 ec->dev = dev;
396 ec->cmd_offset = ec_platform->cmd_offset;
Vincent Palatine4244eb2016-08-01 11:54:37 +0200397 ec->features[0] = -1U; /* Not cached yet */
398 ec->features[1] = -1U; /* Not cached yet */
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200399 device_initialize(&ec->class_dev);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100400 cdev_init(&ec->cdev, &fops);
401
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200402 /*
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200403 * Add the class device
404 * Link to the character device for creating the /dev entry
405 * in devtmpfs.
406 */
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600407 ec->class_dev.devt = MKDEV(ec_major, pdev->id);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200408 ec->class_dev.class = &cros_class;
409 ec->class_dev.parent = dev;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200410
411 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
412 if (retval) {
413 dev_err(dev, "dev_set_name failed => %d\n", retval);
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600414 goto failed;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100415 }
416
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100417 /* check whether this EC is a sensor hub. */
418 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
419 cros_ec_sensors_register(ec);
420
Enric Balletbo i Serra95a4d072018-04-18 12:24:01 +0200421 /* Check whether this EC instance has RTC host command support */
422 if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
423 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
424 cros_ec_rtc_cells,
425 ARRAY_SIZE(cros_ec_rtc_cells),
426 NULL, 0, NULL);
427 if (retval)
428 dev_err(ec->dev,
429 "failed to add cros-ec-rtc device: %d\n",
430 retval);
431 }
432
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100433 /* Take control of the lightbar from the EC. */
434 lb_manual_suspend_ctrl(ec, 1);
435
436 /* We can now add the sysfs class, we know which parameter to show */
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600437 retval = cdev_device_add(&ec->cdev, &ec->class_dev);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200438 if (retval) {
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600439 dev_err(dev, "cdev_device_add failed => %d\n", retval);
440 goto failed;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200441 }
Bill Richardson71af4b52015-02-02 12:26:27 +0100442
Eric Carusoe8626452017-05-16 17:46:48 +0200443 if (cros_ec_debugfs_init(ec))
444 dev_warn(dev, "failed to create debugfs directory\n");
445
Bill Richardsone7c256f2015-02-02 12:26:25 +0100446 return 0;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200447
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600448failed:
449 put_device(&ec->class_dev);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200450 return retval;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100451}
452
453static int ec_device_remove(struct platform_device *pdev)
454{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200455 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
Eric Carusoe8626452017-05-16 17:46:48 +0200456
Eric Caruso405c8432017-05-16 17:46:48 +0200457 /* Let the EC take over the lightbar again. */
Jeffery Yu995c0ec2017-05-16 17:46:48 +0200458 lb_manual_suspend_ctrl(ec, 0);
Eric Caruso405c8432017-05-16 17:46:48 +0200459
Eric Carusoe8626452017-05-16 17:46:48 +0200460 cros_ec_debugfs_remove(ec);
461
Bill Richardsone7c256f2015-02-02 12:26:25 +0100462 cdev_del(&ec->cdev);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200463 device_unregister(&ec->class_dev);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100464 return 0;
465}
466
Daniel Hung-yu Wue15b7f42018-04-18 12:24:02 +0200467static void ec_device_shutdown(struct platform_device *pdev)
468{
469 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
470
471 /* Be sure to clear up debugfs delayed works */
472 cros_ec_debugfs_remove(ec);
473}
474
Javier Martinez Canillasafbf8ec2015-06-22 08:27:20 +0200475static const struct platform_device_id cros_ec_id[] = {
Thierry Escandeea01a312017-11-20 17:15:25 +0100476 { DRV_NAME, 0 },
Wei-Ning Huangabeed712018-04-18 12:24:03 +0200477 { /* sentinel */ }
Javier Martinez Canillasafbf8ec2015-06-22 08:27:20 +0200478};
479MODULE_DEVICE_TABLE(platform, cros_ec_id);
480
Arnd Bergmann5d6a3122017-06-27 17:36:36 +0200481static __maybe_unused int ec_device_suspend(struct device *dev)
Eric Caruso405c8432017-05-16 17:46:48 +0200482{
483 struct cros_ec_dev *ec = dev_get_drvdata(dev);
484
Douglas Anderson44d99d72018-04-18 12:24:00 +0200485 cros_ec_debugfs_suspend(ec);
486
Jeffery Yu995c0ec2017-05-16 17:46:48 +0200487 lb_suspend(ec);
Eric Caruso405c8432017-05-16 17:46:48 +0200488
489 return 0;
490}
491
Arnd Bergmann5d6a3122017-06-27 17:36:36 +0200492static __maybe_unused int ec_device_resume(struct device *dev)
Eric Caruso405c8432017-05-16 17:46:48 +0200493{
494 struct cros_ec_dev *ec = dev_get_drvdata(dev);
495
Douglas Anderson44d99d72018-04-18 12:24:00 +0200496 cros_ec_debugfs_resume(ec);
497
Jeffery Yu995c0ec2017-05-16 17:46:48 +0200498 lb_resume(ec);
Eric Caruso405c8432017-05-16 17:46:48 +0200499
500 return 0;
501}
502
503static const struct dev_pm_ops cros_ec_dev_pm_ops = {
504#ifdef CONFIG_PM_SLEEP
505 .suspend = ec_device_suspend,
506 .resume = ec_device_resume,
507#endif
508};
509
Bill Richardsone7c256f2015-02-02 12:26:25 +0100510static struct platform_driver cros_ec_dev_driver = {
511 .driver = {
Thierry Escandeea01a312017-11-20 17:15:25 +0100512 .name = DRV_NAME,
Eric Caruso405c8432017-05-16 17:46:48 +0200513 .pm = &cros_ec_dev_pm_ops,
Bill Richardsone7c256f2015-02-02 12:26:25 +0100514 },
515 .probe = ec_device_probe,
516 .remove = ec_device_remove,
Daniel Hung-yu Wue15b7f42018-04-18 12:24:02 +0200517 .shutdown = ec_device_shutdown,
Bill Richardsone7c256f2015-02-02 12:26:25 +0100518};
519
520static int __init cros_ec_dev_init(void)
521{
522 int ret;
523 dev_t dev = 0;
524
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200525 ret = class_register(&cros_class);
526 if (ret) {
Bill Richardsone7c256f2015-02-02 12:26:25 +0100527 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200528 return ret;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100529 }
530
531 /* Get a range of minor numbers (starting with 0) to work with */
532 ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
533 if (ret < 0) {
534 pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
535 goto failed_chrdevreg;
536 }
537 ec_major = MAJOR(dev);
538
539 /* Register the driver */
540 ret = platform_driver_register(&cros_ec_dev_driver);
541 if (ret < 0) {
542 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
543 goto failed_devreg;
544 }
545 return 0;
546
547failed_devreg:
548 unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
549failed_chrdevreg:
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200550 class_unregister(&cros_class);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100551 return ret;
552}
553
554static void __exit cros_ec_dev_exit(void)
555{
556 platform_driver_unregister(&cros_ec_dev_driver);
557 unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200558 class_unregister(&cros_class);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100559}
560
561module_init(cros_ec_dev_init);
562module_exit(cros_ec_dev_exit);
563
Thierry Escandeea01a312017-11-20 17:15:25 +0100564MODULE_ALIAS("platform:" DRV_NAME);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100565MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
566MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
567MODULE_VERSION("1.0");
568MODULE_LICENSE("GPL");