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