091d428f553171e01d7f1b62a75c549259d95768
[platform/kernel/linux-rpi.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/mfd/core.h>
9 #include <linux/mfd/cros_ec.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/platform_data/cros_ec_chardev.h>
15 #include <linux/platform_data/cros_ec_commands.h>
16 #include <linux/platform_data/cros_ec_proto.h>
17 #include <linux/slab.h>
18
19 #define DRV_NAME "cros-ec-dev"
20
21 static struct class cros_class = {
22         .owner          = THIS_MODULE,
23         .name           = "chromeos",
24 };
25
26 static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
27 {
28         struct cros_ec_command *msg;
29         int ret;
30
31         if (ec->features[0] == -1U && ec->features[1] == -1U) {
32                 /* features bitmap not read yet */
33
34                 msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
35                 if (!msg)
36                         return -ENOMEM;
37
38                 msg->version = 0;
39                 msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
40                 msg->insize = sizeof(ec->features);
41                 msg->outsize = 0;
42
43                 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
44                 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
45                         dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
46                                  ret, msg->result);
47                         memset(ec->features, 0, sizeof(ec->features));
48                 } else {
49                         memcpy(ec->features, msg->data, sizeof(ec->features));
50                 }
51
52                 dev_dbg(ec->dev, "EC features %08x %08x\n",
53                         ec->features[0], ec->features[1]);
54
55                 kfree(msg);
56         }
57
58         return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
59 }
60
61 static void cros_ec_class_release(struct device *dev)
62 {
63         kfree(to_cros_ec_dev(dev));
64 }
65
66 static void cros_ec_sensors_register(struct cros_ec_dev *ec)
67 {
68         /*
69          * Issue a command to get the number of sensor reported.
70          * Build an array of sensors driver and register them all.
71          */
72         int ret, i, id, sensor_num;
73         struct mfd_cell *sensor_cells;
74         struct cros_ec_sensor_platform *sensor_platforms;
75         int sensor_type[MOTIONSENSE_TYPE_MAX];
76         struct ec_params_motion_sense *params;
77         struct ec_response_motion_sense *resp;
78         struct cros_ec_command *msg;
79
80         msg = kzalloc(sizeof(struct cros_ec_command) +
81                       max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
82         if (msg == NULL)
83                 return;
84
85         msg->version = 2;
86         msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
87         msg->outsize = sizeof(*params);
88         msg->insize = sizeof(*resp);
89
90         params = (struct ec_params_motion_sense *)msg->data;
91         params->cmd = MOTIONSENSE_CMD_DUMP;
92
93         ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
94         if (ret < 0 || msg->result != EC_RES_SUCCESS) {
95                 dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
96                          ret, msg->result);
97                 goto error;
98         }
99
100         resp = (struct ec_response_motion_sense *)msg->data;
101         sensor_num = resp->dump.sensor_count;
102         /*
103          * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
104          */
105         sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
106                                GFP_KERNEL);
107         if (sensor_cells == NULL)
108                 goto error;
109
110         sensor_platforms = kcalloc(sensor_num,
111                                    sizeof(struct cros_ec_sensor_platform),
112                                    GFP_KERNEL);
113         if (sensor_platforms == NULL)
114                 goto error_platforms;
115
116         memset(sensor_type, 0, sizeof(sensor_type));
117         id = 0;
118         for (i = 0; i < sensor_num; i++) {
119                 params->cmd = MOTIONSENSE_CMD_INFO;
120                 params->info.sensor_num = i;
121                 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
122                 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
123                         dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
124                                  i, ret, msg->result);
125                         continue;
126                 }
127                 switch (resp->info.type) {
128                 case MOTIONSENSE_TYPE_ACCEL:
129                         sensor_cells[id].name = "cros-ec-accel";
130                         break;
131                 case MOTIONSENSE_TYPE_BARO:
132                         sensor_cells[id].name = "cros-ec-baro";
133                         break;
134                 case MOTIONSENSE_TYPE_GYRO:
135                         sensor_cells[id].name = "cros-ec-gyro";
136                         break;
137                 case MOTIONSENSE_TYPE_MAG:
138                         sensor_cells[id].name = "cros-ec-mag";
139                         break;
140                 case MOTIONSENSE_TYPE_PROX:
141                         sensor_cells[id].name = "cros-ec-prox";
142                         break;
143                 case MOTIONSENSE_TYPE_LIGHT:
144                         sensor_cells[id].name = "cros-ec-light";
145                         break;
146                 case MOTIONSENSE_TYPE_ACTIVITY:
147                         sensor_cells[id].name = "cros-ec-activity";
148                         break;
149                 default:
150                         dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
151                         continue;
152                 }
153                 sensor_platforms[id].sensor_num = i;
154                 sensor_cells[id].id = sensor_type[resp->info.type];
155                 sensor_cells[id].platform_data = &sensor_platforms[id];
156                 sensor_cells[id].pdata_size =
157                         sizeof(struct cros_ec_sensor_platform);
158
159                 sensor_type[resp->info.type]++;
160                 id++;
161         }
162
163         if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
164                 ec->has_kb_wake_angle = true;
165
166         if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
167                 sensor_cells[id].name = "cros-ec-ring";
168                 id++;
169         }
170         if (cros_ec_check_features(ec,
171                                 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
172                 sensor_cells[id].name = "cros-ec-lid-angle";
173                 id++;
174         }
175
176         ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
177                               NULL, 0, NULL);
178         if (ret)
179                 dev_err(ec->dev, "failed to add EC sensors\n");
180
181         kfree(sensor_platforms);
182 error_platforms:
183         kfree(sensor_cells);
184 error:
185         kfree(msg);
186 }
187
188 static struct cros_ec_sensor_platform sensor_platforms[] = {
189         { .sensor_num = 0 },
190         { .sensor_num = 1 }
191 };
192
193 static const struct mfd_cell cros_ec_accel_legacy_cells[] = {
194         {
195                 .name = "cros-ec-accel-legacy",
196                 .platform_data = &sensor_platforms[0],
197                 .pdata_size = sizeof(struct cros_ec_sensor_platform),
198         },
199         {
200                 .name = "cros-ec-accel-legacy",
201                 .platform_data = &sensor_platforms[1],
202                 .pdata_size = sizeof(struct cros_ec_sensor_platform),
203         }
204 };
205
206 static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
207 {
208         struct cros_ec_device *ec_dev = ec->ec_dev;
209         u8 status;
210         int ret;
211
212         /*
213          * ECs that need legacy support are the main EC, directly connected to
214          * the AP.
215          */
216         if (ec->cmd_offset != 0)
217                 return;
218
219         /*
220          * Check if EC supports direct memory reads and if EC has
221          * accelerometers.
222          */
223         if (ec_dev->cmd_readmem) {
224                 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS, 1,
225                                           &status);
226                 if (ret < 0) {
227                         dev_warn(ec->dev, "EC direct read error.\n");
228                         return;
229                 }
230
231                 /* Check if EC has accelerometers. */
232                 if (!(status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
233                         dev_info(ec->dev, "EC does not have accelerometers.\n");
234                         return;
235                 }
236         }
237
238         /*
239          * The device may still support accelerometers:
240          * it would be an older ARM based device that do not suppor the
241          * EC_CMD_GET_FEATURES command.
242          *
243          * Register 2 accelerometers, we will fail in the IIO driver if there
244          * are no sensors.
245          */
246         ret = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
247                               cros_ec_accel_legacy_cells,
248                               ARRAY_SIZE(cros_ec_accel_legacy_cells),
249                               NULL, 0, NULL);
250         if (ret)
251                 dev_err(ec_dev->dev, "failed to add EC sensors\n");
252 }
253
254 static const struct mfd_cell cros_ec_cec_cells[] = {
255         { .name = "cros-ec-cec" }
256 };
257
258 static const struct mfd_cell cros_ec_rtc_cells[] = {
259         { .name = "cros-ec-rtc" }
260 };
261
262 static const struct mfd_cell cros_usbpd_charger_cells[] = {
263         { .name = "cros-usbpd-charger" },
264         { .name = "cros-usbpd-logger" },
265 };
266
267 static const struct mfd_cell cros_ec_platform_cells[] = {
268         { .name = "cros-ec-chardev" },
269         { .name = "cros-ec-debugfs" },
270         { .name = "cros-ec-lightbar" },
271         { .name = "cros-ec-sysfs" },
272 };
273
274 static const struct mfd_cell cros_ec_vbc_cells[] = {
275         { .name = "cros-ec-vbc" }
276 };
277
278 static int ec_device_probe(struct platform_device *pdev)
279 {
280         int retval = -ENOMEM;
281         struct device_node *node;
282         struct device *dev = &pdev->dev;
283         struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
284         struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
285
286         if (!ec)
287                 return retval;
288
289         dev_set_drvdata(dev, ec);
290         ec->ec_dev = dev_get_drvdata(dev->parent);
291         ec->dev = dev;
292         ec->cmd_offset = ec_platform->cmd_offset;
293         ec->features[0] = -1U; /* Not cached yet */
294         ec->features[1] = -1U; /* Not cached yet */
295         device_initialize(&ec->class_dev);
296
297         /* Check whether this is actually a Fingerprint MCU rather than an EC */
298         if (cros_ec_check_features(ec, EC_FEATURE_FINGERPRINT)) {
299                 dev_info(dev, "CrOS Fingerprint MCU detected.\n");
300                 /*
301                  * Help userspace differentiating ECs from FP MCU,
302                  * regardless of the probing order.
303                  */
304                 ec_platform->ec_name = CROS_EC_DEV_FP_NAME;
305         }
306
307         /*
308          * Check whether this is actually an Integrated Sensor Hub (ISH)
309          * rather than an EC.
310          */
311         if (cros_ec_check_features(ec, EC_FEATURE_ISH)) {
312                 dev_info(dev, "CrOS ISH MCU detected.\n");
313                 /*
314                  * Help userspace differentiating ECs from ISH MCU,
315                  * regardless of the probing order.
316                  */
317                 ec_platform->ec_name = CROS_EC_DEV_ISH_NAME;
318         }
319
320         /* Check whether this is actually a Touchpad MCU rather than an EC */
321         if (cros_ec_check_features(ec, EC_FEATURE_TOUCHPAD)) {
322                 dev_info(dev, "CrOS Touchpad MCU detected.\n");
323                 /*
324                  * Help userspace differentiating ECs from TP MCU,
325                  * regardless of the probing order.
326                  */
327                 ec_platform->ec_name = CROS_EC_DEV_TP_NAME;
328         }
329
330         /* Check whether this is actually a SCP rather than an EC. */
331         if (cros_ec_check_features(ec, EC_FEATURE_SCP)) {
332                 dev_info(dev, "CrOS SCP MCU detected.\n");
333                 /*
334                  * Help userspace differentiating ECs from SCP,
335                  * regardless of the probing order.
336                  */
337                 ec_platform->ec_name = CROS_EC_DEV_SCP_NAME;
338         }
339
340         /*
341          * Add the class device
342          */
343         ec->class_dev.class = &cros_class;
344         ec->class_dev.parent = dev;
345         ec->class_dev.release = cros_ec_class_release;
346
347         retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
348         if (retval) {
349                 dev_err(dev, "dev_set_name failed => %d\n", retval);
350                 goto failed;
351         }
352
353         retval = device_add(&ec->class_dev);
354         if (retval)
355                 goto failed;
356
357         /* check whether this EC is a sensor hub. */
358         if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
359                 cros_ec_sensors_register(ec);
360         else
361                 /* Workaroud for older EC firmware */
362                 cros_ec_accel_legacy_register(ec);
363
364         /* Check whether this EC instance has CEC host command support */
365         if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
366                 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
367                                          cros_ec_cec_cells,
368                                          ARRAY_SIZE(cros_ec_cec_cells),
369                                          NULL, 0, NULL);
370                 if (retval)
371                         dev_err(ec->dev,
372                                 "failed to add cros-ec-cec device: %d\n",
373                                 retval);
374         }
375
376         /* Check whether this EC instance has RTC host command support */
377         if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
378                 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
379                                          cros_ec_rtc_cells,
380                                          ARRAY_SIZE(cros_ec_rtc_cells),
381                                          NULL, 0, NULL);
382                 if (retval)
383                         dev_err(ec->dev,
384                                 "failed to add cros-ec-rtc device: %d\n",
385                                 retval);
386         }
387
388         /* Check whether this EC instance has the PD charge manager */
389         if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
390                 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
391                                          cros_usbpd_charger_cells,
392                                          ARRAY_SIZE(cros_usbpd_charger_cells),
393                                          NULL, 0, NULL);
394                 if (retval)
395                         dev_err(ec->dev,
396                                 "failed to add cros-usbpd-charger device: %d\n",
397                                 retval);
398         }
399
400         retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
401                                  cros_ec_platform_cells,
402                                  ARRAY_SIZE(cros_ec_platform_cells),
403                                  NULL, 0, NULL);
404         if (retval)
405                 dev_warn(ec->dev,
406                          "failed to add cros-ec platform devices: %d\n",
407                          retval);
408
409         /* Check whether this EC instance has a VBC NVRAM */
410         node = ec->ec_dev->dev->of_node;
411         if (of_property_read_bool(node, "google,has-vbc-nvram")) {
412                 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
413                                          cros_ec_vbc_cells,
414                                          ARRAY_SIZE(cros_ec_vbc_cells),
415                                          NULL, 0, NULL);
416                 if (retval)
417                         dev_warn(ec->dev, "failed to add VBC devices: %d\n",
418                                  retval);
419         }
420
421         return 0;
422
423 failed:
424         put_device(&ec->class_dev);
425         return retval;
426 }
427
428 static int ec_device_remove(struct platform_device *pdev)
429 {
430         struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
431
432         mfd_remove_devices(ec->dev);
433         device_unregister(&ec->class_dev);
434         return 0;
435 }
436
437 static const struct platform_device_id cros_ec_id[] = {
438         { DRV_NAME, 0 },
439         { /* sentinel */ }
440 };
441 MODULE_DEVICE_TABLE(platform, cros_ec_id);
442
443 static struct platform_driver cros_ec_dev_driver = {
444         .driver = {
445                 .name = DRV_NAME,
446         },
447         .id_table = cros_ec_id,
448         .probe = ec_device_probe,
449         .remove = ec_device_remove,
450 };
451
452 static int __init cros_ec_dev_init(void)
453 {
454         int ret;
455
456         ret  = class_register(&cros_class);
457         if (ret) {
458                 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
459                 return ret;
460         }
461
462         /* Register the driver */
463         ret = platform_driver_register(&cros_ec_dev_driver);
464         if (ret < 0) {
465                 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
466                 goto failed_devreg;
467         }
468         return 0;
469
470 failed_devreg:
471         class_unregister(&cros_class);
472         return ret;
473 }
474
475 static void __exit cros_ec_dev_exit(void)
476 {
477         platform_driver_unregister(&cros_ec_dev_driver);
478         class_unregister(&cros_class);
479 }
480
481 module_init(cros_ec_dev_init);
482 module_exit(cros_ec_dev_exit);
483
484 MODULE_ALIAS("platform:" DRV_NAME);
485 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
486 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
487 MODULE_VERSION("1.0");
488 MODULE_LICENSE("GPL");