blob: daf0ffd367a220b8b7d4bf4156deb033ccecb8db [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
Eric Carusoe8626452017-05-16 17:46:48 +020028#include "cros_ec_debugfs.h"
Bill Richardsone7c256f2015-02-02 12:26:25 +010029#include "cros_ec_dev.h"
30
Thierry Escandeea01a312017-11-20 17:15:25 +010031#define DRV_NAME "cros-ec-dev"
32
Bill Richardsone7c256f2015-02-02 12:26:25 +010033/* Device variables */
34#define CROS_MAX_DEV 128
Bill Richardsone7c256f2015-02-02 12:26:25 +010035static int ec_major;
36
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020037static const struct attribute_group *cros_ec_groups[] = {
38 &cros_ec_attr_group,
39 &cros_ec_lightbar_attr_group,
Emilio López18800fc2015-09-21 10:38:22 -030040 &cros_ec_vbc_attr_group,
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020041 NULL,
42};
43
44static struct class cros_class = {
45 .owner = THIS_MODULE,
46 .name = "chromeos",
47 .dev_groups = cros_ec_groups,
48};
49
Bill Richardsone7c256f2015-02-02 12:26:25 +010050/* Basic communication */
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020051static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
Bill Richardsone7c256f2015-02-02 12:26:25 +010052{
53 struct ec_response_get_version *resp;
54 static const char * const current_image_name[] = {
55 "unknown", "read-only", "read-write", "invalid",
56 };
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020057 struct cros_ec_command *msg;
Bill Richardsone7c256f2015-02-02 12:26:25 +010058 int ret;
59
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020060 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
61 if (!msg)
62 return -ENOMEM;
Bill Richardsone7c256f2015-02-02 12:26:25 +010063
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020064 msg->version = 0;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020065 msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020066 msg->insize = sizeof(*resp);
67 msg->outsize = 0;
68
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020069 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020070 if (ret < 0)
71 goto exit;
72
73 if (msg->result != EC_RES_SUCCESS) {
Bill Richardsone7c256f2015-02-02 12:26:25 +010074 snprintf(str, maxlen,
75 "%s\nUnknown EC version: EC returned %d\n",
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020076 CROS_EC_DEV_VERSION, msg->result);
77 ret = -EINVAL;
78 goto exit;
Bill Richardsone7c256f2015-02-02 12:26:25 +010079 }
80
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020081 resp = (struct ec_response_get_version *)msg->data;
Bill Richardsone7c256f2015-02-02 12:26:25 +010082 if (resp->current_image >= ARRAY_SIZE(current_image_name))
83 resp->current_image = 3; /* invalid */
84
Olof Johanssonef59c252015-03-03 13:22:32 +010085 snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
Bill Richardsone7c256f2015-02-02 12:26:25 +010086 resp->version_string_ro, resp->version_string_rw,
87 current_image_name[resp->current_image]);
88
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020089 ret = 0;
90exit:
91 kfree(msg);
92 return ret;
Bill Richardsone7c256f2015-02-02 12:26:25 +010093}
94
Vincent Palatine4244eb2016-08-01 11:54:37 +020095static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
96{
97 struct cros_ec_command *msg;
98 int ret;
99
100 if (ec->features[0] == -1U && ec->features[1] == -1U) {
101 /* features bitmap not read yet */
102
103 msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
104 if (!msg)
105 return -ENOMEM;
106
107 msg->version = 0;
108 msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
109 msg->insize = sizeof(ec->features);
110 msg->outsize = 0;
111
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",
115 ret, msg->result);
116 memset(ec->features, 0, sizeof(ec->features));
117 }
118
119 memcpy(ec->features, msg->data, sizeof(ec->features));
120
121 dev_dbg(ec->dev, "EC features %08x %08x\n",
122 ec->features[0], ec->features[1]);
123
124 kfree(msg);
125 }
126
127 return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
128}
129
Bill Richardsone7c256f2015-02-02 12:26:25 +0100130/* Device file ops */
131static int ec_device_open(struct inode *inode, struct file *filp)
132{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200133 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);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100137 return 0;
138}
139
140static int ec_device_release(struct inode *inode, struct file *filp)
141{
142 return 0;
143}
144
145static ssize_t ec_device_read(struct file *filp, char __user *buffer,
146 size_t length, loff_t *offset)
147{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200148 struct cros_ec_dev *ec = filp->private_data;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100149 char msg[sizeof(struct ec_response_get_version) +
150 sizeof(CROS_EC_DEV_VERSION)];
151 size_t count;
152 int ret;
153
154 if (*offset != 0)
155 return 0;
156
157 ret = ec_get_version(ec, msg, sizeof(msg));
158 if (ret)
159 return ret;
160
161 count = min(length, strlen(msg));
162
163 if (copy_to_user(buffer, msg, count))
164 return -EFAULT;
165
166 *offset = count;
167 return count;
168}
169
170/* Ioctls */
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200171static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
Bill Richardsone7c256f2015-02-02 12:26:25 +0100172{
173 long ret;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200174 struct cros_ec_command u_cmd;
175 struct cros_ec_command *s_cmd;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100176
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200177 if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
Bill Richardsone7c256f2015-02-02 12:26:25 +0100178 return -EFAULT;
179
Gwendal Grignou5d749d02016-03-08 09:13:52 -0800180 if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
181 (u_cmd.insize > EC_MAX_MSG_BYTES))
182 return -EINVAL;
183
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200184 s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
185 GFP_KERNEL);
186 if (!s_cmd)
187 return -ENOMEM;
188
189 if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
190 ret = -EFAULT;
191 goto exit;
192 }
193
Dan Carpenter096cdc62016-06-21 16:58:46 +0300194 if (u_cmd.outsize != s_cmd->outsize ||
195 u_cmd.insize != s_cmd->insize) {
196 ret = -EINVAL;
197 goto exit;
198 }
199
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200200 s_cmd->command += ec->cmd_offset;
201 ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100202 /* Only copy data to userland if data was received. */
203 if (ret < 0)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200204 goto exit;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100205
Dan Carpenter096cdc62016-06-21 16:58:46 +0300206 if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200207 ret = -EFAULT;
208exit:
209 kfree(s_cmd);
210 return ret;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100211}
212
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200213static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
Bill Richardsone7c256f2015-02-02 12:26:25 +0100214{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200215 struct cros_ec_device *ec_dev = ec->ec_dev;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100216 struct cros_ec_readmem s_mem = { };
217 long num;
218
219 /* Not every platform supports direct reads */
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200220 if (!ec_dev->cmd_readmem)
Bill Richardsone7c256f2015-02-02 12:26:25 +0100221 return -ENOTTY;
222
223 if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
224 return -EFAULT;
225
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200226 num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
227 s_mem.buffer);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100228 if (num <= 0)
229 return num;
230
231 if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
232 return -EFAULT;
233
234 return 0;
235}
236
237static long ec_device_ioctl(struct file *filp, unsigned int cmd,
238 unsigned long arg)
239{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200240 struct cros_ec_dev *ec = filp->private_data;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100241
242 if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
243 return -ENOTTY;
244
245 switch (cmd) {
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);
250 }
251
252 return -ENOTTY;
253}
254
255/* Module initialization */
256static 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,
Guenter Roeck2521ea32016-04-14 19:35:29 -0700261#ifdef CONFIG_COMPAT
262 .compat_ioctl = ec_device_ioctl,
263#endif
Bill Richardsone7c256f2015-02-02 12:26:25 +0100264};
265
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200266static void __remove(struct device *dev)
267{
268 struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
269 class_dev);
270 kfree(ec);
271}
272
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +0200273static void cros_ec_sensors_register(struct cros_ec_dev *ec)
274{
275 /*
276 * Issue a command to get the number of sensor reported.
277 * Build an array of sensors driver and register them all.
278 */
279 int ret, i, id, sensor_num;
280 struct mfd_cell *sensor_cells;
281 struct cros_ec_sensor_platform *sensor_platforms;
282 int sensor_type[MOTIONSENSE_TYPE_MAX];
283 struct ec_params_motion_sense *params;
284 struct ec_response_motion_sense *resp;
285 struct cros_ec_command *msg;
286
287 msg = kzalloc(sizeof(struct cros_ec_command) +
288 max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
289 if (msg == NULL)
290 return;
291
292 msg->version = 2;
293 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
294 msg->outsize = sizeof(*params);
295 msg->insize = sizeof(*resp);
296
297 params = (struct ec_params_motion_sense *)msg->data;
298 params->cmd = MOTIONSENSE_CMD_DUMP;
299
300 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
301 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
302 dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
303 ret, msg->result);
304 goto error;
305 }
306
307 resp = (struct ec_response_motion_sense *)msg->data;
308 sensor_num = resp->dump.sensor_count;
309 /* Allocate 2 extra sensors in case lid angle or FIFO are needed */
310 sensor_cells = kzalloc(sizeof(struct mfd_cell) * (sensor_num + 2),
311 GFP_KERNEL);
312 if (sensor_cells == NULL)
313 goto error;
314
315 sensor_platforms = kzalloc(sizeof(struct cros_ec_sensor_platform) *
316 (sensor_num + 1), GFP_KERNEL);
317 if (sensor_platforms == NULL)
318 goto error_platforms;
319
320 memset(sensor_type, 0, sizeof(sensor_type));
321 id = 0;
322 for (i = 0; i < sensor_num; i++) {
323 params->cmd = MOTIONSENSE_CMD_INFO;
324 params->info.sensor_num = i;
325 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
326 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
327 dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
328 i, ret, msg->result);
329 continue;
330 }
331 switch (resp->info.type) {
332 case MOTIONSENSE_TYPE_ACCEL:
333 sensor_cells[id].name = "cros-ec-accel";
334 break;
Gwendal Grignoud7322482017-01-24 14:41:41 +0100335 case MOTIONSENSE_TYPE_BARO:
336 sensor_cells[id].name = "cros-ec-baro";
337 break;
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +0200338 case MOTIONSENSE_TYPE_GYRO:
339 sensor_cells[id].name = "cros-ec-gyro";
340 break;
341 case MOTIONSENSE_TYPE_MAG:
342 sensor_cells[id].name = "cros-ec-mag";
343 break;
344 case MOTIONSENSE_TYPE_PROX:
345 sensor_cells[id].name = "cros-ec-prox";
346 break;
347 case MOTIONSENSE_TYPE_LIGHT:
348 sensor_cells[id].name = "cros-ec-light";
349 break;
350 case MOTIONSENSE_TYPE_ACTIVITY:
351 sensor_cells[id].name = "cros-ec-activity";
352 break;
353 default:
354 dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
355 continue;
356 }
357 sensor_platforms[id].sensor_num = i;
358 sensor_cells[id].id = sensor_type[resp->info.type];
359 sensor_cells[id].platform_data = &sensor_platforms[id];
360 sensor_cells[id].pdata_size =
361 sizeof(struct cros_ec_sensor_platform);
362
363 sensor_type[resp->info.type]++;
364 id++;
365 }
366 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) {
367 sensor_platforms[id].sensor_num = sensor_num;
368
369 sensor_cells[id].name = "cros-ec-angle";
370 sensor_cells[id].id = 0;
371 sensor_cells[id].platform_data = &sensor_platforms[id];
372 sensor_cells[id].pdata_size =
373 sizeof(struct cros_ec_sensor_platform);
374 id++;
375 }
376 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
377 sensor_cells[id].name = "cros-ec-ring";
378 id++;
379 }
380
381 ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
382 NULL, 0, NULL);
383 if (ret)
384 dev_err(ec->dev, "failed to add EC sensors\n");
385
386 kfree(sensor_platforms);
387error_platforms:
388 kfree(sensor_cells);
389error:
390 kfree(msg);
391}
392
Bill Richardsone7c256f2015-02-02 12:26:25 +0100393static int ec_device_probe(struct platform_device *pdev)
394{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200395 int retval = -ENOMEM;
396 struct device *dev = &pdev->dev;
397 struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200398 struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100399
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200400 if (!ec)
401 return retval;
402
403 dev_set_drvdata(dev, ec);
404 ec->ec_dev = dev_get_drvdata(dev->parent);
405 ec->dev = dev;
406 ec->cmd_offset = ec_platform->cmd_offset;
Vincent Palatine4244eb2016-08-01 11:54:37 +0200407 ec->features[0] = -1U; /* Not cached yet */
408 ec->features[1] = -1U; /* Not cached yet */
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200409 device_initialize(&ec->class_dev);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100410 cdev_init(&ec->cdev, &fops);
411
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200412 /*
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200413 * Add the class device
414 * Link to the character device for creating the /dev entry
415 * in devtmpfs.
416 */
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600417 ec->class_dev.devt = MKDEV(ec_major, pdev->id);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200418 ec->class_dev.class = &cros_class;
419 ec->class_dev.parent = dev;
420 ec->class_dev.release = __remove;
421
422 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
423 if (retval) {
424 dev_err(dev, "dev_set_name failed => %d\n", retval);
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600425 goto failed;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100426 }
427
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600428 retval = cdev_device_add(&ec->cdev, &ec->class_dev);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200429 if (retval) {
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600430 dev_err(dev, "cdev_device_add failed => %d\n", retval);
431 goto failed;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200432 }
Bill Richardson71af4b52015-02-02 12:26:27 +0100433
Eric Carusoe8626452017-05-16 17:46:48 +0200434 if (cros_ec_debugfs_init(ec))
435 dev_warn(dev, "failed to create debugfs directory\n");
436
Enric Balletbo i Serra5668bfd2016-08-01 11:54:38 +0200437 /* check whether this EC is a sensor hub. */
438 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
439 cros_ec_sensors_register(ec);
440
Eric Caruso405c8432017-05-16 17:46:48 +0200441 /* Take control of the lightbar from the EC. */
Jeffery Yu995c0ec2017-05-16 17:46:48 +0200442 lb_manual_suspend_ctrl(ec, 1);
Eric Caruso405c8432017-05-16 17:46:48 +0200443
Bill Richardsone7c256f2015-02-02 12:26:25 +0100444 return 0;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200445
Logan Gunthorpe1c1d1522017-03-17 12:48:14 -0600446failed:
447 put_device(&ec->class_dev);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200448 return retval;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100449}
450
451static int ec_device_remove(struct platform_device *pdev)
452{
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200453 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
Eric Carusoe8626452017-05-16 17:46:48 +0200454
Eric Caruso405c8432017-05-16 17:46:48 +0200455 /* Let the EC take over the lightbar again. */
Jeffery Yu995c0ec2017-05-16 17:46:48 +0200456 lb_manual_suspend_ctrl(ec, 0);
Eric Caruso405c8432017-05-16 17:46:48 +0200457
Eric Carusoe8626452017-05-16 17:46:48 +0200458 cros_ec_debugfs_remove(ec);
459
Bill Richardsone7c256f2015-02-02 12:26:25 +0100460 cdev_del(&ec->cdev);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200461 device_unregister(&ec->class_dev);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100462 return 0;
463}
464
Javier Martinez Canillasafbf8ec2015-06-22 08:27:20 +0200465static const struct platform_device_id cros_ec_id[] = {
Thierry Escandeea01a312017-11-20 17:15:25 +0100466 { DRV_NAME, 0 },
Javier Martinez Canillasafbf8ec2015-06-22 08:27:20 +0200467 { /* sentinel */ },
468};
469MODULE_DEVICE_TABLE(platform, cros_ec_id);
470
Arnd Bergmann5d6a3122017-06-27 17:36:36 +0200471static __maybe_unused int ec_device_suspend(struct device *dev)
Eric Caruso405c8432017-05-16 17:46:48 +0200472{
473 struct cros_ec_dev *ec = dev_get_drvdata(dev);
474
Jeffery Yu995c0ec2017-05-16 17:46:48 +0200475 lb_suspend(ec);
Eric Caruso405c8432017-05-16 17:46:48 +0200476
477 return 0;
478}
479
Arnd Bergmann5d6a3122017-06-27 17:36:36 +0200480static __maybe_unused int ec_device_resume(struct device *dev)
Eric Caruso405c8432017-05-16 17:46:48 +0200481{
482 struct cros_ec_dev *ec = dev_get_drvdata(dev);
483
Jeffery Yu995c0ec2017-05-16 17:46:48 +0200484 lb_resume(ec);
Eric Caruso405c8432017-05-16 17:46:48 +0200485
486 return 0;
487}
488
489static const struct dev_pm_ops cros_ec_dev_pm_ops = {
490#ifdef CONFIG_PM_SLEEP
491 .suspend = ec_device_suspend,
492 .resume = ec_device_resume,
493#endif
494};
495
Bill Richardsone7c256f2015-02-02 12:26:25 +0100496static struct platform_driver cros_ec_dev_driver = {
497 .driver = {
Thierry Escandeea01a312017-11-20 17:15:25 +0100498 .name = DRV_NAME,
Eric Caruso405c8432017-05-16 17:46:48 +0200499 .pm = &cros_ec_dev_pm_ops,
Bill Richardsone7c256f2015-02-02 12:26:25 +0100500 },
501 .probe = ec_device_probe,
502 .remove = ec_device_remove,
503};
504
505static int __init cros_ec_dev_init(void)
506{
507 int ret;
508 dev_t dev = 0;
509
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200510 ret = class_register(&cros_class);
511 if (ret) {
Bill Richardsone7c256f2015-02-02 12:26:25 +0100512 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200513 return ret;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100514 }
515
516 /* Get a range of minor numbers (starting with 0) to work with */
517 ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
518 if (ret < 0) {
519 pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
520 goto failed_chrdevreg;
521 }
522 ec_major = MAJOR(dev);
523
524 /* Register the driver */
525 ret = platform_driver_register(&cros_ec_dev_driver);
526 if (ret < 0) {
527 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
528 goto failed_devreg;
529 }
530 return 0;
531
532failed_devreg:
533 unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
534failed_chrdevreg:
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200535 class_unregister(&cros_class);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100536 return ret;
537}
538
539static void __exit cros_ec_dev_exit(void)
540{
541 platform_driver_unregister(&cros_ec_dev_driver);
542 unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200543 class_unregister(&cros_class);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100544}
545
546module_init(cros_ec_dev_init);
547module_exit(cros_ec_dev_exit);
548
Thierry Escandeea01a312017-11-20 17:15:25 +0100549MODULE_ALIAS("platform:" DRV_NAME);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100550MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
551MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
552MODULE_VERSION("1.0");
553MODULE_LICENSE("GPL");