• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <linux/of_platform.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/mfd/core.h>
25 #include <linux/mfd/cros_ec.h>
26 
27 #define CROS_EC_DEV_EC_INDEX 0
28 #define CROS_EC_DEV_PD_INDEX 1
29 
30 static struct cros_ec_platform ec_p = {
31 	.ec_name = CROS_EC_DEV_NAME,
32 	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
33 };
34 
35 static struct cros_ec_platform pd_p = {
36 	.ec_name = CROS_EC_DEV_PD_NAME,
37 	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
38 };
39 
40 static const struct mfd_cell ec_cell = {
41 	.name = "cros-ec-ctl",
42 	.platform_data = &ec_p,
43 	.pdata_size = sizeof(ec_p),
44 };
45 
46 static const struct mfd_cell ec_pd_cell = {
47 	.name = "cros-ec-ctl",
48 	.platform_data = &pd_p,
49 	.pdata_size = sizeof(pd_p),
50 };
51 
cros_ec_register(struct cros_ec_device * ec_dev)52 int cros_ec_register(struct cros_ec_device *ec_dev)
53 {
54 	struct device *dev = ec_dev->dev;
55 	int err = 0;
56 
57 	ec_dev->max_request = sizeof(struct ec_params_hello);
58 	ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
59 	ec_dev->max_passthru = 0;
60 
61 	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
62 	if (!ec_dev->din)
63 		return -ENOMEM;
64 
65 	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
66 	if (!ec_dev->dout)
67 		return -ENOMEM;
68 
69 	mutex_init(&ec_dev->lock);
70 
71 	err = cros_ec_query_all(ec_dev);
72 	if (err) {
73 		dev_err(dev, "Cannot identify the EC: error %d\n", err);
74 		return err;
75 	}
76 
77 	err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1,
78 			      NULL, ec_dev->irq, NULL);
79 	if (err) {
80 		dev_err(dev,
81 			"Failed to register Embedded Controller subdevice %d\n",
82 			err);
83 		return err;
84 	}
85 
86 	if (ec_dev->max_passthru) {
87 		/*
88 		 * Register a PD device as well on top of this device.
89 		 * We make the following assumptions:
90 		 * - behind an EC, we have a pd
91 		 * - only one device added.
92 		 * - the EC is responsive at init time (it is not true for a
93 		 *   sensor hub.
94 		 */
95 		err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
96 				      &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
97 		if (err) {
98 			dev_err(dev,
99 				"Failed to register Power Delivery subdevice %d\n",
100 				err);
101 			return err;
102 		}
103 	}
104 
105 	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
106 		err = of_platform_populate(dev->of_node, NULL, NULL, dev);
107 		if (err) {
108 			mfd_remove_devices(dev);
109 			dev_err(dev, "Failed to register sub-devices\n");
110 			return err;
111 		}
112 	}
113 
114 	dev_info(dev, "Chrome EC device registered\n");
115 
116 	return 0;
117 }
118 EXPORT_SYMBOL(cros_ec_register);
119 
cros_ec_remove(struct cros_ec_device * ec_dev)120 int cros_ec_remove(struct cros_ec_device *ec_dev)
121 {
122 	mfd_remove_devices(ec_dev->dev);
123 
124 	return 0;
125 }
126 EXPORT_SYMBOL(cros_ec_remove);
127 
128 #ifdef CONFIG_PM_SLEEP
cros_ec_suspend(struct cros_ec_device * ec_dev)129 int cros_ec_suspend(struct cros_ec_device *ec_dev)
130 {
131 	struct device *dev = ec_dev->dev;
132 
133 	if (device_may_wakeup(dev))
134 		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
135 
136 	disable_irq(ec_dev->irq);
137 	ec_dev->was_wake_device = ec_dev->wake_enabled;
138 
139 	return 0;
140 }
141 EXPORT_SYMBOL(cros_ec_suspend);
142 
cros_ec_resume(struct cros_ec_device * ec_dev)143 int cros_ec_resume(struct cros_ec_device *ec_dev)
144 {
145 	enable_irq(ec_dev->irq);
146 
147 	if (ec_dev->wake_enabled) {
148 		disable_irq_wake(ec_dev->irq);
149 		ec_dev->wake_enabled = 0;
150 	}
151 
152 	return 0;
153 }
154 EXPORT_SYMBOL(cros_ec_resume);
155 
156 #endif
157 
158 MODULE_LICENSE("GPL");
159 MODULE_DESCRIPTION("ChromeOS EC core driver");
160