Lines Matching defs:tps

115 	int (*register_port)(struct tps6598x *tps, struct fwnode_handle *node);
118 int (*apply_patch)(struct tps6598x *tps);
119 int (*init)(struct tps6598x *tps);
120 int (*reset)(struct tps6598x *tps);
167 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
175 if (!tps->i2c_protocol)
176 return regmap_raw_read(tps->regmap, reg, val, len);
178 ret = regmap_raw_read(tps->regmap, reg, data, len + 1);
189 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
197 if (!tps->i2c_protocol)
198 return regmap_raw_write(tps->regmap, reg, val, len);
203 return regmap_raw_write(tps->regmap, reg, data, len + 1);
206 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val)
208 return tps6598x_block_read(tps, reg, val, sizeof(u8));
211 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
213 return tps6598x_block_read(tps, reg, val, sizeof(u16));
216 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
218 return tps6598x_block_read(tps, reg, val, sizeof(u32));
221 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
223 return tps6598x_block_read(tps, reg, val, sizeof(u64));
226 static inline int tps6598x_write8(struct tps6598x *tps, u8 reg, u8 val)
228 return tps6598x_block_write(tps, reg, &val, sizeof(u8));
231 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
233 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
237 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
239 return tps6598x_block_write(tps, reg, val, 4);
242 static int tps6598x_read_partner_identity(struct tps6598x *tps)
247 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
252 tps->partner_identity = id.identity;
257 static void tps6598x_set_data_role(struct tps6598x *tps,
270 usb_role_switch_set_role(tps->role_sw, role_val);
271 typec_set_data_role(tps->port, role);
274 static int tps6598x_connect(struct tps6598x *tps, u32 status)
280 if (tps->partner)
283 mode = TPS_POWER_STATUS_PWROPMODE(tps->pwr_status);
290 ret = tps6598x_read_partner_identity(tps);
293 desc.identity = &tps->partner_identity;
296 typec_set_pwr_opmode(tps->port, mode);
297 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
298 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
300 typec_set_orientation(tps->port, TYPEC_ORIENTATION_REVERSE);
302 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NORMAL);
303 typec_set_mode(tps->port, TYPEC_STATE_USB);
304 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
306 tps->partner = typec_register_partner(tps->port, &desc);
307 if (IS_ERR(tps->partner))
308 return PTR_ERR(tps->partner);
311 typec_partner_set_identity(tps->partner);
313 power_supply_changed(tps->psy);
318 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
320 if (!IS_ERR(tps->partner))
321 typec_unregister_partner(tps->partner);
322 tps->partner = NULL;
323 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
324 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
325 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
326 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NONE);
327 typec_set_mode(tps->port, TYPEC_STATE_SAFE);
328 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
330 power_supply_changed(tps->psy);
333 static int tps6598x_exec_cmd_tmo(struct tps6598x *tps, const char *cmd,
342 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
349 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
355 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
362 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
376 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
382 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
399 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
403 return tps6598x_exec_cmd_tmo(tps, cmd, in_len, in_data,
410 struct tps6598x *tps = typec_get_drvdata(port);
414 mutex_lock(&tps->lock);
416 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
420 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
429 tps6598x_set_data_role(tps, role, true);
432 mutex_unlock(&tps->lock);
440 struct tps6598x *tps = typec_get_drvdata(port);
444 mutex_lock(&tps->lock);
446 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
450 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
459 typec_set_pwr_role(tps->port, role);
462 mutex_unlock(&tps->lock);
472 static bool tps6598x_read_status(struct tps6598x *tps, u32 *status)
476 ret = tps6598x_read32(tps, TPS_REG_STATUS, status);
478 dev_err(tps->dev, "%s: failed to read status\n", __func__);
482 if (tps->data->trace_status)
483 tps->data->trace_status(*status);
488 static bool tps6598x_read_data_status(struct tps6598x *tps)
493 ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
495 dev_err(tps->dev, "failed to read data status: %d\n", ret);
503 static bool tps6598x_read_power_status(struct tps6598x *tps)
508 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
510 dev_err(tps->dev, "failed to read power status: %d\n", ret);
513 tps->pwr_status = pwr_status;
515 if (tps->data->trace_power_status)
516 tps->data->trace_power_status(pwr_status);
521 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status)
526 ret = tps6598x_connect(tps, status);
528 dev_err(tps->dev, "failed to register partner\n");
530 tps6598x_disconnect(tps, status);
536 struct tps6598x *tps = data;
541 mutex_lock(&tps->lock);
543 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event);
545 dev_err(tps->dev, "%s: failed to read events\n", __func__);
553 if (!tps6598x_read_status(tps, &status))
557 if (!tps6598x_read_power_status(tps))
561 if (!tps6598x_read_data_status(tps))
566 tps6598x_handle_plug_event(tps, status);
569 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event);
572 mutex_unlock(&tps->lock);
579 static bool tps6598x_has_role_changed(struct tps6598x *tps, u32 status)
581 status ^= tps->status;
588 struct tps6598x *tps = data;
593 mutex_lock(&tps->lock);
595 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event, 11);
597 dev_err(tps->dev, "%s: failed to read events\n", __func__);
605 if (!tps6598x_read_status(tps, &status))
609 if (!tps6598x_read_power_status(tps))
613 if (!tps6598x_read_data_status(tps))
622 tps6598x_has_role_changed(tps, status))
623 tps6598x_handle_plug_event(tps, status);
625 tps->status = status;
628 tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event, 11);
631 mutex_unlock(&tps->lock);
641 struct tps6598x *tps = data;
648 mutex_lock(&tps->lock);
650 ret = tps6598x_read32(tps, TPS_REG_VERSION, &version);
652 dev_warn(tps->dev, "%s: failed to read version (%d)\n",
659 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event1, intev_len);
661 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event1, intev_len);
663 dev_err(tps->dev, "%s: failed to read event1\n", __func__);
666 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT2, event2, intev_len);
668 dev_err(tps->dev, "%s: failed to read event2\n", __func__);
676 if (!tps6598x_read_status(tps, &status))
680 if (!tps6598x_read_power_status(tps))
684 if (!tps6598x_read_data_status(tps))
689 tps6598x_handle_plug_event(tps, status);
692 tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event1, intev_len);
693 tps6598x_block_write(tps, TPS_REG_INT_CLEAR2, event2, intev_len);
696 mutex_unlock(&tps->lock);
708 struct tps6598x *tps = container_of(to_delayed_work(work),
711 tps->data->irq_handler(0, tps);
713 &tps->wq_poll, msecs_to_jiffies(POLL_INTERVAL));
716 static int tps6598x_check_mode(struct tps6598x *tps)
721 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
732 dev_warn(tps->dev, "dead-battery condition\n");
737 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
751 static int tps6598x_psy_get_online(struct tps6598x *tps,
754 if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) &&
755 TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) {
767 struct tps6598x *tps = power_supply_get_drvdata(psy);
772 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD)
778 ret = tps6598x_psy_get_online(tps, val);
788 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state)
793 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
800 ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL);
804 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
814 static int devm_tps6598_psy_register(struct tps6598x *tps)
817 const char *port_dev_name = dev_name(tps->dev);
820 psy_cfg.drv_data = tps;
821 psy_cfg.fwnode = dev_fwnode(tps->dev);
823 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
828 tps->psy_desc.name = psy_name;
829 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
830 tps->psy_desc.usb_types = tps6598x_psy_usb_types;
831 tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
832 tps->psy_desc.properties = tps6598x_psy_props;
833 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
834 tps->psy_desc.get_property = tps6598x_psy_get_prop;
836 tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
838 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
840 return PTR_ERR_OR_ZERO(tps->psy);
844 tps6598x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
850 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
857 typec_cap.driver_data = tps;
888 tps->port = typec_register_port(tps->dev, &typec_cap);
889 if (IS_ERR(tps->port))
890 return PTR_ERR(tps->port);
895 static int tps_request_firmware(struct tps6598x *tps, const struct firmware **fw)
900 ret = device_property_read_string(tps->dev, "firmware-name",
905 ret = request_firmware(fw, firmware_name, tps->dev);
907 dev_err(tps->dev, "failed to retrieve \"%s\"\n", firmware_name);
920 tps25750_write_firmware(struct tps6598x *tps,
923 struct i2c_client *client = to_i2c_client(tps->dev);
938 ret = regmap_raw_write(tps->regmap, data[0], &data[1], len - 1);
947 tps25750_exec_pbms(struct tps6598x *tps, u8 *in_data, size_t in_len)
952 ret = tps6598x_exec_cmd_tmo(tps, "PBMs", in_len, in_data,
959 dev_err(tps->dev, "%s: invalid fw size\n", __func__);
962 dev_err(tps->dev, "%s: invalid slave address\n", __func__);
965 dev_err(tps->dev, "%s: timed out\n", __func__);
974 static int tps25750_abort_patch_process(struct tps6598x *tps)
978 ret = tps6598x_exec_cmd(tps, "PBMe", 0, NULL, 0, NULL);
982 ret = tps6598x_check_mode(tps);
984 dev_err(tps->dev, "failed to switch to \"PTCH\" mode\n");
989 static int tps25750_start_patch_burst_mode(struct tps6598x *tps)
1000 struct device_node *np = tps->dev->of_node;
1002 ret = device_property_read_string(tps->dev, "firmware-name",
1007 ret = tps_request_firmware(tps, &fw);
1013 dev_err(tps->dev, "failed to get patch-address %d\n", ret);
1022 dev_err(tps->dev, "wrong patch address %u\n", addr);
1031 ret = tps25750_exec_pbms(tps, (u8 *)&bpms_data, sizeof(bpms_data));
1035 ret = tps25750_write_firmware(tps, bpms_data.addr, fw->data, fw->size);
1037 dev_err(tps->dev, "Failed to write patch %s of %zu bytes\n",
1056 static int tps25750_complete_patch_process(struct tps6598x *tps)
1066 ret = tps6598x_exec_cmd_tmo(tps, "PBMc", sizeof(dummy), dummy,
1072 dev_err(tps->dev,
1079 dev_err(tps->dev,
1088 static int tps25750_apply_patch(struct tps6598x *tps)
1094 ret = tps6598x_block_read(tps, TPS_REG_BOOT_STATUS, &status, 5);
1104 ret = tps25750_start_patch_burst_mode(tps);
1106 tps25750_abort_patch_process(tps);
1110 ret = tps25750_complete_patch_process(tps);
1118 ret = tps6598x_check_mode(tps);
1135 ret = tps6598x_exec_cmd(tps, "DBfg", 0, NULL, 0, NULL);
1137 dev_err(tps->dev, "failed to clear dead battery %d\n", ret);
1142 dev_info(tps->dev, "controller switched to \"APP\" mode\n");
1147 static int tps6598x_apply_patch(struct tps6598x *tps)
1158 ret = device_property_read_string(tps->dev, "firmware-name",
1163 ret = tps_request_firmware(tps, &fw);
1167 ret = tps6598x_exec_cmd(tps, "PTCs", in_len, &in,
1172 dev_err(tps->dev, "Update start failed (%d)\n", ret);
1182 ret = tps6598x_exec_cmd(tps, "PTCd", in_len,
1189 dev_err(tps->dev, "Patch download failed (%d)\n", ret);
1196 ret = tps6598x_exec_cmd(tps, "PTCc", 0, NULL, TPS_PTCC_OUT_BYTES, out);
1200 dev_err(tps->dev, "Update completion failed (%d)\n", ret);
1204 dev_info(tps->dev, "Firmware update succeeded\n");
1212 static int cd321x_init(struct tps6598x *tps)
1217 static int tps25750_init(struct tps6598x *tps)
1221 ret = tps->data->apply_patch(tps);
1225 ret = tps6598x_write8(tps, TPS_REG_SLEEP_CONF,
1228 dev_warn(tps->dev,
1235 static int tps6598x_init(struct tps6598x *tps)
1237 return tps->data->apply_patch(tps);
1240 static int cd321x_reset(struct tps6598x *tps)
1245 static int tps25750_reset(struct tps6598x *tps)
1247 return tps6598x_exec_cmd_tmo(tps, "GAID", 0, NULL, 0, NULL, 2000, 0);
1250 static int tps6598x_reset(struct tps6598x *tps)
1256 tps25750_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1263 ret = tps6598x_read8(tps, TPS_REG_PD_STATUS, &pd_status);
1269 dev_err(tps->dev, "data-role not found: %d\n", ret);
1275 dev_err(tps->dev, "unknown data-role: %s\n", data_role);
1282 typec_cap.driver_data = tps;
1302 tps->port = typec_register_port(tps->dev, &typec_cap);
1303 if (IS_ERR(tps->port))
1304 return PTR_ERR(tps->port);
1312 struct tps6598x *tps;
1319 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
1320 if (!tps)
1323 mutex_init(&tps->lock);
1324 tps->dev = &client->dev;
1326 tps->reset = devm_gpiod_get_optional(tps->dev, "reset", GPIOD_OUT_LOW);
1327 if (IS_ERR(tps->reset))
1328 return dev_err_probe(tps->dev, PTR_ERR(tps->reset),
1330 if (tps->reset)
1333 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
1334 if (IS_ERR(tps->regmap))
1335 return PTR_ERR(tps->regmap);
1337 if (!device_is_compatible(tps->dev, "ti,tps25750")) {
1338 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
1348 tps->i2c_protocol = true;
1352 ret = cd321x_switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0);
1368 tps->data = i2c_get_match_data(client);
1369 if (!tps->data)
1373 ret = tps6598x_check_mode(tps);
1378 ret = tps->data->init(tps);
1383 ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, mask1);
1387 if (!tps6598x_read_status(tps, &status)) {
1403 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
1404 if (IS_ERR(tps->role_sw)) {
1405 ret = PTR_ERR(tps->role_sw);
1409 ret = devm_tps6598_psy_register(tps);
1413 ret = tps->data->register_port(tps, fwnode);
1418 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &tps->pwr_status);
1420 dev_err(tps->dev, "failed to read power status: %d\n", ret);
1423 ret = tps6598x_connect(tps, status);
1430 tps->data->irq_handler,
1432 dev_name(&client->dev), tps);
1434 dev_warn(tps->dev, "Unable to find the interrupt, switching to polling\n");
1435 INIT_DELAYED_WORK(&tps->wq_poll, tps6598x_poll_work);
1436 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
1443 i2c_set_clientdata(client, tps);
1446 tps->wakeup = device_property_read_bool(tps->dev, "wakeup-source");
1447 if (tps->wakeup && client->irq) {
1455 tps6598x_disconnect(tps, 0);
1457 typec_unregister_port(tps->port);
1459 usb_role_switch_put(tps->role_sw);
1463 tps6598x_write64(tps, TPS_REG_INT_MASK1, 0);
1466 tps->data->reset(tps);
1473 struct tps6598x *tps = i2c_get_clientdata(client);
1476 cancel_delayed_work_sync(&tps->wq_poll);
1478 devm_free_irq(tps->dev, client->irq, tps);
1479 tps6598x_disconnect(tps, 0);
1480 typec_unregister_port(tps->port);
1481 usb_role_switch_put(tps->role_sw);
1484 tps->data->reset(tps);
1486 if (tps->reset)
1487 gpiod_set_value_cansleep(tps->reset, 1);
1493 struct tps6598x *tps = i2c_get_clientdata(client);
1495 if (tps->wakeup) {
1498 } else if (tps->reset) {
1499 gpiod_set_value_cansleep(tps->reset, 1);
1503 cancel_delayed_work_sync(&tps->wq_poll);
1511 struct tps6598x *tps = i2c_get_clientdata(client);
1514 ret = tps6598x_check_mode(tps);
1519 ret = tps->data->init(tps);
1524 if (tps->wakeup) {
1527 } else if (tps->reset) {
1528 gpiod_set_value_cansleep(tps->reset, 0);
1533 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,