hc
2023-11-06 e3e12f52b214121840b44c91de5b3e5af5d3eb84
kernel/drivers/mfd/fusb302.c
....@@ -17,6 +17,8 @@
1717 #include <linux/of_gpio.h>
1818 #include <linux/regmap.h>
1919 #include <linux/power_supply.h>
20
+#include <linux/kthread.h>
21
+#include <uapi/linux/sched/types.h>
2022
2123 #include "fusb302.h"
2224
....@@ -188,12 +190,12 @@
188190 break;
189191 case 1:
190192 /* Battery */
191
- if ((CAP_VPDO_VOLTAGE(chip->rec_load[i]) * 50) <=
193
+ if ((CAP_VPDO_MAX_VOLTAGE(chip->rec_load[i]) * 50) <=
192194 max_vol &&
193195 (CAP_VPDO_CURRENT(chip->rec_load[i]) * 10) <=
194196 max_cur) {
195197 chip->pos_power = i + 1;
196
- tmp = CAP_VPDO_VOLTAGE(chip->rec_load[i]);
198
+ tmp = CAP_VPDO_MAX_VOLTAGE(chip->rec_load[i]);
197199 chip->pd_output_vol = tmp * 50;
198200 tmp = CAP_VPDO_CURRENT(chip->rec_load[i]);
199201 chip->pd_output_cur = tmp * 10;
....@@ -929,6 +931,7 @@
929931 static void set_mesg(struct fusb30x_chip *chip, int cmd, int is_DMT)
930932 {
931933 int i;
934
+ uint32_t rec_load;
932935 struct PD_CAP_INFO *pd_cap_info = &chip->pd_cap_info;
933936
934937 chip->send_head = ((chip->msg_id & 0x7) << 9) |
....@@ -962,23 +965,62 @@
962965 (0 << 25) |
963966 (0 << 24);
964967
965
- switch (CAP_POWER_TYPE(chip->rec_load[chip->pos_power - 1])) {
968
+ rec_load = chip->rec_load[chip->pos_power - 1];
969
+ switch (CAP_POWER_TYPE(rec_load)) {
966970 case 0:
967971 /* Fixed Supply */
968
- chip->send_load[0] |= ((CAP_FPDO_VOLTAGE(chip->rec_load[chip->pos_power - 1]) << 10) & 0x3ff);
969
- chip->send_load[0] |= (CAP_FPDO_CURRENT(chip->rec_load[chip->pos_power - 1]) & 0x3ff);
972
+ chip->sink_supply_type = 0;
973
+ chip->sink_volt = CAP_FPDO_VOLTAGE(rec_load);
974
+ chip->sink_opr_cur = CAP_FPDO_CURRENT(rec_load);
975
+ chip->send_load[0] |= chip->sink_volt << 10;
976
+ chip->send_load[0] |= chip->sink_opr_cur;
970977 break;
971978 case 1:
972
- /* Battery */
973
- chip->send_load[0] |= ((CAP_VPDO_VOLTAGE(chip->rec_load[chip->pos_power - 1]) << 10) & 0x3ff);
974
- chip->send_load[0] |= (CAP_VPDO_CURRENT(chip->rec_load[chip->pos_power - 1]) & 0x3ff);
979
+ /* Battery Supply */
980
+ chip->sink_supply_type = 1;
981
+ chip->sink_max_volt =
982
+ CAP_VPDO_MAX_VOLTAGE(rec_load);
983
+ chip->sink_min_volt =
984
+ CAP_VPDO_MIN_VOLTAGE(rec_load);
985
+ chip->sink_opr_power =
986
+ CAP_VPDO_CURRENT(rec_load);
987
+ chip->send_load[0] |= chip->sink_max_volt << 20;
988
+ chip->send_load[0] |= chip->sink_min_volt << 10;
989
+ chip->send_load[0] |= chip->sink_opr_power;
975990 break;
976991 default:
977
- /* not meet battery caps */
992
+ dev_warn(chip->dev, "No support supply req type %d\n",
993
+ CAP_POWER_TYPE(rec_load));
978994 break;
979995 }
980996 break;
981997 case DMT_SINKCAPABILITIES:
998
+ chip->send_head |= (1 << 12) | (cmd & 0xf);
999
+ switch (chip->sink_supply_type) {
1000
+ case 0:
1001
+ /*
1002
+ * Fixed Supply
1003
+ * bit26 for 'USB Communiications Capable'
1004
+ */
1005
+ chip->send_load[0] =
1006
+ (chip->sink_supply_type << 30) |
1007
+ (1 << 26) |
1008
+ (chip->sink_volt << 10) |
1009
+ (chip->sink_opr_cur);
1010
+ break;
1011
+ case 1:
1012
+ /* Battery Supply */
1013
+ chip->send_load[0] =
1014
+ (chip->sink_supply_type << 30) |
1015
+ (chip->sink_max_volt << 20) |
1016
+ (chip->sink_min_volt << 10) |
1017
+ (chip->sink_opr_cur);
1018
+ break;
1019
+ default:
1020
+ dev_warn(chip->dev, "No support sink supply type %d\n",
1021
+ chip->sink_supply_type);
1022
+ break;
1023
+ }
9821024 break;
9831025 case DMT_VENDERDEFINED:
9841026 break;
....@@ -2529,7 +2571,7 @@
25292571 break;
25302572 case 1:
25312573 /* Battery */
2532
- if (CAP_VPDO_VOLTAGE(chip->rec_load[tmp]) <= 100)
2574
+ if (CAP_VPDO_MAX_VOLTAGE(chip->rec_load[tmp]) <= 100)
25332575 chip->pos_power = tmp + 1;
25342576 break;
25352577 default:
....@@ -2620,6 +2662,14 @@
26202662 chip->notify.is_pd_connected = true;
26212663 dev_info(chip->dev,
26222664 "PD connected as UFP, fetching 5V\n");
2665
+ tcpm_get_message(chip);
2666
+ if (PACKET_IS_CONTROL_MSG(chip->rec_head,
2667
+ CMT_GETSINKCAP)) {
2668
+ set_mesg(chip, DMT_SINKCAPABILITIES,
2669
+ DATAMESSAGE);
2670
+ chip->tx_state = tx_idle;
2671
+ policy_send_data(chip);
2672
+ }
26232673 set_state(chip, policy_snk_ready);
26242674 } else if (PACKET_IS_DATA_MSG(chip->rec_head,
26252675 DMT_SOURCECAPABILITIES)) {
....@@ -2671,7 +2721,12 @@
26712721 static void fusb_state_snk_ready(struct fusb30x_chip *chip, u32 evt)
26722722 {
26732723 if (evt & EVENT_RX) {
2674
- if (PACKET_IS_DATA_MSG(chip->rec_head, DMT_VENDERDEFINED)) {
2724
+ if (PACKET_IS_CONTROL_MSG(chip->rec_head, CMT_GETSINKCAP)) {
2725
+ set_mesg(chip, DMT_SINKCAPABILITIES, DATAMESSAGE);
2726
+ chip->tx_state = tx_idle;
2727
+ policy_send_data(chip);
2728
+ } else if (PACKET_IS_DATA_MSG(chip->rec_head,
2729
+ DMT_VENDERDEFINED)) {
26752730 process_vdm_msg(chip);
26762731 chip->work_continue |= EVENT_WORK_CONTINUE;
26772732 chip->timer_state = T_DISABLED;
....@@ -3189,22 +3244,22 @@
31893244
31903245 BACK:
31913246 if (chip->work_continue) {
3192
- queue_work(chip->fusb30x_wq, &chip->work);
3247
+ kthread_queue_work(chip->irq_worker, &chip->irq_work);
31933248 return;
31943249 }
31953250
31963251 if (!platform_get_device_irq_state(chip))
31973252 fusb_irq_enable(chip);
31983253 else
3199
- queue_work(chip->fusb30x_wq, &chip->work);
3254
+ kthread_queue_work(chip->irq_worker, &chip->irq_work);
32003255 }
32013256
32023257 static irqreturn_t cc_interrupt_handler(int irq, void *dev_id)
32033258 {
32043259 struct fusb30x_chip *chip = dev_id;
32053260
3206
- queue_work(chip->fusb30x_wq, &chip->work);
32073261 fusb_irq_disable(chip);
3262
+ kthread_queue_work(chip->irq_worker, &chip->irq_work);
32083263 return IRQ_HANDLED;
32093264 }
32103265
....@@ -3266,8 +3321,8 @@
32663321 }
32673322
32683323 if (i != fusb30x_port_used)
3269
- queue_work(fusb30x_port_info[i]->fusb30x_wq,
3270
- &fusb30x_port_info[i]->work);
3324
+ kthread_queue_work(fusb30x_port_info[i]->irq_worker,
3325
+ &fusb30x_port_info[i]->irq_work);
32713326
32723327 return HRTIMER_NORESTART;
32733328 }
....@@ -3286,11 +3341,11 @@
32863341 chip->timer_mux = T_DISABLED;
32873342 }
32883343
3289
-static void fusb302_work_func(struct work_struct *work)
3344
+static void fusb302_work_func(struct kthread_work *work)
32903345 {
32913346 struct fusb30x_chip *chip;
32923347
3293
- chip = container_of(work, struct fusb30x_chip, work);
3348
+ chip = container_of(work, struct fusb30x_chip, irq_work);
32943349 if (!chip->suspended)
32953350 state_machine_typec(chip);
32963351 }
....@@ -3300,6 +3355,7 @@
33003355 {
33013356 struct fusb30x_chip *chip;
33023357 struct PD_CAP_INFO *pd_cap_info;
3358
+ struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
33033359 int ret;
33043360 char *string[2];
33053361
....@@ -3326,8 +3382,11 @@
33263382
33273383 fusb_initialize_timer(chip);
33283384
3329
- chip->fusb30x_wq = create_workqueue("fusb302_wq");
3330
- INIT_WORK(&chip->work, fusb302_work_func);
3385
+ chip->irq_worker = kthread_create_worker(0, dev_name(chip->dev));
3386
+ if (IS_ERR(chip->irq_worker))
3387
+ return PTR_ERR(chip->irq_worker);
3388
+ sched_setscheduler_nocheck(chip->irq_worker->task, SCHED_FIFO, &param);
3389
+ kthread_init_work(&chip->irq_work, fusb302_work_func);
33313390
33323391 chip->role = ROLE_MODE_NONE;
33333392 chip->try_role = ROLE_MODE_NONE;
....@@ -3368,6 +3427,8 @@
33683427 chip->n_caps_used = 1;
33693428 chip->source_power_supply[0] = 0x64;
33703429 chip->source_max_current[0] = 0x96;
3430
+ chip->sink_volt = 100;
3431
+ chip->sink_opr_cur = 200;
33713432
33723433 pd_cap_info = &chip->pd_cap_info;
33733434 pd_cap_info->dual_role_power = 1;
....@@ -3468,13 +3529,8 @@
34683529 goto IRQ_ERR;
34693530 }
34703531
3471
- ret = devm_request_threaded_irq(&client->dev,
3472
- chip->gpio_int_irq,
3473
- NULL,
3474
- cc_interrupt_handler,
3475
- IRQF_ONESHOT | IRQF_TRIGGER_LOW,
3476
- client->name,
3477
- chip);
3532
+ ret = request_irq(chip->gpio_int_irq, cc_interrupt_handler,
3533
+ IRQF_TRIGGER_LOW, "fsc_interrupt_int_n", chip);
34783534 if (ret) {
34793535 dev_err(&client->dev, "irq request failed\n");
34803536 goto IRQ_ERR;
....@@ -3503,7 +3559,7 @@
35033559 }
35043560 return 0;
35053561 IRQ_ERR:
3506
- destroy_workqueue(chip->fusb30x_wq);
3562
+ kthread_destroy_worker(chip->irq_worker);
35073563 return ret;
35083564 }
35093565
....@@ -3511,7 +3567,8 @@
35113567 {
35123568 struct fusb30x_chip *chip = i2c_get_clientdata(client);
35133569
3514
- destroy_workqueue(chip->fusb30x_wq);
3570
+ free_irq(chip->gpio_int_irq, chip);
3571
+ kthread_destroy_worker(chip->irq_worker);
35153572 return 0;
35163573 }
35173574
....@@ -3534,7 +3591,7 @@
35343591
35353592 fusb_irq_disable(chip);
35363593 chip->suspended = true;
3537
- cancel_work_sync(&chip->work);
3594
+ kthread_cancel_work_sync(&chip->irq_work);
35383595
35393596 return 0;
35403597 }
....@@ -3545,7 +3602,7 @@
35453602
35463603 fusb_irq_enable(chip);
35473604 chip->suspended = false;
3548
- queue_work(chip->fusb30x_wq, &chip->work);
3605
+ kthread_queue_work(chip->irq_worker, &chip->irq_work);
35493606
35503607 return 0;
35513608 }