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 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s " fmt, __func__ |
| 17 | |
| 18 | #include <linux/bitops.h> |
| 19 | #include <linux/completion.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/of_gpio.h> |
| 27 | #include <linux/of_irq.h> |
| 28 | #include <linux/regmap.h> |
| 29 | #include <linux/workqueue.h> |
AleX Pelosi | 856679c | 2020-09-01 13:47:28 -0700 | [diff] [blame] | 30 | #include "gbms_power_supply.h" |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 31 | #include "gbms_storage.h" |
| 32 | |
| 33 | #define MAX77729_UIC_HW_REV 0x00 |
| 34 | #define MAX77729_UIC_FW_REV 0x01 |
| 35 | #define MAX77729_UIC_INT 0x02 |
| 36 | #define MAX77729_CC_INT 0x03 |
| 37 | #define MAX77729_PD_INT 0x04 |
| 38 | #define MAX77729_VDM_INT 0x05 |
| 39 | #define MAX77729_USBC_STATUS1 0x06 |
| 40 | #define MAX77729_USBC_STATUS2 0x07 |
| 41 | #define MAX77729_BC_STATUS 0x08 |
| 42 | #define MAX77729_UIC_FW_REV2 0x09 |
| 43 | #define MAX77729_CC_STATUS1 0x0a |
| 44 | #define MAX77729_CC_STATUS2 0x0b |
| 45 | #define MAX77729_PD_STATUS1 0x0c |
| 46 | #define MAX77729_PD_STATUS2 0x0d |
| 47 | #define MAX77729_UIC_INT_M 0x0e |
| 48 | #define MAX77729_CC_INT_M 0x0f |
| 49 | #define MAX77729_PD_INT_M 0x10 |
| 50 | |
| 51 | #define MAX77729_AP_DATAOUT0 0x21 |
| 52 | #define MAX77729_AP_DATAOUT1 0x22 |
| 53 | #define MAX77729_AP_DATAOUT32 0x41 |
| 54 | #define MAX77729_AP_DATAIN0 0x51 |
| 55 | #define MAX77729_AP_DATAIN1 0x52 |
| 56 | |
| 57 | #define MAX77729_UIC_BC_CTRL1_CONFIG_READ 0x01 |
| 58 | #define MAX77729_UIC_BC_CTRL1_CONFIG_WRITE 0x02 |
| 59 | |
| 60 | #define NOAUTOIBUS_SHIFT 5 |
| 61 | #define WAIT_STEP_MS 10 |
| 62 | #define WAIT_MAX_TRIES 10 |
| 63 | |
| 64 | #define MAX77729_UIC_INT_APCMDRESI 0x80 |
| 65 | #define MAX77729_UIC_INT_SYSMSGI 0x40 |
| 66 | #define MAX77729_UIC_INT_VBUSDETI 0x20 |
| 67 | #define MAX77729_UIC_INT_VBADCI 0x10 |
| 68 | #define MAX77729_UIC_INT_DCDTMOI 0x08 |
| 69 | #define MAX77729_UIC_INT_FAKEVBUSI 0x04 |
| 70 | #define MAX77729_UIC_INT_CHGTYPI 0x02 |
| 71 | #define MAX77729_UIC_INT_UIDADCI 0x01 |
| 72 | |
| 73 | #define MAX77729_BC_STATUS_VBUSDET 0x80 |
| 74 | |
| 75 | #define NAI_DWELL_TIME 3000 |
| 76 | #define BC_CTRL1_DWELL_TIME 1000 |
| 77 | #define BC_CTRL1_DEFAULT 0xe5 |
| 78 | #define OP_CC_CTRL_WRITE 0x0c |
| 79 | #define OP_CC_CTRL_CCSRCSNK 0x10 |
| 80 | |
| 81 | #define CHGTYP_MASK GENMASK(1, 0) |
| 82 | #define CHGTYP_NONE 0x0 |
| 83 | #define CHGTYP_SDP 0x1 |
| 84 | #define CHGTYP_CDP 0x2 |
| 85 | #define CHGTYP_DCP 0x3 |
| 86 | |
| 87 | #define OP_GPIOX_READ 0x23 |
| 88 | #define OP_GPIOX_WRITE 0x24 |
| 89 | #define GPIO_DIR_OUT 1 |
| 90 | #define GPIO_DIR_IN 0 |
| 91 | #define GPIO_OUT_HI 1 |
| 92 | #define GPIO_OUT_LO 0 |
| 93 | |
| 94 | #define OP_CC_CTRL3_READ 0xF |
| 95 | #define CC_LP_MODE BIT(4) |
| 96 | #define OP_CC_CTRL3_WRITE 0x10 |
| 97 | |
| 98 | #define MAX77729_STORAGE_SIZE 8 |
| 99 | #define MAX77729_STORAGE_BASE (MAX77729_AP_DATAOUT0 + MAX77729_STORAGE_SIZE) |
| 100 | |
| 101 | #ifdef CONFIG_DEBUG_FS |
| 102 | #include <linux/debugfs.h> |
| 103 | #include <linux/seq_file.h> |
| 104 | #endif |
| 105 | |
| 106 | #define MAX77729_INT_DEFAULT_MASK (MAX77729_UIC_INT_VBUSDETI | \ |
| 107 | MAX77729_UIC_INT_APCMDRESI | \ |
| 108 | MAX77729_UIC_INT_CHGTYPI | \ |
| 109 | MAX77729_UIC_INT_DCDTMOI) |
| 110 | |
| 111 | struct max77729_uic_data { |
| 112 | struct device *dev; |
| 113 | struct i2c_client *client; |
| 114 | struct regmap *regmap; |
| 115 | int irq; |
| 116 | int irq_gpio; |
| 117 | uint8_t bc_ctrl1; |
| 118 | uint8_t cmd_pending; |
| 119 | struct delayed_work noautoibus_work; |
| 120 | struct completion cmd_done; |
| 121 | struct mutex io_lock; |
| 122 | struct power_supply *usb_psy; |
| 123 | struct mutex gpio_lock; |
| 124 | struct mutex cc_ctrl3_lock; |
| 125 | bool probe_done; |
| 126 | |
| 127 | struct dentry *de; |
| 128 | }; |
| 129 | |
AleX Pelosi | 5077fc5 | 2020-09-10 21:44:48 -0700 | [diff] [blame] | 130 | /* silence warning */ |
| 131 | extern int max77729_disable_water_detection(struct i2c_client *client); |
| 132 | extern int max77729_gpio_set(struct i2c_client *client, unsigned int gpio, |
| 133 | bool dir_out, bool out_hi); |
| 134 | |
| 135 | |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 136 | static bool max77729_uic_is_reg(struct device *dev, unsigned int reg) |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 137 | { |
| 138 | int ret; |
| 139 | |
| 140 | switch (reg) { |
| 141 | case 0x00 ... 0x71: |
| 142 | ret = true; |
| 143 | break; |
| 144 | default: |
| 145 | ret = false; |
| 146 | break; |
| 147 | } |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static const struct regmap_config max77729_uic_regmap_cfg = { |
| 152 | .name = "max77729_uic", |
| 153 | .reg_bits = 8, |
| 154 | .val_bits = 8, |
| 155 | .val_format_endian = REGMAP_ENDIAN_NATIVE, |
| 156 | .max_register = 0x71, |
| 157 | .readable_reg = max77729_uic_is_reg, |
| 158 | .volatile_reg = max77729_uic_is_reg, |
| 159 | }; |
| 160 | |
| 161 | static inline int max77729_uic_read(struct regmap *uic_regmap, |
| 162 | int addr, u8 *val, int len) |
| 163 | { |
| 164 | int rc; |
| 165 | |
| 166 | if (!uic_regmap) |
| 167 | return -ENXIO; |
| 168 | |
| 169 | rc = regmap_bulk_read(uic_regmap, addr, val, len); |
| 170 | if (rc < 0) { |
| 171 | pr_err("regmap_read failed for address %04x rc=%d\n", |
| 172 | addr, rc); |
| 173 | return rc; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static inline int max77729_uic_write(struct regmap *uic_regmap, int addr, |
| 180 | const u8 *val, int len) |
| 181 | { |
| 182 | int rc; |
| 183 | |
| 184 | if (!uic_regmap) |
| 185 | return -ENXIO; |
| 186 | |
| 187 | rc = regmap_bulk_write(uic_regmap, addr, val, len); |
| 188 | if (rc < 0) { |
| 189 | pr_err("regmap_write failed for address %04x rc=%d\n", |
| 190 | addr, rc); |
| 191 | return rc; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static inline int max77729_uic_wait(struct i2c_client *client) |
| 198 | { |
| 199 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 200 | unsigned long rc; |
| 201 | |
| 202 | rc = wait_for_completion_timeout(&data->cmd_done, |
| 203 | msecs_to_jiffies(BC_CTRL1_DWELL_TIME)); |
| 204 | if (!rc) { |
| 205 | dev_err(data->dev, "timeout waiting for cmd 0x%02x\n", |
| 206 | data->cmd_pending); |
| 207 | return -ETIME; |
| 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* Adopted from one of the earlier patches from Jim */ |
| 213 | static int max77729_uic_opcode_read(struct i2c_client *client, |
| 214 | uint8_t op, uint8_t *val) |
| 215 | { |
| 216 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 217 | int rc; |
| 218 | uint8_t buf[2]; |
| 219 | |
| 220 | mutex_lock(&data->io_lock); |
| 221 | buf[0] = MAX77729_AP_DATAOUT0; |
| 222 | buf[1] = op; |
| 223 | rc = i2c_master_send(client, buf, 2); |
| 224 | if (rc < 0) |
| 225 | goto opcode_read_out; |
| 226 | buf[0] = MAX77729_AP_DATAOUT32; |
| 227 | buf[1] = 0x00; |
| 228 | rc = i2c_master_send(client, buf, 2); |
| 229 | if (rc < 0) |
| 230 | goto opcode_read_out; |
| 231 | |
| 232 | rc = max77729_uic_wait(client); |
| 233 | if (rc < 0) |
| 234 | goto opcode_read_out; |
| 235 | |
| 236 | buf[0] = MAX77729_AP_DATAIN1; |
| 237 | rc = i2c_master_send(client, buf, 1); |
| 238 | if (rc < 0) |
| 239 | goto opcode_read_out; |
| 240 | rc = i2c_master_recv(client, buf, 1); |
| 241 | if (rc < 0) |
| 242 | goto opcode_read_out; |
| 243 | |
| 244 | *val = buf[0]; |
| 245 | |
| 246 | opcode_read_out: |
| 247 | mutex_unlock(&data->io_lock); |
| 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | static int max77729_uic_opcode_write(struct i2c_client *client, |
| 252 | uint8_t op, uint8_t val) |
| 253 | { |
| 254 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 255 | int rc; |
| 256 | uint8_t buf[2]; |
| 257 | |
| 258 | mutex_lock(&data->io_lock); |
| 259 | data->cmd_pending = op; |
| 260 | reinit_completion(&data->cmd_done); |
| 261 | |
| 262 | /* write updated result */ |
| 263 | buf[0] = MAX77729_AP_DATAOUT0; |
| 264 | buf[1] = op; |
| 265 | rc = i2c_master_send(client, buf, 2); |
| 266 | if (rc < 0) |
| 267 | goto opcode_write_out; |
| 268 | buf[0] = MAX77729_AP_DATAOUT1; |
| 269 | buf[1] = val; |
| 270 | rc = i2c_master_send(client, buf, 2); |
| 271 | if (rc < 0) |
| 272 | goto opcode_write_out; |
| 273 | buf[0] = MAX77729_AP_DATAOUT32; |
| 274 | buf[1] = 0x00; |
| 275 | rc = i2c_master_send(client, buf, 2); |
| 276 | if (rc < 0) |
| 277 | goto opcode_write_out; |
| 278 | |
| 279 | rc = max77729_uic_wait(client); |
| 280 | |
| 281 | data->cmd_pending = 0; |
| 282 | opcode_write_out: |
| 283 | mutex_unlock(&data->io_lock); |
| 284 | |
| 285 | return rc; |
| 286 | } |
| 287 | |
| 288 | int max77729_disable_water_detection(struct i2c_client *client) |
| 289 | { |
| 290 | uint8_t ctrl3_write, ctrl3_readback; |
| 291 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 292 | |
| 293 | if (!data || !data->probe_done) |
| 294 | return -EAGAIN; |
| 295 | |
| 296 | mutex_lock(&data->cc_ctrl3_lock); |
| 297 | max77729_uic_opcode_read(client, OP_CC_CTRL3_READ, |
| 298 | &ctrl3_write); |
| 299 | ctrl3_write = ctrl3_write & ~CC_LP_MODE; |
| 300 | |
| 301 | max77729_uic_opcode_write(client, OP_CC_CTRL3_WRITE, |
| 302 | ctrl3_write); |
| 303 | max77729_uic_opcode_read(client, OP_CC_CTRL3_READ, |
| 304 | &ctrl3_readback); |
| 305 | mutex_unlock(&data->cc_ctrl3_lock); |
| 306 | |
| 307 | return ctrl3_write == ctrl3_readback ? 0 : -1; |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(max77729_disable_water_detection); |
| 310 | |
| 311 | int max77729_gpio_set(struct i2c_client *client, unsigned int gpio, |
| 312 | bool dir_out, bool out_hi) |
| 313 | { |
| 314 | uint8_t gpio_dir_val = 0, gpio_out_val = 0, gpio_val = 0; |
| 315 | uint8_t gpio_dir_offset = 0, gpio_current_setting = 0, |
| 316 | gpio_read_back; |
| 317 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 318 | |
| 319 | if (!data || !data->probe_done) |
| 320 | return -EAGAIN; |
| 321 | |
| 322 | gpio_dir_val = dir_out ? GPIO_DIR_OUT : GPIO_DIR_IN; |
| 323 | if (dir_out) |
| 324 | gpio_out_val = out_hi ? GPIO_OUT_HI : GPIO_OUT_LO; |
| 325 | |
| 326 | gpio_dir_offset = (gpio - 1) * 2; |
| 327 | |
| 328 | gpio_val = ((gpio_out_val << 1) | gpio_dir_val) << gpio_dir_offset; |
| 329 | |
| 330 | mutex_lock(&data->gpio_lock); |
| 331 | max77729_uic_opcode_read(client, OP_GPIOX_READ, |
| 332 | &gpio_current_setting); |
| 333 | |
| 334 | /* Leave other gpios in the same state */ |
| 335 | gpio_current_setting &= (0xFF & ~(3 << gpio_dir_offset)); |
| 336 | gpio_current_setting |= gpio_val; |
| 337 | |
| 338 | max77729_uic_opcode_write(client, OP_GPIOX_WRITE, |
| 339 | gpio_current_setting); |
| 340 | |
| 341 | max77729_uic_opcode_read(client, OP_GPIOX_READ, |
| 342 | &gpio_read_back); |
| 343 | mutex_unlock(&data->gpio_lock); |
| 344 | |
| 345 | return gpio_current_setting == gpio_read_back ? 0 : -1; |
| 346 | } |
| 347 | EXPORT_SYMBOL_GPL(max77729_gpio_set); |
| 348 | |
| 349 | /* TODO: implement this */ |
| 350 | static int max77729_uic_noautoibus_get(struct i2c_client *client) |
| 351 | { |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | static int max77729_uic_noautoibus_set(struct i2c_client *client) |
| 356 | { |
| 357 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 358 | uint8_t bc_stat = 0; |
| 359 | int ret; |
| 360 | |
| 361 | ret = max77729_uic_read(data->regmap, MAX77729_BC_STATUS, &bc_stat, 1); |
| 362 | if (ret < 0) |
| 363 | return ret; |
| 364 | |
| 365 | if (bc_stat & MAX77729_BC_STATUS_VBUSDET) |
| 366 | max77729_uic_opcode_write(data->client, OP_CC_CTRL_WRITE, |
| 367 | OP_CC_CTRL_CCSRCSNK); |
| 368 | |
| 369 | max77729_uic_opcode_write(data->client, |
| 370 | MAX77729_UIC_BC_CTRL1_CONFIG_WRITE, |
| 371 | data->bc_ctrl1); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static inline void max77729_cmd_complete(struct i2c_client *client) |
| 376 | { |
| 377 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 378 | int rc; |
| 379 | uint8_t buf[1]; |
| 380 | |
| 381 | buf[0] = MAX77729_AP_DATAIN0; |
| 382 | rc = i2c_master_send(client, buf, 1); |
| 383 | if (rc < 0) |
| 384 | return; |
| 385 | rc = i2c_master_recv(client, buf, 1); |
| 386 | if (rc < 0) |
| 387 | return; |
| 388 | |
| 389 | if (buf[0] == data->cmd_pending) |
| 390 | complete_all(&data->cmd_done); |
| 391 | } |
| 392 | |
| 393 | static void max77729_report_chgtype(struct max77729_uic_data *data) |
| 394 | { |
| 395 | union power_supply_propval val = { 0 }; |
| 396 | enum power_supply_usb_type usb_type; |
| 397 | uint8_t bc_status = 0; |
| 398 | int ret; |
| 399 | |
| 400 | ret = max77729_uic_read(data->regmap, MAX77729_BC_STATUS, &bc_status, |
| 401 | 1); |
| 402 | dev_info(data->dev, "report_chgtype bc_status:%x ret:%d\n", |
| 403 | bc_status, ret); |
| 404 | if (ret < 0) |
| 405 | return; |
| 406 | |
| 407 | switch (bc_status & CHGTYP_MASK) { |
| 408 | case CHGTYP_NONE: |
| 409 | usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; |
| 410 | break; |
| 411 | case CHGTYP_SDP: |
| 412 | usb_type = POWER_SUPPLY_USB_TYPE_SDP; |
| 413 | break; |
| 414 | case CHGTYP_CDP: |
| 415 | usb_type = POWER_SUPPLY_USB_TYPE_CDP; |
| 416 | break; |
| 417 | case CHGTYP_DCP: |
| 418 | usb_type = POWER_SUPPLY_USB_TYPE_DCP; |
| 419 | break; |
| 420 | default: |
| 421 | usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | val.intval = usb_type; |
| 426 | ret = power_supply_set_property(data->usb_psy, |
| 427 | POWER_SUPPLY_PROP_USB_TYPE, |
| 428 | &val); |
| 429 | if (ret) |
| 430 | dev_err(data->dev, "BC12: usb_psy update failed (%d)", ret); |
| 431 | } |
| 432 | |
| 433 | static irqreturn_t max77729_uic_irq(int irq, void *client) |
| 434 | { |
| 435 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 436 | uint8_t pd_int = 0, cc_int = 0, vdm_int = 0, uic_int; |
| 437 | int ret; |
| 438 | |
| 439 | /* clear any ints we don't care about */ |
| 440 | max77729_uic_read(data->regmap, MAX77729_PD_INT, &pd_int, 1); |
| 441 | max77729_uic_read(data->regmap, MAX77729_CC_INT, &cc_int, 1); |
| 442 | max77729_uic_read(data->regmap, MAX77729_VDM_INT, &vdm_int, 1); |
| 443 | |
| 444 | ret = max77729_uic_read(data->regmap, MAX77729_UIC_INT, &uic_int, 1); |
| 445 | if (ret < 0) { |
| 446 | dev_err_ratelimited(data->dev, |
| 447 | "failed to read register 0x%02x\n", MAX77729_UIC_INT); |
| 448 | return IRQ_NONE; |
| 449 | } |
| 450 | if (!uic_int) |
| 451 | return IRQ_NONE; |
| 452 | |
| 453 | if (uic_int & MAX77729_UIC_INT_VBUSDETI) |
| 454 | mod_delayed_work(system_wq, &data->noautoibus_work, |
| 455 | msecs_to_jiffies(NAI_DWELL_TIME)); |
| 456 | |
| 457 | if (uic_int & MAX77729_UIC_INT_APCMDRESI) |
| 458 | max77729_cmd_complete(client); |
| 459 | |
| 460 | if (uic_int & MAX77729_UIC_INT_CHGTYPI) { |
| 461 | dev_info(data->dev, "BC1.2 CHGTYPI\n"); |
| 462 | max77729_report_chgtype(data); |
| 463 | } |
| 464 | |
| 465 | if (uic_int & MAX77729_UIC_INT_DCDTMOI) |
| 466 | dev_err(data->dev, "BC1.2 DCDTMO\n"); |
| 467 | |
| 468 | return IRQ_HANDLED; |
| 469 | } |
| 470 | |
| 471 | static uint8_t max77729_get_bc_ctrl1(struct device *dev) |
| 472 | { |
| 473 | struct property *np; |
| 474 | uint32_t val; |
| 475 | uint8_t cfg = BC_CTRL1_DEFAULT; |
| 476 | |
| 477 | np = of_find_property(dev->of_node, "bc1_config", NULL); |
| 478 | if (np) { |
| 479 | if (!of_property_read_u32(dev->of_node, "bc1_config", &val)) |
| 480 | cfg = val & 0xff; |
| 481 | } |
| 482 | return cfg; |
| 483 | } |
| 484 | |
| 485 | static void max77729_noautoibus_worker(struct work_struct *work) |
| 486 | { |
| 487 | struct max77729_uic_data *data = container_of(to_delayed_work(work), |
| 488 | struct max77729_uic_data, noautoibus_work); |
| 489 | int ret, nai = -1; |
| 490 | |
| 491 | ret = max77729_uic_noautoibus_set(data->client); |
| 492 | if (ret == 0) |
| 493 | nai = max77729_uic_noautoibus_get(data->client); |
| 494 | if (nai <= 0) |
| 495 | mod_delayed_work(system_wq, &data->noautoibus_work, |
| 496 | msecs_to_jiffies(BC_CTRL1_DWELL_TIME)); |
| 497 | |
| 498 | dev_err(data->dev, "NoAutoIbus WORK ret = %d, nai=%d\n", ret, nai); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | #ifdef CONFIG_DEBUG_FS |
| 503 | static int max77729_dbg_set_noautoibus(void *d, u64 val) |
| 504 | { |
| 505 | struct max77729_uic_data *data = d; |
| 506 | int ret, nai = -1; |
| 507 | |
| 508 | ret = max77729_uic_noautoibus_set(data->client); |
| 509 | if (ret == 0) |
| 510 | nai = max77729_uic_noautoibus_get(data->client); |
| 511 | |
| 512 | dev_err(data->dev, "NoAutoIbus = %d\n", nai); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | DEFINE_SIMPLE_ATTRIBUTE(max77729_noautoibus_fops, NULL, |
AleX Pelosi | 5077fc5 | 2020-09-10 21:44:48 -0700 | [diff] [blame] | 517 | max77729_dbg_set_noautoibus, "%llu\n"); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 518 | |
| 519 | static int dbg_init_fs(struct max77729_uic_data *data) |
| 520 | { |
| 521 | data->de = debugfs_create_dir("max77729_maxq", NULL); |
| 522 | if (!data->de) |
| 523 | return -EINVAL; |
| 524 | |
| 525 | debugfs_create_file("noautoibus", 0644, data->de, data, |
| 526 | &max77729_noautoibus_fops); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | #else |
| 531 | static int dbg_init_fs(struct max77729_chgr_data *data) |
| 532 | { |
| 533 | return 0; |
| 534 | } |
| 535 | #endif |
| 536 | |
| 537 | static int max77729_uic_storage_iter(int index, gbms_tag_t *tag, void *ptr) |
| 538 | { |
| 539 | if (index < 0 || index > (GBMS_TAG_RRS7 - GBMS_TAG_RRS0)) |
| 540 | return -ENOENT; |
| 541 | *tag = GBMS_TAG_RRS0 + index; |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int max77729_uic_storage_read(gbms_tag_t tag, void *buff, size_t size, |
| 546 | void *ptr) |
| 547 | { |
| 548 | const int base = MAX77729_STORAGE_BASE + tag - GBMS_TAG_RRS0; |
| 549 | struct max77729_uic_data *data = ptr; |
| 550 | int ret; |
| 551 | |
| 552 | if (tag < GBMS_TAG_RRS0 || tag > GBMS_TAG_RRS7) |
| 553 | return -ENOENT; |
Wasb Liu | f19aa97 | 2020-11-13 23:36:08 +0800 | [diff] [blame] | 554 | if ((tag + size - 1) > GBMS_TAG_RRS7) |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 555 | return -ERANGE; |
| 556 | |
| 557 | ret = max77729_uic_read(data->regmap, base, buff, size); |
| 558 | if (ret < 0) |
| 559 | ret = -EIO; |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | static int max77729_uic_storage_write(gbms_tag_t tag, const void *buff, |
| 564 | size_t size, void *ptr) |
| 565 | { |
| 566 | const int base = MAX77729_STORAGE_BASE + tag - GBMS_TAG_RRS0; |
| 567 | struct max77729_uic_data *data = ptr; |
| 568 | int ret; |
| 569 | |
| 570 | if (tag < GBMS_TAG_RRS0 || tag > GBMS_TAG_RRS7) |
| 571 | return -ENOENT; |
Wasb Liu | f19aa97 | 2020-11-13 23:36:08 +0800 | [diff] [blame] | 572 | if ((tag + size - 1) > GBMS_TAG_RRS7) |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 573 | return -ERANGE; |
| 574 | |
| 575 | ret = max77729_uic_write(data->regmap, base, buff, size); |
| 576 | if (ret < 0) |
| 577 | ret = -EIO; |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | static struct gbms_storage_desc max77729_uic_storage_dsc = { |
| 582 | .iter = max77729_uic_storage_iter, |
| 583 | .read = max77729_uic_storage_read, |
| 584 | .write = max77729_uic_storage_write, |
| 585 | }; |
| 586 | |
| 587 | static int max77729_uic_probe(struct i2c_client *client, |
| 588 | const struct i2c_device_id *id) |
| 589 | { |
| 590 | struct max77729_uic_data *data; |
| 591 | struct device *dev = &client->dev; |
| 592 | struct power_supply *usb_psy; |
| 593 | const char *usb_psy_name; |
| 594 | uint8_t irq_mask = 0x0; /* unmask all */ |
| 595 | uint8_t hw_rev = 0; |
| 596 | int ret; |
| 597 | |
| 598 | usb_psy_name = of_get_property(dev->of_node, "usb-psy-name", NULL); |
| 599 | if (!usb_psy_name) { |
| 600 | dev_err(dev, "usb-psy-name not set\n"); |
| 601 | return -EINVAL; |
| 602 | } |
| 603 | |
| 604 | usb_psy = power_supply_get_by_name(usb_psy_name); |
| 605 | if (!usb_psy) { |
| 606 | dev_err(&client->dev, "usb psy not up, retrying....\n"); |
| 607 | return -EPROBE_DEFER; |
| 608 | } |
| 609 | |
| 610 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 611 | if (!data) |
| 612 | return -ENOMEM; |
| 613 | |
| 614 | data->dev = dev; |
| 615 | i2c_set_clientdata(client, data); |
| 616 | data->client = client; |
| 617 | data->usb_psy = usb_psy; |
| 618 | |
| 619 | data->regmap = devm_regmap_init_i2c(client, &max77729_uic_regmap_cfg); |
| 620 | if (IS_ERR(data->regmap)) { |
| 621 | dev_err(dev, "Failed to initialize regmap\n"); |
| 622 | ret = -EINVAL; |
| 623 | goto exit; |
| 624 | } |
| 625 | |
| 626 | /* check chip is available */ |
| 627 | ret = max77729_uic_read(data->regmap, MAX77729_UIC_HW_REV, &hw_rev, 1); |
| 628 | if (ret < 0) { |
| 629 | dev_err(dev, "device not available\n"); |
| 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | init_completion(&data->cmd_done); |
| 634 | INIT_DELAYED_WORK(&data->noautoibus_work, max77729_noautoibus_worker); |
| 635 | mutex_init(&data->io_lock); |
| 636 | mutex_init(&data->gpio_lock); |
| 637 | mutex_init(&data->cc_ctrl3_lock); |
| 638 | |
| 639 | data->bc_ctrl1 = max77729_get_bc_ctrl1(dev); |
| 640 | |
| 641 | data->irq_gpio = of_get_named_gpio(dev->of_node, |
| 642 | "max77729,irq-gpio", 0); |
| 643 | if (data->irq_gpio < 0) { |
| 644 | dev_err(dev, "failed get irq_gpio\n"); |
| 645 | return -EINVAL; |
| 646 | } |
| 647 | client->irq = gpio_to_irq(data->irq_gpio); |
| 648 | |
| 649 | ret = devm_request_threaded_irq(data->dev, client->irq, NULL, |
| 650 | max77729_uic_irq, |
| 651 | IRQF_TRIGGER_LOW | |
| 652 | IRQF_SHARED | |
| 653 | IRQF_ONESHOT, |
| 654 | "maxq", client); |
| 655 | if (ret == 0) |
| 656 | enable_irq_wake(client->irq); |
| 657 | |
| 658 | /* handle pending interrupts, unmask the interesting ones */ |
| 659 | max77729_uic_irq(-1, client); |
| 660 | ret = max77729_uic_write(data->regmap, MAX77729_UIC_INT_M, |
| 661 | &irq_mask, 1); |
| 662 | if (ret < 0) |
| 663 | dev_err(dev, "cannot reset irq mask %d", ret); |
| 664 | /* report port type since the irq might have been cleared in BL */ |
| 665 | max77729_report_chgtype(data); |
| 666 | |
| 667 | /* TODO: move at the beginning of probe */ |
| 668 | ret = gbms_storage_register(&max77729_uic_storage_dsc, |
| 669 | "max77729uic", data); |
| 670 | |
| 671 | dev_info(dev, "maxq: hw_rev=%x mask=%x st=%d init_work done\n", |
| 672 | hw_rev, irq_mask, ret); |
| 673 | |
| 674 | if (ret == -EBUSY) |
| 675 | ret = 0; |
| 676 | |
| 677 | dbg_init_fs(data); |
| 678 | data->probe_done = true; |
| 679 | exit: |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static int max77729_uic_remove(struct i2c_client *client) |
| 684 | { |
| 685 | struct max77729_uic_data *data = i2c_get_clientdata(client); |
| 686 | |
| 687 | cancel_delayed_work(&data->noautoibus_work); |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static const struct of_device_id max77729_uic_of_match_table[] = { |
| 693 | { .compatible = "maxim,max77729uic"}, |
| 694 | {}, |
| 695 | }; |
| 696 | MODULE_DEVICE_TABLE(of, max77729_uic_of_match_table); |
| 697 | |
| 698 | static const struct i2c_device_id max77729_uic_id[] = { |
| 699 | {"max77729_uic", 0}, |
| 700 | {} |
| 701 | }; |
| 702 | MODULE_DEVICE_TABLE(i2c, max77729_uic_id); |
| 703 | |
| 704 | #if defined CONFIG_PM |
| 705 | static int max77729_uic_pm_suspend(struct device *dev) |
| 706 | { |
| 707 | return 0; /* TODO */ |
| 708 | } |
| 709 | |
| 710 | static int max77729_uic_pm_resume(struct device *dev) |
| 711 | { |
| 712 | return 0; /* TODO */ |
| 713 | } |
| 714 | #endif |
| 715 | |
| 716 | static const struct dev_pm_ops max77729_uic_pm_ops = { |
| 717 | SET_LATE_SYSTEM_SLEEP_PM_OPS( |
| 718 | max77729_uic_pm_suspend, |
| 719 | max77729_uic_pm_resume) |
| 720 | }; |
| 721 | |
| 722 | static struct i2c_driver max77729_uic_i2c_driver = { |
| 723 | .driver = { |
| 724 | .name = "max77729-uic", |
| 725 | .owner = THIS_MODULE, |
| 726 | .of_match_table = max77729_uic_of_match_table, |
| 727 | #ifdef CONFIG_PM |
| 728 | .pm = &max77729_uic_pm_ops, |
| 729 | #endif |
| 730 | }, |
| 731 | .id_table = max77729_uic_id, |
| 732 | .probe = max77729_uic_probe, |
| 733 | .remove = max77729_uic_remove, |
| 734 | }; |
| 735 | |
| 736 | module_i2c_driver(max77729_uic_i2c_driver); |
| 737 | MODULE_DESCRIPTION("Maxim 77729 UIC driver"); |
| 738 | MODULE_AUTHOR("Jim Wylder jwylder@google.com"); |
| 739 | MODULE_LICENSE("GPL"); |