2 * ds2482.c - provides i2c to w1-master bridge(s)
3 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5 * The DS2482 is a sensor chip made by Dallas Semiconductor (Maxim).
6 * It is a I2C to 1-wire bridge.
7 * There are two variations: -100 and -800, which have 1 or 8 1-wire ports.
8 * The complete datasheet can be obtained from MAXIM's website at:
9 * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4382
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/delay.h>
21 #include <asm/delay.h>
24 #include "../w1_int.h"
27 * Address is selected using 2 pins, resulting in 4 possible addresses.
28 * 0x18, 0x19, 0x1a, 0x1b
29 * However, the chip cannot be detected without doing an i2c write,
30 * so use the force module parameter.
32 static const unsigned short normal_i2c
[] = { I2C_CLIENT_END
};
37 I2C_CLIENT_INSMOD_1(ds2482
);
40 * The DS2482 registers - there are 3 registers that are addressed by a read
41 * pointer. The read pointer is set by the last command executed.
43 * To read the data, issue a register read for any address
45 #define DS2482_CMD_RESET 0xF0 /* No param */
46 #define DS2482_CMD_SET_READ_PTR 0xE1 /* Param: DS2482_PTR_CODE_xxx */
47 #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */
48 #define DS2482_CMD_WRITE_CONFIG 0xD2 /* Param: Config byte */
49 #define DS2482_CMD_1WIRE_RESET 0xB4 /* Param: None */
50 #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87 /* Param: Bit byte (bit7) */
51 #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */
52 #define DS2482_CMD_1WIRE_READ_BYTE 0x96 /* Param: None */
53 /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
54 #define DS2482_CMD_1WIRE_TRIPLET 0x78 /* Param: Dir byte (bit7) */
56 /* Values for DS2482_CMD_SET_READ_PTR */
57 #define DS2482_PTR_CODE_STATUS 0xF0
58 #define DS2482_PTR_CODE_DATA 0xE1
59 #define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */
60 #define DS2482_PTR_CODE_CONFIG 0xC3
63 * Configure Register bit definitions
64 * The top 4 bits always read 0.
65 * To write, the top nibble must be the 1's compl. of the low nibble.
67 #define DS2482_REG_CFG_1WS 0x08
68 #define DS2482_REG_CFG_SPU 0x04
69 #define DS2482_REG_CFG_PPM 0x02
70 #define DS2482_REG_CFG_APU 0x01
74 * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
75 * To set the channel, write the value at the index of the channel.
76 * Read and compare against the corresponding value to verify the change.
78 static const u8 ds2482_chan_wr
[8] =
79 { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
80 static const u8 ds2482_chan_rd
[8] =
81 { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
85 * Status Register bit definitions (read only)
87 #define DS2482_REG_STS_DIR 0x80
88 #define DS2482_REG_STS_TSB 0x40
89 #define DS2482_REG_STS_SBR 0x20
90 #define DS2482_REG_STS_RST 0x10
91 #define DS2482_REG_STS_LL 0x08
92 #define DS2482_REG_STS_SD 0x04
93 #define DS2482_REG_STS_PPD 0x02
94 #define DS2482_REG_STS_1WB 0x01
97 static int ds2482_attach_adapter(struct i2c_adapter
*adapter
);
98 static int ds2482_detect(struct i2c_adapter
*adapter
, int address
, int kind
);
99 static int ds2482_detach_client(struct i2c_client
*client
);
103 * Driver data (common to all clients)
105 static struct i2c_driver ds2482_driver
= {
107 .owner
= THIS_MODULE
,
110 .attach_adapter
= ds2482_attach_adapter
,
111 .detach_client
= ds2482_detach_client
,
115 * Client data (each client gets its own)
120 struct ds2482_w1_chan
{
121 struct ds2482_data
*pdev
;
123 struct w1_bus_master w1_bm
;
127 struct i2c_client client
;
128 struct mutex access_lock
;
130 /* 1-wire interface(s) */
131 int w1_count
; /* 1 or 8 */
132 struct ds2482_w1_chan w1_ch
[8];
134 /* per-device values */
136 u8 read_prt
; /* see DS2482_PTR_CODE_xxx */
142 * Sets the read pointer.
143 * @param pdev The ds2482 client pointer
144 * @param read_ptr see DS2482_PTR_CODE_xxx above
145 * @return -1 on failure, 0 on success
147 static inline int ds2482_select_register(struct ds2482_data
*pdev
, u8 read_ptr
)
149 if (pdev
->read_prt
!= read_ptr
) {
150 if (i2c_smbus_write_byte_data(&pdev
->client
,
151 DS2482_CMD_SET_READ_PTR
,
155 pdev
->read_prt
= read_ptr
;
161 * Sends a command without a parameter
162 * @param pdev The ds2482 client pointer
163 * @param cmd DS2482_CMD_RESET,
164 * DS2482_CMD_1WIRE_RESET,
165 * DS2482_CMD_1WIRE_READ_BYTE
166 * @return -1 on failure, 0 on success
168 static inline int ds2482_send_cmd(struct ds2482_data
*pdev
, u8 cmd
)
170 if (i2c_smbus_write_byte(&pdev
->client
, cmd
) < 0)
173 pdev
->read_prt
= DS2482_PTR_CODE_STATUS
;
178 * Sends a command with a parameter
179 * @param pdev The ds2482 client pointer
180 * @param cmd DS2482_CMD_WRITE_CONFIG,
181 * DS2482_CMD_1WIRE_SINGLE_BIT,
182 * DS2482_CMD_1WIRE_WRITE_BYTE,
183 * DS2482_CMD_1WIRE_TRIPLET
184 * @param byte The data to send
185 * @return -1 on failure, 0 on success
187 static inline int ds2482_send_cmd_data(struct ds2482_data
*pdev
,
190 if (i2c_smbus_write_byte_data(&pdev
->client
, cmd
, byte
) < 0)
193 /* all cmds leave in STATUS, except CONFIG */
194 pdev
->read_prt
= (cmd
!= DS2482_CMD_WRITE_CONFIG
) ?
195 DS2482_PTR_CODE_STATUS
: DS2482_PTR_CODE_CONFIG
;
201 * 1-Wire interface code
204 #define DS2482_WAIT_IDLE_TIMEOUT 100
207 * Waits until the 1-wire interface is idle (not busy)
209 * @param pdev Pointer to the device structure
210 * @return the last value read from status or -1 (failure)
212 static int ds2482_wait_1wire_idle(struct ds2482_data
*pdev
)
217 if (!ds2482_select_register(pdev
, DS2482_PTR_CODE_STATUS
)) {
219 temp
= i2c_smbus_read_byte(&pdev
->client
);
220 } while ((temp
>= 0) && (temp
& DS2482_REG_STS_1WB
) &&
221 (++retries
< DS2482_WAIT_IDLE_TIMEOUT
));
224 if (retries
> DS2482_WAIT_IDLE_TIMEOUT
)
225 printk(KERN_ERR
"%s: timeout on channel %d\n",
226 __func__
, pdev
->channel
);
232 * Selects a w1 channel.
233 * The 1-wire interface must be idle before calling this function.
235 * @param pdev The ds2482 client pointer
237 * @return -1 (failure) or 0 (success)
239 static int ds2482_set_channel(struct ds2482_data
*pdev
, u8 channel
)
241 if (i2c_smbus_write_byte_data(&pdev
->client
, DS2482_CMD_CHANNEL_SELECT
,
242 ds2482_chan_wr
[channel
]) < 0)
245 pdev
->read_prt
= DS2482_PTR_CODE_CHANNEL
;
247 if (i2c_smbus_read_byte(&pdev
->client
) == ds2482_chan_rd
[channel
]) {
248 pdev
->channel
= channel
;
256 * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
258 * @param data The ds2482 channel pointer
259 * @param bit The level to write: 0 or non-zero
260 * @return The level read: 0 or 1
262 static u8
ds2482_w1_touch_bit(void *data
, u8 bit
)
264 struct ds2482_w1_chan
*pchan
= data
;
265 struct ds2482_data
*pdev
= pchan
->pdev
;
268 mutex_lock(&pdev
->access_lock
);
270 /* Select the channel */
271 ds2482_wait_1wire_idle(pdev
);
272 if (pdev
->w1_count
> 1)
273 ds2482_set_channel(pdev
, pchan
->channel
);
275 /* Send the touch command, wait until 1WB == 0, return the status */
276 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_SINGLE_BIT
,
278 status
= ds2482_wait_1wire_idle(pdev
);
280 mutex_unlock(&pdev
->access_lock
);
282 return (status
& DS2482_REG_STS_SBR
) ? 1 : 0;
286 * Performs the triplet function, which reads two bits and writes a bit.
287 * The bit written is determined by the two reads:
288 * 00 => dbit, 01 => 0, 10 => 1
290 * @param data The ds2482 channel pointer
291 * @param dbit The direction to choose if both branches are valid
292 * @return b0=read1 b1=read2 b3=bit written
294 static u8
ds2482_w1_triplet(void *data
, u8 dbit
)
296 struct ds2482_w1_chan
*pchan
= data
;
297 struct ds2482_data
*pdev
= pchan
->pdev
;
298 int status
= (3 << 5);
300 mutex_lock(&pdev
->access_lock
);
302 /* Select the channel */
303 ds2482_wait_1wire_idle(pdev
);
304 if (pdev
->w1_count
> 1)
305 ds2482_set_channel(pdev
, pchan
->channel
);
307 /* Send the triplet command, wait until 1WB == 0, return the status */
308 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_TRIPLET
,
310 status
= ds2482_wait_1wire_idle(pdev
);
312 mutex_unlock(&pdev
->access_lock
);
314 /* Decode the status */
315 return (status
>> 5);
319 * Performs the write byte function.
321 * @param data The ds2482 channel pointer
322 * @param byte The value to write
324 static void ds2482_w1_write_byte(void *data
, u8 byte
)
326 struct ds2482_w1_chan
*pchan
= data
;
327 struct ds2482_data
*pdev
= pchan
->pdev
;
329 mutex_lock(&pdev
->access_lock
);
331 /* Select the channel */
332 ds2482_wait_1wire_idle(pdev
);
333 if (pdev
->w1_count
> 1)
334 ds2482_set_channel(pdev
, pchan
->channel
);
336 /* Send the write byte command */
337 ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_WRITE_BYTE
, byte
);
339 mutex_unlock(&pdev
->access_lock
);
343 * Performs the read byte function.
345 * @param data The ds2482 channel pointer
346 * @return The value read
348 static u8
ds2482_w1_read_byte(void *data
)
350 struct ds2482_w1_chan
*pchan
= data
;
351 struct ds2482_data
*pdev
= pchan
->pdev
;
354 mutex_lock(&pdev
->access_lock
);
356 /* Select the channel */
357 ds2482_wait_1wire_idle(pdev
);
358 if (pdev
->w1_count
> 1)
359 ds2482_set_channel(pdev
, pchan
->channel
);
361 /* Send the read byte command */
362 ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_READ_BYTE
);
364 /* Wait until 1WB == 0 */
365 ds2482_wait_1wire_idle(pdev
);
367 /* Select the data register */
368 ds2482_select_register(pdev
, DS2482_PTR_CODE_DATA
);
370 /* Read the data byte */
371 result
= i2c_smbus_read_byte(&pdev
->client
);
373 mutex_unlock(&pdev
->access_lock
);
380 * Sends a reset on the 1-wire interface
382 * @param data The ds2482 channel pointer
383 * @return 0=Device present, 1=No device present or error
385 static u8
ds2482_w1_reset_bus(void *data
)
387 struct ds2482_w1_chan
*pchan
= data
;
388 struct ds2482_data
*pdev
= pchan
->pdev
;
392 mutex_lock(&pdev
->access_lock
);
394 /* Select the channel */
395 ds2482_wait_1wire_idle(pdev
);
396 if (pdev
->w1_count
> 1)
397 ds2482_set_channel(pdev
, pchan
->channel
);
399 /* Send the reset command */
400 err
= ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_RESET
);
402 /* Wait until the reset is complete */
403 err
= ds2482_wait_1wire_idle(pdev
);
404 retval
= !(err
& DS2482_REG_STS_PPD
);
406 /* If the chip did reset since detect, re-config it */
407 if (err
& DS2482_REG_STS_RST
)
408 ds2482_send_cmd_data(pdev
, DS2482_CMD_WRITE_CONFIG
,
412 mutex_unlock(&pdev
->access_lock
);
419 * Called to see if the device exists on an i2c bus.
421 static int ds2482_attach_adapter(struct i2c_adapter
*adapter
)
423 return i2c_probe(adapter
, &addr_data
, ds2482_detect
);
428 * The following function does more than just detection. If detection
429 * succeeds, it also registers the new chip.
431 static int ds2482_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
433 struct ds2482_data
*data
;
434 struct i2c_client
*new_client
;
439 if (!i2c_check_functionality(adapter
,
440 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
441 I2C_FUNC_SMBUS_BYTE
))
444 if (!(data
= kzalloc(sizeof(struct ds2482_data
), GFP_KERNEL
))) {
449 new_client
= &data
->client
;
450 i2c_set_clientdata(new_client
, data
);
451 new_client
->addr
= address
;
452 new_client
->driver
= &ds2482_driver
;
453 new_client
->adapter
= adapter
;
455 /* Reset the device (sets the read_ptr to status) */
456 if (ds2482_send_cmd(data
, DS2482_CMD_RESET
) < 0) {
457 dev_dbg(&adapter
->dev
, "DS2482 reset failed at 0x%02x.\n",
462 /* Sleep at least 525ns to allow the reset to complete */
465 /* Read the status byte - only reset bit and line should be set */
466 temp1
= i2c_smbus_read_byte(new_client
);
467 if (temp1
!= (DS2482_REG_STS_LL
| DS2482_REG_STS_RST
)) {
468 dev_dbg(&adapter
->dev
, "DS2482 (0x%02x) reset status "
469 "0x%02X - not a DS2482\n", address
, temp1
);
473 /* Detect the 8-port version */
475 if (ds2482_set_channel(data
, 7) == 0)
478 /* Set all config items to 0 (off) */
479 ds2482_send_cmd_data(data
, DS2482_CMD_WRITE_CONFIG
, 0xF0);
481 /* We can fill in the remaining client fields */
482 snprintf(new_client
->name
, sizeof(new_client
->name
), "ds2482-%d00",
485 mutex_init(&data
->access_lock
);
487 /* Tell the I2C layer a new client has arrived */
488 if ((err
= i2c_attach_client(new_client
)))
491 /* Register 1-wire interface(s) */
492 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
493 data
->w1_ch
[idx
].pdev
= data
;
494 data
->w1_ch
[idx
].channel
= idx
;
496 /* Populate all the w1 bus master stuff */
497 data
->w1_ch
[idx
].w1_bm
.data
= &data
->w1_ch
[idx
];
498 data
->w1_ch
[idx
].w1_bm
.read_byte
= ds2482_w1_read_byte
;
499 data
->w1_ch
[idx
].w1_bm
.write_byte
= ds2482_w1_write_byte
;
500 data
->w1_ch
[idx
].w1_bm
.touch_bit
= ds2482_w1_touch_bit
;
501 data
->w1_ch
[idx
].w1_bm
.triplet
= ds2482_w1_triplet
;
502 data
->w1_ch
[idx
].w1_bm
.reset_bus
= ds2482_w1_reset_bus
;
504 err
= w1_add_master_device(&data
->w1_ch
[idx
].w1_bm
);
506 data
->w1_ch
[idx
].pdev
= NULL
;
514 i2c_detach_client(new_client
);
516 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
517 if (data
->w1_ch
[idx
].pdev
!= NULL
)
518 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
526 static int ds2482_detach_client(struct i2c_client
*client
)
528 struct ds2482_data
*data
= i2c_get_clientdata(client
);
531 /* Unregister the 1-wire bridge(s) */
532 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
533 if (data
->w1_ch
[idx
].pdev
!= NULL
)
534 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
537 /* Detach the i2c device */
538 if ((err
= i2c_detach_client(client
))) {
539 dev_err(&client
->dev
,
540 "Deregistration failed, client not detached.\n");
544 /* Free the memory */
549 static int __init
sensors_ds2482_init(void)
551 return i2c_add_driver(&ds2482_driver
);
554 static void __exit
sensors_ds2482_exit(void)
556 i2c_del_driver(&ds2482_driver
);
559 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
560 MODULE_DESCRIPTION("DS2482 driver");
561 MODULE_LICENSE("GPL");
563 module_init(sensors_ds2482_init
);
564 module_exit(sensors_ds2482_exit
);