Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
[linux-2.6-microblaze.git] / drivers / mfd / cros_ec_dev.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space
4  *
5  * Copyright (C) 2014 Google, Inc.
6  */
7
8 #include <linux/dmi.h>
9 #include <linux/kconfig.h>
10 #include <linux/mfd/core.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/platform_data/cros_ec_chardev.h>
16 #include <linux/platform_data/cros_ec_commands.h>
17 #include <linux/platform_data/cros_ec_proto.h>
18 #include <linux/slab.h>
19
20 #define DRV_NAME "cros-ec-dev"
21
22 static struct class cros_class = {
23         .name           = "chromeos",
24 };
25
26 /**
27  * struct cros_feature_to_name - CrOS feature id to name/short description.
28  * @id: The feature identifier.
29  * @name: Device name associated with the feature id.
30  * @desc: Short name that will be displayed.
31  */
32 struct cros_feature_to_name {
33         unsigned int id;
34         const char *name;
35         const char *desc;
36 };
37
38 /**
39  * struct 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
50 static const struct cros_feature_to_name cros_mcu_devices[] = {
51         {
52                 .id     = EC_FEATURE_FINGERPRINT,
53                 .name   = CROS_EC_DEV_FP_NAME,
54                 .desc   = "Fingerprint",
55         },
56         {
57                 .id     = EC_FEATURE_ISH,
58                 .name   = CROS_EC_DEV_ISH_NAME,
59                 .desc   = "Integrated Sensor Hub",
60         },
61         {
62                 .id     = EC_FEATURE_SCP,
63                 .name   = CROS_EC_DEV_SCP_NAME,
64                 .desc   = "System Control Processor",
65         },
66         {
67                 .id     = EC_FEATURE_TOUCHPAD,
68                 .name   = CROS_EC_DEV_TP_NAME,
69                 .desc   = "Touchpad",
70         },
71 };
72
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_gpio_cells[] = {
78         { .name = "cros-ec-gpio", },
79 };
80
81 static const struct mfd_cell cros_ec_rtc_cells[] = {
82         { .name = "cros-ec-rtc", },
83 };
84
85 static const struct mfd_cell cros_ec_sensorhub_cells[] = {
86         { .name = "cros-ec-sensorhub", },
87 };
88
89 static const struct mfd_cell cros_usbpd_charger_cells[] = {
90         { .name = "cros-usbpd-charger", },
91         { .name = "cros-usbpd-logger", },
92 };
93
94 static const struct mfd_cell cros_usbpd_notify_cells[] = {
95         { .name = "cros-usbpd-notify", },
96 };
97
98 static const struct mfd_cell cros_ec_wdt_cells[] = {
99         { .name = "cros-ec-wdt", }
100 };
101
102 static const struct cros_feature_to_cells cros_subdevices[] = {
103         {
104                 .id             = EC_FEATURE_CEC,
105                 .mfd_cells      = cros_ec_cec_cells,
106                 .num_cells      = ARRAY_SIZE(cros_ec_cec_cells),
107         },
108         {
109                 .id             = EC_FEATURE_GPIO,
110                 .mfd_cells      = cros_ec_gpio_cells,
111                 .num_cells      = ARRAY_SIZE(cros_ec_gpio_cells),
112         },
113         {
114                 .id             = EC_FEATURE_RTC,
115                 .mfd_cells      = cros_ec_rtc_cells,
116                 .num_cells      = ARRAY_SIZE(cros_ec_rtc_cells),
117         },
118         {
119                 .id             = EC_FEATURE_USB_PD,
120                 .mfd_cells      = cros_usbpd_charger_cells,
121                 .num_cells      = ARRAY_SIZE(cros_usbpd_charger_cells),
122         },
123         {
124                 .id             = EC_FEATURE_HANG_DETECT,
125                 .mfd_cells      = cros_ec_wdt_cells,
126                 .num_cells      = ARRAY_SIZE(cros_ec_wdt_cells),
127         },
128 };
129
130 static const struct mfd_cell cros_ec_platform_cells[] = {
131         { .name = "cros-ec-chardev", },
132         { .name = "cros-ec-debugfs", },
133         { .name = "cros-ec-sysfs", },
134 };
135
136 static const struct mfd_cell cros_ec_pchg_cells[] = {
137         { .name = "cros-ec-pchg", },
138 };
139
140 static const struct mfd_cell cros_ec_lightbar_cells[] = {
141         { .name = "cros-ec-lightbar", }
142 };
143
144 static const struct mfd_cell cros_ec_vbc_cells[] = {
145         { .name = "cros-ec-vbc", }
146 };
147
148 static void cros_ec_class_release(struct device *dev)
149 {
150         kfree(to_cros_ec_dev(dev));
151 }
152
153 static int ec_device_probe(struct platform_device *pdev)
154 {
155         int retval = -ENOMEM;
156         struct device_node *node;
157         struct device *dev = &pdev->dev;
158         struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
159         struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
160         struct ec_response_pchg_count pchg_count;
161         int i;
162
163         if (!ec)
164                 return retval;
165
166         dev_set_drvdata(dev, ec);
167         ec->ec_dev = dev_get_drvdata(dev->parent);
168         ec->dev = dev;
169         ec->cmd_offset = ec_platform->cmd_offset;
170         ec->features.flags[0] = -1U; /* Not cached yet */
171         ec->features.flags[1] = -1U; /* Not cached yet */
172         device_initialize(&ec->class_dev);
173
174         for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) {
175                 /*
176                  * Check whether this is actually a dedicated MCU rather
177                  * than an standard EC.
178                  */
179                 if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) {
180                         dev_info(dev, "CrOS %s MCU detected\n",
181                                  cros_mcu_devices[i].desc);
182                         /*
183                          * Help userspace differentiating ECs from other MCU,
184                          * regardless of the probing order.
185                          */
186                         ec_platform->ec_name = cros_mcu_devices[i].name;
187                         break;
188                 }
189         }
190
191         /*
192          * Add the class device
193          */
194         ec->class_dev.class = &cros_class;
195         ec->class_dev.parent = dev;
196         ec->class_dev.release = cros_ec_class_release;
197
198         retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
199         if (retval) {
200                 dev_err(dev, "dev_set_name failed => %d\n", retval);
201                 goto failed;
202         }
203
204         retval = device_add(&ec->class_dev);
205         if (retval)
206                 goto failed;
207
208         /* check whether this EC is a sensor hub. */
209         if (cros_ec_get_sensor_count(ec) > 0) {
210                 retval = mfd_add_hotplug_devices(ec->dev,
211                                 cros_ec_sensorhub_cells,
212                                 ARRAY_SIZE(cros_ec_sensorhub_cells));
213                 if (retval)
214                         dev_err(ec->dev, "failed to add %s subdevice: %d\n",
215                                 cros_ec_sensorhub_cells->name, retval);
216         }
217
218         /*
219          * The following subdevices can be detected by sending the
220          * EC_FEATURE_GET_CMD Embedded Controller device.
221          */
222         for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) {
223                 if (cros_ec_check_features(ec, cros_subdevices[i].id)) {
224                         retval = mfd_add_hotplug_devices(ec->dev,
225                                                 cros_subdevices[i].mfd_cells,
226                                                 cros_subdevices[i].num_cells);
227                         if (retval)
228                                 dev_err(ec->dev,
229                                         "failed to add %s subdevice: %d\n",
230                                         cros_subdevices[i].mfd_cells->name,
231                                         retval);
232                 }
233         }
234
235         /*
236          * Lightbar is a special case. Newer devices support autodetection,
237          * but older ones do not.
238          */
239         if (cros_ec_check_features(ec, EC_FEATURE_LIGHTBAR) ||
240             dmi_match(DMI_PRODUCT_NAME, "Link")) {
241                 retval = mfd_add_hotplug_devices(ec->dev,
242                                         cros_ec_lightbar_cells,
243                                         ARRAY_SIZE(cros_ec_lightbar_cells));
244                 if (retval)
245                         dev_warn(ec->dev, "failed to add lightbar: %d\n",
246                                  retval);
247         }
248
249         /*
250          * The PD notifier driver cell is separate since it only needs to be
251          * explicitly added on platforms that don't have the PD notifier ACPI
252          * device entry defined.
253          */
254         if (IS_ENABLED(CONFIG_OF) && ec->ec_dev->dev->of_node) {
255                 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
256                         retval = mfd_add_hotplug_devices(ec->dev,
257                                         cros_usbpd_notify_cells,
258                                         ARRAY_SIZE(cros_usbpd_notify_cells));
259                         if (retval)
260                                 dev_err(ec->dev,
261                                         "failed to add PD notify devices: %d\n",
262                                         retval);
263                 }
264         }
265
266         /*
267          * The PCHG device cannot be detected by sending EC_FEATURE_GET_CMD, but
268          * it can be detected by querying the number of peripheral chargers.
269          */
270         retval = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_PCHG_COUNT, NULL, 0,
271                              &pchg_count, sizeof(pchg_count));
272         if (retval >= 0 && pchg_count.port_count) {
273                 retval = mfd_add_hotplug_devices(ec->dev,
274                                         cros_ec_pchg_cells,
275                                         ARRAY_SIZE(cros_ec_pchg_cells));
276                 if (retval)
277                         dev_warn(ec->dev, "failed to add pchg: %d\n",
278                                  retval);
279         }
280
281         /*
282          * The following subdevices cannot be detected by sending the
283          * EC_FEATURE_GET_CMD to the Embedded Controller device.
284          */
285         retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells,
286                                          ARRAY_SIZE(cros_ec_platform_cells));
287         if (retval)
288                 dev_warn(ec->dev,
289                          "failed to add cros-ec platform devices: %d\n",
290                          retval);
291
292         /* Check whether this EC instance has a VBC NVRAM */
293         node = ec->ec_dev->dev->of_node;
294         if (of_property_read_bool(node, "google,has-vbc-nvram")) {
295                 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells,
296                                                 ARRAY_SIZE(cros_ec_vbc_cells));
297                 if (retval)
298                         dev_warn(ec->dev, "failed to add VBC devices: %d\n",
299                                  retval);
300         }
301
302         return 0;
303
304 failed:
305         put_device(&ec->class_dev);
306         return retval;
307 }
308
309 static void ec_device_remove(struct platform_device *pdev)
310 {
311         struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
312
313         mfd_remove_devices(ec->dev);
314         device_unregister(&ec->class_dev);
315 }
316
317 static const struct platform_device_id cros_ec_id[] = {
318         { DRV_NAME, 0 },
319         { /* sentinel */ }
320 };
321 MODULE_DEVICE_TABLE(platform, cros_ec_id);
322
323 static struct platform_driver cros_ec_dev_driver = {
324         .driver = {
325                 .name = DRV_NAME,
326         },
327         .id_table = cros_ec_id,
328         .probe = ec_device_probe,
329         .remove_new = ec_device_remove,
330 };
331
332 static int __init cros_ec_dev_init(void)
333 {
334         int ret;
335
336         ret  = class_register(&cros_class);
337         if (ret) {
338                 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
339                 return ret;
340         }
341
342         /* Register the driver */
343         ret = platform_driver_register(&cros_ec_dev_driver);
344         if (ret < 0) {
345                 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
346                 goto failed_devreg;
347         }
348         return 0;
349
350 failed_devreg:
351         class_unregister(&cros_class);
352         return ret;
353 }
354
355 static void __exit cros_ec_dev_exit(void)
356 {
357         platform_driver_unregister(&cros_ec_dev_driver);
358         class_unregister(&cros_class);
359 }
360
361 module_init(cros_ec_dev_init);
362 module_exit(cros_ec_dev_exit);
363
364 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
365 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
366 MODULE_VERSION("1.0");
367 MODULE_LICENSE("GPL");