linux/drivers/power/supply/max14656_charger_detector.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Maxim MAX14656 / AL32 USB Charger Detector driver
   4 *
   5 * Copyright (C) 2014 LG Electronics, Inc
   6 * Copyright (C) 2016 Alexander Kurz <akurz@blala.de>
   7 *
   8 * Components from Maxim AL32 Charger detection Driver for MX50 Yoshi Board
   9 * Copyright (C) Amazon Technologies Inc. All rights reserved.
  10 * Manish Lachwani (lachwani@lab126.com)
  11 */
  12#include <linux/module.h>
  13#include <linux/init.h>
  14#include <linux/delay.h>
  15#include <linux/i2c.h>
  16#include <linux/interrupt.h>
  17#include <linux/slab.h>
  18#include <linux/gpio.h>
  19#include <linux/of_gpio.h>
  20#include <linux/of_device.h>
  21#include <linux/workqueue.h>
  22#include <linux/power_supply.h>
  23
  24#define MAX14656_MANUFACTURER   "Maxim Integrated"
  25#define MAX14656_NAME           "max14656"
  26
  27#define MAX14656_DEVICE_ID      0x00
  28#define MAX14656_INTERRUPT_1    0x01
  29#define MAX14656_INTERRUPT_2    0x02
  30#define MAX14656_STATUS_1       0x03
  31#define MAX14656_STATUS_2       0x04
  32#define MAX14656_INTMASK_1      0x05
  33#define MAX14656_INTMASK_2      0x06
  34#define MAX14656_CONTROL_1      0x07
  35#define MAX14656_CONTROL_2      0x08
  36#define MAX14656_CONTROL_3      0x09
  37
  38#define DEVICE_VENDOR_MASK      0xf0
  39#define DEVICE_REV_MASK         0x0f
  40#define INT_EN_REG_MASK         BIT(4)
  41#define CHG_TYPE_INT_MASK       BIT(0)
  42#define STATUS1_VB_VALID_MASK   BIT(4)
  43#define STATUS1_CHG_TYPE_MASK   0xf
  44#define INT1_DCD_TIMEOUT_MASK   BIT(7)
  45#define CONTROL1_DEFAULT        0x0d
  46#define CONTROL1_INT_EN         BIT(4)
  47#define CONTROL1_INT_ACTIVE_HIGH        BIT(5)
  48#define CONTROL1_EDGE           BIT(7)
  49#define CONTROL2_DEFAULT        0x8e
  50#define CONTROL2_ADC_EN         BIT(0)
  51#define CONTROL3_DEFAULT        0x8d
  52
  53enum max14656_chg_type {
  54        MAX14656_NO_CHARGER     = 0,
  55        MAX14656_SDP_CHARGER,
  56        MAX14656_CDP_CHARGER,
  57        MAX14656_DCP_CHARGER,
  58        MAX14656_APPLE_500MA_CHARGER,
  59        MAX14656_APPLE_1A_CHARGER,
  60        MAX14656_APPLE_2A_CHARGER,
  61        MAX14656_SPECIAL_500MA_CHARGER,
  62        MAX14656_APPLE_12W,
  63        MAX14656_CHARGER_LAST
  64};
  65
  66static const struct max14656_chg_type_props {
  67        enum power_supply_type type;
  68} chg_type_props[] = {
  69        { POWER_SUPPLY_TYPE_UNKNOWN },
  70        { POWER_SUPPLY_TYPE_USB },
  71        { POWER_SUPPLY_TYPE_USB_CDP },
  72        { POWER_SUPPLY_TYPE_USB_DCP },
  73        { POWER_SUPPLY_TYPE_USB_DCP },
  74        { POWER_SUPPLY_TYPE_USB_DCP },
  75        { POWER_SUPPLY_TYPE_USB_DCP },
  76        { POWER_SUPPLY_TYPE_USB_DCP },
  77        { POWER_SUPPLY_TYPE_USB },
  78};
  79
  80struct max14656_chip {
  81        struct i2c_client       *client;
  82        struct power_supply     *detect_psy;
  83        struct power_supply_desc psy_desc;
  84        struct delayed_work     irq_work;
  85
  86        int irq;
  87        int online;
  88};
  89
  90static int max14656_read_reg(struct i2c_client *client, int reg, u8 *val)
  91{
  92        s32 ret;
  93
  94        ret = i2c_smbus_read_byte_data(client, reg);
  95        if (ret < 0) {
  96                dev_err(&client->dev,
  97                        "i2c read fail: can't read from %02x: %d\n",
  98                        reg, ret);
  99                return ret;
 100        }
 101        *val = ret;
 102        return 0;
 103}
 104
 105static int max14656_write_reg(struct i2c_client *client, int reg, u8 val)
 106{
 107        s32 ret;
 108
 109        ret = i2c_smbus_write_byte_data(client, reg, val);
 110        if (ret < 0) {
 111                dev_err(&client->dev,
 112                        "i2c write fail: can't write %02x to %02x: %d\n",
 113                        val, reg, ret);
 114                return ret;
 115        }
 116        return 0;
 117}
 118
 119static int max14656_read_block_reg(struct i2c_client *client, u8 reg,
 120                                  u8 length, u8 *val)
 121{
 122        int ret;
 123
 124        ret = i2c_smbus_read_i2c_block_data(client, reg, length, val);
 125        if (ret < 0) {
 126                dev_err(&client->dev, "failed to block read reg 0x%x: %d\n",
 127                                reg, ret);
 128                return ret;
 129        }
 130
 131        return 0;
 132}
 133
 134#define        REG_TOTAL_NUM   5
 135static void max14656_irq_worker(struct work_struct *work)
 136{
 137        struct max14656_chip *chip =
 138                container_of(work, struct max14656_chip, irq_work.work);
 139
 140        u8 buf[REG_TOTAL_NUM];
 141        u8 chg_type;
 142
 143        max14656_read_block_reg(chip->client, MAX14656_DEVICE_ID,
 144                                REG_TOTAL_NUM, buf);
 145
 146        if ((buf[MAX14656_STATUS_1] & STATUS1_VB_VALID_MASK) &&
 147                (buf[MAX14656_STATUS_1] & STATUS1_CHG_TYPE_MASK)) {
 148                chg_type = buf[MAX14656_STATUS_1] & STATUS1_CHG_TYPE_MASK;
 149                if (chg_type < MAX14656_CHARGER_LAST)
 150                        chip->psy_desc.type = chg_type_props[chg_type].type;
 151                else
 152                        chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
 153                chip->online = 1;
 154        } else {
 155                chip->online = 0;
 156                chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
 157        }
 158
 159        power_supply_changed(chip->detect_psy);
 160}
 161
 162static irqreturn_t max14656_irq(int irq, void *dev_id)
 163{
 164        struct max14656_chip *chip = dev_id;
 165
 166        schedule_delayed_work(&chip->irq_work, msecs_to_jiffies(100));
 167
 168        return IRQ_HANDLED;
 169}
 170
 171static int max14656_hw_init(struct max14656_chip *chip)
 172{
 173        uint8_t val = 0;
 174        uint8_t rev;
 175        struct i2c_client *client = chip->client;
 176
 177        if (max14656_read_reg(client, MAX14656_DEVICE_ID, &val))
 178                return -ENODEV;
 179
 180        if ((val & DEVICE_VENDOR_MASK) != 0x20) {
 181                dev_err(&client->dev, "wrong vendor ID %d\n",
 182                        ((val & DEVICE_VENDOR_MASK) >> 4));
 183                return -ENODEV;
 184        }
 185        rev = val & DEVICE_REV_MASK;
 186
 187        /* Turn on ADC_EN */
 188        if (max14656_write_reg(client, MAX14656_CONTROL_2, CONTROL2_ADC_EN))
 189                return -EINVAL;
 190
 191        /* turn on interrupts and low power mode */
 192        if (max14656_write_reg(client, MAX14656_CONTROL_1,
 193                CONTROL1_DEFAULT |
 194                CONTROL1_INT_EN |
 195                CONTROL1_INT_ACTIVE_HIGH |
 196                CONTROL1_EDGE))
 197                return -EINVAL;
 198
 199        if (max14656_write_reg(client, MAX14656_INTMASK_1, 0x3))
 200                return -EINVAL;
 201
 202        if (max14656_write_reg(client, MAX14656_INTMASK_2, 0x1))
 203                return -EINVAL;
 204
 205        dev_info(&client->dev, "detected revision %d\n", rev);
 206        return 0;
 207}
 208
 209static int max14656_get_property(struct power_supply *psy,
 210                            enum power_supply_property psp,
 211                            union power_supply_propval *val)
 212{
 213        struct max14656_chip *chip = power_supply_get_drvdata(psy);
 214
 215        switch (psp) {
 216        case POWER_SUPPLY_PROP_ONLINE:
 217                val->intval = chip->online;
 218                break;
 219        case POWER_SUPPLY_PROP_MODEL_NAME:
 220                val->strval = MAX14656_NAME;
 221                break;
 222        case POWER_SUPPLY_PROP_MANUFACTURER:
 223                val->strval = MAX14656_MANUFACTURER;
 224                break;
 225        default:
 226                return -EINVAL;
 227        }
 228
 229        return 0;
 230}
 231
 232static enum power_supply_property max14656_battery_props[] = {
 233        POWER_SUPPLY_PROP_ONLINE,
 234        POWER_SUPPLY_PROP_MODEL_NAME,
 235        POWER_SUPPLY_PROP_MANUFACTURER,
 236};
 237
 238static void stop_irq_work(void *data)
 239{
 240        struct max14656_chip *chip = data;
 241
 242        cancel_delayed_work_sync(&chip->irq_work);
 243}
 244
 245
 246static int max14656_probe(struct i2c_client *client,
 247                          const struct i2c_device_id *id)
 248{
 249        struct i2c_adapter *adapter = client->adapter;
 250        struct device *dev = &client->dev;
 251        struct power_supply_config psy_cfg = {};
 252        struct max14656_chip *chip;
 253        int irq = client->irq;
 254        int ret = 0;
 255
 256        if (irq <= 0) {
 257                dev_err(dev, "invalid irq number: %d\n", irq);
 258                return -ENODEV;
 259        }
 260
 261        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
 262                dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
 263                return -ENODEV;
 264        }
 265
 266        chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
 267        if (!chip)
 268                return -ENOMEM;
 269
 270        psy_cfg.drv_data = chip;
 271        chip->client = client;
 272        chip->online = 0;
 273        chip->psy_desc.name = MAX14656_NAME;
 274        chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
 275        chip->psy_desc.properties = max14656_battery_props;
 276        chip->psy_desc.num_properties = ARRAY_SIZE(max14656_battery_props);
 277        chip->psy_desc.get_property = max14656_get_property;
 278        chip->irq = irq;
 279
 280        ret = max14656_hw_init(chip);
 281        if (ret)
 282                return -ENODEV;
 283
 284        chip->detect_psy = devm_power_supply_register(dev,
 285                       &chip->psy_desc, &psy_cfg);
 286        if (IS_ERR(chip->detect_psy)) {
 287                dev_err(dev, "power_supply_register failed\n");
 288                return -EINVAL;
 289        }
 290
 291        INIT_DELAYED_WORK(&chip->irq_work, max14656_irq_worker);
 292        ret = devm_add_action(dev, stop_irq_work, chip);
 293        if (ret) {
 294                dev_err(dev, "devm_add_action %d failed\n", ret);
 295                return ret;
 296        }
 297
 298        ret = devm_request_irq(dev, chip->irq, max14656_irq,
 299                               IRQF_TRIGGER_FALLING,
 300                               MAX14656_NAME, chip);
 301        if (ret) {
 302                dev_err(dev, "request_irq %d failed\n", chip->irq);
 303                return -EINVAL;
 304        }
 305        enable_irq_wake(chip->irq);
 306
 307        schedule_delayed_work(&chip->irq_work, msecs_to_jiffies(2000));
 308
 309        return 0;
 310}
 311
 312static const struct i2c_device_id max14656_id[] = {
 313        { "max14656", 0 },
 314        {}
 315};
 316MODULE_DEVICE_TABLE(i2c, max14656_id);
 317
 318static const struct of_device_id max14656_match_table[] = {
 319        { .compatible = "maxim,max14656", },
 320        {}
 321};
 322MODULE_DEVICE_TABLE(of, max14656_match_table);
 323
 324static struct i2c_driver max14656_i2c_driver = {
 325        .driver = {
 326                .name   = "max14656",
 327                .of_match_table = max14656_match_table,
 328        },
 329        .probe          = max14656_probe,
 330        .id_table       = max14656_id,
 331};
 332module_i2c_driver(max14656_i2c_driver);
 333
 334MODULE_DESCRIPTION("MAX14656 USB charger detector");
 335MODULE_LICENSE("GPL v2");
 336