Lines Matching refs:tps

114 	int (*register_port)(struct tps6598x *tps, struct fwnode_handle *node);
117 int (*apply_patch)(struct tps6598x *tps);
118 int (*init)(struct tps6598x *tps);
119 int (*reset)(struct tps6598x *tps);
166 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
174 if (!tps->i2c_protocol)
175 return regmap_raw_read(tps->regmap, reg, val, len);
177 ret = regmap_raw_read(tps->regmap, reg, data, len + 1);
188 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
196 if (!tps->i2c_protocol)
197 return regmap_raw_write(tps->regmap, reg, val, len);
202 return regmap_raw_write(tps->regmap, reg, data, len + 1);
205 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val)
207 return tps6598x_block_read(tps, reg, val, sizeof(u8));
210 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
212 return tps6598x_block_read(tps, reg, val, sizeof(u16));
215 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
217 return tps6598x_block_read(tps, reg, val, sizeof(u32));
220 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
222 return tps6598x_block_read(tps, reg, val, sizeof(u64));
225 static inline int tps6598x_write8(struct tps6598x *tps, u8 reg, u8 val)
227 return tps6598x_block_write(tps, reg, &val, sizeof(u8));
230 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
232 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
236 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
238 return tps6598x_block_write(tps, reg, val, 4);
241 static int tps6598x_read_partner_identity(struct tps6598x *tps)
246 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
251 tps->partner_identity = id.identity;
256 static void tps6598x_set_data_role(struct tps6598x *tps,
269 usb_role_switch_set_role(tps->role_sw, role_val);
270 typec_set_data_role(tps->port, role);
273 static int tps6598x_connect(struct tps6598x *tps, u32 status)
279 if (tps->partner)
282 mode = TPS_POWER_STATUS_PWROPMODE(tps->pwr_status);
289 ret = tps6598x_read_partner_identity(tps);
292 desc.identity = &tps->partner_identity;
295 typec_set_pwr_opmode(tps->port, mode);
296 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
297 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
299 typec_set_orientation(tps->port, TYPEC_ORIENTATION_REVERSE);
301 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NORMAL);
302 typec_set_mode(tps->port, TYPEC_STATE_USB);
303 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
305 tps->partner = typec_register_partner(tps->port, &desc);
306 if (IS_ERR(tps->partner))
307 return PTR_ERR(tps->partner);
310 typec_partner_set_identity(tps->partner);
312 power_supply_changed(tps->psy);
317 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
319 if (!IS_ERR(tps->partner))
320 typec_unregister_partner(tps->partner);
321 tps->partner = NULL;
322 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
323 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
324 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
325 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NONE);
326 typec_set_mode(tps->port, TYPEC_STATE_SAFE);
327 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
329 power_supply_changed(tps->psy);
332 static int tps6598x_exec_cmd_tmo(struct tps6598x *tps, const char *cmd,
341 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
348 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
354 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
361 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
375 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
381 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
398 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
402 return tps6598x_exec_cmd_tmo(tps, cmd, in_len, in_data,
409 struct tps6598x *tps = typec_get_drvdata(port);
413 mutex_lock(&tps->lock);
415 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
419 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
428 tps6598x_set_data_role(tps, role, true);
431 mutex_unlock(&tps->lock);
439 struct tps6598x *tps = typec_get_drvdata(port);
443 mutex_lock(&tps->lock);
445 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
449 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
458 typec_set_pwr_role(tps->port, role);
461 mutex_unlock(&tps->lock);
471 static bool tps6598x_read_status(struct tps6598x *tps, u32 *status)
475 ret = tps6598x_read32(tps, TPS_REG_STATUS, status);
477 dev_err(tps->dev, "%s: failed to read status\n", __func__);
481 if (tps->data->trace_status)
482 tps->data->trace_status(*status);
487 static bool tps6598x_read_data_status(struct tps6598x *tps)
492 ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
494 dev_err(tps->dev, "failed to read data status: %d\n", ret);
502 static bool tps6598x_read_power_status(struct tps6598x *tps)
507 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
509 dev_err(tps->dev, "failed to read power status: %d\n", ret);
512 tps->pwr_status = pwr_status;
514 if (tps->data->trace_power_status)
515 tps->data->trace_power_status(pwr_status);
520 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status)
525 ret = tps6598x_connect(tps, status);
527 dev_err(tps->dev, "failed to register partner\n");
529 tps6598x_disconnect(tps, status);
535 struct tps6598x *tps = data;
540 mutex_lock(&tps->lock);
542 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event);
544 dev_err(tps->dev, "%s: failed to read events\n", __func__);
552 if (!tps6598x_read_status(tps, &status))
556 if (!tps6598x_read_power_status(tps))
560 if (!tps6598x_read_data_status(tps))
565 tps6598x_handle_plug_event(tps, status);
568 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event);
571 mutex_unlock(&tps->lock);
578 static bool tps6598x_has_role_changed(struct tps6598x *tps, u32 status)
580 status ^= tps->status;
587 struct tps6598x *tps = data;
592 mutex_lock(&tps->lock);
594 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event, 11);
596 dev_err(tps->dev, "%s: failed to read events\n", __func__);
604 if (!tps6598x_read_status(tps, &status))
608 if (!tps6598x_read_power_status(tps))
612 if (!tps6598x_read_data_status(tps))
621 tps6598x_has_role_changed(tps, status))
622 tps6598x_handle_plug_event(tps, status);
624 tps->status = status;
627 tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event, 11);
630 mutex_unlock(&tps->lock);
639 struct tps6598x *tps = data;
645 mutex_lock(&tps->lock);
647 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
648 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
650 dev_err(tps->dev, "%s: failed to read events\n", __func__);
658 if (!tps6598x_read_status(tps, &status))
662 if (!tps6598x_read_power_status(tps))
666 if (!tps6598x_read_data_status(tps))
671 tps6598x_handle_plug_event(tps, status);
674 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
675 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
678 mutex_unlock(&tps->lock);
689 struct tps6598x *tps = container_of(to_delayed_work(work),
692 tps->data->irq_handler(0, tps);
694 &tps->wq_poll, msecs_to_jiffies(POLL_INTERVAL));
697 static int tps6598x_check_mode(struct tps6598x *tps)
702 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
713 dev_warn(tps->dev, "dead-battery condition\n");
718 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
732 static int tps6598x_psy_get_online(struct tps6598x *tps,
735 if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) &&
736 TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) {
748 struct tps6598x *tps = power_supply_get_drvdata(psy);
753 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD)
759 ret = tps6598x_psy_get_online(tps, val);
769 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state)
774 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
781 ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL);
785 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
795 static int devm_tps6598_psy_register(struct tps6598x *tps)
798 const char *port_dev_name = dev_name(tps->dev);
801 psy_cfg.drv_data = tps;
802 psy_cfg.fwnode = dev_fwnode(tps->dev);
804 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
809 tps->psy_desc.name = psy_name;
810 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
811 tps->psy_desc.usb_types = tps6598x_psy_usb_types;
812 tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
813 tps->psy_desc.properties = tps6598x_psy_props;
814 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
815 tps->psy_desc.get_property = tps6598x_psy_get_prop;
817 tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
819 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
821 return PTR_ERR_OR_ZERO(tps->psy);
825 tps6598x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
831 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
838 typec_cap.driver_data = tps;
869 tps->port = typec_register_port(tps->dev, &typec_cap);
870 if (IS_ERR(tps->port))
871 return PTR_ERR(tps->port);
876 static int tps_request_firmware(struct tps6598x *tps, const struct firmware **fw)
881 ret = device_property_read_string(tps->dev, "firmware-name",
886 ret = request_firmware(fw, firmware_name, tps->dev);
888 dev_err(tps->dev, "failed to retrieve \"%s\"\n", firmware_name);
901 tps25750_write_firmware(struct tps6598x *tps,
904 struct i2c_client *client = to_i2c_client(tps->dev);
919 ret = regmap_raw_write(tps->regmap, data[0], &data[1], len - 1);
928 tps25750_exec_pbms(struct tps6598x *tps, u8 *in_data, size_t in_len)
933 ret = tps6598x_exec_cmd_tmo(tps, "PBMs", in_len, in_data,
940 dev_err(tps->dev, "%s: invalid fw size\n", __func__);
943 dev_err(tps->dev, "%s: invalid slave address\n", __func__);
946 dev_err(tps->dev, "%s: timed out\n", __func__);
955 static int tps25750_abort_patch_process(struct tps6598x *tps)
959 ret = tps6598x_exec_cmd(tps, "PBMe", 0, NULL, 0, NULL);
963 ret = tps6598x_check_mode(tps);
965 dev_err(tps->dev, "failed to switch to \"PTCH\" mode\n");
970 static int tps25750_start_patch_burst_mode(struct tps6598x *tps)
981 struct device_node *np = tps->dev->of_node;
983 ret = device_property_read_string(tps->dev, "firmware-name",
988 ret = tps_request_firmware(tps, &fw);
994 dev_err(tps->dev, "failed to get patch-address %d\n", ret);
1003 dev_err(tps->dev, "wrong patch address %u\n", addr);
1012 ret = tps25750_exec_pbms(tps, (u8 *)&bpms_data, sizeof(bpms_data));
1016 ret = tps25750_write_firmware(tps, bpms_data.addr, fw->data, fw->size);
1018 dev_err(tps->dev, "Failed to write patch %s of %zu bytes\n",
1037 static int tps25750_complete_patch_process(struct tps6598x *tps)
1047 ret = tps6598x_exec_cmd_tmo(tps, "PBMc", sizeof(dummy), dummy,
1053 dev_err(tps->dev,
1060 dev_err(tps->dev,
1069 static int tps25750_apply_patch(struct tps6598x *tps)
1075 ret = tps6598x_block_read(tps, TPS_REG_BOOT_STATUS, &status, 5);
1085 ret = tps25750_start_patch_burst_mode(tps);
1087 tps25750_abort_patch_process(tps);
1091 ret = tps25750_complete_patch_process(tps);
1099 ret = tps6598x_check_mode(tps);
1116 ret = tps6598x_exec_cmd(tps, "DBfg", 0, NULL, 0, NULL);
1118 dev_err(tps->dev, "failed to clear dead battery %d\n", ret);
1123 dev_info(tps->dev, "controller switched to \"APP\" mode\n");
1128 static int tps6598x_apply_patch(struct tps6598x *tps)
1139 ret = device_property_read_string(tps->dev, "firmware-name",
1144 ret = tps_request_firmware(tps, &fw);
1148 ret = tps6598x_exec_cmd(tps, "PTCs", in_len, &in,
1153 dev_err(tps->dev, "Update start failed (%d)\n", ret);
1163 ret = tps6598x_exec_cmd(tps, "PTCd", in_len,
1170 dev_err(tps->dev, "Patch download failed (%d)\n", ret);
1177 ret = tps6598x_exec_cmd(tps, "PTCc", 0, NULL, TPS_PTCC_OUT_BYTES, out);
1181 dev_err(tps->dev, "Update completion failed (%d)\n", ret);
1185 dev_info(tps->dev, "Firmware update succeeded\n");
1193 static int cd321x_init(struct tps6598x *tps)
1198 static int tps25750_init(struct tps6598x *tps)
1202 ret = tps->data->apply_patch(tps);
1206 ret = tps6598x_write8(tps, TPS_REG_SLEEP_CONF,
1209 dev_warn(tps->dev,
1216 static int tps6598x_init(struct tps6598x *tps)
1218 return tps->data->apply_patch(tps);
1221 static int cd321x_reset(struct tps6598x *tps)
1226 static int tps25750_reset(struct tps6598x *tps)
1228 return tps6598x_exec_cmd_tmo(tps, "GAID", 0, NULL, 0, NULL, 2000, 0);
1231 static int tps6598x_reset(struct tps6598x *tps)
1237 tps25750_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1244 ret = tps6598x_read8(tps, TPS_REG_PD_STATUS, &pd_status);
1250 dev_err(tps->dev, "data-role not found: %d\n", ret);
1256 dev_err(tps->dev, "unknown data-role: %s\n", data_role);
1263 typec_cap.driver_data = tps;
1283 tps->port = typec_register_port(tps->dev, &typec_cap);
1284 if (IS_ERR(tps->port))
1285 return PTR_ERR(tps->port);
1293 struct tps6598x *tps;
1300 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
1301 if (!tps)
1304 mutex_init(&tps->lock);
1305 tps->dev = &client->dev;
1307 tps->reset = devm_gpiod_get_optional(tps->dev, "reset", GPIOD_OUT_LOW);
1308 if (IS_ERR(tps->reset))
1309 return dev_err_probe(tps->dev, PTR_ERR(tps->reset),
1311 if (tps->reset)
1314 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
1315 if (IS_ERR(tps->regmap))
1316 return PTR_ERR(tps->regmap);
1318 if (!device_is_compatible(tps->dev, "ti,tps25750")) {
1319 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
1329 tps->i2c_protocol = true;
1333 ret = cd321x_switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0);
1349 if (dev_fwnode(tps->dev))
1350 tps->data = device_get_match_data(tps->dev);
1352 tps->data = i2c_get_match_data(client);
1353 if (!tps->data)
1357 ret = tps6598x_check_mode(tps);
1362 ret = tps->data->init(tps);
1367 ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, mask1);
1371 if (!tps6598x_read_status(tps, &status)) {
1387 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
1388 if (IS_ERR(tps->role_sw)) {
1389 ret = PTR_ERR(tps->role_sw);
1393 ret = devm_tps6598_psy_register(tps);
1397 ret = tps->data->register_port(tps, fwnode);
1402 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &tps->pwr_status);
1404 dev_err(tps->dev, "failed to read power status: %d\n", ret);
1407 ret = tps6598x_connect(tps, status);
1414 tps->data->irq_handler,
1416 dev_name(&client->dev), tps);
1418 dev_warn(tps->dev, "Unable to find the interrupt, switching to polling\n");
1419 INIT_DELAYED_WORK(&tps->wq_poll, tps6598x_poll_work);
1420 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
1427 i2c_set_clientdata(client, tps);
1430 tps->wakeup = device_property_read_bool(tps->dev, "wakeup-source");
1431 if (tps->wakeup && client->irq) {
1439 tps6598x_disconnect(tps, 0);
1441 typec_unregister_port(tps->port);
1443 usb_role_switch_put(tps->role_sw);
1447 tps6598x_write64(tps, TPS_REG_INT_MASK1, 0);
1450 tps->data->reset(tps);
1457 struct tps6598x *tps = i2c_get_clientdata(client);
1460 cancel_delayed_work_sync(&tps->wq_poll);
1462 devm_free_irq(tps->dev, client->irq, tps);
1463 tps6598x_disconnect(tps, 0);
1464 typec_unregister_port(tps->port);
1465 usb_role_switch_put(tps->role_sw);
1468 tps->data->reset(tps);
1470 if (tps->reset)
1471 gpiod_set_value_cansleep(tps->reset, 1);
1477 struct tps6598x *tps = i2c_get_clientdata(client);
1479 if (tps->wakeup) {
1482 } else if (tps->reset) {
1483 gpiod_set_value_cansleep(tps->reset, 1);
1487 cancel_delayed_work_sync(&tps->wq_poll);
1495 struct tps6598x *tps = i2c_get_clientdata(client);
1498 ret = tps6598x_check_mode(tps);
1503 ret = tps->data->init(tps);
1508 if (tps->wakeup) {
1511 } else if (tps->reset) {
1512 gpiod_set_value_cansleep(tps->reset, 0);
1517 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,