Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ADGS1408/ADGS1409 SPI MUX driver
0004  *
0005  * Copyright 2018 Analog Devices Inc.
0006  */
0007 
0008 #include <linux/err.h>
0009 #include <linux/mod_devicetable.h>
0010 #include <linux/module.h>
0011 #include <linux/mux/driver.h>
0012 #include <linux/property.h>
0013 #include <linux/spi/spi.h>
0014 
0015 #define ADGS1408_SW_DATA       (0x01)
0016 #define ADGS1408_REG_READ(reg) ((reg) | 0x80)
0017 #define ADGS1408_DISABLE       (0x00)
0018 #define ADGS1408_MUX(state)    (((state) << 1) | 1)
0019 
0020 enum adgs1408_chip_id {
0021     ADGS1408 = 1,
0022     ADGS1409,
0023 };
0024 
0025 static int adgs1408_spi_reg_write(struct spi_device *spi,
0026                   u8 reg_addr, u8 reg_data)
0027 {
0028     u8 tx_buf[2];
0029 
0030     tx_buf[0] = reg_addr;
0031     tx_buf[1] = reg_data;
0032 
0033     return spi_write_then_read(spi, tx_buf, sizeof(tx_buf), NULL, 0);
0034 }
0035 
0036 static int adgs1408_set(struct mux_control *mux, int state)
0037 {
0038     struct spi_device *spi = to_spi_device(mux->chip->dev.parent);
0039     u8 reg;
0040 
0041     if (state == MUX_IDLE_DISCONNECT)
0042         reg = ADGS1408_DISABLE;
0043     else
0044         reg = ADGS1408_MUX(state);
0045 
0046     return adgs1408_spi_reg_write(spi, ADGS1408_SW_DATA, reg);
0047 }
0048 
0049 static const struct mux_control_ops adgs1408_ops = {
0050     .set = adgs1408_set,
0051 };
0052 
0053 static int adgs1408_probe(struct spi_device *spi)
0054 {
0055     struct device *dev = &spi->dev;
0056     enum adgs1408_chip_id chip_id;
0057     struct mux_chip *mux_chip;
0058     struct mux_control *mux;
0059     s32 idle_state;
0060     int ret;
0061 
0062     chip_id = (enum adgs1408_chip_id)device_get_match_data(dev);
0063     if (!chip_id)
0064         chip_id = spi_get_device_id(spi)->driver_data;
0065 
0066     mux_chip = devm_mux_chip_alloc(dev, 1, 0);
0067     if (IS_ERR(mux_chip))
0068         return PTR_ERR(mux_chip);
0069 
0070     mux_chip->ops = &adgs1408_ops;
0071 
0072     ret = adgs1408_spi_reg_write(spi, ADGS1408_SW_DATA, ADGS1408_DISABLE);
0073     if (ret < 0)
0074         return ret;
0075 
0076     ret = device_property_read_u32(dev, "idle-state", (u32 *)&idle_state);
0077     if (ret < 0)
0078         idle_state = MUX_IDLE_AS_IS;
0079 
0080     mux = mux_chip->mux;
0081 
0082     if (chip_id == ADGS1408)
0083         mux->states = 8;
0084     else
0085         mux->states = 4;
0086 
0087     switch (idle_state) {
0088     case MUX_IDLE_DISCONNECT:
0089     case MUX_IDLE_AS_IS:
0090     case 0 ... 7:
0091         /* adgs1409 supports only 4 states */
0092         if (idle_state < mux->states) {
0093             mux->idle_state = idle_state;
0094             break;
0095         }
0096         fallthrough;
0097     default:
0098         dev_err(dev, "invalid idle-state %d\n", idle_state);
0099         return -EINVAL;
0100     }
0101 
0102     return devm_mux_chip_register(dev, mux_chip);
0103 }
0104 
0105 static const struct spi_device_id adgs1408_spi_id[] = {
0106     { "adgs1408", ADGS1408 },
0107     { "adgs1409", ADGS1409 },
0108     { }
0109 };
0110 MODULE_DEVICE_TABLE(spi, adgs1408_spi_id);
0111 
0112 static const struct of_device_id adgs1408_of_match[] = {
0113     { .compatible = "adi,adgs1408", .data = (void *)ADGS1408, },
0114     { .compatible = "adi,adgs1409", .data = (void *)ADGS1409, },
0115     { }
0116 };
0117 MODULE_DEVICE_TABLE(of, adgs1408_of_match);
0118 
0119 static struct spi_driver adgs1408_driver = {
0120     .driver = {
0121         .name = "adgs1408",
0122         .of_match_table = adgs1408_of_match,
0123     },
0124     .probe = adgs1408_probe,
0125     .id_table = adgs1408_spi_id,
0126 };
0127 module_spi_driver(adgs1408_driver);
0128 
0129 MODULE_AUTHOR("Mircea Caprioru <mircea.caprioru@analog.com>");
0130 MODULE_DESCRIPTION("Analog Devices ADGS1408 MUX driver");
0131 MODULE_LICENSE("GPL");