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