Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ChromeOS EC multi-function device |
| 3 | * |
| 4 | * Copyright (C) 2012 Google, Inc |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * The ChromeOS EC multi function device is used to mux all the requests |
| 16 | * to the EC device for its multiple features: keyboard controller, |
| 17 | * battery charging and regulator control, firmware update. |
| 18 | */ |
| 19 | |
Todd Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 20 | #include <linux/of_platform.h> |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/slab.h> |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 23 | #include <linux/module.h> |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 24 | #include <linux/mfd/core.h> |
| 25 | #include <linux/mfd/cros_ec.h> |
Andrew Bresticker | a6551a7 | 2014-09-18 17:18:56 +0200 | [diff] [blame] | 26 | |
Geert Uytterhoeven | 5ac9855 | 2013-11-18 14:33:06 +0100 | [diff] [blame] | 27 | static const struct mfd_cell cros_devs[] = { |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 28 | { |
Javier Martinez Canillas | 8a4be85 | 2015-02-02 12:26:26 +0100 | [diff] [blame] | 29 | .name = "cros-ec-ctl", |
Todd Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 30 | .id = PLATFORM_DEVID_AUTO, |
Javier Martinez Canillas | 8a4be85 | 2015-02-02 12:26:26 +0100 | [diff] [blame] | 31 | }, |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | int cros_ec_register(struct cros_ec_device *ec_dev) |
| 35 | { |
| 36 | struct device *dev = ec_dev->dev; |
| 37 | int err = 0; |
| 38 | |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame^] | 39 | ec_dev->max_request = sizeof(struct ec_params_hello); |
| 40 | ec_dev->max_response = sizeof(struct ec_response_get_protocol_info); |
| 41 | ec_dev->max_passthru = 0; |
| 42 | |
| 43 | ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL); |
| 44 | if (!ec_dev->din) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL); |
| 48 | if (!ec_dev->dout) |
| 49 | return -ENOMEM; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 50 | |
Andrew Bresticker | 6342753 | 2014-09-18 17:18:57 +0200 | [diff] [blame] | 51 | mutex_init(&ec_dev->lock); |
| 52 | |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame^] | 53 | cros_ec_query_all(ec_dev); |
| 54 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 55 | err = mfd_add_devices(dev, 0, cros_devs, |
| 56 | ARRAY_SIZE(cros_devs), |
| 57 | NULL, ec_dev->irq, NULL); |
| 58 | if (err) { |
| 59 | dev_err(dev, "failed to add mfd devices\n"); |
Andrew Bresticker | d1fd345 | 2014-06-18 11:14:07 -0700 | [diff] [blame] | 60 | return err; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Todd Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 63 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 64 | err = of_platform_populate(dev->of_node, NULL, NULL, dev); |
| 65 | if (err) { |
| 66 | mfd_remove_devices(dev); |
| 67 | dev_err(dev, "Failed to register sub-devices\n"); |
| 68 | return err; |
| 69 | } |
| 70 | } |
| 71 | |
Bill Richardson | 533cec8 | 2014-06-18 11:14:03 -0700 | [diff] [blame] | 72 | dev_info(dev, "Chrome EC device registered\n"); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 73 | |
| 74 | return 0; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 75 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 76 | EXPORT_SYMBOL(cros_ec_register); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 77 | |
| 78 | int cros_ec_remove(struct cros_ec_device *ec_dev) |
| 79 | { |
| 80 | mfd_remove_devices(ec_dev->dev); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 81 | |
| 82 | return 0; |
| 83 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 84 | EXPORT_SYMBOL(cros_ec_remove); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 85 | |
| 86 | #ifdef CONFIG_PM_SLEEP |
| 87 | int cros_ec_suspend(struct cros_ec_device *ec_dev) |
| 88 | { |
| 89 | struct device *dev = ec_dev->dev; |
| 90 | |
| 91 | if (device_may_wakeup(dev)) |
| 92 | ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq); |
| 93 | |
| 94 | disable_irq(ec_dev->irq); |
| 95 | ec_dev->was_wake_device = ec_dev->wake_enabled; |
| 96 | |
| 97 | return 0; |
| 98 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 99 | EXPORT_SYMBOL(cros_ec_suspend); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 100 | |
| 101 | int cros_ec_resume(struct cros_ec_device *ec_dev) |
| 102 | { |
| 103 | enable_irq(ec_dev->irq); |
| 104 | |
| 105 | if (ec_dev->wake_enabled) { |
| 106 | disable_irq_wake(ec_dev->irq); |
| 107 | ec_dev->wake_enabled = 0; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 112 | EXPORT_SYMBOL(cros_ec_resume); |
| 113 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 114 | #endif |
Bill Richardson | a865a58 | 2014-04-17 12:32:17 -0700 | [diff] [blame] | 115 | |
| 116 | MODULE_LICENSE("GPL"); |
| 117 | MODULE_DESCRIPTION("ChromeOS EC core driver"); |