linux/drivers/i2c/muxes/i2c-mux-ltc4306.c
<<
>>
Prefs
   1/*
   2 * Linear Technology LTC4306 and LTC4305 I2C multiplexer/switch
   3 *
   4 * Copyright (C) 2017 Analog Devices Inc.
   5 *
   6 * Licensed under the GPL-2.
   7 *
   8 * Based on: i2c-mux-pca954x.c
   9 *
  10 * Datasheet: http://cds.linear.com/docs/en/datasheet/4306.pdf
  11 */
  12
  13#include <linux/gpio/consumer.h>
  14#include <linux/gpio/driver.h>
  15#include <linux/i2c-mux.h>
  16#include <linux/i2c.h>
  17#include <linux/module.h>
  18#include <linux/of.h>
  19#include <linux/of_device.h>
  20#include <linux/property.h>
  21#include <linux/regmap.h>
  22#include <linux/slab.h>
  23
  24#define LTC4305_MAX_NCHANS 2
  25#define LTC4306_MAX_NCHANS 4
  26
  27#define LTC_REG_STATUS  0x0
  28#define LTC_REG_CONFIG  0x1
  29#define LTC_REG_MODE    0x2
  30#define LTC_REG_SWITCH  0x3
  31
  32#define LTC_DOWNSTREAM_ACCL_EN  BIT(6)
  33#define LTC_UPSTREAM_ACCL_EN    BIT(7)
  34
  35#define LTC_GPIO_ALL_INPUT      0xC0
  36#define LTC_SWITCH_MASK         0xF0
  37
  38enum ltc_type {
  39        ltc_4305,
  40        ltc_4306,
  41};
  42
  43struct chip_desc {
  44        u8 nchans;
  45        u8 num_gpios;
  46};
  47
  48struct ltc4306 {
  49        struct regmap *regmap;
  50        struct gpio_chip gpiochip;
  51        const struct chip_desc *chip;
  52};
  53
  54static const struct chip_desc chips[] = {
  55        [ltc_4305] = {
  56                .nchans = LTC4305_MAX_NCHANS,
  57        },
  58        [ltc_4306] = {
  59                .nchans = LTC4306_MAX_NCHANS,
  60                .num_gpios = 2,
  61        },
  62};
  63
  64static bool ltc4306_is_volatile_reg(struct device *dev, unsigned int reg)
  65{
  66        return (reg == LTC_REG_CONFIG) ? true : false;
  67}
  68
  69static const struct regmap_config ltc4306_regmap_config = {
  70        .reg_bits = 8,
  71        .val_bits = 8,
  72        .max_register = LTC_REG_SWITCH,
  73        .volatile_reg = ltc4306_is_volatile_reg,
  74        .cache_type = REGCACHE_FLAT,
  75};
  76
  77static int ltc4306_gpio_get(struct gpio_chip *chip, unsigned int offset)
  78{
  79        struct ltc4306 *data = gpiochip_get_data(chip);
  80        unsigned int val;
  81        int ret;
  82
  83        ret = regmap_read(data->regmap, LTC_REG_CONFIG, &val);
  84        if (ret < 0)
  85                return ret;
  86
  87        return !!(val & BIT(1 - offset));
  88}
  89
  90static void ltc4306_gpio_set(struct gpio_chip *chip, unsigned int offset,
  91                             int value)
  92{
  93        struct ltc4306 *data = gpiochip_get_data(chip);
  94
  95        regmap_update_bits(data->regmap, LTC_REG_CONFIG, BIT(5 - offset),
  96                           value ? BIT(5 - offset) : 0);
  97}
  98
  99static int ltc4306_gpio_get_direction(struct gpio_chip *chip,
 100                                      unsigned int offset)
 101{
 102        struct ltc4306 *data = gpiochip_get_data(chip);
 103        unsigned int val;
 104        int ret;
 105
 106        ret = regmap_read(data->regmap, LTC_REG_MODE, &val);
 107        if (ret < 0)
 108                return ret;
 109
 110        return !!(val & BIT(7 - offset));
 111}
 112
 113static int ltc4306_gpio_direction_input(struct gpio_chip *chip,
 114                                        unsigned int offset)
 115{
 116        struct ltc4306 *data = gpiochip_get_data(chip);
 117
 118        return regmap_update_bits(data->regmap, LTC_REG_MODE,
 119                                  BIT(7 - offset), BIT(7 - offset));
 120}
 121
 122static int ltc4306_gpio_direction_output(struct gpio_chip *chip,
 123                                         unsigned int offset, int value)
 124{
 125        struct ltc4306 *data = gpiochip_get_data(chip);
 126
 127        ltc4306_gpio_set(chip, offset, value);
 128        return regmap_update_bits(data->regmap, LTC_REG_MODE,
 129                                  BIT(7 - offset), 0);
 130}
 131
 132static int ltc4306_gpio_set_config(struct gpio_chip *chip,
 133                                   unsigned int offset, unsigned long config)
 134{
 135        struct ltc4306 *data = gpiochip_get_data(chip);
 136        unsigned int val;
 137
 138        switch (pinconf_to_config_param(config)) {
 139        case PIN_CONFIG_DRIVE_OPEN_DRAIN:
 140                val = 0;
 141                break;
 142        case PIN_CONFIG_DRIVE_PUSH_PULL:
 143                val = BIT(4 - offset);
 144                break;
 145        default:
 146                return -ENOTSUPP;
 147        }
 148
 149        return regmap_update_bits(data->regmap, LTC_REG_MODE,
 150                                  BIT(4 - offset), val);
 151}
 152
 153static int ltc4306_gpio_init(struct ltc4306 *data)
 154{
 155        struct device *dev = regmap_get_device(data->regmap);
 156
 157        if (!data->chip->num_gpios)
 158                return 0;
 159
 160        data->gpiochip.label = dev_name(dev);
 161        data->gpiochip.base = -1;
 162        data->gpiochip.ngpio = data->chip->num_gpios;
 163        data->gpiochip.parent = dev;
 164        data->gpiochip.can_sleep = true;
 165        data->gpiochip.get_direction = ltc4306_gpio_get_direction;
 166        data->gpiochip.direction_input = ltc4306_gpio_direction_input;
 167        data->gpiochip.direction_output = ltc4306_gpio_direction_output;
 168        data->gpiochip.get = ltc4306_gpio_get;
 169        data->gpiochip.set = ltc4306_gpio_set;
 170        data->gpiochip.set_config = ltc4306_gpio_set_config;
 171        data->gpiochip.owner = THIS_MODULE;
 172
 173        /* gpiolib assumes all GPIOs default input */
 174        regmap_write(data->regmap, LTC_REG_MODE, LTC_GPIO_ALL_INPUT);
 175
 176        return devm_gpiochip_add_data(dev, &data->gpiochip, data);
 177}
 178
 179static int ltc4306_select_mux(struct i2c_mux_core *muxc, u32 chan)
 180{
 181        struct ltc4306 *data = i2c_mux_priv(muxc);
 182
 183        return regmap_update_bits(data->regmap, LTC_REG_SWITCH,
 184                                  LTC_SWITCH_MASK, BIT(7 - chan));
 185}
 186
 187static int ltc4306_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
 188{
 189        struct ltc4306 *data = i2c_mux_priv(muxc);
 190
 191        return regmap_update_bits(data->regmap, LTC_REG_SWITCH,
 192                                  LTC_SWITCH_MASK, 0);
 193}
 194
 195static const struct i2c_device_id ltc4306_id[] = {
 196        { "ltc4305", ltc_4305 },
 197        { "ltc4306", ltc_4306 },
 198        { }
 199};
 200MODULE_DEVICE_TABLE(i2c, ltc4306_id);
 201
 202static const struct of_device_id ltc4306_of_match[] = {
 203        { .compatible = "lltc,ltc4305", .data = &chips[ltc_4305] },
 204        { .compatible = "lltc,ltc4306", .data = &chips[ltc_4306] },
 205        { }
 206};
 207MODULE_DEVICE_TABLE(of, ltc4306_of_match);
 208
 209static int ltc4306_probe(struct i2c_client *client,
 210                         const struct i2c_device_id *id)
 211{
 212        struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
 213        const struct chip_desc *chip;
 214        struct i2c_mux_core *muxc;
 215        struct ltc4306 *data;
 216        struct gpio_desc *gpio;
 217        bool idle_disc;
 218        unsigned int val = 0;
 219        int num, ret;
 220
 221        chip = of_device_get_match_data(&client->dev);
 222
 223        if (!chip)
 224                chip = &chips[id->driver_data];
 225
 226        idle_disc = device_property_read_bool(&client->dev,
 227                                              "i2c-mux-idle-disconnect");
 228
 229        muxc = i2c_mux_alloc(adap, &client->dev,
 230                             chip->nchans, sizeof(*data),
 231                             I2C_MUX_LOCKED, ltc4306_select_mux,
 232                             idle_disc ? ltc4306_deselect_mux : NULL);
 233        if (!muxc)
 234                return -ENOMEM;
 235        data = i2c_mux_priv(muxc);
 236        data->chip = chip;
 237
 238        i2c_set_clientdata(client, muxc);
 239
 240        data->regmap = devm_regmap_init_i2c(client, &ltc4306_regmap_config);
 241        if (IS_ERR(data->regmap)) {
 242                ret = PTR_ERR(data->regmap);
 243                dev_err(&client->dev, "Failed to allocate register map: %d\n",
 244                        ret);
 245                return ret;
 246        }
 247
 248        /* Reset and enable the mux if an enable GPIO is specified. */
 249        gpio = devm_gpiod_get_optional(&client->dev, "enable", GPIOD_OUT_LOW);
 250        if (IS_ERR(gpio))
 251                return PTR_ERR(gpio);
 252
 253        if (gpio) {
 254                udelay(1);
 255                gpiod_set_value(gpio, 1);
 256        }
 257
 258        /*
 259         * Write the mux register at addr to verify
 260         * that the mux is in fact present. This also
 261         * initializes the mux to disconnected state.
 262         */
 263        if (regmap_write(data->regmap, LTC_REG_SWITCH, 0) < 0) {
 264                dev_warn(&client->dev, "probe failed\n");
 265                return -ENODEV;
 266        }
 267
 268        if (device_property_read_bool(&client->dev,
 269                                      "ltc,downstream-accelerators-enable"))
 270                val |= LTC_DOWNSTREAM_ACCL_EN;
 271
 272        if (device_property_read_bool(&client->dev,
 273                                      "ltc,upstream-accelerators-enable"))
 274                val |= LTC_UPSTREAM_ACCL_EN;
 275
 276        if (regmap_write(data->regmap, LTC_REG_CONFIG, val) < 0)
 277                return -ENODEV;
 278
 279        ret = ltc4306_gpio_init(data);
 280        if (ret < 0)
 281                return ret;
 282
 283        /* Now create an adapter for each channel */
 284        for (num = 0; num < chip->nchans; num++) {
 285                ret = i2c_mux_add_adapter(muxc, 0, num, 0);
 286                if (ret) {
 287                        i2c_mux_del_adapters(muxc);
 288                        return ret;
 289                }
 290        }
 291
 292        dev_info(&client->dev,
 293                 "registered %d multiplexed busses for I2C switch %s\n",
 294                 num, client->name);
 295
 296        return 0;
 297}
 298
 299static int ltc4306_remove(struct i2c_client *client)
 300{
 301        struct i2c_mux_core *muxc = i2c_get_clientdata(client);
 302
 303        i2c_mux_del_adapters(muxc);
 304
 305        return 0;
 306}
 307
 308static struct i2c_driver ltc4306_driver = {
 309        .driver         = {
 310                .name   = "ltc4306",
 311                .of_match_table = of_match_ptr(ltc4306_of_match),
 312        },
 313        .probe          = ltc4306_probe,
 314        .remove         = ltc4306_remove,
 315        .id_table       = ltc4306_id,
 316};
 317
 318module_i2c_driver(ltc4306_driver);
 319
 320MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
 321MODULE_DESCRIPTION("Linear Technology LTC4306, LTC4305 I2C mux/switch driver");
 322MODULE_LICENSE("GPL v2");
 323