Skip to content

Commit 7aa703b

Browse files
Enric Balletbo i SerraLee Jones
authored andcommitted
mfd / platform: cros_ec: Handle chained ECs as platform devices
An MFD is a device that contains several sub-devices (cells). For instance, the ChromeOS EC fits in this description as usually contains a charger and can have other devices with different functions like a Real-Time Clock, an Audio codec, a Real-Time Clock, ... If you look at the driver, though, we're doing something odd. We have two MFD cros-ec drivers where one of them (cros-ec-core) instantiates another MFD driver as sub-driver (cros-ec-dev), and the latest instantiates the different sub-devices (Real-Time Clock, Audio codec, etc). MFD ------------------------------------------ cros-ec-core |___ mfd-cellA (cros-ec-dev) | |__ mfd-cell0 | |__ mfd-cell1 | |__ ... | |___ mfd-cellB (cros-ec-dev) |__ mfd-cell0 |__ mfd-cell1 |__ ... The problem that was trying to solve is to describe some kind of topology for the case where we have an EC (cros-ec) chained with another EC (cros-pd). Apart from that this extends the bounds of what MFD was designed to do we might be interested on have other kinds of topology that can't be implemented in that way. Let's prepare the code to move the cros-ec-core part from MFD to platform/chrome as this is clearly a platform specific thing non-related to a MFD device. platform/chrome | MFD ------------------------------------------ | cros-ec ________|___ cros-ec-dev | |__ mfd-cell0 | |__ mfd-cell1 | |__ ... | cros-pd ________|___ cros-ec-dev | |__ mfd-cell0 | |__ mfd-cell1 | |__ ... Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com> Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reviewed-by: Gwendal Grignou <gwendal@chromium.org> Tested-by: Gwendal Grignou <gwendal@chromium.org> Signed-off-by: Lee Jones <lee.jones@linaro.org>
1 parent 5f9e832 commit 7aa703b

6 files changed

Lines changed: 69 additions & 31 deletions

File tree

‎drivers/mfd/cros_ec.c‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <linux/interrupt.h>
1414
#include <linux/slab.h>
1515
#include <linux/module.h>
16-
#include <linux/mfd/core.h>
1716
#include <linux/mfd/cros_ec.h>
1817
#include <linux/suspend.h>
1918
#include <asm/unaligned.h>
@@ -31,18 +30,6 @@ static struct cros_ec_platform pd_p = {
3130
.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
3231
};
3332

34-
static const struct mfd_cell ec_cell = {
35-
.name = "cros-ec-dev",
36-
.platform_data = &ec_p,
37-
.pdata_size = sizeof(ec_p),
38-
};
39-
40-
static const struct mfd_cell ec_pd_cell = {
41-
.name = "cros-ec-dev",
42-
.platform_data = &pd_p,
43-
.pdata_size = sizeof(pd_p),
44-
};
45-
4633
static irqreturn_t ec_irq_thread(int irq, void *data)
4734
{
4835
struct cros_ec_device *ec_dev = data;
@@ -154,38 +141,42 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
154141
}
155142
}
156143

157-
err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell,
158-
1, NULL, ec_dev->irq, NULL);
159-
if (err) {
160-
dev_err(dev,
161-
"Failed to register Embedded Controller subdevice %d\n",
162-
err);
163-
return err;
144+
/* Register a platform device for the main EC instance */
145+
ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
146+
PLATFORM_DEVID_AUTO, &ec_p,
147+
sizeof(struct cros_ec_platform));
148+
if (IS_ERR(ec_dev->ec)) {
149+
dev_err(ec_dev->dev,
150+
"Failed to create CrOS EC platform device\n");
151+
return PTR_ERR(ec_dev->ec);
164152
}
165153

166154
if (ec_dev->max_passthru) {
167155
/*
168-
* Register a PD device as well on top of this device.
156+
* Register a platform device for the PD behind the main EC.
169157
* We make the following assumptions:
170158
* - behind an EC, we have a pd
171159
* - only one device added.
172160
* - the EC is responsive at init time (it is not true for a
173-
* sensor hub.
161+
* sensor hub).
174162
*/
175-
err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
176-
&ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
177-
if (err) {
178-
dev_err(dev,
179-
"Failed to register Power Delivery subdevice %d\n",
180-
err);
181-
return err;
163+
ec_dev->pd = platform_device_register_data(ec_dev->dev,
164+
"cros-ec-dev",
165+
PLATFORM_DEVID_AUTO, &pd_p,
166+
sizeof(struct cros_ec_platform));
167+
if (IS_ERR(ec_dev->pd)) {
168+
dev_err(ec_dev->dev,
169+
"Failed to create CrOS PD platform device\n");
170+
platform_device_unregister(ec_dev->ec);
171+
return PTR_ERR(ec_dev->pd);
182172
}
183173
}
184174

185175
if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
186176
err = devm_of_platform_populate(dev);
187177
if (err) {
188-
mfd_remove_devices(dev);
178+
platform_device_unregister(ec_dev->pd);
179+
platform_device_unregister(ec_dev->ec);
189180
dev_err(dev, "Failed to register sub-devices\n");
190181
return err;
191182
}
@@ -206,6 +197,16 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
206197
}
207198
EXPORT_SYMBOL(cros_ec_register);
208199

