1a2679b64SGuenter Roeck // SPDX-License-Identifier: GPL-2.0
2a2679b64SGuenter Roeck /*
3a2679b64SGuenter Roeck * Logging driver for ChromeOS EC based USBPD Charger.
4a2679b64SGuenter Roeck *
5a2679b64SGuenter Roeck * Copyright 2018 Google LLC.
6a2679b64SGuenter Roeck */
7a2679b64SGuenter Roeck
8a2679b64SGuenter Roeck #include <linux/ktime.h>
9840d9f13SEnric Balletbo i Serra #include <linux/math64.h>
10*e0e59c53STzung-Bi Shih #include <linux/mod_devicetable.h>
11a2679b64SGuenter Roeck #include <linux/module.h>
12840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h>
13840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_proto.h>
14a2679b64SGuenter Roeck #include <linux/platform_device.h>
15a2679b64SGuenter Roeck #include <linux/rtc.h>
16a2679b64SGuenter Roeck
17a2679b64SGuenter Roeck #define DRV_NAME "cros-usbpd-logger"
18a2679b64SGuenter Roeck
19a2679b64SGuenter Roeck #define CROS_USBPD_MAX_LOG_ENTRIES 30
20a2679b64SGuenter Roeck #define CROS_USBPD_LOG_UPDATE_DELAY msecs_to_jiffies(60000)
21a2679b64SGuenter Roeck #define CROS_USBPD_DATA_SIZE 16
22a2679b64SGuenter Roeck #define CROS_USBPD_LOG_RESP_SIZE (sizeof(struct ec_response_pd_log) + \
23a2679b64SGuenter Roeck CROS_USBPD_DATA_SIZE)
24a2679b64SGuenter Roeck #define CROS_USBPD_BUFFER_SIZE (sizeof(struct cros_ec_command) + \
25a2679b64SGuenter Roeck CROS_USBPD_LOG_RESP_SIZE)
26a2679b64SGuenter Roeck /* Buffer for building the PDLOG string */
27a2679b64SGuenter Roeck #define BUF_SIZE 80
28a2679b64SGuenter Roeck
29a2679b64SGuenter Roeck struct logger_data {
30a2679b64SGuenter Roeck struct device *dev;
31a2679b64SGuenter Roeck struct cros_ec_dev *ec_dev;
32a2679b64SGuenter Roeck u8 ec_buffer[CROS_USBPD_BUFFER_SIZE];
33a2679b64SGuenter Roeck struct delayed_work log_work;
34a2679b64SGuenter Roeck struct workqueue_struct *log_workqueue;
35a2679b64SGuenter Roeck };
36a2679b64SGuenter Roeck
37a2679b64SGuenter Roeck static const char * const chg_type_names[] = {
38a2679b64SGuenter Roeck "None", "PD", "Type-C", "Proprietary", "DCP", "CDP", "SDP",
39a2679b64SGuenter Roeck "Other", "VBUS"
40a2679b64SGuenter Roeck };
41a2679b64SGuenter Roeck
42a2679b64SGuenter Roeck static const char * const role_names[] = {
43a2679b64SGuenter Roeck "Disconnected", "SRC", "SNK", "SNK (not charging)"
44a2679b64SGuenter Roeck };
45a2679b64SGuenter Roeck
46a2679b64SGuenter Roeck static const char * const fault_names[] = {
47a2679b64SGuenter Roeck "---", "OCP", "fast OCP", "OVP", "Discharge"
48a2679b64SGuenter Roeck };
49a2679b64SGuenter Roeck
50bbb7ad49SEnric Balletbo i Serra __printf(3, 4)
append_str(char * buf,int pos,const char * fmt,...)51a2679b64SGuenter Roeck static int append_str(char *buf, int pos, const char *fmt, ...)
52a2679b64SGuenter Roeck {
53a2679b64SGuenter Roeck va_list args;
54a2679b64SGuenter Roeck int i;
55a2679b64SGuenter Roeck
56a2679b64SGuenter Roeck va_start(args, fmt);
57a2679b64SGuenter Roeck i = vsnprintf(buf + pos, BUF_SIZE - pos, fmt, args);
58a2679b64SGuenter Roeck va_end(args);
59a2679b64SGuenter Roeck
60a2679b64SGuenter Roeck return i;
61a2679b64SGuenter Roeck }
62a2679b64SGuenter Roeck
ec_get_log_entry(struct logger_data * logger)63a2679b64SGuenter Roeck static struct ec_response_pd_log *ec_get_log_entry(struct logger_data *logger)
64a2679b64SGuenter Roeck {
65a2679b64SGuenter Roeck struct cros_ec_dev *ec_dev = logger->ec_dev;
66a2679b64SGuenter Roeck struct cros_ec_command *msg;
67a2679b64SGuenter Roeck int ret;
68a2679b64SGuenter Roeck
69a2679b64SGuenter Roeck msg = (struct cros_ec_command *)logger->ec_buffer;
70a2679b64SGuenter Roeck
71a2679b64SGuenter Roeck msg->command = ec_dev->cmd_offset + EC_CMD_PD_GET_LOG_ENTRY;
72a2679b64SGuenter Roeck msg->insize = CROS_USBPD_LOG_RESP_SIZE;
73a2679b64SGuenter Roeck
74a2679b64SGuenter Roeck ret = cros_ec_cmd_xfer_status(ec_dev->ec_dev, msg);
75a2679b64SGuenter Roeck if (ret < 0)
76a2679b64SGuenter Roeck return ERR_PTR(ret);
77a2679b64SGuenter Roeck
78a2679b64SGuenter Roeck return (struct ec_response_pd_log *)msg->data;
79a2679b64SGuenter Roeck }
80a2679b64SGuenter Roeck
cros_usbpd_print_log_entry(struct ec_response_pd_log * r,ktime_t tstamp)81a2679b64SGuenter Roeck static void cros_usbpd_print_log_entry(struct ec_response_pd_log *r,
82a2679b64SGuenter Roeck ktime_t tstamp)
83a2679b64SGuenter Roeck {
84a2679b64SGuenter Roeck const char *fault, *role, *chg_type;
85a2679b64SGuenter Roeck struct usb_chg_measures *meas;
86a2679b64SGuenter Roeck struct mcdp_info *minfo;
87a2679b64SGuenter Roeck int role_idx, type_idx;
88a2679b64SGuenter Roeck char buf[BUF_SIZE + 1];
89a2679b64SGuenter Roeck struct rtc_time rt;
90a2679b64SGuenter Roeck int len = 0;
91a2679b64SGuenter Roeck s32 rem;
92a2679b64SGuenter Roeck int i;
93a2679b64SGuenter Roeck
94a2679b64SGuenter Roeck /* The timestamp is the number of 1024th of seconds in the past */
95a2679b64SGuenter Roeck tstamp = ktime_sub_us(tstamp, r->timestamp << PD_LOG_TIMESTAMP_SHIFT);
96a2679b64SGuenter Roeck rt = rtc_ktime_to_tm(tstamp);
97a2679b64SGuenter Roeck
98a2679b64SGuenter Roeck switch (r->type) {
99a2679b64SGuenter Roeck case PD_EVENT_MCU_CHARGE:
100a2679b64SGuenter Roeck if (r->data & CHARGE_FLAGS_OVERRIDE)
101a2679b64SGuenter Roeck len += append_str(buf, len, "override ");
102a2679b64SGuenter Roeck
103a2679b64SGuenter Roeck if (r->data & CHARGE_FLAGS_DELAYED_OVERRIDE)
104a2679b64SGuenter Roeck len += append_str(buf, len, "pending_override ");
105a2679b64SGuenter Roeck
106a2679b64SGuenter Roeck role_idx = r->data & CHARGE_FLAGS_ROLE_MASK;
107a2679b64SGuenter Roeck role = role_idx < ARRAY_SIZE(role_names) ?
108a2679b64SGuenter Roeck role_names[role_idx] : "Unknown";
109a2679b64SGuenter Roeck
110a2679b64SGuenter Roeck type_idx = (r->data & CHARGE_FLAGS_TYPE_MASK)
111a2679b64SGuenter Roeck >> CHARGE_FLAGS_TYPE_SHIFT;
112a2679b64SGuenter Roeck
113a2679b64SGuenter Roeck chg_type = type_idx < ARRAY_SIZE(chg_type_names) ?
114a2679b64SGuenter Roeck chg_type_names[type_idx] : "???";
115a2679b64SGuenter Roeck
116a2679b64SGuenter Roeck if (role_idx == USB_PD_PORT_POWER_DISCONNECTED ||
117a2679b64SGuenter Roeck role_idx == USB_PD_PORT_POWER_SOURCE) {
118a2679b64SGuenter Roeck len += append_str(buf, len, "%s", role);
119a2679b64SGuenter Roeck break;
120a2679b64SGuenter Roeck }
121a2679b64SGuenter Roeck
122a2679b64SGuenter Roeck meas = (struct usb_chg_measures *)r->payload;
123a2679b64SGuenter Roeck len += append_str(buf, len, "%s %s %s %dmV max %dmV / %dmA",
124a2679b64SGuenter Roeck role, r->data & CHARGE_FLAGS_DUAL_ROLE ?
125a2679b64SGuenter Roeck "DRP" : "Charger",
126a2679b64SGuenter Roeck chg_type, meas->voltage_now,
127a2679b64SGuenter Roeck meas->voltage_max, meas->current_max);
128a2679b64SGuenter Roeck break;
129a2679b64SGuenter Roeck case PD_EVENT_ACC_RW_FAIL:
130a2679b64SGuenter Roeck len += append_str(buf, len, "RW signature check failed");
131a2679b64SGuenter Roeck break;
132a2679b64SGuenter Roeck case PD_EVENT_PS_FAULT:
133a2679b64SGuenter Roeck fault = r->data < ARRAY_SIZE(fault_names) ? fault_names[r->data]
134a2679b64SGuenter Roeck : "???";
135a2679b64SGuenter Roeck len += append_str(buf, len, "Power supply fault: %s", fault);
136a2679b64SGuenter Roeck break;
137a2679b64SGuenter Roeck case PD_EVENT_VIDEO_DP_MODE:
138a2679b64SGuenter Roeck len += append_str(buf, len, "DP mode %sabled", r->data == 1 ?
139a2679b64SGuenter Roeck "en" : "dis");
140a2679b64SGuenter Roeck break;
141a2679b64SGuenter Roeck case PD_EVENT_VIDEO_CODEC:
142a2679b64SGuenter Roeck minfo = (struct mcdp_info *)r->payload;
143a2679b64SGuenter Roeck len += append_str(buf, len, "HDMI info: family:%04x chipid:%04x ",
144a2679b64SGuenter Roeck MCDP_FAMILY(minfo->family),
145a2679b64SGuenter Roeck MCDP_CHIPID(minfo->chipid));
146a2679b64SGuenter Roeck len += append_str(buf, len, "irom:%d.%d.%d fw:%d.%d.%d",
147a2679b64SGuenter Roeck minfo->irom.major, minfo->irom.minor,
148a2679b64SGuenter Roeck minfo->irom.build, minfo->fw.major,
149a2679b64SGuenter Roeck minfo->fw.minor, minfo->fw.build);
150a2679b64SGuenter Roeck break;
151a2679b64SGuenter Roeck default:
152a2679b64SGuenter Roeck len += append_str(buf, len, "Event %02x (%04x) [", r->type,
153a2679b64SGuenter Roeck r->data);
154a2679b64SGuenter Roeck
155a2679b64SGuenter Roeck for (i = 0; i < PD_LOG_SIZE(r->size_port); i++)
156a2679b64SGuenter Roeck len += append_str(buf, len, "%02x ", r->payload[i]);
157a2679b64SGuenter Roeck
158a2679b64SGuenter Roeck len += append_str(buf, len, "]");
159a2679b64SGuenter Roeck break;
160a2679b64SGuenter Roeck }
161a2679b64SGuenter Roeck
162a2679b64SGuenter Roeck div_s64_rem(ktime_to_ms(tstamp), MSEC_PER_SEC, &rem);
163a2679b64SGuenter Roeck pr_info("PDLOG %d/%02d/%02d %02d:%02d:%02d.%03d P%d %s\n",
164a2679b64SGuenter Roeck rt.tm_year + 1900, rt.tm_mon + 1, rt.tm_mday,
165a2679b64SGuenter Roeck rt.tm_hour, rt.tm_min, rt.tm_sec, rem,
166a2679b64SGuenter Roeck PD_LOG_PORT(r->size_port), buf);
167a2679b64SGuenter Roeck }
168a2679b64SGuenter Roeck
cros_usbpd_log_check(struct work_struct * work)169a2679b64SGuenter Roeck static void cros_usbpd_log_check(struct work_struct *work)
170a2679b64SGuenter Roeck {
171a2679b64SGuenter Roeck struct logger_data *logger = container_of(to_delayed_work(work),
172a2679b64SGuenter Roeck struct logger_data,
173a2679b64SGuenter Roeck log_work);
174a2679b64SGuenter Roeck struct device *dev = logger->dev;
175a2679b64SGuenter Roeck struct ec_response_pd_log *r;
176a2679b64SGuenter Roeck int entries = 0;
177a2679b64SGuenter Roeck ktime_t now;
178a2679b64SGuenter Roeck
179a2679b64SGuenter Roeck while (entries++ < CROS_USBPD_MAX_LOG_ENTRIES) {
180a2679b64SGuenter Roeck r = ec_get_log_entry(logger);
181a2679b64SGuenter Roeck now = ktime_get_real();
182a2679b64SGuenter Roeck if (IS_ERR(r)) {
183a2679b64SGuenter Roeck dev_dbg(dev, "Cannot get PD log %ld\n", PTR_ERR(r));
184a2679b64SGuenter Roeck break;
185a2679b64SGuenter Roeck }
186a2679b64SGuenter Roeck if (r->type == PD_EVENT_NO_ENTRY)
187a2679b64SGuenter Roeck break;
188a2679b64SGuenter Roeck
189a2679b64SGuenter Roeck cros_usbpd_print_log_entry(r, now);
190a2679b64SGuenter Roeck }
191a2679b64SGuenter Roeck
192a2679b64SGuenter Roeck queue_delayed_work(logger->log_workqueue, &logger->log_work,
193a2679b64SGuenter Roeck CROS_USBPD_LOG_UPDATE_DELAY);
194a2679b64SGuenter Roeck }
195a2679b64SGuenter Roeck
cros_usbpd_logger_probe(struct platform_device * pd)196a2679b64SGuenter Roeck static int cros_usbpd_logger_probe(struct platform_device *pd)
197a2679b64SGuenter Roeck {
198a2679b64SGuenter Roeck struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
199a2679b64SGuenter Roeck struct device *dev = &pd->dev;
200a2679b64SGuenter Roeck struct logger_data *logger;
201a2679b64SGuenter Roeck
202a2679b64SGuenter Roeck logger = devm_kzalloc(dev, sizeof(*logger), GFP_KERNEL);
203a2679b64SGuenter Roeck if (!logger)
204a2679b64SGuenter Roeck return -ENOMEM;
205a2679b64SGuenter Roeck
206a2679b64SGuenter Roeck logger->dev = dev;
207a2679b64SGuenter Roeck logger->ec_dev = ec_dev;
208a2679b64SGuenter Roeck
209a2679b64SGuenter Roeck platform_set_drvdata(pd, logger);
210a2679b64SGuenter Roeck
211a2679b64SGuenter Roeck /* Retrieve PD event logs periodically */
212a2679b64SGuenter Roeck INIT_DELAYED_WORK(&logger->log_work, cros_usbpd_log_check);
213a2679b64SGuenter Roeck logger->log_workqueue = create_singlethread_workqueue("cros_usbpd_log");
2144c1fde50SNavid Emamdoost if (!logger->log_workqueue)
2154c1fde50SNavid Emamdoost return -ENOMEM;
2164c1fde50SNavid Emamdoost
217a2679b64SGuenter Roeck queue_delayed_work(logger->log_workqueue, &logger->log_work,
218a2679b64SGuenter Roeck CROS_USBPD_LOG_UPDATE_DELAY);
219a2679b64SGuenter Roeck
220a2679b64SGuenter Roeck return 0;
221a2679b64SGuenter Roeck }
222a2679b64SGuenter Roeck
cros_usbpd_logger_remove(struct platform_device * pd)223b6c1fea8SUwe Kleine-König static void cros_usbpd_logger_remove(struct platform_device *pd)
224a2679b64SGuenter Roeck {
225a2679b64SGuenter Roeck struct logger_data *logger = platform_get_drvdata(pd);
226a2679b64SGuenter Roeck
227a2679b64SGuenter Roeck cancel_delayed_work_sync(&logger->log_work);
228c2ce4d23SChuhong Yuan destroy_workqueue(logger->log_workqueue);
229a2679b64SGuenter Roeck }
230a2679b64SGuenter Roeck
cros_usbpd_logger_resume(struct device * dev)231a2679b64SGuenter Roeck static int __maybe_unused cros_usbpd_logger_resume(struct device *dev)
232a2679b64SGuenter Roeck {
233a2679b64SGuenter Roeck struct logger_data *logger = dev_get_drvdata(dev);
234a2679b64SGuenter Roeck
235a2679b64SGuenter Roeck queue_delayed_work(logger->log_workqueue, &logger->log_work,
236a2679b64SGuenter Roeck CROS_USBPD_LOG_UPDATE_DELAY);
237a2679b64SGuenter Roeck
238a2679b64SGuenter Roeck return 0;
239a2679b64SGuenter Roeck }
240a2679b64SGuenter Roeck
cros_usbpd_logger_suspend(struct device * dev)241a2679b64SGuenter Roeck static int __maybe_unused cros_usbpd_logger_suspend(struct device *dev)
242a2679b64SGuenter Roeck {
243a2679b64SGuenter Roeck struct logger_data *logger = dev_get_drvdata(dev);
244a2679b64SGuenter Roeck
245a2679b64SGuenter Roeck cancel_delayed_work_sync(&logger->log_work);
246a2679b64SGuenter Roeck
247a2679b64SGuenter Roeck return 0;
248a2679b64SGuenter Roeck }
249a2679b64SGuenter Roeck
250a2679b64SGuenter Roeck static SIMPLE_DEV_PM_OPS(cros_usbpd_logger_pm_ops, cros_usbpd_logger_suspend,
251a2679b64SGuenter Roeck cros_usbpd_logger_resume);
252a2679b64SGuenter Roeck
253*e0e59c53STzung-Bi Shih static const struct platform_device_id cros_usbpd_logger_id[] = {
254*e0e59c53STzung-Bi Shih { DRV_NAME, 0 },
255*e0e59c53STzung-Bi Shih {}
256*e0e59c53STzung-Bi Shih };
257*e0e59c53STzung-Bi Shih MODULE_DEVICE_TABLE(platform, cros_usbpd_logger_id);
258*e0e59c53STzung-Bi Shih
259a2679b64SGuenter Roeck static struct platform_driver cros_usbpd_logger_driver = {
260a2679b64SGuenter Roeck .driver = {
261a2679b64SGuenter Roeck .name = DRV_NAME,
262a2679b64SGuenter Roeck .pm = &cros_usbpd_logger_pm_ops,
263a2679b64SGuenter Roeck },
264a2679b64SGuenter Roeck .probe = cros_usbpd_logger_probe,
265b6c1fea8SUwe Kleine-König .remove_new = cros_usbpd_logger_remove,
266*e0e59c53STzung-Bi Shih .id_table = cros_usbpd_logger_id,
267a2679b64SGuenter Roeck };
268a2679b64SGuenter Roeck
269a2679b64SGuenter Roeck module_platform_driver(cros_usbpd_logger_driver);
270a2679b64SGuenter Roeck
271a2679b64SGuenter Roeck MODULE_LICENSE("GPL v2");
272a2679b64SGuenter Roeck MODULE_DESCRIPTION("Logging driver for ChromeOS EC USBPD Charger.");
273