Commit | Line | Data |
---|---|---|
1ccea77e | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
e7c256fb BR |
2 | /* |
3 | * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space | |
4 | * | |
5 | * Copyright (C) 2014 Google, Inc. | |
e7c256fb BR |
6 | */ |
7 | ||
8 | #include <linux/fs.h> | |
5668bfdd | 9 | #include <linux/mfd/core.h> |
e7c256fb | 10 | #include <linux/module.h> |
ac316725 | 11 | #include <linux/mod_devicetable.h> |
0545625b | 12 | #include <linux/of_platform.h> |
e7c256fb | 13 | #include <linux/platform_device.h> |
405c8430 | 14 | #include <linux/pm.h> |
a8411784 | 15 | #include <linux/slab.h> |
e7c256fb BR |
16 | #include <linux/uaccess.h> |
17 | ||
18 | #include "cros_ec_dev.h" | |
19 | ||
ea01a31b TE |
20 | #define DRV_NAME "cros-ec-dev" |
21 | ||
e7c256fb BR |
22 | /* Device variables */ |
23 | #define CROS_MAX_DEV 128 | |
e7c256fb BR |
24 | static int ec_major; |
25 | ||
57b33ff0 GG |
26 | static struct class cros_class = { |
27 | .owner = THIS_MODULE, | |
28 | .name = "chromeos", | |
57b33ff0 GG |
29 | }; |
30 | ||
e7c256fb | 31 | /* Basic communication */ |
57b33ff0 | 32 | static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen) |
e7c256fb BR |
33 | { |
34 | struct ec_response_get_version *resp; | |
35 | static const char * const current_image_name[] = { | |
36 | "unknown", "read-only", "read-write", "invalid", | |
37 | }; | |
a8411784 | 38 | struct cros_ec_command *msg; |
e7c256fb BR |
39 | int ret; |
40 | ||
a8411784 JMC |
41 | msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL); |
42 | if (!msg) | |
43 | return -ENOMEM; | |
44 | ||
45 | msg->version = 0; | |
57b33ff0 | 46 | msg->command = EC_CMD_GET_VERSION + ec->cmd_offset; |
a8411784 JMC |
47 | msg->insize = sizeof(*resp); |
48 | msg->outsize = 0; | |
49 | ||
57b33ff0 | 50 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
e7c256fb | 51 | if (ret < 0) |
a8411784 | 52 | goto exit; |
e7c256fb | 53 | |
a8411784 | 54 | if (msg->result != EC_RES_SUCCESS) { |
e7c256fb BR |
55 | snprintf(str, maxlen, |
56 | "%s\nUnknown EC version: EC returned %d\n", | |
a8411784 JMC |
57 | CROS_EC_DEV_VERSION, msg->result); |
58 | ret = -EINVAL; | |
59 | goto exit; | |
e7c256fb BR |
60 | } |
61 | ||
a8411784 | 62 | resp = (struct ec_response_get_version *)msg->data; |
e7c256fb BR |
63 | if (resp->current_image >= ARRAY_SIZE(current_image_name)) |
64 | resp->current_image = 3; /* invalid */ | |
65 | ||
ef59c25d | 66 | snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION, |
e7c256fb BR |
67 | resp->version_string_ro, resp->version_string_rw, |
68 | current_image_name[resp->current_image]); | |
69 | ||
a8411784 JMC |
70 | ret = 0; |
71 | exit: | |
72 | kfree(msg); | |
73 | return ret; | |
e7c256fb BR |
74 | } |
75 | ||
e4244ebd VP |
76 | static int cros_ec_check_features(struct cros_ec_dev *ec, int feature) |
77 | { | |
78 | struct cros_ec_command *msg; | |
79 | int ret; | |
80 | ||
81 | if (ec->features[0] == -1U && ec->features[1] == -1U) { | |
82 | /* features bitmap not read yet */ | |
83 | ||
84 | msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL); | |
85 | if (!msg) | |
86 | return -ENOMEM; | |
87 | ||
88 | msg->version = 0; | |
89 | msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset; | |
90 | msg->insize = sizeof(ec->features); | |
91 | msg->outsize = 0; | |
92 | ||
93 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); | |
94 | if (ret < 0 || msg->result != EC_RES_SUCCESS) { | |
95 | dev_warn(ec->dev, "cannot get EC features: %d/%d\n", | |
96 | ret, msg->result); | |
97 | memset(ec->features, 0, sizeof(ec->features)); | |
df7c3bf2 SB |
98 | } else { |
99 | memcpy(ec->features, msg->data, sizeof(ec->features)); | |
e4244ebd VP |
100 | } |
101 | ||
e4244ebd VP |
102 | dev_dbg(ec->dev, "EC features %08x %08x\n", |
103 | ec->features[0], ec->features[1]); | |
104 | ||
105 | kfree(msg); | |
106 | } | |
107 | ||
108 | return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature); | |
109 | } | |
110 | ||
e7c256fb BR |
111 | /* Device file ops */ |
112 | static int ec_device_open(struct inode *inode, struct file *filp) | |
113 | { | |
57b33ff0 GG |
114 | struct cros_ec_dev *ec = container_of(inode->i_cdev, |
115 | struct cros_ec_dev, cdev); | |
116 | filp->private_data = ec; | |
117 | nonseekable_open(inode, filp); | |
e7c256fb BR |
118 | return 0; |
119 | } | |
120 | ||
121 | static int ec_device_release(struct inode *inode, struct file *filp) | |
122 | { | |
123 | return 0; | |
124 | } | |
125 | ||
126 | static ssize_t ec_device_read(struct file *filp, char __user *buffer, | |
127 | size_t length, loff_t *offset) | |
128 | { | |
57b33ff0 | 129 | struct cros_ec_dev *ec = filp->private_data; |
e7c256fb BR |
130 | char msg[sizeof(struct ec_response_get_version) + |
131 | sizeof(CROS_EC_DEV_VERSION)]; | |
132 | size_t count; | |
133 | int ret; | |
134 | ||
135 | if (*offset != 0) | |
136 | return 0; | |
137 | ||
138 | ret = ec_get_version(ec, msg, sizeof(msg)); | |
139 | if (ret) | |
140 | return ret; | |
141 | ||
142 | count = min(length, strlen(msg)); | |
143 | ||
144 | if (copy_to_user(buffer, msg, count)) | |
145 | return -EFAULT; | |
146 | ||
147 | *offset = count; | |
148 | return count; | |
149 | } | |
150 | ||
151 | /* Ioctls */ | |
57b33ff0 | 152 | static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg) |
e7c256fb BR |
153 | { |
154 | long ret; | |
a8411784 JMC |
155 | struct cros_ec_command u_cmd; |
156 | struct cros_ec_command *s_cmd; | |
e7c256fb | 157 | |
a8411784 | 158 | if (copy_from_user(&u_cmd, arg, sizeof(u_cmd))) |
e7c256fb BR |
159 | return -EFAULT; |
160 | ||
5d749d0b GG |
161 | if ((u_cmd.outsize > EC_MAX_MSG_BYTES) || |
162 | (u_cmd.insize > EC_MAX_MSG_BYTES)) | |
163 | return -EINVAL; | |
164 | ||
a8411784 JMC |
165 | s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize), |
166 | GFP_KERNEL); | |
167 | if (!s_cmd) | |
168 | return -ENOMEM; | |
169 | ||
170 | if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) { | |
171 | ret = -EFAULT; | |
172 | goto exit; | |
173 | } | |
174 | ||
096cdc6f DC |
175 | if (u_cmd.outsize != s_cmd->outsize || |
176 | u_cmd.insize != s_cmd->insize) { | |
177 | ret = -EINVAL; | |
178 | goto exit; | |
179 | } | |
180 | ||
57b33ff0 GG |
181 | s_cmd->command += ec->cmd_offset; |
182 | ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd); | |
e7c256fb BR |
183 | /* Only copy data to userland if data was received. */ |
184 | if (ret < 0) | |
a8411784 | 185 | goto exit; |
e7c256fb | 186 | |
096cdc6f | 187 | if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize)) |
a8411784 JMC |
188 | ret = -EFAULT; |
189 | exit: | |
190 | kfree(s_cmd); | |
191 | return ret; | |
e7c256fb BR |
192 | } |
193 | ||
57b33ff0 | 194 | static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg) |
e7c256fb | 195 | { |
57b33ff0 | 196 | struct cros_ec_device *ec_dev = ec->ec_dev; |
e7c256fb BR |
197 | struct cros_ec_readmem s_mem = { }; |
198 | long num; | |
199 | ||
200 | /* Not every platform supports direct reads */ | |
57b33ff0 | 201 | if (!ec_dev->cmd_readmem) |
e7c256fb BR |
202 | return -ENOTTY; |
203 | ||
204 | if (copy_from_user(&s_mem, arg, sizeof(s_mem))) | |
205 | return -EFAULT; | |
206 | ||
57b33ff0 GG |
207 | num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes, |
208 | s_mem.buffer); | |
e7c256fb BR |
209 | if (num <= 0) |
210 | return num; | |
211 | ||
212 | if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem))) | |
213 | return -EFAULT; | |
214 | ||
c1778e58 | 215 | return num; |
e7c256fb BR |
216 | } |
217 | ||
218 | static long ec_device_ioctl(struct file *filp, unsigned int cmd, | |
219 | unsigned long arg) | |
220 | { | |
57b33ff0 | 221 | struct cros_ec_dev *ec = filp->private_data; |
e7c256fb BR |
222 | |
223 | if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC) | |
224 | return -ENOTTY; | |
225 | ||
226 | switch (cmd) { | |
227 | case CROS_EC_DEV_IOCXCMD: | |
228 | return ec_device_ioctl_xcmd(ec, (void __user *)arg); | |
229 | case CROS_EC_DEV_IOCRDMEM: | |
230 | return ec_device_ioctl_readmem(ec, (void __user *)arg); | |
231 | } | |
232 | ||
233 | return -ENOTTY; | |
234 | } | |
235 | ||
236 | /* Module initialization */ | |
237 | static const struct file_operations fops = { | |
238 | .open = ec_device_open, | |
239 | .release = ec_device_release, | |
240 | .read = ec_device_read, | |
241 | .unlocked_ioctl = ec_device_ioctl, | |
2521ea3e GR |
242 | #ifdef CONFIG_COMPAT |
243 | .compat_ioctl = ec_device_ioctl, | |
244 | #endif | |
e7c256fb BR |
245 | }; |
246 | ||
48a2ca0e EBS |
247 | static void cros_ec_class_release(struct device *dev) |
248 | { | |
249 | kfree(to_cros_ec_dev(dev)); | |
250 | } | |
251 | ||
5668bfdd EBS |
252 | static void cros_ec_sensors_register(struct cros_ec_dev *ec) |
253 | { | |
254 | /* | |
255 | * Issue a command to get the number of sensor reported. | |
256 | * Build an array of sensors driver and register them all. | |
257 | */ | |
258 | int ret, i, id, sensor_num; | |
259 | struct mfd_cell *sensor_cells; | |
260 | struct cros_ec_sensor_platform *sensor_platforms; | |
261 | int sensor_type[MOTIONSENSE_TYPE_MAX]; | |
262 | struct ec_params_motion_sense *params; | |
263 | struct ec_response_motion_sense *resp; | |
264 | struct cros_ec_command *msg; | |
265 | ||
266 | msg = kzalloc(sizeof(struct cros_ec_command) + | |
267 | max(sizeof(*params), sizeof(*resp)), GFP_KERNEL); | |
268 | if (msg == NULL) | |
269 | return; | |
270 | ||
271 | msg->version = 2; | |
272 | msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; | |
273 | msg->outsize = sizeof(*params); | |
274 | msg->insize = sizeof(*resp); | |
275 | ||
276 | params = (struct ec_params_motion_sense *)msg->data; | |
277 | params->cmd = MOTIONSENSE_CMD_DUMP; | |
278 | ||
279 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); | |
280 | if (ret < 0 || msg->result != EC_RES_SUCCESS) { | |
281 | dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n", | |
282 | ret, msg->result); | |
283 | goto error; | |
284 | } | |
285 | ||
286 | resp = (struct ec_response_motion_sense *)msg->data; | |
287 | sensor_num = resp->dump.sensor_count; | |
c1d1e91a | 288 | /* Allocate 1 extra sensors in FIFO are needed */ |
6396bb22 | 289 | sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell), |
5668bfdd EBS |
290 | GFP_KERNEL); |
291 | if (sensor_cells == NULL) | |
292 | goto error; | |
293 | ||
6396bb22 KC |
294 | sensor_platforms = kcalloc(sensor_num + 1, |
295 | sizeof(struct cros_ec_sensor_platform), | |
296 | GFP_KERNEL); | |
5668bfdd EBS |
297 | if (sensor_platforms == NULL) |
298 | goto error_platforms; | |
299 | ||
300 | memset(sensor_type, 0, sizeof(sensor_type)); | |
301 | id = 0; | |
302 | for (i = 0; i < sensor_num; i++) { | |
303 | params->cmd = MOTIONSENSE_CMD_INFO; | |
304 | params->info.sensor_num = i; | |
305 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); | |
306 | if (ret < 0 || msg->result != EC_RES_SUCCESS) { | |
307 | dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n", | |
308 | i, ret, msg->result); | |
309 | continue; | |
310 | } | |
311 | switch (resp->info.type) { | |
312 | case MOTIONSENSE_TYPE_ACCEL: | |
313 | sensor_cells[id].name = "cros-ec-accel"; | |
314 | break; | |
d732248f GG |
315 | case MOTIONSENSE_TYPE_BARO: |
316 | sensor_cells[id].name = "cros-ec-baro"; | |
317 | break; | |
5668bfdd EBS |
318 | case MOTIONSENSE_TYPE_GYRO: |
319 | sensor_cells[id].name = "cros-ec-gyro"; | |
320 | break; | |
321 | case MOTIONSENSE_TYPE_MAG: | |
322 | sensor_cells[id].name = "cros-ec-mag"; | |
323 | break; | |
324 | case MOTIONSENSE_TYPE_PROX: | |
325 | sensor_cells[id].name = "cros-ec-prox"; | |
326 | break; | |
327 | case MOTIONSENSE_TYPE_LIGHT: | |
328 | sensor_cells[id].name = "cros-ec-light"; | |
329 | break; | |
330 | case MOTIONSENSE_TYPE_ACTIVITY: | |
331 | sensor_cells[id].name = "cros-ec-activity"; | |
332 | break; | |
333 | default: | |
334 | dev_warn(ec->dev, "unknown type %d\n", resp->info.type); | |
335 | continue; | |
336 | } | |
337 | sensor_platforms[id].sensor_num = i; | |
338 | sensor_cells[id].id = sensor_type[resp->info.type]; | |
339 | sensor_cells[id].platform_data = &sensor_platforms[id]; | |
340 | sensor_cells[id].pdata_size = | |
341 | sizeof(struct cros_ec_sensor_platform); | |
342 | ||
343 | sensor_type[resp->info.type]++; | |
344 | id++; | |
345 | } | |
5668bfdd | 346 | |
c1d1e91a GG |
347 | if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) |
348 | ec->has_kb_wake_angle = true; | |
349 | ||
5668bfdd EBS |
350 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { |
351 | sensor_cells[id].name = "cros-ec-ring"; | |
352 | id++; | |
353 | } | |
354 | ||
355 | ret = mfd_add_devices(ec->dev, 0, sensor_cells, id, | |
356 | NULL, 0, NULL); | |
357 | if (ret) | |
358 | dev_err(ec->dev, "failed to add EC sensors\n"); | |
359 | ||
360 | kfree(sensor_platforms); | |
361 | error_platforms: | |
362 | kfree(sensor_cells); | |
363 | error: | |
364 | kfree(msg); | |
365 | } | |
366 | ||
03a5755c NA |
367 | static const struct mfd_cell cros_ec_cec_cells[] = { |
368 | { .name = "cros-ec-cec" } | |
369 | }; | |
370 | ||
95a4d07f EBS |
371 | static const struct mfd_cell cros_ec_rtc_cells[] = { |
372 | { .name = "cros-ec-rtc" } | |
373 | }; | |
374 | ||
3144dce7 | 375 | static const struct mfd_cell cros_usbpd_charger_cells[] = { |
49a65e3c EBS |
376 | { .name = "cros-usbpd-charger" }, |
377 | { .name = "cros-usbpd-logger" }, | |
3144dce7 EBS |
378 | }; |
379 | ||
ecf8a6cd | 380 | static const struct mfd_cell cros_ec_platform_cells[] = { |
6fce0a2c | 381 | { .name = "cros-ec-debugfs" }, |
ecf8a6cd | 382 | { .name = "cros-ec-lightbar" }, |
6fd7f2bb | 383 | { .name = "cros-ec-sysfs" }, |
0545625b EBS |
384 | }; |
385 | ||
386 | static const struct mfd_cell cros_ec_vbc_cells[] = { | |
387 | { .name = "cros-ec-vbc" } | |
ecf8a6cd EBS |
388 | }; |
389 | ||
e7c256fb BR |
390 | static int ec_device_probe(struct platform_device *pdev) |
391 | { | |
57b33ff0 | 392 | int retval = -ENOMEM; |
0545625b | 393 | struct device_node *node; |
57b33ff0 GG |
394 | struct device *dev = &pdev->dev; |
395 | struct cros_ec_platform *ec_platform = dev_get_platdata(dev); | |
48a2ca0e | 396 | struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL); |
57b33ff0 GG |
397 | |
398 | if (!ec) | |
399 | return retval; | |
e7c256fb | 400 | |
57b33ff0 GG |
401 | dev_set_drvdata(dev, ec); |
402 | ec->ec_dev = dev_get_drvdata(dev->parent); | |
403 | ec->dev = dev; | |
404 | ec->cmd_offset = ec_platform->cmd_offset; | |
e4244ebd VP |
405 | ec->features[0] = -1U; /* Not cached yet */ |
406 | ec->features[1] = -1U; /* Not cached yet */ | |
57b33ff0 | 407 | device_initialize(&ec->class_dev); |
e7c256fb BR |
408 | cdev_init(&ec->cdev, &fops); |
409 | ||
90486af5 EBS |
410 | /* Check whether this is actually a Fingerprint MCU rather than an EC */ |
411 | if (cros_ec_check_features(ec, EC_FEATURE_FINGERPRINT)) { | |
412 | dev_info(dev, "CrOS Fingerprint MCU detected.\n"); | |
413 | /* | |
414 | * Help userspace differentiating ECs from FP MCU, | |
415 | * regardless of the probing order. | |
416 | */ | |
417 | ec_platform->ec_name = CROS_EC_DEV_FP_NAME; | |
418 | } | |
419 | ||
d4cee950 RK |
420 | /* |
421 | * Check whether this is actually an Integrated Sensor Hub (ISH) | |
422 | * rather than an EC. | |
423 | */ | |
424 | if (cros_ec_check_features(ec, EC_FEATURE_ISH)) { | |
425 | dev_info(dev, "CrOS ISH MCU detected.\n"); | |
426 | /* | |
427 | * Help userspace differentiating ECs from ISH MCU, | |
428 | * regardless of the probing order. | |
429 | */ | |
430 | ec_platform->ec_name = CROS_EC_DEV_ISH_NAME; | |
431 | } | |
432 | ||
4f8f2bb7 EBS |
433 | /* Check whether this is actually a Touchpad MCU rather than an EC */ |
434 | if (cros_ec_check_features(ec, EC_FEATURE_TOUCHPAD)) { | |
435 | dev_info(dev, "CrOS Touchpad MCU detected.\n"); | |
436 | /* | |
437 | * Help userspace differentiating ECs from TP MCU, | |
438 | * regardless of the probing order. | |
439 | */ | |
440 | ec_platform->ec_name = CROS_EC_DEV_TP_NAME; | |
441 | } | |
442 | ||
57b33ff0 GG |
443 | /* |
444 | * Add the class device | |
445 | * Link to the character device for creating the /dev entry | |
446 | * in devtmpfs. | |
447 | */ | |
1c1d152c | 448 | ec->class_dev.devt = MKDEV(ec_major, pdev->id); |
57b33ff0 GG |
449 | ec->class_dev.class = &cros_class; |
450 | ec->class_dev.parent = dev; | |
48a2ca0e | 451 | ec->class_dev.release = cros_ec_class_release; |
57b33ff0 GG |
452 | |
453 | retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name); | |
454 | if (retval) { | |
455 | dev_err(dev, "dev_set_name failed => %d\n", retval); | |
1c1d152c | 456 | goto failed; |
e7c256fb BR |
457 | } |
458 | ||
c1d1e91a GG |
459 | /* check whether this EC is a sensor hub. */ |
460 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) | |
461 | cros_ec_sensors_register(ec); | |
462 | ||
03a5755c NA |
463 | /* Check whether this EC instance has CEC host command support */ |
464 | if (cros_ec_check_features(ec, EC_FEATURE_CEC)) { | |
465 | retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, | |
466 | cros_ec_cec_cells, | |
467 | ARRAY_SIZE(cros_ec_cec_cells), | |
468 | NULL, 0, NULL); | |
469 | if (retval) | |
470 | dev_err(ec->dev, | |
471 | "failed to add cros-ec-cec device: %d\n", | |
472 | retval); | |
473 | } | |
474 | ||
95a4d07f EBS |
475 | /* Check whether this EC instance has RTC host command support */ |
476 | if (cros_ec_check_features(ec, EC_FEATURE_RTC)) { | |
477 | retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, | |
478 | cros_ec_rtc_cells, | |
479 | ARRAY_SIZE(cros_ec_rtc_cells), | |
480 | NULL, 0, NULL); | |
481 | if (retval) | |
482 | dev_err(ec->dev, | |
483 | "failed to add cros-ec-rtc device: %d\n", | |
484 | retval); | |
485 | } | |
486 | ||
3144dce7 EBS |
487 | /* Check whether this EC instance has the PD charge manager */ |
488 | if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) { | |
489 | retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, | |
490 | cros_usbpd_charger_cells, | |
491 | ARRAY_SIZE(cros_usbpd_charger_cells), | |
492 | NULL, 0, NULL); | |
493 | if (retval) | |
494 | dev_err(ec->dev, | |
495 | "failed to add cros-usbpd-charger device: %d\n", | |
496 | retval); | |
497 | } | |
498 | ||
c1d1e91a | 499 | /* We can now add the sysfs class, we know which parameter to show */ |
1c1d152c | 500 | retval = cdev_device_add(&ec->cdev, &ec->class_dev); |
57b33ff0 | 501 | if (retval) { |
1c1d152c LG |
502 | dev_err(dev, "cdev_device_add failed => %d\n", retval); |
503 | goto failed; | |
57b33ff0 | 504 | } |
71af4b52 | 505 | |
ecf8a6cd EBS |
506 | retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, |
507 | cros_ec_platform_cells, | |
508 | ARRAY_SIZE(cros_ec_platform_cells), | |
509 | NULL, 0, NULL); | |
510 | if (retval) | |
511 | dev_warn(ec->dev, | |
512 | "failed to add cros-ec platform devices: %d\n", | |
513 | retval); | |
514 | ||
0545625b EBS |
515 | /* Check whether this EC instance has a VBC NVRAM */ |
516 | node = ec->ec_dev->dev->of_node; | |
517 | if (of_property_read_bool(node, "google,has-vbc-nvram")) { | |
518 | retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, | |
519 | cros_ec_vbc_cells, | |
520 | ARRAY_SIZE(cros_ec_vbc_cells), | |
521 | NULL, 0, NULL); | |
522 | if (retval) | |
523 | dev_warn(ec->dev, "failed to add VBC devices: %d\n", | |
524 | retval); | |
525 | } | |
526 | ||
e7c256fb | 527 | return 0; |
57b33ff0 | 528 | |
1c1d152c LG |
529 | failed: |
530 | put_device(&ec->class_dev); | |
57b33ff0 | 531 | return retval; |
e7c256fb BR |
532 | } |
533 | ||
534 | static int ec_device_remove(struct platform_device *pdev) | |
535 | { | |
57b33ff0 | 536 | struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev); |
e8626459 | 537 | |
18e294dd | 538 | mfd_remove_devices(ec->dev); |
e7c256fb | 539 | cdev_del(&ec->cdev); |
57b33ff0 | 540 | device_unregister(&ec->class_dev); |
e7c256fb BR |
541 | return 0; |
542 | } | |
543 | ||
afbf8ec7 | 544 | static const struct platform_device_id cros_ec_id[] = { |
ea01a31b | 545 | { DRV_NAME, 0 }, |
abeed71b | 546 | { /* sentinel */ } |
afbf8ec7 JMC |
547 | }; |
548 | MODULE_DEVICE_TABLE(platform, cros_ec_id); | |
549 | ||
e7c256fb BR |
550 | static struct platform_driver cros_ec_dev_driver = { |
551 | .driver = { | |
ea01a31b | 552 | .name = DRV_NAME, |
e7c256fb | 553 | }, |
6eb35784 | 554 | .id_table = cros_ec_id, |
e7c256fb BR |
555 | .probe = ec_device_probe, |
556 | .remove = ec_device_remove, | |
557 | }; | |
558 | ||
559 | static int __init cros_ec_dev_init(void) | |
560 | { | |
561 | int ret; | |
562 | dev_t dev = 0; | |
563 | ||
57b33ff0 GG |
564 | ret = class_register(&cros_class); |
565 | if (ret) { | |
e7c256fb | 566 | pr_err(CROS_EC_DEV_NAME ": failed to register device class\n"); |
57b33ff0 | 567 | return ret; |
e7c256fb BR |
568 | } |
569 | ||
570 | /* Get a range of minor numbers (starting with 0) to work with */ | |
571 | ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME); | |
572 | if (ret < 0) { | |
573 | pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n"); | |
574 | goto failed_chrdevreg; | |
575 | } | |
576 | ec_major = MAJOR(dev); | |
577 | ||
578 | /* Register the driver */ | |
579 | ret = platform_driver_register(&cros_ec_dev_driver); | |
580 | if (ret < 0) { | |
581 | pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret); | |
582 | goto failed_devreg; | |
583 | } | |
584 | return 0; | |
585 | ||
586 | failed_devreg: | |
587 | unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV); | |
588 | failed_chrdevreg: | |
57b33ff0 | 589 | class_unregister(&cros_class); |
e7c256fb BR |
590 | return ret; |
591 | } | |
592 | ||
593 | static void __exit cros_ec_dev_exit(void) | |
594 | { | |
595 | platform_driver_unregister(&cros_ec_dev_driver); | |
596 | unregister_chrdev(ec_major, CROS_EC_DEV_NAME); | |
57b33ff0 | 597 | class_unregister(&cros_class); |
e7c256fb BR |
598 | } |
599 | ||
600 | module_init(cros_ec_dev_init); | |
601 | module_exit(cros_ec_dev_exit); | |
602 | ||
ea01a31b | 603 | MODULE_ALIAS("platform:" DRV_NAME); |
e7c256fb BR |
604 | MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>"); |
605 | MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller"); | |
606 | MODULE_VERSION("1.0"); | |
607 | MODULE_LICENSE("GPL"); |