Skip to content

Commit 832a636

Browse files
Enric Balletbo i SerraLee Jones
authored andcommitted
mfd: cros_ec: Add convenience struct to define autodetectable CrOS EC subdevices
The CrOS EC is gaining lots of subdevices that are autodetectable by sending the EC_FEATURE_GET_CMD, it takes fair amount of boiler plate code to add those devices. So, add a struct that can be used to quickly add new subdevices without having to duplicate code. Signed-off-by: Enric Balletbo i Serra <[email protected]> Acked-by: Andy Shevchenko <[email protected]> Reviewed-by: Gwendal Grignou <[email protected]> Tested-by: Gwendal Grignou <[email protected]> Signed-off-by: Lee Jones <[email protected]>
1 parent b027dcf commit 832a636

File tree

1 file changed

+73
-58
lines changed

1 file changed

+73
-58
lines changed

drivers/mfd/cros_ec_dev.c

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ struct cros_feature_to_name {
3535
const char *desc;
3636
};
3737

38+
/**
39+
* cros_feature_to_cells - CrOS feature id to mfd cells association.
40+
* @id: The feature identifier.
41+
* @mfd_cells: Pointer to the array of mfd cells that needs to be added.
42+
* @num_cells: Number of mfd cells into the array.
43+
*/
44+
struct cros_feature_to_cells {
45+
unsigned int id;
46+
const struct mfd_cell *mfd_cells;
47+
unsigned int num_cells;
48+
};
49+
3850
static const struct cros_feature_to_name cros_mcu_devices[] = {
3951
{
4052
.id = EC_FEATURE_FINGERPRINT,
@@ -58,6 +70,48 @@ static const struct cros_feature_to_name cros_mcu_devices[] = {
5870
},
5971
};
6072

73+
static const struct mfd_cell cros_ec_cec_cells[] = {
74+
{ .name = "cros-ec-cec", },
75+
};
76+
77+
static const struct mfd_cell cros_ec_rtc_cells[] = {
78+
{ .name = "cros-ec-rtc", },
79+
};
80+
81+
static const struct mfd_cell cros_usbpd_charger_cells[] = {
82+
{ .name = "cros-usbpd-charger", },
83+
{ .name = "cros-usbpd-logger", },
84+
};
85+
86+
static const struct cros_feature_to_cells cros_subdevices[] = {
87+
{
88+
.id = EC_FEATURE_CEC,
89+
.mfd_cells = cros_ec_cec_cells,
90+
.num_cells = ARRAY_SIZE(cros_ec_cec_cells),
91+
},
92+
{
93+
.id = EC_FEATURE_RTC,
94+
.mfd_cells = cros_ec_rtc_cells,
95+
.num_cells = ARRAY_SIZE(cros_ec_rtc_cells),
96+
},
97+
{
98+
.id = EC_FEATURE_USB_PD,
99+
.mfd_cells = cros_usbpd_charger_cells,
100+
.num_cells = ARRAY_SIZE(cros_usbpd_charger_cells),
101+
},
102+
};
103+
104+
static const struct mfd_cell cros_ec_platform_cells[] = {
105+
{ .name = "cros-ec-chardev", },
106+
{ .name = "cros-ec-debugfs", },
107+
{ .name = "cros-ec-lightbar", },
108+
{ .name = "cros-ec-sysfs", },
109+
};
110+
111+
static const struct mfd_cell cros_ec_vbc_cells[] = {
112+
{ .name = "cros-ec-vbc", }
113+
};
114+
61115
static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
62116
{
63117
struct cros_ec_command *msg;
@@ -283,30 +337,6 @@ static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
283337
dev_err(ec_dev->dev, "failed to add EC sensors\n");
284338
}
285339

286-
static const struct mfd_cell cros_ec_cec_cells[] = {
287-
{ .name = "cros-ec-cec" }
288-
};
289-
290-
static const struct mfd_cell cros_ec_rtc_cells[] = {
291-
{ .name = "cros-ec-rtc" }
292-
};
293-
294-
static const struct mfd_cell cros_usbpd_charger_cells[] = {
295-
{ .name = "cros-usbpd-charger" },
296-
{ .name = "cros-usbpd-logger" },
297-
};
298-
299-
static const struct mfd_cell cros_ec_platform_cells[] = {
300-
{ .name = "cros-ec-chardev" },
301-
{ .name = "cros-ec-debugfs" },
302-
{ .name = "cros-ec-lightbar" },
303-
{ .name = "cros-ec-sysfs" },
304-
};
305-
306-
static const struct mfd_cell cros_ec_vbc_cells[] = {
307-
{ .name = "cros-ec-vbc" }
308-
};
309-
310340
static int ec_device_probe(struct platform_device *pdev)
311341
{
312342
int retval = -ENOMEM;
@@ -368,42 +398,27 @@ static int ec_device_probe(struct platform_device *pdev)
368398
/* Workaroud for older EC firmware */
369399
cros_ec_accel_legacy_register(ec);
370400

371-
/* Check whether this EC instance has CEC host command support */
372-
if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
373-
retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
374-
cros_ec_cec_cells,
375-
ARRAY_SIZE(cros_ec_cec_cells),
376-
NULL, 0, NULL);
377-
if (retval)
378-
dev_err(ec->dev,
379-
"failed to add cros-ec-cec device: %d\n",
380-
retval);
381-
}
382-
383-
/* Check whether this EC instance has RTC host command support */
384-
if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
385-
retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
386-
cros_ec_rtc_cells,
387-
ARRAY_SIZE(cros_ec_rtc_cells),
388-
NULL, 0, NULL);
389-
if (retval)
390-
dev_err(ec->dev,
391-
"failed to add cros-ec-rtc device: %d\n",
392-
retval);
393-
}
394-
395-
/* Check whether this EC instance has the PD charge manager */
396-
if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
397-
retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
398-
cros_usbpd_charger_cells,
399-
ARRAY_SIZE(cros_usbpd_charger_cells),
400-
NULL, 0, NULL);
401-
if (retval)
402-
dev_err(ec->dev,
403-
"failed to add cros-usbpd-charger device: %d\n",
404-
retval);
401+
/*
402+
* The following subdevices can be detected by sending the
403+
* EC_FEATURE_GET_CMD Embedded Controller device.
404+
*/
405+
for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) {
406+
if (cros_ec_check_features(ec, cros_subdevices[i].id)) {
407+
retval = mfd_add_hotplug_devices(ec->dev,
408+
cros_subdevices[i].mfd_cells,
409+
cros_subdevices[i].num_cells);
410+
if (retval)
411+
dev_err(ec->dev,
412+
"failed to add %s subdevice: %d\n",
413+
cros_subdevices[i].mfd_cells->name,
414+
retval);
415+
}
405416
}
406417

418+
/*
419+
* The following subdevices cannot be detected by sending the
420+
* EC_FEATURE_GET_CMD to the Embedded Controller device.
421+
*/
407422
retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
408423
cros_ec_platform_cells,
409424
ARRAY_SIZE(cros_ec_platform_cells),

0 commit comments

Comments
 (0)