linux/drivers/mux/adg792a.c
<<
>>
Prefs
   1/*
   2 * Multiplexer driver for Analog Devices ADG792A/G Triple 4:1 mux
   3 *
   4 * Copyright (C) 2017 Axentia Technologies AB
   5 *
   6 * Author: Peter Rosin <peda@axentia.se>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/err.h>
  14#include <linux/i2c.h>
  15#include <linux/module.h>
  16#include <linux/mux/driver.h>
  17#include <linux/property.h>
  18
  19#define ADG792A_LDSW            BIT(0)
  20#define ADG792A_RESETB          BIT(1)
  21#define ADG792A_DISABLE(mux)    (0x50 | (mux))
  22#define ADG792A_DISABLE_ALL     (0x5f)
  23#define ADG792A_MUX(mux, state) (0xc0 | (((mux) + 1) << 2) | (state))
  24#define ADG792A_MUX_ALL(state)  (0xc0 | (state))
  25
  26static int adg792a_write_cmd(struct i2c_client *i2c, u8 cmd, int reset)
  27{
  28        u8 data = ADG792A_RESETB | ADG792A_LDSW;
  29
  30        /* ADG792A_RESETB is active low, the chip resets when it is zero. */
  31        if (reset)
  32                data &= ~ADG792A_RESETB;
  33
  34        return i2c_smbus_write_byte_data(i2c, cmd, data);
  35}
  36
  37static int adg792a_set(struct mux_control *mux, int state)
  38{
  39        struct i2c_client *i2c = to_i2c_client(mux->chip->dev.parent);
  40        u8 cmd;
  41
  42        if (mux->chip->controllers == 1) {
  43                /* parallel mux controller operation */
  44                if (state == MUX_IDLE_DISCONNECT)
  45                        cmd = ADG792A_DISABLE_ALL;
  46                else
  47                        cmd = ADG792A_MUX_ALL(state);
  48        } else {
  49                unsigned int controller = mux_control_get_index(mux);
  50
  51                if (state == MUX_IDLE_DISCONNECT)
  52                        cmd = ADG792A_DISABLE(controller);
  53                else
  54                        cmd = ADG792A_MUX(controller, state);
  55        }
  56
  57        return adg792a_write_cmd(i2c, cmd, 0);
  58}
  59
  60static const struct mux_control_ops adg792a_ops = {
  61        .set = adg792a_set,
  62};
  63
  64static int adg792a_probe(struct i2c_client *i2c,
  65                         const struct i2c_device_id *id)
  66{
  67        struct device *dev = &i2c->dev;
  68        struct mux_chip *mux_chip;
  69        s32 idle_state[3];
  70        u32 cells;
  71        int ret;
  72        int i;
  73
  74        if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  75                return -ENODEV;
  76
  77        ret = device_property_read_u32(dev, "#mux-control-cells", &cells);
  78        if (ret < 0)
  79                return ret;
  80        if (cells >= 2)
  81                return -EINVAL;
  82
  83        mux_chip = devm_mux_chip_alloc(dev, cells ? 3 : 1, 0);
  84        if (IS_ERR(mux_chip))
  85                return PTR_ERR(mux_chip);
  86
  87        mux_chip->ops = &adg792a_ops;
  88
  89        ret = adg792a_write_cmd(i2c, ADG792A_DISABLE_ALL, 1);
  90        if (ret < 0)
  91                return ret;
  92
  93        ret = device_property_read_u32_array(dev, "idle-state",
  94                                             (u32 *)idle_state,
  95                                             mux_chip->controllers);
  96        if (ret < 0) {
  97                idle_state[0] = MUX_IDLE_AS_IS;
  98                idle_state[1] = MUX_IDLE_AS_IS;
  99                idle_state[2] = MUX_IDLE_AS_IS;
 100        }
 101
 102        for (i = 0; i < mux_chip->controllers; ++i) {
 103                struct mux_control *mux = &mux_chip->mux[i];
 104
 105                mux->states = 4;
 106
 107                switch (idle_state[i]) {
 108                case MUX_IDLE_DISCONNECT:
 109                case MUX_IDLE_AS_IS:
 110                case 0 ... 4:
 111                        mux->idle_state = idle_state[i];
 112                        break;
 113                default:
 114                        dev_err(dev, "invalid idle-state %d\n", idle_state[i]);
 115                        return -EINVAL;
 116                }
 117        }
 118
 119        ret = devm_mux_chip_register(dev, mux_chip);
 120        if (ret < 0)
 121                return ret;
 122
 123        if (cells)
 124                dev_info(dev, "3x single pole quadruple throw muxes registered\n");
 125        else
 126                dev_info(dev, "triple pole quadruple throw mux registered\n");
 127
 128        return 0;
 129}
 130
 131static const struct i2c_device_id adg792a_id[] = {
 132        { .name = "adg792a", },
 133        { .name = "adg792g", },
 134        { }
 135};
 136MODULE_DEVICE_TABLE(i2c, adg792a_id);
 137
 138static const struct of_device_id adg792a_of_match[] = {
 139        { .compatible = "adi,adg792a", },
 140        { .compatible = "adi,adg792g", },
 141        { }
 142};
 143MODULE_DEVICE_TABLE(of, adg792a_of_match);
 144
 145static struct i2c_driver adg792a_driver = {
 146        .driver         = {
 147                .name           = "adg792a",
 148                .of_match_table = of_match_ptr(adg792a_of_match),
 149        },
 150        .probe          = adg792a_probe,
 151        .id_table       = adg792a_id,
 152};
 153module_i2c_driver(adg792a_driver);
 154
 155MODULE_DESCRIPTION("Analog Devices ADG792A/G Triple 4:1 mux driver");
 156MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
 157MODULE_LICENSE("GPL v2");
 158