1 // SPDX-License-Identifier: GPL-2.0-only
2
3 /*
4 * Driver for Analogix ANX7411 USB Type-C and PD controller
5 *
6 * Copyright(c) 2022, Analogix Semiconductor. All rights reserved.
7 *
8 */
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/iopoll.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of_graph.h>
17 #include <linux/of_platform.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <linux/usb/pd.h>
23 #include <linux/usb/role.h>
24 #include <linux/usb/tcpci.h>
25 #include <linux/usb/typec.h>
26 #include <linux/usb/typec_dp.h>
27 #include <linux/usb/typec_mux.h>
28 #include <linux/workqueue.h>
29 #include <linux/power_supply.h>
30
31 #define TCPC_ADDRESS1 0x58
32 #define TCPC_ADDRESS2 0x56
33 #define TCPC_ADDRESS3 0x54
34 #define TCPC_ADDRESS4 0x52
35 #define SPI_ADDRESS1 0x7e
36 #define SPI_ADDRESS2 0x6e
37 #define SPI_ADDRESS3 0x64
38 #define SPI_ADDRESS4 0x62
39
40 struct anx7411_i2c_select {
41 u8 tcpc_address;
42 u8 spi_address;
43 };
44
45 #define VID_ANALOGIX 0x1F29
46 #define PID_ANALOGIX 0x7411
47
48 /* TCPC register define */
49
50 #define ANALOG_CTRL_10 0xAA
51
52 #define STATUS_LEN 2
53 #define ALERT_0 0xCB
54 #define RECEIVED_MSG BIT(7)
55 #define SOFTWARE_INT BIT(6)
56 #define MSG_LEN 32
57 #define HEADER_LEN 2
58 #define MSG_HEADER 0x00
59 #define MSG_TYPE 0x01
60 #define MSG_RAWDATA 0x02
61 #define MSG_LEN_MASK 0x1F
62
63 #define ALERT_1 0xCC
64 #define INTP_POW_ON BIT(7)
65 #define INTP_POW_OFF BIT(6)
66
67 #define VBUS_THRESHOLD_H 0xDD
68 #define VBUS_THRESHOLD_L 0xDE
69
70 #define FW_CTRL_0 0xF0
71 #define UNSTRUCT_VDM_EN BIT(0)
72 #define DELAY_200MS BIT(1)
73 #define VSAFE0 0
74 #define VSAFE1 BIT(2)
75 #define VSAFE2 BIT(3)
76 #define VSAFE3 (BIT(2) | BIT(3))
77 #define FRS_EN BIT(7)
78
79 #define FW_PARAM 0xF1
80 #define DONGLE_IOP BIT(0)
81
82 #define FW_CTRL_2 0xF7
83 #define SINK_CTRL_DIS_FLAG BIT(5)
84
85 /* SPI register define */
86 #define OCM_CTRL_0 0x6E
87 #define OCM_RESET BIT(6)
88
89 #define MAX_VOLTAGE 0xAC
90 #define MAX_POWER 0xAD
91 #define MIN_POWER 0xAE
92
93 #define REQUEST_VOLTAGE 0xAF
94 #define VOLTAGE_UNIT 100 /* mV per unit */
95
96 #define REQUEST_CURRENT 0xB1
97 #define CURRENT_UNIT 50 /* mA per unit */
98
99 #define CMD_SEND_BUF 0xC0
100 #define CMD_RECV_BUF 0xE0
101
102 #define REQ_VOL_20V_IN_100MV 0xC8
103 #define REQ_CUR_2_25A_IN_50MA 0x2D
104 #define REQ_CUR_3_25A_IN_50MA 0x41
105
106 #define DEF_5V 5000
107 #define DEF_1_5A 1500
108
109 #define LOBYTE(w) ((u8)((w) & 0xFF))
110 #define HIBYTE(w) ((u8)(((u16)(w) >> 8) & 0xFF))
111
112 enum anx7411_typec_message_type {
113 TYPE_SRC_CAP = 0x00,
114 TYPE_SNK_CAP = 0x01,
115 TYPE_SNK_IDENTITY = 0x02,
116 TYPE_SVID = 0x03,
117 TYPE_SET_SNK_DP_CAP = 0x08,
118 TYPE_PSWAP_REQ = 0x10,
119 TYPE_DSWAP_REQ = 0x11,
120 TYPE_VDM = 0x14,
121 TYPE_OBJ_REQ = 0x16,
122 TYPE_DP_ALT_ENTER = 0x19,
123 TYPE_DP_DISCOVER_MODES_INFO = 0x27,
124 TYPE_GET_DP_CONFIG = 0x29,
125 TYPE_DP_CONFIGURE = 0x2A,
126 TYPE_GET_DP_DISCOVER_MODES_INFO = 0x2E,
127 TYPE_GET_DP_ALT_ENTER = 0x2F,
128 };
129
130 #define FW_CTRL_1 0xB2
131 #define AUTO_PD_EN BIT(1)
132 #define TRYSRC_EN BIT(2)
133 #define TRYSNK_EN BIT(3)
134 #define FORCE_SEND_RDO BIT(6)
135
136 #define FW_VER 0xB4
137 #define FW_SUBVER 0xB5
138
139 #define INT_MASK 0xB6
140 #define INT_STS 0xB7
141 #define OCM_BOOT_UP BIT(0)
142 #define OC_OV_EVENT BIT(1)
143 #define VCONN_CHANGE BIT(2)
144 #define VBUS_CHANGE BIT(3)
145 #define CC_STATUS_CHANGE BIT(4)
146 #define DATA_ROLE_CHANGE BIT(5)
147 #define PR_CONSUMER_GOT_POWER BIT(6)
148 #define HPD_STATUS_CHANGE BIT(7)
149
150 #define SYSTEM_STSTUS 0xB8
151 /* 0: SINK off; 1: SINK on */
152 #define SINK_STATUS BIT(1)
153 /* 0: VCONN off; 1: VCONN on*/
154 #define VCONN_STATUS BIT(2)
155 /* 0: vbus off; 1: vbus on*/
156 #define VBUS_STATUS BIT(3)
157 /* 1: host; 0:device*/
158 #define DATA_ROLE BIT(5)
159 /* 0: Chunking; 1: Unchunked*/
160 #define SUPPORT_UNCHUNKING BIT(6)
161 /* 0: HPD low; 1: HPD high*/
162 #define HPD_STATUS BIT(7)
163
164 #define DATA_DFP 1
165 #define DATA_UFP 2
166 #define POWER_SOURCE 1
167 #define POWER_SINK 2
168
169 #define CC_STATUS 0xB9
170 #define CC1_RD BIT(0)
171 #define CC2_RD BIT(4)
172 #define CC1_RA BIT(1)
173 #define CC2_RA BIT(5)
174 #define CC1_RD BIT(0)
175 #define CC1_RP(cc) (((cc) >> 2) & 0x03)
176 #define CC2_RP(cc) (((cc) >> 6) & 0x03)
177
178 #define PD_REV_INIT 0xBA
179
180 #define PD_EXT_MSG_CTRL 0xBB
181 #define SRC_CAP_EXT_REPLY BIT(0)
182 #define MANUFACTURER_INFO_REPLY BIT(1)
183 #define BATTERY_STS_REPLY BIT(2)
184 #define BATTERY_CAP_REPLY BIT(3)
185 #define ALERT_REPLY BIT(4)
186 #define STATUS_REPLY BIT(5)
187 #define PPS_STATUS_REPLY BIT(6)
188 #define SNK_CAP_EXT_REPLY BIT(7)
189
190 #define NO_CONNECT 0x00
191 #define USB3_1_CONNECTED 0x01
192 #define DP_ALT_4LANES 0x02
193 #define USB3_1_DP_2LANES 0x03
194 #define CC1_CONNECTED 0x01
195 #define CC2_CONNECTED 0x02
196 #define SELECT_PIN_ASSIGMENT_C 0x04
197 #define SELECT_PIN_ASSIGMENT_D 0x08
198 #define SELECT_PIN_ASSIGMENT_E 0x10
199 #define SELECT_PIN_ASSIGMENT_U 0x00
200 #define REDRIVER_ADDRESS 0x20
201 #define REDRIVER_OFFSET 0x00
202
203 #define DP_SVID 0xFF01
204 #define VDM_ACK 0x40
205 #define VDM_CMD_RES 0x00
206 #define VDM_CMD_DIS_ID 0x01
207 #define VDM_CMD_DIS_SVID 0x02
208 #define VDM_CMD_DIS_MOD 0x03
209 #define VDM_CMD_ENTER_MODE 0x04
210 #define VDM_CMD_EXIT_MODE 0x05
211 #define VDM_CMD_ATTENTION 0x06
212 #define VDM_CMD_GET_STS 0x10
213 #define VDM_CMD_AND_ACK_MASK 0x5F
214
215 #define MAX_ALTMODE 2
216
217 #define HAS_SOURCE_CAP BIT(0)
218 #define HAS_SINK_CAP BIT(1)
219 #define HAS_SINK_WATT BIT(2)
220
221 enum anx7411_psy_state {
222 /* copy from drivers/usb/typec/tcpm */
223 ANX7411_PSY_OFFLINE = 0,
224 ANX7411_PSY_FIXED_ONLINE,
225
226 /* private */
227 /* PD keep in, but disconnct power to bq25700,
228 * this state can be active when higher capacity adapter plug in,
229 * and change to ONLINE state when higher capacity adapter plug out
230 */
231 ANX7411_PSY_HANG = 0xff,
232 };
233
234 struct typec_params {
235 int request_current; /* ma */
236 int request_voltage; /* mv */
237 int cc_connect;
238 int cc_orientation_valid;
239 int cc_status;
240 int data_role;
241 int power_role;
242 int vconn_role;
243 int dp_altmode_enter;
244 int cust_altmode_enter;
245 struct usb_role_switch *role_sw;
246 struct typec_port *port;
247 struct typec_partner *partner;
248 struct typec_mux_dev *typec_mux;
249 struct typec_switch_dev *typec_switch;
250 struct typec_altmode *amode[MAX_ALTMODE];
251 struct typec_altmode *port_amode[MAX_ALTMODE];
252 struct typec_displayport_data data;
253 int pin_assignment;
254 struct typec_capability caps;
255 u32 src_pdo[PDO_MAX_OBJECTS];
256 u32 sink_pdo[PDO_MAX_OBJECTS];
257 u8 caps_flags;
258 u8 src_pdo_nr;
259 u8 sink_pdo_nr;
260 u8 sink_watt;
261 u8 sink_voltage;
262 };
263
264 #define MAX_BUF_LEN 30
265 struct fw_msg {
266 u8 msg_len;
267 u8 msg_type;
268 u8 buf[MAX_BUF_LEN];
269 } __packed;
270
271 struct anx7411_data {
272 int fw_version;
273 int fw_subversion;
274 struct i2c_client *tcpc_client;
275 struct i2c_client *spi_client;
276 struct fw_msg send_msg;
277 struct fw_msg recv_msg;
278 struct gpio_desc *intp_gpiod;
279 struct fwnode_handle *connector_fwnode;
280 struct typec_params typec;
281 int intp_irq;
282 struct work_struct work;
283 struct workqueue_struct *workqueue;
284 /* Lock for interrupt work queue */
285 struct mutex lock;
286
287 enum anx7411_psy_state psy_online;
288 enum power_supply_usb_type usb_type;
289 struct power_supply *psy;
290 struct power_supply_desc psy_desc;
291 struct device *dev;
292 struct fwnode_handle *switch_node;
293 struct fwnode_handle *mux_node;
294 };
295
296 static u8 snk_identity[] = {
297 LOBYTE(VID_ANALOGIX), HIBYTE(VID_ANALOGIX), 0x00, 0x82, /* snk_id_hdr */
298 0x00, 0x00, 0x00, 0x00, /* snk_cert */
299 0x00, 0x00, LOBYTE(PID_ANALOGIX), HIBYTE(PID_ANALOGIX), /* 5snk_ama */
300 };
301
302 static u8 dp_caps[4] = {0xC6, 0x00, 0x00, 0x00};
303
anx7411_reg_read(struct i2c_client * client,u8 reg_addr)304 static int anx7411_reg_read(struct i2c_client *client,
305 u8 reg_addr)
306 {
307 return i2c_smbus_read_byte_data(client, reg_addr);
308 }
309
anx7411_reg_block_read(struct i2c_client * client,u8 reg_addr,u8 len,u8 * buf)310 static int anx7411_reg_block_read(struct i2c_client *client,
311 u8 reg_addr, u8 len, u8 *buf)
312 {
313 return i2c_smbus_read_i2c_block_data(client, reg_addr, len, buf);
314 }
315
anx7411_reg_write(struct i2c_client * client,u8 reg_addr,u8 reg_val)316 static int anx7411_reg_write(struct i2c_client *client,
317 u8 reg_addr, u8 reg_val)
318 {
319 return i2c_smbus_write_byte_data(client, reg_addr, reg_val);
320 }
321
anx7411_reg_block_write(struct i2c_client * client,u8 reg_addr,u8 len,u8 * buf)322 static int anx7411_reg_block_write(struct i2c_client *client,
323 u8 reg_addr, u8 len, u8 *buf)
324 {
325 return i2c_smbus_write_i2c_block_data(client, reg_addr, len, buf);
326 }
327
328 static struct anx7411_i2c_select anx7411_i2c_addr[] = {
329 {TCPC_ADDRESS1, SPI_ADDRESS1},
330 {TCPC_ADDRESS2, SPI_ADDRESS2},
331 {TCPC_ADDRESS3, SPI_ADDRESS3},
332 {TCPC_ADDRESS4, SPI_ADDRESS4},
333 };
334
anx7411_detect_power_mode(struct anx7411_data * ctx)335 static int anx7411_detect_power_mode(struct anx7411_data *ctx)
336 {
337 int ret;
338 int mode;
339
340 ret = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT);
341 if (ret < 0)
342 return ret;
343
344 ctx->typec.request_current = ret * CURRENT_UNIT; /* 50ma per unit */
345
346 ret = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE);
347 if (ret < 0)
348 return ret;
349
350 ctx->typec.request_voltage = ret * VOLTAGE_UNIT; /* 100mv per unit */
351
352 if (ctx->psy_online == ANX7411_PSY_OFFLINE) {
353 ctx->psy_online = ANX7411_PSY_FIXED_ONLINE;
354 ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD;
355 power_supply_changed(ctx->psy);
356 }
357
358 if (!ctx->typec.cc_orientation_valid)
359 return 0;
360
361 if (ctx->typec.cc_connect == CC1_CONNECTED)
362 mode = CC1_RP(ctx->typec.cc_status);
363 else
364 mode = CC2_RP(ctx->typec.cc_status);
365 if (mode) {
366 typec_set_pwr_opmode(ctx->typec.port, mode - 1);
367 return 0;
368 }
369
370 typec_set_pwr_opmode(ctx->typec.port, TYPEC_PWR_MODE_PD);
371
372 return 0;
373 }
374
anx7411_register_partner(struct anx7411_data * ctx,int pd,int accessory)375 static int anx7411_register_partner(struct anx7411_data *ctx,
376 int pd, int accessory)
377 {
378 struct typec_partner_desc desc;
379 struct typec_partner *partner;
380
381 if (ctx->typec.partner)
382 return 0;
383
384 desc.usb_pd = pd;
385 desc.accessory = accessory;
386 desc.identity = NULL;
387 partner = typec_register_partner(ctx->typec.port, &desc);
388 if (IS_ERR(partner))
389 return PTR_ERR(partner);
390
391 ctx->typec.partner = partner;
392
393 return 0;
394 }
395
anx7411_detect_cc_orientation(struct anx7411_data * ctx)396 static int anx7411_detect_cc_orientation(struct anx7411_data *ctx)
397 {
398 struct device *dev = &ctx->spi_client->dev;
399 int ret;
400 int cc1_rd, cc2_rd;
401 int cc1_ra, cc2_ra;
402 int cc1_rp, cc2_rp;
403
404 ret = anx7411_reg_read(ctx->spi_client, CC_STATUS);
405 if (ret < 0)
406 return ret;
407
408 ctx->typec.cc_status = ret;
409
410 cc1_rd = ret & CC1_RD ? 1 : 0;
411 cc2_rd = ret & CC2_RD ? 1 : 0;
412 cc1_ra = ret & CC1_RA ? 1 : 0;
413 cc2_ra = ret & CC2_RA ? 1 : 0;
414 cc1_rp = CC1_RP(ret);
415 cc2_rp = CC2_RP(ret);
416
417 /* Debug cable, nothing to do */
418 if (cc1_rd && cc2_rd) {
419 ctx->typec.cc_orientation_valid = 0;
420 return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_DEBUG);
421 }
422
423 if (cc1_ra && cc2_ra) {
424 ctx->typec.cc_orientation_valid = 0;
425 return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_AUDIO);
426 }
427
428 ctx->typec.cc_orientation_valid = 1;
429
430 ret = anx7411_register_partner(ctx, 1, TYPEC_ACCESSORY_NONE);
431 if (ret) {
432 dev_err(dev, "register partner\n");
433 return ret;
434 }
435
436 if (cc1_rd || cc1_rp) {
437 typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_NORMAL);
438 ctx->typec.cc_connect = CC1_CONNECTED;
439 }
440
441 if (cc2_rd || cc2_rp) {
442 typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_REVERSE);
443 ctx->typec.cc_connect = CC2_CONNECTED;
444 }
445
446 return 0;
447 }
448
anx7411_set_mux(struct anx7411_data * ctx,int pin_assignment)449 static int anx7411_set_mux(struct anx7411_data *ctx, int pin_assignment)
450 {
451 int mode = TYPEC_STATE_SAFE;
452
453 switch (pin_assignment) {
454 case SELECT_PIN_ASSIGMENT_U:
455 /* default 4 line USB 3.1 */
456 mode = TYPEC_STATE_MODAL;
457 break;
458 case SELECT_PIN_ASSIGMENT_C:
459 case SELECT_PIN_ASSIGMENT_E:
460 /* 4 line DP */
461 mode = TYPEC_STATE_SAFE;
462 break;
463 case SELECT_PIN_ASSIGMENT_D:
464 /* 2 line DP, 2 line USB */
465 mode = TYPEC_MODE_USB3;
466 break;
467 default:
468 mode = TYPEC_STATE_SAFE;
469 break;
470 }
471
472 ctx->typec.pin_assignment = pin_assignment;
473
474 return typec_set_mode(ctx->typec.port, mode);
475 }
476
anx7411_set_usb_role(struct anx7411_data * ctx,enum usb_role role)477 static int anx7411_set_usb_role(struct anx7411_data *ctx, enum usb_role role)
478 {
479 if (!ctx->typec.role_sw)
480 return 0;
481
482 return usb_role_switch_set_role(ctx->typec.role_sw, role);
483 }
484
anx7411_data_role_detect(struct anx7411_data * ctx)485 static int anx7411_data_role_detect(struct anx7411_data *ctx)
486 {
487 int ret;
488
489 ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS);
490 if (ret < 0)
491 return ret;
492
493 ctx->typec.data_role = (ret & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE;
494 ctx->typec.vconn_role = (ret & VCONN_STATUS) ? TYPEC_SOURCE : TYPEC_SINK;
495
496 typec_set_data_role(ctx->typec.port, ctx->typec.data_role);
497
498 typec_set_vconn_role(ctx->typec.port, ctx->typec.vconn_role);
499
500 if (ctx->typec.data_role == TYPEC_HOST)
501 return anx7411_set_usb_role(ctx, USB_ROLE_HOST);
502
503 return anx7411_set_usb_role(ctx, USB_ROLE_DEVICE);
504 }
505
anx7411_power_role_detect(struct anx7411_data * ctx)506 static int anx7411_power_role_detect(struct anx7411_data *ctx)
507 {
508 int ret;
509
510 ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS);
511 if (ret < 0)
512 return ret;
513
514 ctx->typec.power_role = (ret & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE;
515
516 if (ctx->typec.power_role == TYPEC_SOURCE) {
517 ctx->typec.request_current = DEF_1_5A;
518 ctx->typec.request_voltage = DEF_5V;
519 }
520
521 typec_set_pwr_role(ctx->typec.port, ctx->typec.power_role);
522
523 return 0;
524 }
525
anx7411_cc_status_detect(struct anx7411_data * ctx)526 static int anx7411_cc_status_detect(struct anx7411_data *ctx)
527 {
528 anx7411_detect_cc_orientation(ctx);
529 anx7411_detect_power_mode(ctx);
530
531 return 0;
532 }
533
anx7411_partner_unregister_altmode(struct anx7411_data * ctx)534 static void anx7411_partner_unregister_altmode(struct anx7411_data *ctx)
535 {
536 int i;
537
538 ctx->typec.dp_altmode_enter = 0;
539 ctx->typec.cust_altmode_enter = 0;
540
541 for (i = 0; i < MAX_ALTMODE; i++)
542 if (ctx->typec.amode[i]) {
543 typec_unregister_altmode(ctx->typec.amode[i]);
544 ctx->typec.amode[i] = NULL;
545 }
546
547 ctx->typec.pin_assignment = 0;
548 }
549
anx7411_typec_register_altmode(struct anx7411_data * ctx,int svid,int vdo)550 static int anx7411_typec_register_altmode(struct anx7411_data *ctx,
551 int svid, int vdo)
552 {
553 struct device *dev = &ctx->spi_client->dev;
554 struct typec_altmode_desc desc;
555 int err;
556 int i;
557
558 desc.svid = svid;
559 desc.vdo = vdo;
560
561 for (i = 0; i < MAX_ALTMODE; i++)
562 if (!ctx->typec.amode[i])
563 break;
564
565 desc.mode = i + 1; /* start with 1 */
566
567 if (i >= MAX_ALTMODE) {
568 dev_err(dev, "no altmode space for registering\n");
569 return -ENOMEM;
570 }
571
572 ctx->typec.amode[i] = typec_partner_register_altmode(ctx->typec.partner,
573 &desc);
574 if (IS_ERR(ctx->typec.amode[i])) {
575 dev_err(dev, "failed to register altmode\n");
576 err = PTR_ERR(ctx->typec.amode[i]);
577 ctx->typec.amode[i] = NULL;
578 return err;
579 }
580
581 return 0;
582 }
583
anx7411_unregister_partner(struct anx7411_data * ctx)584 static void anx7411_unregister_partner(struct anx7411_data *ctx)
585 {
586 if (ctx->typec.partner) {
587 typec_unregister_partner(ctx->typec.partner);
588 ctx->typec.partner = NULL;
589 }
590 }
591
anx7411_update_altmode(struct anx7411_data * ctx,int svid)592 static int anx7411_update_altmode(struct anx7411_data *ctx, int svid)
593 {
594 int i;
595
596 if (svid == DP_SVID)
597 ctx->typec.dp_altmode_enter = 1;
598 else
599 ctx->typec.cust_altmode_enter = 1;
600
601 for (i = 0; i < MAX_ALTMODE; i++) {
602 if (!ctx->typec.amode[i])
603 continue;
604
605 if (ctx->typec.amode[i]->svid == svid) {
606 typec_altmode_update_active(ctx->typec.amode[i], true);
607 typec_altmode_notify(ctx->typec.amode[i],
608 ctx->typec.pin_assignment,
609 &ctx->typec.data);
610 break;
611 }
612 }
613
614 return 0;
615 }
616
anx7411_register_altmode(struct anx7411_data * ctx,bool dp_altmode,u8 * buf)617 static int anx7411_register_altmode(struct anx7411_data *ctx,
618 bool dp_altmode, u8 *buf)
619 {
620 int ret;
621 int svid;
622 int mid;
623
624 if (!ctx->typec.partner)
625 return 0;
626
627 svid = DP_SVID;
628 if (dp_altmode) {
629 mid = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
630
631 return anx7411_typec_register_altmode(ctx, svid, mid);
632 }
633
634 svid = (buf[3] << 8) | buf[2];
635 if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_ENTER_MODE))
636 return anx7411_update_altmode(ctx, svid);
637
638 if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_DIS_MOD))
639 return 0;
640
641 mid = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
642
643 ret = anx7411_typec_register_altmode(ctx, svid, mid);
644 if (ctx->typec.cust_altmode_enter)
645 ret |= anx7411_update_altmode(ctx, svid);
646
647 return ret;
648 }
649
anx7411_parse_cmd(struct anx7411_data * ctx,u8 type,u8 * buf,u8 len)650 static int anx7411_parse_cmd(struct anx7411_data *ctx, u8 type, u8 *buf, u8 len)
651 {
652 struct device *dev = &ctx->spi_client->dev;
653 u8 cur_50ma, vol_100mv;
654
655 switch (type) {
656 case TYPE_SRC_CAP:
657 cur_50ma = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT);
658 vol_100mv = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE);
659
660 ctx->typec.request_voltage = vol_100mv * VOLTAGE_UNIT;
661 ctx->typec.request_current = cur_50ma * CURRENT_UNIT;
662
663 ctx->psy_online = ANX7411_PSY_FIXED_ONLINE;
664 ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD;
665 power_supply_changed(ctx->psy);
666 break;
667 case TYPE_SNK_CAP:
668 break;
669 case TYPE_SVID:
670 break;
671 case TYPE_SNK_IDENTITY:
672 break;
673 case TYPE_GET_DP_ALT_ENTER:
674 /* DP alt mode enter success */
675 if (buf[0])
676 anx7411_update_altmode(ctx, DP_SVID);
677 break;
678 case TYPE_DP_ALT_ENTER:
679 /* Update DP altmode */
680 anx7411_update_altmode(ctx, DP_SVID);
681 break;
682 case TYPE_OBJ_REQ:
683 anx7411_detect_power_mode(ctx);
684 break;
685 case TYPE_DP_CONFIGURE:
686 anx7411_set_mux(ctx, buf[1]);
687 break;
688 case TYPE_DP_DISCOVER_MODES_INFO:
689 /* Make sure discover modes valid */
690 if (buf[0] | buf[1])
691 /* Register DP Altmode */
692 anx7411_register_altmode(ctx, 1, buf);
693 break;
694 case TYPE_VDM:
695 /* Register other altmode */
696 anx7411_register_altmode(ctx, 0, buf);
697 break;
698 default:
699 dev_err(dev, "ignore message(0x%.02x).\n", type);
700 break;
701 }
702
703 return 0;
704 }
705
checksum(struct device * dev,u8 * buf,u8 len)706 static u8 checksum(struct device *dev, u8 *buf, u8 len)
707 {
708 u8 ret = 0;
709 u8 i;
710
711 for (i = 0; i < len; i++)
712 ret += buf[i];
713
714 return ret;
715 }
716
anx7411_read_msg_ctrl_status(struct i2c_client * client)717 static int anx7411_read_msg_ctrl_status(struct i2c_client *client)
718 {
719 return anx7411_reg_read(client, CMD_SEND_BUF);
720 }
721
anx7411_wait_msg_empty(struct i2c_client * client)722 static int anx7411_wait_msg_empty(struct i2c_client *client)
723 {
724 int val;
725
726 return readx_poll_timeout(anx7411_read_msg_ctrl_status,
727 client, val, (val < 0) || (val == 0),
728 2000, 2000 * 150);
729 }
730
anx7411_send_msg(struct anx7411_data * ctx,u8 type,u8 * buf,u8 size)731 static int anx7411_send_msg(struct anx7411_data *ctx, u8 type, u8 *buf, u8 size)
732 {
733 struct device *dev = &ctx->spi_client->dev;
734 struct fw_msg *msg = &ctx->send_msg;
735 u8 crc;
736 int ret;
737
738 size = min_t(u8, size, (u8)MAX_BUF_LEN);
739 memcpy(msg->buf, buf, size);
740 msg->msg_type = type;
741 /* msg len equals buffer length + msg_type */
742 msg->msg_len = size + 1;
743
744 /* Do CRC check for all buffer data and msg_len and msg_type */
745 crc = checksum(dev, (u8 *)msg, size + HEADER_LEN);
746 msg->buf[size] = 0 - crc;
747
748 ret = anx7411_wait_msg_empty(ctx->spi_client);
749 if (ret)
750 return ret;
751
752 ret = anx7411_reg_block_write(ctx->spi_client,
753 CMD_SEND_BUF + 1, size + HEADER_LEN,
754 &msg->msg_type);
755 ret |= anx7411_reg_write(ctx->spi_client, CMD_SEND_BUF,
756 msg->msg_len);
757 return ret;
758 }
759
anx7411_process_cmd(struct anx7411_data * ctx)760 static int anx7411_process_cmd(struct anx7411_data *ctx)
761 {
762 struct device *dev = &ctx->spi_client->dev;
763 struct fw_msg *msg = &ctx->recv_msg;
764 u8 len;
765 u8 crc;
766 int ret;
767
768 /* Read message from firmware */
769 ret = anx7411_reg_block_read(ctx->spi_client, CMD_RECV_BUF,
770 MSG_LEN, (u8 *)msg);
771 if (ret < 0)
772 return 0;
773
774 if (!msg->msg_len)
775 return 0;
776
777 ret = anx7411_reg_write(ctx->spi_client, CMD_RECV_BUF, 0);
778 if (ret)
779 return ret;
780
781 len = msg->msg_len & MSG_LEN_MASK;
782 crc = checksum(dev, (u8 *)msg, len + HEADER_LEN);
783 if (crc) {
784 dev_err(dev, "message error crc(0x%.02x)\n", crc);
785 return -ERANGE;
786 }
787
788 return anx7411_parse_cmd(ctx, msg->msg_type, msg->buf, len - 1);
789 }
790
anx7411_translate_payload(struct device * dev,__le32 * payload,u32 * pdo,int nr,const char * type)791 static void anx7411_translate_payload(struct device *dev, __le32 *payload,
792 u32 *pdo, int nr, const char *type)
793 {
794 int i;
795
796 if (nr > PDO_MAX_OBJECTS) {
797 dev_err(dev, "nr(%d) exceed PDO_MAX_OBJECTS(%d)\n",
798 nr, PDO_MAX_OBJECTS);
799
800 return;
801 }
802
803 for (i = 0; i < nr; i++)
804 payload[i] = cpu_to_le32(pdo[i]);
805 }
806
anx7411_config(struct anx7411_data * ctx)807 static int anx7411_config(struct anx7411_data *ctx)
808 {
809 struct device *dev = &ctx->spi_client->dev;
810 struct typec_params *typecp = &ctx->typec;
811 __le32 payload[PDO_MAX_OBJECTS];
812 int ret;
813
814 /* Config PD FW work under PD 2.0 */
815 ret = anx7411_reg_write(ctx->spi_client, PD_REV_INIT, PD_REV20);
816 ret |= anx7411_reg_write(ctx->tcpc_client, FW_CTRL_0,
817 UNSTRUCT_VDM_EN | DELAY_200MS |
818 VSAFE1 | FRS_EN);
819 ret |= anx7411_reg_write(ctx->spi_client, FW_CTRL_1,
820 AUTO_PD_EN | FORCE_SEND_RDO);
821
822 /* Set VBUS current threshold */
823 ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_H, 0xff);
824 ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_L, 0x03);
825
826 /* Fix dongle compatible issue */
827 ret |= anx7411_reg_write(ctx->tcpc_client, FW_PARAM,
828 anx7411_reg_read(ctx->tcpc_client, FW_PARAM) |
829 DONGLE_IOP);
830 ret |= anx7411_reg_write(ctx->spi_client, INT_MASK, 0);
831
832 ret |= anx7411_reg_write(ctx->spi_client, PD_EXT_MSG_CTRL, 0xFF);
833 if (ret)
834 return ret;
835
836 if (typecp->caps_flags & HAS_SOURCE_CAP) {
837 anx7411_translate_payload(dev, payload, typecp->src_pdo,
838 typecp->src_pdo_nr, "source");
839 anx7411_send_msg(ctx, TYPE_SRC_CAP, (u8 *)&payload,
840 typecp->src_pdo_nr * 4);
841 anx7411_send_msg(ctx, TYPE_SNK_IDENTITY, snk_identity,
842 sizeof(snk_identity));
843 anx7411_send_msg(ctx, TYPE_SET_SNK_DP_CAP, dp_caps,
844 sizeof(dp_caps));
845 }
846
847 if (typecp->caps_flags & HAS_SINK_CAP) {
848 anx7411_translate_payload(dev, payload, typecp->sink_pdo,
849 typecp->sink_pdo_nr, "sink");
850 anx7411_send_msg(ctx, TYPE_SNK_CAP, (u8 *)&payload,
851 typecp->sink_pdo_nr * 4);
852 }
853
854 if (typecp->caps_flags & HAS_SINK_WATT) {
855 if (typecp->sink_watt) {
856 ret |= anx7411_reg_write(ctx->spi_client, MAX_POWER,
857 typecp->sink_watt);
858 /* Set min power to 1W */
859 ret |= anx7411_reg_write(ctx->spi_client, MIN_POWER, 2);
860 }
861
862 if (typecp->sink_voltage)
863 ret |= anx7411_reg_write(ctx->spi_client, MAX_VOLTAGE,
864 typecp->sink_voltage);
865 if (ret)
866 return ret;
867 }
868
869 if (!typecp->caps_flags)
870 usleep_range(5000, 6000);
871
872 ctx->fw_version = anx7411_reg_read(ctx->spi_client, FW_VER);
873 ctx->fw_subversion = anx7411_reg_read(ctx->spi_client, FW_SUBVER);
874
875 return 0;
876 }
877
anx7411_chip_standby(struct anx7411_data * ctx)878 static void anx7411_chip_standby(struct anx7411_data *ctx)
879 {
880 int ret;
881 u8 cc1, cc2;
882 struct device *dev = &ctx->spi_client->dev;
883
884 ret = anx7411_reg_write(ctx->spi_client, OCM_CTRL_0,
885 anx7411_reg_read(ctx->spi_client, OCM_CTRL_0) |
886 OCM_RESET);
887 ret |= anx7411_reg_write(ctx->tcpc_client, ANALOG_CTRL_10, 0x80);
888 /* Set TCPC to RD and DRP enable */
889 cc1 = TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT;
890 cc2 = TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT;
891 ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_ROLE_CTRL,
892 TCPC_ROLE_CTRL_DRP | cc1 | cc2);
893
894 /* Send DRP toggle command */
895 ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_COMMAND,
896 TCPC_CMD_LOOK4CONNECTION);
897
898 /* Send TCPC enter standby command */
899 ret |= anx7411_reg_write(ctx->tcpc_client,
900 TCPC_COMMAND, TCPC_CMD_I2C_IDLE);
901 if (ret)
902 dev_err(dev, "Chip standby failed\n");
903 }
904
anx7411_work_func(struct work_struct * work)905 static void anx7411_work_func(struct work_struct *work)
906 {
907 int ret;
908 u8 buf[STATUS_LEN];
909 u8 int_change; /* Interrupt change */
910 u8 int_status; /* Firmware status update */
911 u8 alert0, alert1; /* Interrupt alert source */
912 struct anx7411_data *ctx = container_of(work, struct anx7411_data, work);
913 struct device *dev = &ctx->spi_client->dev;
914
915 mutex_lock(&ctx->lock);
916
917 /* Read interrupt change status */
918 ret = anx7411_reg_block_read(ctx->spi_client, INT_STS, STATUS_LEN, buf);
919 if (ret < 0) {
920 /* Power standby mode, just return */
921 goto unlock;
922 }
923 int_change = buf[0];
924 int_status = buf[1];
925
926 /* Read alert register */
927 ret = anx7411_reg_block_read(ctx->tcpc_client, ALERT_0, STATUS_LEN, buf);
928 if (ret < 0)
929 goto unlock;
930
931 alert0 = buf[0];
932 alert1 = buf[1];
933
934 /* Clear interrupt and alert status */
935 ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0);
936 ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, alert0);
937 ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, alert1);
938 if (ret)
939 goto unlock;
940
941 if (alert1 & INTP_POW_OFF) {
942 anx7411_partner_unregister_altmode(ctx);
943 if (anx7411_set_usb_role(ctx, USB_ROLE_NONE))
944 dev_err(dev, "Set usb role\n");
945 anx7411_unregister_partner(ctx);
946 ctx->psy_online = ANX7411_PSY_OFFLINE;
947 ctx->usb_type = POWER_SUPPLY_USB_TYPE_C;
948 ctx->typec.request_voltage = 0;
949 ctx->typec.request_current = 0;
950 power_supply_changed(ctx->psy);
951 anx7411_chip_standby(ctx);
952 goto unlock;
953 }
954
955 if ((alert0 & SOFTWARE_INT) && (int_change & OCM_BOOT_UP)) {
956 if (anx7411_config(ctx))
957 dev_err(dev, "Config failed\n");
958 if (anx7411_data_role_detect(ctx))
959 dev_err(dev, "set PD data role\n");
960 if (anx7411_power_role_detect(ctx))
961 dev_err(dev, "set PD power role\n");
962 anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C);
963 }
964
965 if (alert0 & RECEIVED_MSG)
966 anx7411_process_cmd(ctx);
967
968 ret = (int_status & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE;
969 if (ctx->typec.data_role != ret)
970 if (anx7411_data_role_detect(ctx))
971 dev_err(dev, "set PD data role\n");
972
973 ret = (int_status & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE;
974 if (ctx->typec.power_role != ret)
975 if (anx7411_power_role_detect(ctx))
976 dev_err(dev, "set PD power role\n");
977
978 if ((alert0 & SOFTWARE_INT) && (int_change & CC_STATUS_CHANGE))
979 anx7411_cc_status_detect(ctx);
980
981 unlock:
982 mutex_unlock(&ctx->lock);
983 }
984
anx7411_intr_isr(int irq,void * data)985 static irqreturn_t anx7411_intr_isr(int irq, void *data)
986 {
987 struct anx7411_data *ctx = (struct anx7411_data *)data;
988
989 queue_work(ctx->workqueue, &ctx->work);
990
991 return IRQ_HANDLED;
992 }
993
anx7411_register_i2c_dummy_clients(struct anx7411_data * ctx,struct i2c_client * client)994 static int anx7411_register_i2c_dummy_clients(struct anx7411_data *ctx,
995 struct i2c_client *client)
996 {
997 int i;
998 u8 spi_addr;
999
1000 for (i = 0; i < ARRAY_SIZE(anx7411_i2c_addr); i++) {
1001 if (client->addr == (anx7411_i2c_addr[i].tcpc_address >> 1)) {
1002 spi_addr = anx7411_i2c_addr[i].spi_address >> 1;
1003 ctx->spi_client = i2c_new_dummy_device(client->adapter,
1004 spi_addr);
1005 if (!IS_ERR(ctx->spi_client))
1006 return 0;
1007 }
1008 }
1009
1010 dev_err(&client->dev, "unable to get SPI slave\n");
1011 return -ENOMEM;
1012 }
1013
anx7411_port_unregister_altmodes(struct typec_altmode ** adev)1014 static void anx7411_port_unregister_altmodes(struct typec_altmode **adev)
1015 {
1016 int i;
1017
1018 for (i = 0; i < MAX_ALTMODE; i++)
1019 if (adev[i]) {
1020 typec_unregister_altmode(adev[i]);
1021 adev[i] = NULL;
1022 }
1023 }
1024
anx7411_port_unregister(struct typec_params * typecp)1025 static void anx7411_port_unregister(struct typec_params *typecp)
1026 {
1027 fwnode_handle_put(typecp->caps.fwnode);
1028 anx7411_port_unregister_altmodes(typecp->port_amode);
1029 if (typecp->port)
1030 typec_unregister_port(typecp->port);
1031 if (typecp->role_sw)
1032 usb_role_switch_put(typecp->role_sw);
1033 }
1034
anx7411_usb_mux_set(struct typec_mux_dev * mux,struct typec_mux_state * state)1035 static int anx7411_usb_mux_set(struct typec_mux_dev *mux,
1036 struct typec_mux_state *state)
1037 {
1038 struct anx7411_data *ctx = typec_mux_get_drvdata(mux);
1039 struct device *dev = &ctx->spi_client->dev;
1040 int has_dp;
1041
1042 has_dp = (state->alt && state->alt->svid == USB_TYPEC_DP_SID &&
1043 state->alt->mode == USB_TYPEC_DP_MODE);
1044 if (!has_dp)
1045 dev_err(dev, "dp altmode not register\n");
1046
1047 return 0;
1048 }
1049
anx7411_usb_set_orientation(struct typec_switch_dev * sw,enum typec_orientation orientation)1050 static int anx7411_usb_set_orientation(struct typec_switch_dev *sw,
1051 enum typec_orientation orientation)
1052 {
1053 /* No need set */
1054
1055 return 0;
1056 }
1057
anx7411_register_switch(struct anx7411_data * ctx,struct device * dev,struct fwnode_handle * fwnode)1058 static int anx7411_register_switch(struct anx7411_data *ctx,
1059 struct device *dev,
1060 struct fwnode_handle *fwnode)
1061 {
1062 struct typec_switch_desc sw_desc = { };
1063
1064 sw_desc.fwnode = fwnode;
1065 sw_desc.drvdata = ctx;
1066 sw_desc.name = fwnode_get_name(fwnode);
1067 sw_desc.set = anx7411_usb_set_orientation;
1068
1069 ctx->typec.typec_switch = typec_switch_register(dev, &sw_desc);
1070 if (IS_ERR(ctx->typec.typec_switch)) {
1071 dev_err(dev, "switch register failed\n");
1072 return PTR_ERR(ctx->typec.typec_switch);
1073 }
1074
1075 return 0;
1076 }
1077
anx7411_register_mux(struct anx7411_data * ctx,struct device * dev,struct fwnode_handle * fwnode)1078 static int anx7411_register_mux(struct anx7411_data *ctx,
1079 struct device *dev,
1080 struct fwnode_handle *fwnode)
1081 {
1082 struct typec_mux_desc mux_desc = { };
1083
1084 mux_desc.fwnode = fwnode;
1085 mux_desc.drvdata = ctx;
1086 mux_desc.name = fwnode_get_name(fwnode);
1087 mux_desc.set = anx7411_usb_mux_set;
1088
1089 ctx->typec.typec_mux = typec_mux_register(dev, &mux_desc);
1090 if (IS_ERR(ctx->typec.typec_mux)) {
1091 dev_err(dev, "mux register failed\n");
1092 return PTR_ERR(ctx->typec.typec_mux);
1093 }
1094
1095 return 0;
1096 }
1097
anx7411_unregister_mux(struct anx7411_data * ctx)1098 static void anx7411_unregister_mux(struct anx7411_data *ctx)
1099 {
1100 if (ctx->typec.typec_mux) {
1101 typec_mux_unregister(ctx->typec.typec_mux);
1102 ctx->typec.typec_mux = NULL;
1103 fwnode_handle_put(ctx->mux_node);
1104 }
1105 }
1106
anx7411_unregister_switch(struct anx7411_data * ctx)1107 static void anx7411_unregister_switch(struct anx7411_data *ctx)
1108 {
1109 if (ctx->typec.typec_switch) {
1110 typec_switch_unregister(ctx->typec.typec_switch);
1111 ctx->typec.typec_switch = NULL;
1112 fwnode_handle_put(ctx->switch_node);
1113 }
1114 }
1115
anx7411_typec_switch_probe(struct anx7411_data * ctx,struct device * dev)1116 static int anx7411_typec_switch_probe(struct anx7411_data *ctx,
1117 struct device *dev)
1118 {
1119 int ret;
1120
1121 ctx->switch_node = device_get_named_child_node(dev, "orientation_switch");
1122 if (!ctx->switch_node)
1123 return 0;
1124
1125 ret = anx7411_register_switch(ctx, dev, ctx->switch_node);
1126 if (ret) {
1127 dev_err(dev, "failed register switch");
1128 fwnode_handle_put(ctx->switch_node);
1129 return ret;
1130 }
1131
1132 ctx->mux_node = device_get_named_child_node(dev, "mode_switch");
1133 if (!ctx->mux_node) {
1134 dev_err(dev, "no typec mux exist");
1135 ret = -ENODEV;
1136 goto unregister_switch;
1137 }
1138
1139 ret = anx7411_register_mux(ctx, dev, ctx->mux_node);
1140 if (ret) {
1141 dev_err(dev, "failed register mode switch");
1142 fwnode_handle_put(ctx->mux_node);
1143 ret = -ENODEV;
1144 goto unregister_switch;
1145 }
1146
1147 return 0;
1148
1149 unregister_switch:
1150 anx7411_unregister_switch(ctx);
1151
1152 return ret;
1153 }
1154
anx7411_typec_port_probe(struct anx7411_data * ctx,struct device * dev)1155 static int anx7411_typec_port_probe(struct anx7411_data *ctx,
1156 struct device *dev)
1157 {
1158 struct typec_capability *cap = &ctx->typec.caps;
1159 struct typec_params *typecp = &ctx->typec;
1160 struct fwnode_handle *fwnode;
1161 const char *buf;
1162 int ret, i;
1163
1164 fwnode = device_get_named_child_node(dev, "connector");
1165 if (!fwnode)
1166 return -EINVAL;
1167
1168 ret = fwnode_property_read_string(fwnode, "power-role", &buf);
1169 if (ret) {
1170 dev_err(dev, "power-role not found: %d\n", ret);
1171 goto put_fwnode;
1172 }
1173
1174 ret = typec_find_port_power_role(buf);
1175 if (ret < 0)
1176 goto put_fwnode;
1177 cap->type = ret;
1178
1179 ret = fwnode_property_read_string(fwnode, "data-role", &buf);
1180 if (ret) {
1181 dev_err(dev, "data-role not found: %d\n", ret);
1182 goto put_fwnode;
1183 }
1184
1185 ret = typec_find_port_data_role(buf);
1186 if (ret < 0)
1187 goto put_fwnode;
1188 cap->data = ret;
1189
1190 ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
1191 if (ret) {
1192 dev_err(dev, "try-power-role not found: %d\n", ret);
1193 goto put_fwnode;
1194 }
1195
1196 ret = typec_find_power_role(buf);
1197 if (ret < 0)
1198 goto put_fwnode;
1199 cap->prefer_role = ret;
1200
1201 /* Get source pdos */
1202 ret = fwnode_property_count_u32(fwnode, "source-pdos");
1203 if (ret > 0) {
1204 typecp->src_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS);
1205 ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
1206 typecp->src_pdo,
1207 typecp->src_pdo_nr);
1208 if (ret < 0) {
1209 dev_err(dev, "source cap validate failed: %d\n", ret);
1210 goto put_fwnode;
1211 }
1212
1213 typecp->caps_flags |= HAS_SOURCE_CAP;
1214 }
1215
1216 ret = fwnode_property_count_u32(fwnode, "sink-pdos");
1217 if (ret > 0) {
1218 typecp->sink_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS);
1219 ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
1220 typecp->sink_pdo,
1221 typecp->sink_pdo_nr);
1222 if (ret < 0) {
1223 dev_err(dev, "sink cap validate failed: %d\n", ret);
1224 goto put_fwnode;
1225 }
1226
1227 for (i = 0; i < typecp->sink_pdo_nr; i++) {
1228 ret = 0;
1229 switch (pdo_type(typecp->sink_pdo[i])) {
1230 case PDO_TYPE_FIXED:
1231 ret = pdo_fixed_voltage(typecp->sink_pdo[i]);
1232 break;
1233 case PDO_TYPE_BATT:
1234 case PDO_TYPE_VAR:
1235 ret = pdo_max_voltage(typecp->sink_pdo[i]);
1236 break;
1237 case PDO_TYPE_APDO:
1238 default:
1239 ret = 0;
1240 break;
1241 }
1242
1243 /* 100mv per unit */
1244 typecp->sink_voltage = max(5000, ret) / 100;
1245 }
1246
1247 typecp->caps_flags |= HAS_SINK_CAP;
1248 }
1249
1250 if (!fwnode_property_read_u32(fwnode, "op-sink-microwatt", &ret)) {
1251 typecp->sink_watt = ret / 500000; /* 500mw per unit */
1252 typecp->caps_flags |= HAS_SINK_WATT;
1253 }
1254
1255 cap->fwnode = fwnode;
1256
1257 ctx->typec.role_sw = usb_role_switch_get(dev);
1258 if (IS_ERR(ctx->typec.role_sw)) {
1259 dev_err(dev, "USB role switch not found.\n");
1260 ctx->typec.role_sw = NULL;
1261 }
1262
1263 ctx->typec.port = typec_register_port(dev, cap);
1264 if (IS_ERR(ctx->typec.port)) {
1265 ret = PTR_ERR(ctx->typec.port);
1266 ctx->typec.port = NULL;
1267 dev_err(dev, "Failed to register type c port %d\n", ret);
1268 goto put_usb_role_switch;
1269 }
1270
1271 typec_port_register_altmodes(ctx->typec.port, NULL, ctx,
1272 ctx->typec.port_amode,
1273 MAX_ALTMODE);
1274 return 0;
1275
1276 put_usb_role_switch:
1277 if (ctx->typec.role_sw)
1278 usb_role_switch_put(ctx->typec.role_sw);
1279 put_fwnode:
1280 fwnode_handle_put(fwnode);
1281
1282 return ret;
1283 }
1284
anx7411_typec_check_connection(struct anx7411_data * ctx)1285 static int anx7411_typec_check_connection(struct anx7411_data *ctx)
1286 {
1287 int ret;
1288
1289 ret = anx7411_reg_read(ctx->spi_client, FW_VER);
1290 if (ret < 0)
1291 return 0; /* No device attached in typec port */
1292
1293 /* Clear interrupt and alert status */
1294 ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0);
1295 ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, 0xFF);
1296 ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, 0xFF);
1297 if (ret)
1298 return ret;
1299
1300 ret = anx7411_cc_status_detect(ctx);
1301 ret |= anx7411_power_role_detect(ctx);
1302 ret |= anx7411_data_role_detect(ctx);
1303 ret |= anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C);
1304 if (ret)
1305 return ret;
1306
1307 ret = anx7411_send_msg(ctx, TYPE_GET_DP_ALT_ENTER, NULL, 0);
1308 ret |= anx7411_send_msg(ctx, TYPE_GET_DP_DISCOVER_MODES_INFO, NULL, 0);
1309
1310 return ret;
1311 }
1312
anx7411_runtime_pm_suspend(struct device * dev)1313 static int __maybe_unused anx7411_runtime_pm_suspend(struct device *dev)
1314 {
1315 struct anx7411_data *ctx = dev_get_drvdata(dev);
1316
1317 mutex_lock(&ctx->lock);
1318
1319 anx7411_partner_unregister_altmode(ctx);
1320
1321 if (ctx->typec.partner)
1322 anx7411_unregister_partner(ctx);
1323
1324 mutex_unlock(&ctx->lock);
1325
1326 return 0;
1327 }
1328
anx7411_runtime_pm_resume(struct device * dev)1329 static int __maybe_unused anx7411_runtime_pm_resume(struct device *dev)
1330 {
1331 struct anx7411_data *ctx = dev_get_drvdata(dev);
1332
1333 mutex_lock(&ctx->lock);
1334 /* Detect PD connection */
1335 if (anx7411_typec_check_connection(ctx))
1336 dev_err(dev, "check connection");
1337
1338 mutex_unlock(&ctx->lock);
1339
1340 return 0;
1341 }
1342
1343 static const struct dev_pm_ops anx7411_pm_ops = {
1344 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1345 pm_runtime_force_resume)
1346 SET_RUNTIME_PM_OPS(anx7411_runtime_pm_suspend,
1347 anx7411_runtime_pm_resume, NULL)
1348 };
1349
anx7411_get_gpio_irq(struct anx7411_data * ctx)1350 static void anx7411_get_gpio_irq(struct anx7411_data *ctx)
1351 {
1352 struct device *dev = &ctx->tcpc_client->dev;
1353
1354 ctx->intp_gpiod = devm_gpiod_get_optional(dev, "interrupt", GPIOD_IN);
1355 if (IS_ERR_OR_NULL(ctx->intp_gpiod)) {
1356 dev_err(dev, "no interrupt gpio property\n");
1357 return;
1358 }
1359
1360 ctx->intp_irq = gpiod_to_irq(ctx->intp_gpiod);
1361 if (ctx->intp_irq < 0)
1362 dev_err(dev, "failed to get GPIO IRQ\n");
1363 }
1364
1365 static enum power_supply_usb_type anx7411_psy_usb_types[] = {
1366 POWER_SUPPLY_USB_TYPE_C,
1367 POWER_SUPPLY_USB_TYPE_PD,
1368 POWER_SUPPLY_USB_TYPE_PD_PPS,
1369 };
1370
1371 static enum power_supply_property anx7411_psy_props[] = {
1372 POWER_SUPPLY_PROP_USB_TYPE,
1373 POWER_SUPPLY_PROP_ONLINE,
1374 POWER_SUPPLY_PROP_VOLTAGE_MIN,
1375 POWER_SUPPLY_PROP_VOLTAGE_MAX,
1376 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1377 POWER_SUPPLY_PROP_CURRENT_MAX,
1378 POWER_SUPPLY_PROP_CURRENT_NOW,
1379 };
1380
anx7411_psy_set_prop(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)1381 static int anx7411_psy_set_prop(struct power_supply *psy,
1382 enum power_supply_property psp,
1383 const union power_supply_propval *val)
1384 {
1385 struct anx7411_data *ctx = power_supply_get_drvdata(psy);
1386 int ret = 0;
1387
1388 if (psp == POWER_SUPPLY_PROP_ONLINE)
1389 ctx->psy_online = val->intval;
1390 else
1391 ret = -EINVAL;
1392
1393 power_supply_changed(ctx->psy);
1394 return ret;
1395 }
1396
anx7411_psy_prop_writeable(struct power_supply * psy,enum power_supply_property psp)1397 static int anx7411_psy_prop_writeable(struct power_supply *psy,
1398 enum power_supply_property psp)
1399 {
1400 return psp == POWER_SUPPLY_PROP_ONLINE;
1401 }
1402
anx7411_psy_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)1403 static int anx7411_psy_get_prop(struct power_supply *psy,
1404 enum power_supply_property psp,
1405 union power_supply_propval *val)
1406 {
1407 struct anx7411_data *ctx = power_supply_get_drvdata(psy);
1408 int ret = 0;
1409
1410 switch (psp) {
1411 case POWER_SUPPLY_PROP_USB_TYPE:
1412 val->intval = ctx->usb_type;
1413 break;
1414 case POWER_SUPPLY_PROP_ONLINE:
1415 val->intval = ctx->psy_online;
1416 break;
1417 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1418 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
1419 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
1420 val->intval = (ctx->psy_online) ?
1421 ctx->typec.request_voltage * 1000 : 0;
1422 break;
1423 case POWER_SUPPLY_PROP_CURRENT_NOW:
1424 case POWER_SUPPLY_PROP_CURRENT_MAX:
1425 val->intval = (ctx->psy_online) ?
1426 ctx->typec.request_current * 1000 : 0;
1427 break;
1428 default:
1429 ret = -EINVAL;
1430 break;
1431 }
1432 return ret;
1433 }
1434
anx7411_psy_register(struct anx7411_data * ctx)1435 static int anx7411_psy_register(struct anx7411_data *ctx)
1436 {
1437 struct power_supply_desc *psy_desc = &ctx->psy_desc;
1438 struct power_supply_config psy_cfg = {};
1439 char *psy_name;
1440
1441 psy_name = devm_kasprintf(ctx->dev, GFP_KERNEL, "anx7411-source-psy-%s",
1442 dev_name(ctx->dev));
1443 if (!psy_name)
1444 return -ENOMEM;
1445
1446 psy_desc->name = psy_name;
1447 psy_desc->type = POWER_SUPPLY_TYPE_USB;
1448 psy_desc->usb_types = anx7411_psy_usb_types;
1449 psy_desc->num_usb_types = ARRAY_SIZE(anx7411_psy_usb_types);
1450 psy_desc->properties = anx7411_psy_props;
1451 psy_desc->num_properties = ARRAY_SIZE(anx7411_psy_props);
1452
1453 psy_desc->get_property = anx7411_psy_get_prop;
1454 psy_desc->set_property = anx7411_psy_set_prop;
1455 psy_desc->property_is_writeable = anx7411_psy_prop_writeable;
1456
1457 ctx->usb_type = POWER_SUPPLY_USB_TYPE_C;
1458 ctx->psy = devm_power_supply_register(ctx->dev, psy_desc, &psy_cfg);
1459
1460 if (IS_ERR(ctx->psy))
1461 dev_warn(ctx->dev, "unable to register psy\n");
1462
1463 return PTR_ERR_OR_ZERO(ctx->psy);
1464 }
1465
anx7411_i2c_probe(struct i2c_client * client)1466 static int anx7411_i2c_probe(struct i2c_client *client)
1467 {
1468 struct anx7411_data *plat;
1469 struct device *dev = &client->dev;
1470 int ret;
1471
1472 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
1473 return -ENODEV;
1474
1475 plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
1476 if (!plat)
1477 return -ENOMEM;
1478
1479 plat->tcpc_client = client;
1480 i2c_set_clientdata(client, plat);
1481
1482 mutex_init(&plat->lock);
1483
1484 ret = anx7411_register_i2c_dummy_clients(plat, client);
1485 if (ret) {
1486 dev_err(dev, "fail to reserve I2C bus\n");
1487 return ret;
1488 }
1489
1490 ret = anx7411_typec_switch_probe(plat, dev);
1491 if (ret) {
1492 dev_err(dev, "fail to probe typec switch\n");
1493 goto free_i2c_dummy;
1494 }
1495
1496 ret = anx7411_typec_port_probe(plat, dev);
1497 if (ret) {
1498 dev_err(dev, "fail to probe typec property.\n");
1499 ret = -ENODEV;
1500 goto free_typec_switch;
1501 }
1502
1503 plat->intp_irq = client->irq;
1504 if (!client->irq)
1505 anx7411_get_gpio_irq(plat);
1506
1507 if (!plat->intp_irq) {
1508 dev_err(dev, "fail to get interrupt IRQ\n");
1509 ret = -EINVAL;
1510 goto free_typec_port;
1511 }
1512
1513 plat->dev = dev;
1514 plat->psy_online = ANX7411_PSY_OFFLINE;
1515 ret = anx7411_psy_register(plat);
1516 if (ret) {
1517 dev_err(dev, "register psy\n");
1518 goto free_typec_port;
1519 }
1520
1521 INIT_WORK(&plat->work, anx7411_work_func);
1522 plat->workqueue = alloc_workqueue("anx7411_work",
1523 WQ_FREEZABLE |
1524 WQ_MEM_RECLAIM,
1525 1);
1526 if (!plat->workqueue) {
1527 dev_err(dev, "fail to create work queue\n");
1528 ret = -ENOMEM;
1529 goto free_typec_port;
1530 }
1531
1532 ret = devm_request_threaded_irq(dev, plat->intp_irq,
1533 NULL, anx7411_intr_isr,
1534 IRQF_TRIGGER_FALLING |
1535 IRQF_ONESHOT,
1536 "anx7411-intp", plat);
1537 if (ret) {
1538 dev_err(dev, "fail to request irq\n");
1539 goto free_wq;
1540 }
1541
1542 if (anx7411_typec_check_connection(plat))
1543 dev_err(dev, "check status\n");
1544
1545 pm_runtime_enable(dev);
1546
1547 return 0;
1548
1549 free_wq:
1550 destroy_workqueue(plat->workqueue);
1551
1552 free_typec_port:
1553 anx7411_port_unregister(&plat->typec);
1554
1555 free_typec_switch:
1556 anx7411_unregister_switch(plat);
1557 anx7411_unregister_mux(plat);
1558
1559 free_i2c_dummy:
1560 i2c_unregister_device(plat->spi_client);
1561
1562 return ret;
1563 }
1564
anx7411_i2c_remove(struct i2c_client * client)1565 static void anx7411_i2c_remove(struct i2c_client *client)
1566 {
1567 struct anx7411_data *plat = i2c_get_clientdata(client);
1568
1569 anx7411_partner_unregister_altmode(plat);
1570 anx7411_unregister_partner(plat);
1571
1572 if (plat->workqueue)
1573 destroy_workqueue(plat->workqueue);
1574
1575 if (plat->spi_client)
1576 i2c_unregister_device(plat->spi_client);
1577
1578 anx7411_unregister_mux(plat);
1579
1580 anx7411_unregister_switch(plat);
1581
1582 anx7411_port_unregister(&plat->typec);
1583 }
1584
1585 static const struct i2c_device_id anx7411_id[] = {
1586 {"anx7411", 0},
1587 {}
1588 };
1589
1590 MODULE_DEVICE_TABLE(i2c, anx7411_id);
1591
1592 static const struct of_device_id anx_match_table[] = {
1593 {.compatible = "analogix,anx7411",},
1594 {},
1595 };
1596
1597 static struct i2c_driver anx7411_driver = {
1598 .driver = {
1599 .name = "anx7411",
1600 .of_match_table = anx_match_table,
1601 .pm = &anx7411_pm_ops,
1602 },
1603 .probe = anx7411_i2c_probe,
1604 .remove = anx7411_i2c_remove,
1605
1606 .id_table = anx7411_id,
1607 };
1608
1609 module_i2c_driver(anx7411_driver);
1610
1611 MODULE_DESCRIPTION("Anx7411 USB Type-C PD driver");
1612 MODULE_AUTHOR("Xin Ji <xji@analogixsemi.com>");
1613 MODULE_LICENSE("GPL");
1614 MODULE_VERSION("0.1.5");
1615