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