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_probe(struct i2c_client
*client
,
98 const struct i2c_device_id
*id
);
99 static int ds2482_detect(struct i2c_client
*client
, int kind
,
100 struct i2c_board_info
*info
);
101 static int ds2482_remove(struct i2c_client
*client
);
105 * Driver data (common to all clients)
107 static const struct i2c_device_id ds2482_id
[] = {
112 static struct i2c_driver ds2482_driver
= {
114 .owner
= THIS_MODULE
,
117 .probe
= ds2482_probe
,
118 .remove
= ds2482_remove
,
119 .id_table
= ds2482_id
,
120 .detect
= ds2482_detect
,
121 .address_data
= &addr_data
,
125 * Client data (each client gets its own)
130 struct ds2482_w1_chan
{
131 struct ds2482_data
*pdev
;
133 struct w1_bus_master w1_bm
;
137 struct i2c_client
*client
;
138 struct mutex access_lock
;
140 /* 1-wire interface(s) */
141 int w1_count
; /* 1 or 8 */
142 struct ds2482_w1_chan w1_ch
[8];
144 /* per-device values */
146 u8 read_prt
; /* see DS2482_PTR_CODE_xxx */
152 * Sets the read pointer.
153 * @param pdev The ds2482 client pointer
154 * @param read_ptr see DS2482_PTR_CODE_xxx above
155 * @return -1 on failure, 0 on success
157 static inline int ds2482_select_register(struct ds2482_data
*pdev
, u8 read_ptr
)
159 if (pdev
->read_prt
!= read_ptr
) {
160 if (i2c_smbus_write_byte_data(pdev
->client
,
161 DS2482_CMD_SET_READ_PTR
,
165 pdev
->read_prt
= read_ptr
;
171 * Sends a command without a parameter
172 * @param pdev The ds2482 client pointer
173 * @param cmd DS2482_CMD_RESET,
174 * DS2482_CMD_1WIRE_RESET,
175 * DS2482_CMD_1WIRE_READ_BYTE
176 * @return -1 on failure, 0 on success
178 static inline int ds2482_send_cmd(struct ds2482_data
*pdev
, u8 cmd
)
180 if (i2c_smbus_write_byte(pdev
->client
, cmd
) < 0)
183 pdev
->read_prt
= DS2482_PTR_CODE_STATUS
;
188 * Sends a command with a parameter
189 * @param pdev The ds2482 client pointer
190 * @param cmd DS2482_CMD_WRITE_CONFIG,
191 * DS2482_CMD_1WIRE_SINGLE_BIT,
192 * DS2482_CMD_1WIRE_WRITE_BYTE,
193 * DS2482_CMD_1WIRE_TRIPLET
194 * @param byte The data to send
195 * @return -1 on failure, 0 on success
197 static inline int ds2482_send_cmd_data(struct ds2482_data
*pdev
,
200 if (i2c_smbus_write_byte_data(pdev
->client
, cmd
, byte
) < 0)
203 /* all cmds leave in STATUS, except CONFIG */
204 pdev
->read_prt
= (cmd
!= DS2482_CMD_WRITE_CONFIG
) ?
205 DS2482_PTR_CODE_STATUS
: DS2482_PTR_CODE_CONFIG
;
211 * 1-Wire interface code
214 #define DS2482_WAIT_IDLE_TIMEOUT 100
217 * Waits until the 1-wire interface is idle (not busy)
219 * @param pdev Pointer to the device structure
220 * @return the last value read from status or -1 (failure)
222 static int ds2482_wait_1wire_idle(struct ds2482_data
*pdev
)
227 if (!ds2482_select_register(pdev
, DS2482_PTR_CODE_STATUS
)) {
229 temp
= i2c_smbus_read_byte(pdev
->client
);
230 } while ((temp
>= 0) && (temp
& DS2482_REG_STS_1WB
) &&
231 (++retries
< DS2482_WAIT_IDLE_TIMEOUT
));
234 if (retries
> DS2482_WAIT_IDLE_TIMEOUT
)
235 printk(KERN_ERR
"%s: timeout on channel %d\n",
236 __func__
, pdev
->channel
);
242 * Selects a w1 channel.
243 * The 1-wire interface must be idle before calling this function.
245 * @param pdev The ds2482 client pointer
247 * @return -1 (failure) or 0 (success)
249 static int ds2482_set_channel(struct ds2482_data
*pdev
, u8 channel
)
251 if (i2c_smbus_write_byte_data(pdev
->client
, DS2482_CMD_CHANNEL_SELECT
,
252 ds2482_chan_wr
[channel
]) < 0)
255 pdev
->read_prt
= DS2482_PTR_CODE_CHANNEL
;
257 if (i2c_smbus_read_byte(pdev
->client
) == ds2482_chan_rd
[channel
]) {
258 pdev
->channel
= channel
;
266 * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
268 * @param data The ds2482 channel pointer
269 * @param bit The level to write: 0 or non-zero
270 * @return The level read: 0 or 1
272 static u8
ds2482_w1_touch_bit(void *data
, u8 bit
)
274 struct ds2482_w1_chan
*pchan
= data
;
275 struct ds2482_data
*pdev
= pchan
->pdev
;
278 mutex_lock(&pdev
->access_lock
);
280 /* Select the channel */
281 ds2482_wait_1wire_idle(pdev
);
282 if (pdev
->w1_count
> 1)
283 ds2482_set_channel(pdev
, pchan
->channel
);
285 /* Send the touch command, wait until 1WB == 0, return the status */
286 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_SINGLE_BIT
,
288 status
= ds2482_wait_1wire_idle(pdev
);
290 mutex_unlock(&pdev
->access_lock
);
292 return (status
& DS2482_REG_STS_SBR
) ? 1 : 0;
296 * Performs the triplet function, which reads two bits and writes a bit.
297 * The bit written is determined by the two reads:
298 * 00 => dbit, 01 => 0, 10 => 1
300 * @param data The ds2482 channel pointer
301 * @param dbit The direction to choose if both branches are valid
302 * @return b0=read1 b1=read2 b3=bit written
304 static u8
ds2482_w1_triplet(void *data
, u8 dbit
)
306 struct ds2482_w1_chan
*pchan
= data
;
307 struct ds2482_data
*pdev
= pchan
->pdev
;
308 int status
= (3 << 5);
310 mutex_lock(&pdev
->access_lock
);
312 /* Select the channel */
313 ds2482_wait_1wire_idle(pdev
);
314 if (pdev
->w1_count
> 1)
315 ds2482_set_channel(pdev
, pchan
->channel
);
317 /* Send the triplet command, wait until 1WB == 0, return the status */
318 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_TRIPLET
,
320 status
= ds2482_wait_1wire_idle(pdev
);
322 mutex_unlock(&pdev
->access_lock
);
324 /* Decode the status */
325 return (status
>> 5);
329 * Performs the write byte function.
331 * @param data The ds2482 channel pointer
332 * @param byte The value to write
334 static void ds2482_w1_write_byte(void *data
, u8 byte
)
336 struct ds2482_w1_chan
*pchan
= data
;
337 struct ds2482_data
*pdev
= pchan
->pdev
;
339 mutex_lock(&pdev
->access_lock
);
341 /* Select the channel */
342 ds2482_wait_1wire_idle(pdev
);
343 if (pdev
->w1_count
> 1)
344 ds2482_set_channel(pdev
, pchan
->channel
);
346 /* Send the write byte command */
347 ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_WRITE_BYTE
, byte
);
349 mutex_unlock(&pdev
->access_lock
);
353 * Performs the read byte function.
355 * @param data The ds2482 channel pointer
356 * @return The value read
358 static u8
ds2482_w1_read_byte(void *data
)
360 struct ds2482_w1_chan
*pchan
= data
;
361 struct ds2482_data
*pdev
= pchan
->pdev
;
364 mutex_lock(&pdev
->access_lock
);
366 /* Select the channel */
367 ds2482_wait_1wire_idle(pdev
);
368 if (pdev
->w1_count
> 1)
369 ds2482_set_channel(pdev
, pchan
->channel
);
371 /* Send the read byte command */
372 ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_READ_BYTE
);
374 /* Wait until 1WB == 0 */
375 ds2482_wait_1wire_idle(pdev
);
377 /* Select the data register */
378 ds2482_select_register(pdev
, DS2482_PTR_CODE_DATA
);
380 /* Read the data byte */
381 result
= i2c_smbus_read_byte(pdev
->client
);
383 mutex_unlock(&pdev
->access_lock
);
390 * Sends a reset on the 1-wire interface
392 * @param data The ds2482 channel pointer
393 * @return 0=Device present, 1=No device present or error
395 static u8
ds2482_w1_reset_bus(void *data
)
397 struct ds2482_w1_chan
*pchan
= data
;
398 struct ds2482_data
*pdev
= pchan
->pdev
;
402 mutex_lock(&pdev
->access_lock
);
404 /* Select the channel */
405 ds2482_wait_1wire_idle(pdev
);
406 if (pdev
->w1_count
> 1)
407 ds2482_set_channel(pdev
, pchan
->channel
);
409 /* Send the reset command */
410 err
= ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_RESET
);
412 /* Wait until the reset is complete */
413 err
= ds2482_wait_1wire_idle(pdev
);
414 retval
= !(err
& DS2482_REG_STS_PPD
);
416 /* If the chip did reset since detect, re-config it */
417 if (err
& DS2482_REG_STS_RST
)
418 ds2482_send_cmd_data(pdev
, DS2482_CMD_WRITE_CONFIG
,
422 mutex_unlock(&pdev
->access_lock
);
428 static int ds2482_detect(struct i2c_client
*client
, int kind
,
429 struct i2c_board_info
*info
)
431 if (!i2c_check_functionality(client
->adapter
,
432 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
433 I2C_FUNC_SMBUS_BYTE
))
436 strlcpy(info
->type
, "ds2482", I2C_NAME_SIZE
);
441 static int ds2482_probe(struct i2c_client
*client
,
442 const struct i2c_device_id
*id
)
444 struct ds2482_data
*data
;
449 if (!(data
= kzalloc(sizeof(struct ds2482_data
), GFP_KERNEL
))) {
454 data
->client
= client
;
455 i2c_set_clientdata(client
, data
);
457 /* Reset the device (sets the read_ptr to status) */
458 if (ds2482_send_cmd(data
, DS2482_CMD_RESET
) < 0) {
459 dev_warn(&client
->dev
, "DS2482 reset failed.\n");
463 /* Sleep at least 525ns to allow the reset to complete */
466 /* Read the status byte - only reset bit and line should be set */
467 temp1
= i2c_smbus_read_byte(client
);
468 if (temp1
!= (DS2482_REG_STS_LL
| DS2482_REG_STS_RST
)) {
469 dev_warn(&client
->dev
, "DS2482 reset status "
470 "0x%02X - not a DS2482\n", temp1
);
474 /* Detect the 8-port version */
476 if (ds2482_set_channel(data
, 7) == 0)
479 /* Set all config items to 0 (off) */
480 ds2482_send_cmd_data(data
, DS2482_CMD_WRITE_CONFIG
, 0xF0);
482 mutex_init(&data
->access_lock
);
484 /* Register 1-wire interface(s) */
485 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
486 data
->w1_ch
[idx
].pdev
= data
;
487 data
->w1_ch
[idx
].channel
= idx
;
489 /* Populate all the w1 bus master stuff */
490 data
->w1_ch
[idx
].w1_bm
.data
= &data
->w1_ch
[idx
];
491 data
->w1_ch
[idx
].w1_bm
.read_byte
= ds2482_w1_read_byte
;
492 data
->w1_ch
[idx
].w1_bm
.write_byte
= ds2482_w1_write_byte
;
493 data
->w1_ch
[idx
].w1_bm
.touch_bit
= ds2482_w1_touch_bit
;
494 data
->w1_ch
[idx
].w1_bm
.triplet
= ds2482_w1_triplet
;
495 data
->w1_ch
[idx
].w1_bm
.reset_bus
= ds2482_w1_reset_bus
;
497 err
= w1_add_master_device(&data
->w1_ch
[idx
].w1_bm
);
499 data
->w1_ch
[idx
].pdev
= NULL
;
507 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
508 if (data
->w1_ch
[idx
].pdev
!= NULL
)
509 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
517 static int ds2482_remove(struct i2c_client
*client
)
519 struct ds2482_data
*data
= i2c_get_clientdata(client
);
522 /* Unregister the 1-wire bridge(s) */
523 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
524 if (data
->w1_ch
[idx
].pdev
!= NULL
)
525 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
528 /* Free the memory */
533 static int __init
sensors_ds2482_init(void)
535 return i2c_add_driver(&ds2482_driver
);
538 static void __exit
sensors_ds2482_exit(void)
540 i2c_del_driver(&ds2482_driver
);
543 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
544 MODULE_DESCRIPTION("DS2482 driver");
545 MODULE_LICENSE("GPL");
547 module_init(sensors_ds2482_init
);
548 module_exit(sensors_ds2482_exit
);