Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ds2482.c - provides i2c to w1-master bridge(s)
0004  * Copyright (C) 2005  Ben Gardner <bgardner@wabtec.com>
0005  *
0006  * The DS2482 is a sensor chip made by Dallas Semiconductor (Maxim).
0007  * It is a I2C to 1-wire bridge.
0008  * There are two variations: -100 and -800, which have 1 or 8 1-wire ports.
0009  * The complete datasheet can be obtained from MAXIM's website at:
0010  *   http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4382
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/slab.h>
0016 #include <linux/i2c.h>
0017 #include <linux/delay.h>
0018 #include <asm/delay.h>
0019 
0020 #include <linux/w1.h>
0021 
0022 /*
0023  * Allow the active pullup to be disabled, default is enabled.
0024  *
0025  * Note from the DS2482 datasheet:
0026  * The APU bit controls whether an active pullup (controlled slew-rate
0027  * transistor) or a passive pullup (Rwpu resistor) will be used to drive
0028  * a 1-Wire line from low to high. When APU = 0, active pullup is disabled
0029  * (resistor mode). Active Pullup should always be selected unless there is
0030  * only a single slave on the 1-Wire line.
0031  */
0032 static int ds2482_active_pullup = 1;
0033 module_param_named(active_pullup, ds2482_active_pullup, int, 0644);
0034 MODULE_PARM_DESC(active_pullup, "Active pullup (apply to all buses): " \
0035                 "0-disable, 1-enable (default)");
0036 
0037 /* extra configurations - e.g. 1WS */
0038 static int extra_config;
0039 module_param(extra_config, int, S_IRUGO | S_IWUSR);
0040 MODULE_PARM_DESC(extra_config, "Extra Configuration settings 1=APU,2=PPM,3=SPU,8=1WS");
0041 
0042 /*
0043  * The DS2482 registers - there are 3 registers that are addressed by a read
0044  * pointer. The read pointer is set by the last command executed.
0045  *
0046  * To read the data, issue a register read for any address
0047  */
0048 #define DS2482_CMD_RESET        0xF0    /* No param */
0049 #define DS2482_CMD_SET_READ_PTR     0xE1    /* Param: DS2482_PTR_CODE_xxx */
0050 #define DS2482_CMD_CHANNEL_SELECT   0xC3    /* Param: Channel byte - DS2482-800 only */
0051 #define DS2482_CMD_WRITE_CONFIG     0xD2    /* Param: Config byte */
0052 #define DS2482_CMD_1WIRE_RESET      0xB4    /* Param: None */
0053 #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87    /* Param: Bit byte (bit7) */
0054 #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5    /* Param: Data byte */
0055 #define DS2482_CMD_1WIRE_READ_BYTE  0x96    /* Param: None */
0056 /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
0057 #define DS2482_CMD_1WIRE_TRIPLET    0x78    /* Param: Dir byte (bit7) */
0058 
0059 /* Values for DS2482_CMD_SET_READ_PTR */
0060 #define DS2482_PTR_CODE_STATUS      0xF0
0061 #define DS2482_PTR_CODE_DATA        0xE1
0062 #define DS2482_PTR_CODE_CHANNEL     0xD2    /* DS2482-800 only */
0063 #define DS2482_PTR_CODE_CONFIG      0xC3
0064 
0065 /*
0066  * Configure Register bit definitions
0067  * The top 4 bits always read 0.
0068  * To write, the top nibble must be the 1's compl. of the low nibble.
0069  */
0070 #define DS2482_REG_CFG_1WS      0x08    /* 1-wire speed */
0071 #define DS2482_REG_CFG_SPU      0x04    /* strong pull-up */
0072 #define DS2482_REG_CFG_PPM      0x02    /* presence pulse masking */
0073 #define DS2482_REG_CFG_APU      0x01    /* active pull-up */
0074 
0075 
0076 /*
0077  * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
0078  * To set the channel, write the value at the index of the channel.
0079  * Read and compare against the corresponding value to verify the change.
0080  */
0081 static const u8 ds2482_chan_wr[8] =
0082     { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
0083 static const u8 ds2482_chan_rd[8] =
0084     { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
0085 
0086 
0087 /*
0088  * Status Register bit definitions (read only)
0089  */
0090 #define DS2482_REG_STS_DIR      0x80
0091 #define DS2482_REG_STS_TSB      0x40
0092 #define DS2482_REG_STS_SBR      0x20
0093 #define DS2482_REG_STS_RST      0x10
0094 #define DS2482_REG_STS_LL       0x08
0095 #define DS2482_REG_STS_SD       0x04
0096 #define DS2482_REG_STS_PPD      0x02
0097 #define DS2482_REG_STS_1WB      0x01
0098 
0099 /*
0100  * Client data (each client gets its own)
0101  */
0102 
0103 struct ds2482_data;
0104 
0105 struct ds2482_w1_chan {
0106     struct ds2482_data  *pdev;
0107     u8          channel;
0108     struct w1_bus_master    w1_bm;
0109 };
0110 
0111 struct ds2482_data {
0112     struct i2c_client   *client;
0113     struct mutex        access_lock;
0114 
0115     /* 1-wire interface(s) */
0116     int         w1_count;   /* 1 or 8 */
0117     struct ds2482_w1_chan   w1_ch[8];
0118 
0119     /* per-device values */
0120     u8          channel;
0121     u8          read_prt;   /* see DS2482_PTR_CODE_xxx */
0122     u8          reg_config;
0123 };
0124 
0125 
0126 /**
0127  * ds2482_calculate_config - Helper to calculate values for configuration register
0128  * @conf: the raw config value
0129  * Return: the value w/ complements that can be written to register
0130  */
0131 static inline u8 ds2482_calculate_config(u8 conf)
0132 {
0133     conf |= extra_config;
0134 
0135     if (ds2482_active_pullup)
0136         conf |= DS2482_REG_CFG_APU;
0137 
0138     return conf | ((~conf & 0x0f) << 4);
0139 }
0140 
0141 
0142 /**
0143  * ds2482_select_register - Sets the read pointer.
0144  * @pdev:       The ds2482 client pointer
0145  * @read_ptr:   see DS2482_PTR_CODE_xxx above
0146  * Return: -1 on failure, 0 on success
0147  */
0148 static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
0149 {
0150     if (pdev->read_prt != read_ptr) {
0151         if (i2c_smbus_write_byte_data(pdev->client,
0152                           DS2482_CMD_SET_READ_PTR,
0153                           read_ptr) < 0)
0154             return -1;
0155 
0156         pdev->read_prt = read_ptr;
0157     }
0158     return 0;
0159 }
0160 
0161 /**
0162  * ds2482_send_cmd - Sends a command without a parameter
0163  * @pdev:   The ds2482 client pointer
0164  * @cmd:    DS2482_CMD_RESET,
0165  *      DS2482_CMD_1WIRE_RESET,
0166  *      DS2482_CMD_1WIRE_READ_BYTE
0167  * Return: -1 on failure, 0 on success
0168  */
0169 static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
0170 {
0171     if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
0172         return -1;
0173 
0174     pdev->read_prt = DS2482_PTR_CODE_STATUS;
0175     return 0;
0176 }
0177 
0178 /**
0179  * ds2482_send_cmd_data - Sends a command with a parameter
0180  * @pdev:   The ds2482 client pointer
0181  * @cmd:    DS2482_CMD_WRITE_CONFIG,
0182  *      DS2482_CMD_1WIRE_SINGLE_BIT,
0183  *      DS2482_CMD_1WIRE_WRITE_BYTE,
0184  *      DS2482_CMD_1WIRE_TRIPLET
0185  * @byte:   The data to send
0186  * Return: -1 on failure, 0 on success
0187  */
0188 static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
0189                        u8 cmd, u8 byte)
0190 {
0191     if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
0192         return -1;
0193 
0194     /* all cmds leave in STATUS, except CONFIG */
0195     pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
0196              DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG;
0197     return 0;
0198 }
0199 
0200 
0201 /*
0202  * 1-Wire interface code
0203  */
0204 
0205 #define DS2482_WAIT_IDLE_TIMEOUT    100
0206 
0207 /**
0208  * ds2482_wait_1wire_idle - Waits until the 1-wire interface is idle (not busy)
0209  *
0210  * @pdev: Pointer to the device structure
0211  * Return: the last value read from status or -1 (failure)
0212  */
0213 static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
0214 {
0215     int temp = -1;
0216     int retries = 0;
0217 
0218     if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) {
0219         do {
0220             temp = i2c_smbus_read_byte(pdev->client);
0221         } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) &&
0222              (++retries < DS2482_WAIT_IDLE_TIMEOUT));
0223     }
0224 
0225     if (retries >= DS2482_WAIT_IDLE_TIMEOUT)
0226         pr_err("%s: timeout on channel %d\n",
0227                __func__, pdev->channel);
0228 
0229     return temp;
0230 }
0231 
0232 /**
0233  * ds2482_set_channel - Selects a w1 channel.
0234  * The 1-wire interface must be idle before calling this function.
0235  *
0236  * @pdev:       The ds2482 client pointer
0237  * @channel:        0-7
0238  * Return:      -1 (failure) or 0 (success)
0239  */
0240 static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
0241 {
0242     if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
0243                       ds2482_chan_wr[channel]) < 0)
0244         return -1;
0245 
0246     pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
0247     pdev->channel = -1;
0248     if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
0249         pdev->channel = channel;
0250         return 0;
0251     }
0252     return -1;
0253 }
0254 
0255 
0256 /**
0257  * ds2482_w1_touch_bit - Performs the touch-bit function, which writes a 0 or 1 and reads the level.
0258  *
0259  * @data:   The ds2482 channel pointer
0260  * @bit:    The level to write: 0 or non-zero
0261  * Return:  The level read: 0 or 1
0262  */
0263 static u8 ds2482_w1_touch_bit(void *data, u8 bit)
0264 {
0265     struct ds2482_w1_chan *pchan = data;
0266     struct ds2482_data    *pdev = pchan->pdev;
0267     int status = -1;
0268 
0269     mutex_lock(&pdev->access_lock);
0270 
0271     /* Select the channel */
0272     ds2482_wait_1wire_idle(pdev);
0273     if (pdev->w1_count > 1)
0274         ds2482_set_channel(pdev, pchan->channel);
0275 
0276     /* Send the touch command, wait until 1WB == 0, return the status */
0277     if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT,
0278                   bit ? 0xFF : 0))
0279         status = ds2482_wait_1wire_idle(pdev);
0280 
0281     mutex_unlock(&pdev->access_lock);
0282 
0283     return (status & DS2482_REG_STS_SBR) ? 1 : 0;
0284 }
0285 
0286 /**
0287  * ds2482_w1_triplet - Performs the triplet function, which reads two bits and writes a bit.
0288  * The bit written is determined by the two reads:
0289  *   00 => dbit, 01 => 0, 10 => 1
0290  *
0291  * @data:   The ds2482 channel pointer
0292  * @dbit:   The direction to choose if both branches are valid
0293  * Return:  b0=read1 b1=read2 b3=bit written
0294  */
0295 static u8 ds2482_w1_triplet(void *data, u8 dbit)
0296 {
0297     struct ds2482_w1_chan *pchan = data;
0298     struct ds2482_data    *pdev = pchan->pdev;
0299     int status = (3 << 5);
0300 
0301     mutex_lock(&pdev->access_lock);
0302 
0303     /* Select the channel */
0304     ds2482_wait_1wire_idle(pdev);
0305     if (pdev->w1_count > 1)
0306         ds2482_set_channel(pdev, pchan->channel);
0307 
0308     /* Send the triplet command, wait until 1WB == 0, return the status */
0309     if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET,
0310                   dbit ? 0xFF : 0))
0311         status = ds2482_wait_1wire_idle(pdev);
0312 
0313     mutex_unlock(&pdev->access_lock);
0314 
0315     /* Decode the status */
0316     return (status >> 5);
0317 }
0318 
0319 /**
0320  * ds2482_w1_write_byte - Performs the write byte function.
0321  *
0322  * @data:   The ds2482 channel pointer
0323  * @byte:   The value to write
0324  */
0325 static void ds2482_w1_write_byte(void *data, u8 byte)
0326 {
0327     struct ds2482_w1_chan *pchan = data;
0328     struct ds2482_data    *pdev = pchan->pdev;
0329 
0330     mutex_lock(&pdev->access_lock);
0331 
0332     /* Select the channel */
0333     ds2482_wait_1wire_idle(pdev);
0334     if (pdev->w1_count > 1)
0335         ds2482_set_channel(pdev, pchan->channel);
0336 
0337     /* Send the write byte command */
0338     ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
0339 
0340     mutex_unlock(&pdev->access_lock);
0341 }
0342 
0343 /**
0344  * ds2482_w1_read_byte - Performs the read byte function.
0345  *
0346  * @data:   The ds2482 channel pointer
0347  * Return:  The value read
0348  */
0349 static u8 ds2482_w1_read_byte(void *data)
0350 {
0351     struct ds2482_w1_chan *pchan = data;
0352     struct ds2482_data    *pdev = pchan->pdev;
0353     int result;
0354 
0355     mutex_lock(&pdev->access_lock);
0356 
0357     /* Select the channel */
0358     ds2482_wait_1wire_idle(pdev);
0359     if (pdev->w1_count > 1)
0360         ds2482_set_channel(pdev, pchan->channel);
0361 
0362     /* Send the read byte command */
0363     ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
0364 
0365     /* Wait until 1WB == 0 */
0366     ds2482_wait_1wire_idle(pdev);
0367 
0368     /* Select the data register */
0369     ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
0370 
0371     /* Read the data byte */
0372     result = i2c_smbus_read_byte(pdev->client);
0373 
0374     mutex_unlock(&pdev->access_lock);
0375 
0376     return result;
0377 }
0378 
0379 
0380 /**
0381  * ds2482_w1_reset_bus - Sends a reset on the 1-wire interface
0382  *
0383  * @data:   The ds2482 channel pointer
0384  * Return:  0=Device present, 1=No device present or error
0385  */
0386 static u8 ds2482_w1_reset_bus(void *data)
0387 {
0388     struct ds2482_w1_chan *pchan = data;
0389     struct ds2482_data    *pdev = pchan->pdev;
0390     int err;
0391     u8 retval = 1;
0392 
0393     mutex_lock(&pdev->access_lock);
0394 
0395     /* Select the channel */
0396     ds2482_wait_1wire_idle(pdev);
0397     if (pdev->w1_count > 1)
0398         ds2482_set_channel(pdev, pchan->channel);
0399 
0400     /* Send the reset command */
0401     err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET);
0402     if (err >= 0) {
0403         /* Wait until the reset is complete */
0404         err = ds2482_wait_1wire_idle(pdev);
0405         retval = !(err & DS2482_REG_STS_PPD);
0406 
0407         /* If the chip did reset since detect, re-config it */
0408         if (err & DS2482_REG_STS_RST)
0409             ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
0410                          ds2482_calculate_config(0x00));
0411     }
0412 
0413     mutex_unlock(&pdev->access_lock);
0414 
0415     return retval;
0416 }
0417 
0418 static u8 ds2482_w1_set_pullup(void *data, int delay)
0419 {
0420     struct ds2482_w1_chan *pchan = data;
0421     struct ds2482_data    *pdev = pchan->pdev;
0422     u8 retval = 1;
0423 
0424     /* if delay is non-zero activate the pullup,
0425      * the strong pullup will be automatically deactivated
0426      * by the master, so do not explicitly deactive it
0427      */
0428     if (delay) {
0429         /* both waits are crucial, otherwise devices might not be
0430          * powered long enough, causing e.g. a w1_therm sensor to
0431          * provide wrong conversion results
0432          */
0433         ds2482_wait_1wire_idle(pdev);
0434         /* note: it seems like both SPU and APU have to be set! */
0435         retval = ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
0436             ds2482_calculate_config(DS2482_REG_CFG_SPU |
0437                         DS2482_REG_CFG_APU));
0438         ds2482_wait_1wire_idle(pdev);
0439     }
0440 
0441     return retval;
0442 }
0443 
0444 
0445 static int ds2482_probe(struct i2c_client *client,
0446             const struct i2c_device_id *id)
0447 {
0448     struct ds2482_data *data;
0449     int err = -ENODEV;
0450     int temp1;
0451     int idx;
0452 
0453     if (!i2c_check_functionality(client->adapter,
0454                      I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
0455                      I2C_FUNC_SMBUS_BYTE))
0456         return -ENODEV;
0457 
0458     if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
0459         err = -ENOMEM;
0460         goto exit;
0461     }
0462 
0463     data->client = client;
0464     i2c_set_clientdata(client, data);
0465 
0466     /* Reset the device (sets the read_ptr to status) */
0467     if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
0468         dev_warn(&client->dev, "DS2482 reset failed.\n");
0469         goto exit_free;
0470     }
0471 
0472     /* Sleep at least 525ns to allow the reset to complete */
0473     ndelay(525);
0474 
0475     /* Read the status byte - only reset bit and line should be set */
0476     temp1 = i2c_smbus_read_byte(client);
0477     if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) {
0478         dev_warn(&client->dev, "DS2482 reset status "
0479              "0x%02X - not a DS2482\n", temp1);
0480         goto exit_free;
0481     }
0482 
0483     /* Detect the 8-port version */
0484     data->w1_count = 1;
0485     if (ds2482_set_channel(data, 7) == 0)
0486         data->w1_count = 8;
0487 
0488     /* Set all config items to 0 (off) */
0489     ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG,
0490         ds2482_calculate_config(0x00));
0491 
0492     mutex_init(&data->access_lock);
0493 
0494     /* Register 1-wire interface(s) */
0495     for (idx = 0; idx < data->w1_count; idx++) {
0496         data->w1_ch[idx].pdev = data;
0497         data->w1_ch[idx].channel = idx;
0498 
0499         /* Populate all the w1 bus master stuff */
0500         data->w1_ch[idx].w1_bm.data       = &data->w1_ch[idx];
0501         data->w1_ch[idx].w1_bm.read_byte  = ds2482_w1_read_byte;
0502         data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
0503         data->w1_ch[idx].w1_bm.touch_bit  = ds2482_w1_touch_bit;
0504         data->w1_ch[idx].w1_bm.triplet    = ds2482_w1_triplet;
0505         data->w1_ch[idx].w1_bm.reset_bus  = ds2482_w1_reset_bus;
0506         data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup;
0507 
0508         err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
0509         if (err) {
0510             data->w1_ch[idx].pdev = NULL;
0511             goto exit_w1_remove;
0512         }
0513     }
0514 
0515     return 0;
0516 
0517 exit_w1_remove:
0518     for (idx = 0; idx < data->w1_count; idx++) {
0519         if (data->w1_ch[idx].pdev != NULL)
0520             w1_remove_master_device(&data->w1_ch[idx].w1_bm);
0521     }
0522 exit_free:
0523     kfree(data);
0524 exit:
0525     return err;
0526 }
0527 
0528 static int ds2482_remove(struct i2c_client *client)
0529 {
0530     struct ds2482_data   *data = i2c_get_clientdata(client);
0531     int idx;
0532 
0533     /* Unregister the 1-wire bridge(s) */
0534     for (idx = 0; idx < data->w1_count; idx++) {
0535         if (data->w1_ch[idx].pdev != NULL)
0536             w1_remove_master_device(&data->w1_ch[idx].w1_bm);
0537     }
0538 
0539     /* Free the memory */
0540     kfree(data);
0541     return 0;
0542 }
0543 
0544 /*
0545  * Driver data (common to all clients)
0546  */
0547 static const struct i2c_device_id ds2482_id[] = {
0548     { "ds2482", 0 },
0549     { }
0550 };
0551 MODULE_DEVICE_TABLE(i2c, ds2482_id);
0552 
0553 static struct i2c_driver ds2482_driver = {
0554     .driver = {
0555         .name   = "ds2482",
0556     },
0557     .probe      = ds2482_probe,
0558     .remove     = ds2482_remove,
0559     .id_table   = ds2482_id,
0560 };
0561 module_i2c_driver(ds2482_driver);
0562 
0563 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
0564 MODULE_DESCRIPTION("DS2482 driver");
0565 
0566 MODULE_LICENSE("GPL");