AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 2 | /* |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 3 | * Copyright 2020 Google, LLC |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #ifndef MAX77759_H_ |
| 17 | #define MAX77759_H_ |
| 18 | |
| 19 | #include <linux/i2c.h> |
AleX Pelosi | 90528cd | 2021-04-22 23:58:50 -0700 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 21 | |
| 22 | #define SCNPRINTF(...) scnprintf(__VA_ARGS__) |
| 23 | #include "max77759_regs.h" |
| 24 | |
| 25 | #define MAX77759_CHG_INT_COUNT 2 |
AleX Pelosi | d615018 | 2021-03-05 18:10:44 -0800 | [diff] [blame] | 26 | |
| 27 | #define MAX77759_PMIC_REV_A0 0x01 |
| 28 | #define MAX77759_PMIC_REV_A1 0x02 |
| 29 | |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 30 | #define MAX77759_PMIC_PMIC_ID_MW 0x3b |
| 31 | |
AleX Pelosi | d615018 | 2021-03-05 18:10:44 -0800 | [diff] [blame] | 32 | int max777x9_pmic_get_id(struct i2c_client *client, u8 *id, u8 *rev); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 33 | int max777x9_pmic_reg_read(struct i2c_client *client, |
| 34 | u8 addr, u8 *val, int len); |
| 35 | int max777x9_pmic_reg_write(struct i2c_client *client, |
| 36 | u8 addr, const u8 *val, int len); |
| 37 | int max777x9_pmic_reg_update(struct i2c_client *client, |
| 38 | u8 reg, u8 mask, u8 value); |
AleX Pelosi | 967cd3b | 2021-03-05 18:29:41 -0800 | [diff] [blame] | 39 | /* write to a register */ |
| 40 | int max77759_chg_reg_write(struct i2c_client *client, u8 reg, u8 value); |
George Lee | 8f1c905 | 2022-01-04 15:08:55 -0800 | [diff] [blame] | 41 | /* read a register */ |
AleX Pelosi | 40eab66 | 2021-05-18 02:40:28 -0700 | [diff] [blame] | 42 | int max77759_chg_reg_read(struct i2c_client *client, u8 reg, u8 *value); |
George Lee | 8f1c905 | 2022-01-04 15:08:55 -0800 | [diff] [blame] | 43 | /* update a register */ |
AleX Pelosi | 967cd3b | 2021-03-05 18:29:41 -0800 | [diff] [blame] | 44 | int max77759_chg_reg_update(struct i2c_client *client, u8 reg, u8 mask, u8 value); |
| 45 | /* change the mode register */ |
| 46 | int max77759_chg_mode_write(struct i2c_client *client, enum max77759_charger_modes mode); |
AleX Pelosi | 5d020a8 | 2021-03-27 20:29:47 -0700 | [diff] [blame] | 47 | /* change the insel register */ |
| 48 | int max77759_chg_insel_write(struct i2c_client *client, u8 mask, u8 value); |
yihsiangpeng | 6a6bd1d | 2021-04-01 15:55:09 +0800 | [diff] [blame] | 49 | /* read the insel register */ |
| 50 | int max77759_chg_insel_read(struct i2c_client *client, u8 *value); |
AleX Pelosi | 967cd3b | 2021-03-05 18:29:41 -0800 | [diff] [blame] | 51 | |
AleX Pelosi | 3289228 | 2020-09-11 02:29:20 -0700 | [diff] [blame] | 52 | #if IS_ENABLED(CONFIG_PMIC_MAX77729) |
Ted Lin | 855504f | 2020-11-23 14:44:12 +0800 | [diff] [blame] | 53 | extern int max77759_read_batt_conn(struct i2c_client *client, int *temp); |
AleX Pelosi | 3289228 | 2020-09-11 02:29:20 -0700 | [diff] [blame] | 54 | extern int max77759_read_usb_temp(struct i2c_client *client, int *temp); |
| 55 | extern int max77759_read_batt_id(struct i2c_client *client, unsigned int *id); |
| 56 | #else |
Ted Lin | 855504f | 2020-11-23 14:44:12 +0800 | [diff] [blame] | 57 | static inline int max77759_read_batt_conn(struct i2c_client *client, int *temp) |
| 58 | { |
| 59 | return -ENODEV; |
| 60 | } |
AleX Pelosi | 3289228 | 2020-09-11 02:29:20 -0700 | [diff] [blame] | 61 | static inline int max77759_read_usb_temp(struct i2c_client *client, int *temp) |
| 62 | { |
| 63 | return -ENODEV; |
| 64 | } |
| 65 | static inline int max77759_read_batt_id(struct i2c_client *client, |
| 66 | unsigned int *id) |
| 67 | { |
| 68 | return -ENODEV; |
| 69 | } |
| 70 | #endif |
| 71 | |
AleX Pelosi | 09a5861 | 2021-02-27 00:09:11 -0800 | [diff] [blame] | 72 | /* ---------------------------------------------------------------------------- |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 73 | * GS101 usecases |
AleX Pelosi | 09a5861 | 2021-02-27 00:09:11 -0800 | [diff] [blame] | 74 | * Platform specific, will need to be moved outside the driver. |
| 75 | * |
| 76 | * Case USB_chg USB_otg WLC_chg WLC_TX PMIC_Charger Ext_B LSx Name |
| 77 | * ---------------------------------------------------------------------------- |
| 78 | * 1-1 1 0 x 0 IF-PMIC-VBUS 0 0/0 USB_CHG |
| 79 | * 1-2 2 0 x 0 DC VBUS 0 0/0 USB_DC |
| 80 | * 2-1 1 0 0 1 IF-PMIC-VBUS 2 0/1 USB_CHG_WLC_TX |
| 81 | * 2-2 2 0 0 1 DC CHG 2 0/1 USB_DC_WLC_TX |
| 82 | * 3-1 0 0 1 0 IF-PMIC-WCIN 0 0/0 WLC_RX |
| 83 | * 3-2 0 0 2 0 DC WCIN 0 0/0 WLC_DC |
| 84 | * 4-1 0 1 1 0 IF-PMIC-WCIN 1 1/0 USB_OTG_WLC_RX |
| 85 | * 4-2 0 1 2 0 DC WCIN 1 1/0 USB_OTG_WLC_DC |
| 86 | * 5-1 0 1 0 0 0 1 1/0 USB_OTG |
| 87 | * 5-2 0 1 0 0 OTG 5V 0 0/0 USB_OTG_FRS |
| 88 | * 6-2 0 0 0 1 0 2 0/1 WLC_TX |
| 89 | * 7-2 0 1 0 1 MW OTG 5V 2 0/1 USB_OTG_WLC_TX |
| 90 | * 8 0 0 0 0 0 0 0/0 IDLE |
| 91 | * ---------------------------------------------------------------------------- |
| 92 | * |
| 93 | * Ext_Boost = 0 off, 1 = OTG 5V, 2 = WTX 7.5 |
| 94 | * USB_chg = 0 off, 1 = on, 2 = PPS |
| 95 | * WLC_chg = 0 off, 1 = on, 2 = PPS |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 96 | */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 97 | struct max77759_foreach_cb_data { |
| 98 | struct gvotable_election *el; |
| 99 | |
| 100 | const char *reason; |
| 101 | |
AleX Pelosi | 8ccfc50 | 2021-05-13 20:04:56 -0700 | [diff] [blame] | 102 | int chgr_on; /* CC_MAX != 0 */ |
AleX Pelosi | 3e4d146 | 2021-03-22 14:20:54 -0700 | [diff] [blame] | 103 | bool stby_on; /* on disconnect, mode=0 */ |
AleX Pelosi | af596a2 | 2021-06-18 17:06:33 -0700 | [diff] [blame] | 104 | bool charge_done; |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 105 | |
AleX Pelosi | dadc949 | 2021-03-23 13:01:11 -0700 | [diff] [blame] | 106 | int chgin_off; /* input_suspend, mode=0 */ |
| 107 | int wlcin_off; /* input_suspend, mode=0 */ |
AleX Pelosi | 960eb0a | 2021-07-20 20:09:18 -0700 | [diff] [blame] | 108 | int usb_wlc; /* input_suspend, mode=0 */ |
AleX Pelosi | dadc949 | 2021-03-23 13:01:11 -0700 | [diff] [blame] | 109 | |
| 110 | /* wlc_on is the same as wlc_rx */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 111 | bool buck_on; /* wired power in (chgin_on) from TCPCI */ |
| 112 | |
| 113 | bool otg_on; /* power out, usually external */ |
AleX Pelosi | 00cd96d | 2021-03-25 09:46:50 -0700 | [diff] [blame] | 114 | bool frs_on; /* power out, fast role swap (internal) */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 115 | |
AleX Pelosi | 29ca355 | 2021-03-15 23:24:37 -0700 | [diff] [blame] | 116 | bool wlc_rx; /* charging wireless */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 117 | bool wlc_tx; /* battery share */ |
| 118 | |
AleX Pelosi | 00cd96d | 2021-03-25 09:46:50 -0700 | [diff] [blame] | 119 | bool dc_on; /* DC requested - wired or wireless */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 120 | |
AleX Pelosi | 00cd96d | 2021-03-25 09:46:50 -0700 | [diff] [blame] | 121 | bool boost_on; /* Compat: old for WLC program */ |
| 122 | bool uno_on; /* Compat: old for WLC program */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 123 | |
| 124 | u8 raw_value; /* hard override */ |
| 125 | bool use_raw; |
| 126 | |
Jack Wu | 15abe93 | 2022-11-14 20:11:24 +0800 | [diff] [blame] | 127 | bool pogo_vin; /* pogo ovp_en */ |
Jack Wu | 71efd60 | 2022-07-07 16:22:31 +0800 | [diff] [blame] | 128 | bool pogo_vout; /* pogo 5v vout */ |
| 129 | |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 130 | u8 reg; |
Wasb Liu | 198b96d | 2022-07-28 11:13:15 +0800 | [diff] [blame] | 131 | |
| 132 | struct gvotable_election *dc_avail_votable; /* DC_AVAIL */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 135 | /* internal system values */ |
| 136 | enum { |
AleX Pelosi | 00cd96d | 2021-03-25 09:46:50 -0700 | [diff] [blame] | 137 | /* Charging disabled (go to mode 0) */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 138 | GBMS_CHGR_MODE_STBY_ON = 0x10 + MAX77759_CHGR_MODE_ALL_OFF, |
AleX Pelosi | dadc949 | 2021-03-23 13:01:11 -0700 | [diff] [blame] | 139 | /* USB inflow off */ |
| 140 | GBMS_CHGR_MODE_CHGIN_OFF = 0x11 + MAX77759_CHGR_MODE_ALL_OFF, |
| 141 | /* WCIN inflow off */ |
| 142 | GBMS_CHGR_MODE_WLCIN_OFF = 0x12 + MAX77759_CHGR_MODE_ALL_OFF, |
AleX Pelosi | 960eb0a | 2021-07-20 20:09:18 -0700 | [diff] [blame] | 143 | /* USB + WLC_RX mode */ |
| 144 | GBMS_CHGR_MODE_USB_WLC_RX = 0x13 + MAX77759_CHGR_MODE_ALL_OFF, |
| 145 | |
AleX Pelosi | dadc949 | 2021-03-23 13:01:11 -0700 | [diff] [blame] | 146 | /* charging enabled (charging current != 0) */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 147 | GBMS_CHGR_MODE_CHGR_BUCK_ON = 0x10 + MAX77759_CHGR_MODE_CHGR_BUCK_ON, |
AleX Pelosi | dadc949 | 2021-03-23 13:01:11 -0700 | [diff] [blame] | 148 | /* Compat: old for programmging */ |
AleX Pelosi | 327e693 | 2021-02-03 15:03:00 -0800 | [diff] [blame] | 149 | GBMS_CHGR_MODE_BOOST_UNO_ON = 0x10 + MAX77759_CHGR_MODE_BOOST_UNO_ON, |
| 150 | }; |
| 151 | |
| 152 | |
Badhri Jagan Sridharan | 991b7b5 | 2020-11-10 00:00:35 -0800 | [diff] [blame] | 153 | #endif |