200+
int cros_ec_unregister(struct cros_ec_device *ec_dev)
201+
{
202+
if (ec_dev->pd)
203+
platform_device_unregister(ec_dev->pd);
204+
platform_device_unregister(ec_dev->ec);
205+
206+
return 0;
207+
}
208+
EXPORT_SYMBOL(cros_ec_unregister);
209+
209210
#ifdef CONFIG_PM_SLEEP
210211
int cros_ec_suspend(struct cros_ec_device *ec_dev)
211212
{

‎drivers/platform/chrome/cros_ec_i2c.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ static int cros_ec_i2c_probe(struct i2c_client *client,
307307
return 0;
308308
}
309309

310+
static int cros_ec_i2c_remove(struct i2c_client *client)
311+
{
312+
struct cros_ec_device *ec_dev = i2c_get_clientdata(client);
313+
314+
return cros_ec_unregister(ec_dev);
315+
}
316+
310317
#ifdef CONFIG_PM_SLEEP
311318
static int cros_ec_i2c_suspend(struct device *dev)
312319
{
@@ -357,6 +364,7 @@ static struct i2c_driver cros_ec_driver = {
357364
.pm = &cros_ec_i2c_pm_ops,
358365
},
359366
.probe = cros_ec_i2c_probe,
367+
.remove = cros_ec_i2c_remove,
360368
.id_table = cros_ec_i2c_id,
361369
};
362370

‎drivers/platform/chrome/cros_ec_lpc.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,15 @@ static int cros_ec_lpc_probe(struct platform_device *pdev)
421421

422422
static int cros_ec_lpc_remove(struct platform_device *pdev)
423423
{
424+
struct cros_ec_device *ec_dev = platform_get_drvdata(pdev);
424425
struct acpi_device *adev;
425426

426427
adev = ACPI_COMPANION(&pdev->dev);
427428
if (adev)
428429
acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
429430
cros_ec_lpc_acpi_notify);
430431

431-
return 0;
432+
return cros_ec_unregister(ec_dev);
432433
}
433434

434435
static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = {

‎drivers/platform/chrome/cros_ec_rpmsg.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev)
233233
struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
234234
struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
235235

236+
cros_ec_unregister(ec_dev);
237+
236238
cancel_work_sync(&ec_rpmsg->host_event_work);
237239
}
238240

‎drivers/platform/chrome/cros_ec_spi.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,13 @@ static int cros_ec_spi_probe(struct spi_device *spi)
785785
return 0;
786786
}
787787

788+
static int cros_ec_spi_remove(struct spi_device *spi)
789+
{
790+
struct cros_ec_device *ec_dev = spi_get_drvdata(spi);
791+
792+
return cros_ec_unregister(ec_dev);
793+
}
794+
788795
#ifdef CONFIG_PM_SLEEP
789796
static int cros_ec_spi_suspend(struct device *dev)
790797
{
@@ -823,6 +830,7 @@ static struct spi_driver cros_ec_driver_spi = {
823830
.pm = &cros_ec_spi_pm_ops,
824831
},
825832
.probe = cros_ec_spi_probe,
833+
.remove = cros_ec_spi_remove,
826834
.id_table = cros_ec_spi_id,
827835
};
828836

‎include/linux/mfd/cros_ec.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ struct cros_ec_command {
121121
* @event_data: Raw payload transferred with the MKBP event.
122122
* @event_size: Size in bytes of the event data.
123123
* @host_event_wake_mask: Mask of host events that cause wake from suspend.
124+
* @ec: The platform_device used by the mfd driver to interface with the
125+
* main EC.
126+
* @pd: The platform_device used by the mfd driver to interface with the
127+
* PD behind an EC.
124128
*/
125129
struct cros_ec_device {
126130
/* These are used by other drivers that want to talk to the EC */
@@ -157,6 +161,10 @@ struct cros_ec_device {
157161
int event_size;
158162
u32 host_event_wake_mask;
159163
u32 last_resume_result;
164+
165+
/* The platform devices used by the mfd driver */
166+
struct platform_device *ec;
167+
struct platform_device *pd;
160168
};
161169

162170
/**
@@ -291,6 +299,16 @@ int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
291299
*/
292300
int cros_ec_register(struct cros_ec_device *ec_dev);
293301

302+
/**
303+
* cros_ec_unregister() - Remove a ChromeOS EC.
304+
* @ec_dev: Device to unregister.
305+
*
306+
* Call this to deregister a ChromeOS EC, then clean up any private data.
307+
*
308+
* Return: 0 on success or negative error code.
309+
*/
310+
int cros_ec_unregister(struct cros_ec_device *ec_dev);
311+
294312
/**
295313
* cros_ec_query_all() - Query the protocol version supported by the
296314
* ChromeOS EC.

0 commit comments

Comments
 (0)