UPSTREAM: regulator: pwm: Retrieve correct voltage
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / fusb302.h
1 /*
2  * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
3  * Author: Zain Wang <zain.wang@rock-chips.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * Some ideas are from chrome ec and fairchild GPL fusb302 driver.
10  */
11
12 #ifndef FUSB302_H
13 #define FUSB302_H
14
15 #include <linux/i2c.h>
16 #include <linux/hrtimer.h>
17
18 #define FUSB_VCONN_SUPPORT
19 /* TODO: more modes would be added here later on */
20 #define FUSB_HAVE_DRP
21
22 const char *FUSB_DT_INTERRUPT_INTN =    "fsc_interrupt_int_n";
23 #define FUSB_DT_GPIO_INTN               "fairchild,int_n"
24 #define FUSB_DT_GPIO_VBUS_5V            "fairchild,vbus5v"
25 #define FUSB_DT_GPIO_VBUS_OTHER         "fairchild,vbusOther"
26
27 #define FUSB30X_I2C_DRIVER_NAME         "fusb302"
28 #define FUSB30X_I2C_DEVICETREE_NAME     "fairchild,fusb302"
29
30 /* FUSB300 Register Addresses */
31 #define FUSB_REG_DEVICEID               0x01
32 #define FUSB_REG_SWITCHES0              0x02
33 #define FUSB_REG_SWITCHES1              0x03
34 #define FUSB_REG_MEASURE                0x04
35 #define FUSB_REG_SLICE                  0x05
36 #define FUSB_REG_CONTROL0               0x06
37 #define FUSB_REG_CONTROL1               0x07
38 #define FUSB_REG_CONTROL2               0x08
39 #define FUSB_REG_CONTROL3               0x09
40 #define FUSB_REG_MASK                   0x0A
41 #define FUSB_REG_POWER                  0x0B
42 #define FUSB_REG_RESET                  0x0C
43 #define FUSB_REG_OCPREG                 0x0D
44 #define FUSB_REG_MASKA                  0x0E
45 #define FUSB_REG_MASKB                  0x0F
46 #define FUSB_REG_CONTROL4               0x10
47 #define FUSB_REG_STATUS0A               0x3C
48 #define FUSB_REG_STATUS1A               0x3D
49 #define FUSB_REG_INTERRUPTA             0x3E
50 #define FUSB_REG_INTERRUPTB             0x3F
51 #define FUSB_REG_STATUS0                0x40
52 #define FUSB_REG_STATUS1                0x41
53 #define FUSB_REG_INTERRUPT              0x42
54 #define FUSB_REG_FIFO                   0x43
55
56 enum connection_state {
57         disabled = 0,
58         error_recovery,
59         unattached,
60         attach_wait_sink,
61         attach_wait_source,
62         attached_source,
63         attached_sink,
64
65         policy_src_startup,
66         policy_src_send_caps,
67         policy_src_discovery,
68         policy_src_negotiate_cap,
69         policy_src_cap_response,
70         policy_src_transition_supply,
71         policy_src_transition_default,
72
73         policy_src_ready,
74         policy_src_get_sink_caps,
75
76         policy_src_send_softrst,
77         policy_src_send_hardrst,
78
79         policy_snk_startup,
80         policy_snk_discovery,
81         policy_snk_wait_caps,
82         policy_snk_evaluate_caps,
83         policy_snk_select_cap,
84         policy_snk_transition_sink,
85         policy_snk_ready,
86
87         policy_snk_send_softrst,
88         policy_snk_send_hardrst,
89
90         policy_snk_transition_default,
91 };
92
93 enum tcpm_rp_value {
94         TYPEC_RP_USB = 0,
95         TYPEC_RP_1A5 = 1,
96         TYPEC_RP_3A0 = 2,
97         TYPEC_RP_RESERVED = 3,
98 };
99
100 #define SBF(s, v)               ((s) << (v))
101 #define SWITCHES0_PDWN1         SBF(1, 0)
102 #define SWITCHES0_PDWN2         SBF(1, 1)
103 #define SWITCHES0_MEAS_CC1      SBF(1, 2)
104 #define SWITCHES0_MEAS_CC2      SBF(1, 3)
105 #define SWITCHES0_VCONN_CC1     SBF(1, 4)
106 #define SWITCHES0_VCONN_CC2     SBF(1, 5)
107 #define SWITCHES0_PU_EN1        SBF(1, 6)
108 #define SWITCHES0_PU_EN2        SBF(1, 7)
109
110 #define SWITCHES1_TXCC1         SBF(1, 0)
111 #define SWITCHES1_TXCC2         SBF(1, 1)
112 #define SWITCHES1_AUTO_CRC      SBF(1, 2)
113 #define SWITCHES1_DATAROLE      SBF(1, 4)
114 #define SWITCHES1_SPECREV       SBF(3, 5)
115 #define SWITCHES1_POWERROLE     SBF(1, 7)
116
117 #define MEASURE_MDAC            SBF(0x3f, 0)
118 #define MEASURE_VBUS            SBF(1, 6)
119
120 #define SLICE_SDAC              SBF(0x3f, 0)
121 #define SLICE_SDAC_HYS          SBF(3, 6)
122
123 #define CONTROL0_TX_START       SBF(1, 0)
124 #define CONTROL0_AUTO_PRE       SBF(1, 1)
125 #define CONTROL0_HOST_CUR       SBF(3, 2)
126 #define CONTROL0_HOST_CUR_USB           SBF(1, 2)
127 #define CONTROL0_HOST_CUR_1A5           SBF(2, 2)
128 #define CONTROL0_HOST_CUR_3A0           SBF(3, 2)
129 #define CONTROL0_INT_MASK       SBF(1, 5)
130 #define CONTROL0_TX_FLUSH       SBF(1, 6)
131
132 #define CONTROL1_ENSOP1         SBF(1, 0)
133 #define CONTROL1_ENSOP2         SBF(1, 1)
134 #define CONTROL1_RX_FLUSH       SBF(1, 2)
135 #define CONTROL1_BIST_MODE2     SBF(1, 4)
136 #define CONTROL1_ENSOP1DB       SBF(1, 5)
137 #define CONTROL1_ENSOP2DB       SBF(1, 6)
138
139 #define CONTROL2_TOGGLE         SBF(1, 0)
140 #define CONTROL2_MODE           SBF(3, 1)
141 #define CONTROL2_WAKE_EN        SBF(1, 3)
142 #define CONTROL2_TOG_RD_ONLY    SBF(1, 5)
143 #define CONTROL2_TOG_SAVE_PWR1  SBF(1, 6)
144 #define CONTROL2_TOG_SAVE_PWR2  SBF(1, 7)
145
146 #define CONTROL3_AUTO_RETRY     SBF(1, 0)
147 #define CONTROL3_N_RETRIES      SBF(3, 1)
148 #define CONTROL3_AUTO_SOFTRESET SBF(1, 3)
149 #define CONTROL3_AUTO_HARDRESET SBF(1, 4)
150 #define CONTROL3_SEND_HARDRESET SBF(1, 6)
151
152 #define MASK_M_BC_LVL           SBF(1, 0)
153 #define MASK_M_COLLISION        SBF(1, 1)
154 #define MASK_M_WAKE             SBF(1, 2)
155 #define MASK_M_ALERT            SBF(1, 3)
156 #define MASK_M_CRC_CHK          SBF(1, 4)
157 #define MASK_M_COMP_CHNG        SBF(1, 5)
158 #define MASK_M_ACTIVITY         SBF(1, 6)
159 #define MASK_M_VBUSOK           SBF(1, 7)
160
161 #define POWER_PWR               SBF(0xf, 0)
162
163 #define RESET_SW_RESET          SBF(1, 0)
164 #define RESET_PD_RESET          SBF(1, 1)
165
166 #define MASKA_M_HARDRST         SBF(1, 0)
167 #define MASKA_M_SOFTRST         SBF(1, 1)
168 #define MASKA_M_TXSENT          SBF(1, 2)
169 #define MASKA_M_HARDSENT        SBF(1, 3)
170 #define MASKA_M_RETRYFAIL       SBF(1, 4)
171 #define MASKA_M_SOFTFAIL        SBF(1, 5)
172 #define MASKA_M_TOGDONE         SBF(1, 6)
173 #define MASKA_M_OCP_TEMP        SBF(1, 7)
174
175 #define MASKB_M_GCRCSEND        SBF(1, 0)
176
177 #define CONTROL4_TOG_USRC_EXIT  SBF(1, 0)
178
179 #define MDAC_1P6V               0x26
180
181 #define STATUS0A_HARDRST        SBF(1, 0)
182 #define STATUS0A_SOFTRST        SBF(1, 1)
183 #define STATUS0A_POWER23        SBF(3, 2)
184 #define STATUS0A_RETRYFAIL      SBF(1, 4)
185 #define STATUS0A_SOFTFAIL       SBF(1, 5)
186 #define STATUS0A_TOGDONE        SBF(1, 6)
187 #define STATUS0A_M_OCP_TEMP     SBF(1, 7)
188
189 #define STATUS1A_RXSOP          SBF(1, 0)
190 #define STATUS1A_RXSOP1DB       SBF(1, 1)
191 #define STATUS1A_RXSOP2DB       SBF(1, 2)
192 #define STATUS1A_TOGSS          SBF(7, 3)
193
194 #define INTERRUPTA_HARDRST      SBF(1, 0)
195 #define INTERRUPTA_SOFTRST      SBF(1, 1)
196 #define INTERRUPTA_TXSENT       SBF(1, 2)
197 #define INTERRUPTA_HARDSENT     SBF(1, 3)
198 #define INTERRUPTA_RETRYFAIL    SBF(1, 4)
199 #define INTERRUPTA_SOFTFAIL     SBF(1, 5)
200 #define INTERRUPTA_TOGDONE      SBF(1, 6)
201 #define INTERRUPTA_OCP_TEMP     SBF(1, 7)
202
203 #define INTERRUPTB_GCRCSENT     SBF(1, 0)
204
205 #define STATUS0_BC_LVL          SBF(3, 0)
206 #define STATUS0_WAKE            SBF(1, 2)
207 #define STATUS0_ALERT           SBF(1, 3)
208 #define STATUS0_CRC_CHK         SBF(1, 4)
209 #define STATUS0_COMP            SBF(1, 5)
210 #define STATUS0_ACTIVITY        SBF(1, 6)
211 #define STATUS0_VBUSOK          SBF(1, 7)
212
213 #define STATUS1_OCP             SBF(1, 0)
214 #define STATUS1_OVRTEMP         SBF(1, 1)
215 #define STATUS1_TX_FULL         SBF(1, 2)
216 #define STATUS1_TX_EMPTY        SBF(1, 3)
217 #define STATUS1_RX_FULL         SBF(1, 4)
218 #define STATUS1_RX_EMPTY        SBF(1, 5)
219 #define STATUS1_RXSOP1          SBF(1, 6)
220 #define STATUS1_RXSOP2          SBF(1, 7)
221
222 #define INTERRUPT_BC_LVL        SBF(1, 0)
223 #define INTERRUPT_COLLISION     SBF(1, 1)
224 #define INTERRUPT_WAKE          SBF(1, 2)
225 #define INTERRUPT_ALERT         SBF(1, 3)
226 #define INTERRUPT_CRC_CHK       SBF(1, 4)
227 #define INTERRUPT_COMP_CHNG     SBF(1, 5)
228 #define INTERRUPT_ACTIVITY      SBF(1, 6)
229 #define INTERRUPT_VBUSOK        SBF(1, 7)
230
231 #define FUSB_TKN_TXON           0xa1
232 #define FUSB_TKN_SYNC1          0x12
233 #define FUSB_TKN_SYNC2          0x13
234 #define FUSB_TKN_SYNC3          0x1b
235 #define FUSB_TKN_RST1           0x15
236 #define FUSB_TKN_RST2           0x16
237 #define FUSB_TKN_PACKSYM        0x80
238 #define FUSB_TKN_JAMCRC         0xff
239 #define FUSB_TKN_EOP            0x14
240 #define FUSB_TKN_TXOFF          0xfe
241
242 /* USB PD Control Message Types */
243 #define CONTROLMESSAGE          0
244 #define CMT_GOODCRC             1
245 #define CMT_GOTOMIN             2
246 #define CMT_ACCEPT              3
247 #define CMT_REJECT              4
248 #define CMT_PING                5
249 #define CMT_PS_RDY              6
250 #define CMT_GETSOURCECAP        7
251 #define CMT_GETSINKCAP          8
252 #define CMT_DR_SWAP             9
253 #define CMT_PR_SWAP             10
254 #define CMT_VCONN_SWAP          11
255 #define CMT_WAIT                12
256 #define CMT_SOFTRESET           13
257
258 /* USB PD Data Message Types */
259 #define DATAMESSAGE             1
260 #define DMT_SOURCECAPABILITIES  1
261 #define DMT_REQUEST             2
262 #define DMT_BIST                3
263 #define DMT_SINKCAPABILITIES    4
264 #define DMT_VENDERDEFINED       15
265
266 /* VDM Command Types */
267 #define VDM_DISCOVERY_ID        0X01
268 #define VDM_DISCOVERY_SVIDS     0X02
269 #define VDM_DISCOVERY_MODES     0X03
270 #define VDM_ENTER_MODE          0X04
271 #define VDM_EXIT_MODE           0X05
272 #define VDM_ATTENTION           0X06
273 #define VDM_DP_STATUS_UPDATE    0X10
274 #define VDM_DP_CONFIG           0X11
275
276 #define VDM_TYPE_INIT           0
277 #define VDM_TYPE_ACK            1
278 #define VDM_TYPE_NACK           2
279 #define VDM_TYPE_BUSY           3
280
281 #define N_DEBOUNCE_CNT          (10 - 1)
282 #define N_CAPS_COUNT            100
283 #define N_HARDRESET_COUNT       0
284
285 #define T_NO_RESPONSE           5000
286 #define T_SRC_RECOVER           830
287 #define T_TYPEC_SEND_SOURCECAP  3
288 #define T_SENDER_RESPONSE       30
289 #define T_SRC_TRANSITION        30
290 #define T_TYPEC_SINK_WAIT_CAP   500
291 #define T_PS_TRANSITION         500
292 #define T_BMC_TIMEOUT           5
293 #define T_PS_HARD_RESET_MAX     35
294 #define T_SAFE_0V               650
295 #define T_SRC_TURN_ON           275
296 #define T_SRC_RECOVER_MAX       1000
297
298 #define T_NO_TRIGGER            500
299 #define T_DISABLED              0xffff
300
301 #define PD_HEADER_CNT(header)           (((header) >> 12) & 7)
302 #define PD_HEADER_TYPE(header)          ((header) & 0xF)
303 #define PD_HEADER_ID(header)            (((header) >> 9) & 7)
304
305 #define VDM_HEADER_TYPE(header)         (((header) >> 6) & 3)
306 #define VDMHEAD_CMD_TYPE_MASK           (3 << 6)
307 #define VDMHEAD_CMD_MASK                (0x1f << 0)
308 #define VDMHEAD_STRUCT_TYPE_MASK        BIT(15)
309
310 #define GET_VDMHEAD_CMD_TYPE(head)      ((head & VDMHEAD_CMD_TYPE_MASK) >> 6)
311 #define GET_VDMHEAD_CMD(head)           (head & VDMHEAD_CMD_MASK)
312 #define GET_VDMHEAD_STRUCT_TYPE(head)   ((head & VDMHEAD_STRUCT_TYPE_MASK) >> 15)
313
314 #define VDM_IDHEAD_USBVID_MASK          (0xffff << 0)
315 #define VDM_IDHEAD_MODALSUPPORT_MASK    BIT(26)
316 #define VDM_IDHEAD_PRODUCTTYPE          (7 << 27)
317 #define VDM_IDHEAD_USBDEVICE            BIT(30)
318 #define VDM_IDHEAD_USBHOST              BIT(30)
319
320 #define CAP_POWER_TYPE(PDO)             ((PDO >> 30) & 3)
321 #define CAP_FPDO_VOLTAGE(PDO)           ((PDO >> 10) & 0x3ff)
322 #define CAP_VPDO_VOLTAGE(PDO)           ((PDO >> 20) & 0x3ff)
323 #define CAP_FPDO_CURRENT(PDO)           ((PDO >> 0) & 0x3ff)
324 #define CAP_VPDO_CURRENT(PDO)           ((PDO >> 0) & 0x3ff)
325
326 enum CC_ORIENTATION {
327         NONE,
328         CC1,
329         CC2,
330 };
331
332 struct notify_info {
333         enum CC_ORIENTATION orientation;
334         /* 0 UFP : 1 DFP */
335         bool power_role;
336         bool data_role;
337
338         bool is_cc_connected;
339         bool is_pd_connected;
340
341         bool is_enter_mode;
342         int pin_assignment_support;
343         int pin_assignment_def;
344         bool attention;
345 };
346
347 enum tx_state {
348         tx_idle,
349         tx_busy,
350         tx_failed,
351         tx_success
352 };
353
354 struct PD_CAP_INFO {
355         u32 peak_current;
356         u32 specification_revision;
357         u32 externally_powered;
358         u32 usb_suspend_support;
359         u32 usb_communications_cap;
360         u32 dual_role_power;
361         u32 data_role_swap;
362         u32 supply_type;
363 };
364
365 struct fusb30x_chip {
366         struct i2c_client *client;
367         struct device *dev;
368         struct regmap *regmap;
369         struct work_struct work;
370         struct workqueue_struct *fusb30x_wq;
371         struct hrtimer timer_state_machine;
372         struct hrtimer timer_mux_machine;
373         struct PD_CAP_INFO pd_cap_info;
374         struct notify_info notify;
375         struct notify_info notify_cmp;
376         struct extcon_dev *extcon;
377         enum connection_state conn_state;
378         struct gpio_desc *gpio_vbus_5v;
379         struct gpio_desc *gpio_vbus_other;
380         struct gpio_desc *gpio_int;
381         struct gpio_desc *gpio_discharge;
382         int timer_state;
383         int timer_mux;
384         int port_num;
385         int work_continue;
386         spinlock_t irq_lock;
387         int gpio_int_irq;
388         int enable_irq;
389
390         /*
391          * ---------------------------------
392          * | role 0x03 << 2, | cc_use 0x03 |
393          * | src  1 << 2,    | cc1 1       |
394          * | snk  2 << 2,    | cc2 2       |
395          * ---------------------------------
396          */
397         u8 cc_state;
398         int cc1;
399         int cc2;
400         /* 0 cc1 : 1 cc2 */
401         bool cc_polarity;
402         u8 val_tmp;
403         u8 debounce_cnt;
404         int sub_state;
405         int caps_counter;
406         u32 send_load[7];
407         u32 rec_load[7];
408         u16 send_head;
409         u16 rec_head;
410         int msg_id;
411         enum tx_state tx_state;
412         int hardrst_count;
413         u32 source_power_supply[7];
414         /* 50mv unit */
415         u32 source_max_current[7];
416         /* 10ma uint*/
417         int pos_power;
418         /*
419          * if PartnerCap[0] == 0xffffffff
420          * show Partner Device do not support supply
421          */
422         u32 partner_cap[7];
423         int n_caps_used;
424         int vdm_state;
425         int vdm_substate;
426         int vdm_send_state;
427         u32 dp_status;
428         u16 vdm_svid[12];
429         int vdm_svid_num;
430         u32 vdm_id;
431         u8 chip_id;
432         bool vconn_enabled;
433         int togdone_pullup;
434         int pd_output_vol;
435         int pd_output_cur;
436         int cc_meas_high;
437         int cc_meas_low;
438 };
439
440 #endif /* FUSB302_H */
441