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 * Allow the active pullup to be disabled, default is enabled.
29 * Note from the DS2482 datasheet:
30 * The APU bit controls whether an active pullup (controlled slew-rate
31 * transistor) or a passive pullup (Rwpu resistor) will be used to drive
32 * a 1-Wire line from low to high. When APU = 0, active pullup is disabled
33 * (resistor mode). Active Pullup should always be selected unless there is
34 * only a single slave on the 1-Wire line.
36 static int ds2482_active_pullup
= 1;
37 module_param_named(active_pullup
, ds2482_active_pullup
, int, 0644);
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 /* 1-wire speed */
68 #define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */
69 #define DS2482_REG_CFG_PPM 0x02 /* presence pulse masking */
70 #define DS2482_REG_CFG_APU 0x01 /* active pull-up */
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_remove(struct i2c_client
*client
);
103 * Driver data (common to all clients)
105 static const struct i2c_device_id ds2482_id
[] = {
109 MODULE_DEVICE_TABLE(i2c
, ds2482_id
);
111 static struct i2c_driver ds2482_driver
= {
115 .probe
= ds2482_probe
,
116 .remove
= ds2482_remove
,
117 .id_table
= ds2482_id
,
121 * Client data (each client gets its own)
126 struct ds2482_w1_chan
{
127 struct ds2482_data
*pdev
;
129 struct w1_bus_master w1_bm
;
133 struct i2c_client
*client
;
134 struct mutex access_lock
;
136 /* 1-wire interface(s) */
137 int w1_count
; /* 1 or 8 */
138 struct ds2482_w1_chan w1_ch
[8];
140 /* per-device values */
142 u8 read_prt
; /* see DS2482_PTR_CODE_xxx */
148 * Helper to calculate values for configuration register
149 * @param conf the raw config value
150 * @return the value w/ complements that can be written to register
152 static inline u8
ds2482_calculate_config(u8 conf
)
154 if (ds2482_active_pullup
)
155 conf
|= DS2482_REG_CFG_APU
;
157 return conf
| ((~conf
& 0x0f) << 4);
162 * Sets the read pointer.
163 * @param pdev The ds2482 client pointer
164 * @param read_ptr see DS2482_PTR_CODE_xxx above
165 * @return -1 on failure, 0 on success
167 static inline int ds2482_select_register(struct ds2482_data
*pdev
, u8 read_ptr
)
169 if (pdev
->read_prt
!= read_ptr
) {
170 if (i2c_smbus_write_byte_data(pdev
->client
,
171 DS2482_CMD_SET_READ_PTR
,
175 pdev
->read_prt
= read_ptr
;
181 * Sends a command without a parameter
182 * @param pdev The ds2482 client pointer
183 * @param cmd DS2482_CMD_RESET,
184 * DS2482_CMD_1WIRE_RESET,
185 * DS2482_CMD_1WIRE_READ_BYTE
186 * @return -1 on failure, 0 on success
188 static inline int ds2482_send_cmd(struct ds2482_data
*pdev
, u8 cmd
)
190 if (i2c_smbus_write_byte(pdev
->client
, cmd
) < 0)
193 pdev
->read_prt
= DS2482_PTR_CODE_STATUS
;
198 * Sends a command with a parameter
199 * @param pdev The ds2482 client pointer
200 * @param cmd DS2482_CMD_WRITE_CONFIG,
201 * DS2482_CMD_1WIRE_SINGLE_BIT,
202 * DS2482_CMD_1WIRE_WRITE_BYTE,
203 * DS2482_CMD_1WIRE_TRIPLET
204 * @param byte The data to send
205 * @return -1 on failure, 0 on success
207 static inline int ds2482_send_cmd_data(struct ds2482_data
*pdev
,
210 if (i2c_smbus_write_byte_data(pdev
->client
, cmd
, byte
) < 0)
213 /* all cmds leave in STATUS, except CONFIG */
214 pdev
->read_prt
= (cmd
!= DS2482_CMD_WRITE_CONFIG
) ?
215 DS2482_PTR_CODE_STATUS
: DS2482_PTR_CODE_CONFIG
;
221 * 1-Wire interface code
224 #define DS2482_WAIT_IDLE_TIMEOUT 100
227 * Waits until the 1-wire interface is idle (not busy)
229 * @param pdev Pointer to the device structure
230 * @return the last value read from status or -1 (failure)
232 static int ds2482_wait_1wire_idle(struct ds2482_data
*pdev
)
237 if (!ds2482_select_register(pdev
, DS2482_PTR_CODE_STATUS
)) {
239 temp
= i2c_smbus_read_byte(pdev
->client
);
240 } while ((temp
>= 0) && (temp
& DS2482_REG_STS_1WB
) &&
241 (++retries
< DS2482_WAIT_IDLE_TIMEOUT
));
244 if (retries
>= DS2482_WAIT_IDLE_TIMEOUT
)
245 pr_err("%s: timeout on channel %d\n",
246 __func__
, pdev
->channel
);
252 * Selects a w1 channel.
253 * The 1-wire interface must be idle before calling this function.
255 * @param pdev The ds2482 client pointer
257 * @return -1 (failure) or 0 (success)
259 static int ds2482_set_channel(struct ds2482_data
*pdev
, u8 channel
)
261 if (i2c_smbus_write_byte_data(pdev
->client
, DS2482_CMD_CHANNEL_SELECT
,
262 ds2482_chan_wr
[channel
]) < 0)
265 pdev
->read_prt
= DS2482_PTR_CODE_CHANNEL
;
267 if (i2c_smbus_read_byte(pdev
->client
) == ds2482_chan_rd
[channel
]) {
268 pdev
->channel
= channel
;
276 * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
278 * @param data The ds2482 channel pointer
279 * @param bit The level to write: 0 or non-zero
280 * @return The level read: 0 or 1
282 static u8
ds2482_w1_touch_bit(void *data
, u8 bit
)
284 struct ds2482_w1_chan
*pchan
= data
;
285 struct ds2482_data
*pdev
= pchan
->pdev
;
288 mutex_lock(&pdev
->access_lock
);
290 /* Select the channel */
291 ds2482_wait_1wire_idle(pdev
);
292 if (pdev
->w1_count
> 1)
293 ds2482_set_channel(pdev
, pchan
->channel
);
295 /* Send the touch command, wait until 1WB == 0, return the status */
296 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_SINGLE_BIT
,
298 status
= ds2482_wait_1wire_idle(pdev
);
300 mutex_unlock(&pdev
->access_lock
);
302 return (status
& DS2482_REG_STS_SBR
) ? 1 : 0;
306 * Performs the triplet function, which reads two bits and writes a bit.
307 * The bit written is determined by the two reads:
308 * 00 => dbit, 01 => 0, 10 => 1
310 * @param data The ds2482 channel pointer
311 * @param dbit The direction to choose if both branches are valid
312 * @return b0=read1 b1=read2 b3=bit written
314 static u8
ds2482_w1_triplet(void *data
, u8 dbit
)
316 struct ds2482_w1_chan
*pchan
= data
;
317 struct ds2482_data
*pdev
= pchan
->pdev
;
318 int status
= (3 << 5);
320 mutex_lock(&pdev
->access_lock
);
322 /* Select the channel */
323 ds2482_wait_1wire_idle(pdev
);
324 if (pdev
->w1_count
> 1)
325 ds2482_set_channel(pdev
, pchan
->channel
);
327 /* Send the triplet command, wait until 1WB == 0, return the status */
328 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_TRIPLET
,
330 status
= ds2482_wait_1wire_idle(pdev
);
332 mutex_unlock(&pdev
->access_lock
);
334 /* Decode the status */
335 return (status
>> 5);
339 * Performs the write byte function.
341 * @param data The ds2482 channel pointer
342 * @param byte The value to write
344 static void ds2482_w1_write_byte(void *data
, u8 byte
)
346 struct ds2482_w1_chan
*pchan
= data
;
347 struct ds2482_data
*pdev
= pchan
->pdev
;
349 mutex_lock(&pdev
->access_lock
);
351 /* Select the channel */
352 ds2482_wait_1wire_idle(pdev
);
353 if (pdev
->w1_count
> 1)
354 ds2482_set_channel(pdev
, pchan
->channel
);
356 /* Send the write byte command */
357 ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_WRITE_BYTE
, byte
);
359 mutex_unlock(&pdev
->access_lock
);
363 * Performs the read byte function.
365 * @param data The ds2482 channel pointer
366 * @return The value read
368 static u8
ds2482_w1_read_byte(void *data
)
370 struct ds2482_w1_chan
*pchan
= data
;
371 struct ds2482_data
*pdev
= pchan
->pdev
;
374 mutex_lock(&pdev
->access_lock
);
376 /* Select the channel */
377 ds2482_wait_1wire_idle(pdev
);
378 if (pdev
->w1_count
> 1)
379 ds2482_set_channel(pdev
, pchan
->channel
);
381 /* Send the read byte command */
382 ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_READ_BYTE
);
384 /* Wait until 1WB == 0 */
385 ds2482_wait_1wire_idle(pdev
);
387 /* Select the data register */
388 ds2482_select_register(pdev
, DS2482_PTR_CODE_DATA
);
390 /* Read the data byte */
391 result
= i2c_smbus_read_byte(pdev
->client
);
393 mutex_unlock(&pdev
->access_lock
);
400 * Sends a reset on the 1-wire interface
402 * @param data The ds2482 channel pointer
403 * @return 0=Device present, 1=No device present or error
405 static u8
ds2482_w1_reset_bus(void *data
)
407 struct ds2482_w1_chan
*pchan
= data
;
408 struct ds2482_data
*pdev
= pchan
->pdev
;
412 mutex_lock(&pdev
->access_lock
);
414 /* Select the channel */
415 ds2482_wait_1wire_idle(pdev
);
416 if (pdev
->w1_count
> 1)
417 ds2482_set_channel(pdev
, pchan
->channel
);
419 /* Send the reset command */
420 err
= ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_RESET
);
422 /* Wait until the reset is complete */
423 err
= ds2482_wait_1wire_idle(pdev
);
424 retval
= !(err
& DS2482_REG_STS_PPD
);
426 /* If the chip did reset since detect, re-config it */
427 if (err
& DS2482_REG_STS_RST
)
428 ds2482_send_cmd_data(pdev
, DS2482_CMD_WRITE_CONFIG
,
429 ds2482_calculate_config(0x00));
432 mutex_unlock(&pdev
->access_lock
);
437 static u8
ds2482_w1_set_pullup(void *data
, int delay
)
439 struct ds2482_w1_chan
*pchan
= data
;
440 struct ds2482_data
*pdev
= pchan
->pdev
;
443 /* if delay is non-zero activate the pullup,
444 * the strong pullup will be automatically deactivated
445 * by the master, so do not explicitly deactive it
448 /* both waits are crucial, otherwise devices might not be
449 * powered long enough, causing e.g. a w1_therm sensor to
450 * provide wrong conversion results
452 ds2482_wait_1wire_idle(pdev
);
453 /* note: it seems like both SPU and APU have to be set! */
454 retval
= ds2482_send_cmd_data(pdev
, DS2482_CMD_WRITE_CONFIG
,
455 ds2482_calculate_config(DS2482_REG_CFG_SPU
|
456 DS2482_REG_CFG_APU
));
457 ds2482_wait_1wire_idle(pdev
);
464 static int ds2482_probe(struct i2c_client
*client
,
465 const struct i2c_device_id
*id
)
467 struct ds2482_data
*data
;
472 if (!i2c_check_functionality(client
->adapter
,
473 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
474 I2C_FUNC_SMBUS_BYTE
))
477 if (!(data
= kzalloc(sizeof(struct ds2482_data
), GFP_KERNEL
))) {
482 data
->client
= client
;
483 i2c_set_clientdata(client
, data
);
485 /* Reset the device (sets the read_ptr to status) */
486 if (ds2482_send_cmd(data
, DS2482_CMD_RESET
) < 0) {
487 dev_warn(&client
->dev
, "DS2482 reset failed.\n");
491 /* Sleep at least 525ns to allow the reset to complete */
494 /* Read the status byte - only reset bit and line should be set */
495 temp1
= i2c_smbus_read_byte(client
);
496 if (temp1
!= (DS2482_REG_STS_LL
| DS2482_REG_STS_RST
)) {
497 dev_warn(&client
->dev
, "DS2482 reset status "
498 "0x%02X - not a DS2482\n", temp1
);
502 /* Detect the 8-port version */
504 if (ds2482_set_channel(data
, 7) == 0)
507 /* Set all config items to 0 (off) */
508 ds2482_send_cmd_data(data
, DS2482_CMD_WRITE_CONFIG
,
509 ds2482_calculate_config(0x00));
511 mutex_init(&data
->access_lock
);
513 /* Register 1-wire interface(s) */
514 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
515 data
->w1_ch
[idx
].pdev
= data
;
516 data
->w1_ch
[idx
].channel
= idx
;
518 /* Populate all the w1 bus master stuff */
519 data
->w1_ch
[idx
].w1_bm
.data
= &data
->w1_ch
[idx
];
520 data
->w1_ch
[idx
].w1_bm
.read_byte
= ds2482_w1_read_byte
;
521 data
->w1_ch
[idx
].w1_bm
.write_byte
= ds2482_w1_write_byte
;
522 data
->w1_ch
[idx
].w1_bm
.touch_bit
= ds2482_w1_touch_bit
;
523 data
->w1_ch
[idx
].w1_bm
.triplet
= ds2482_w1_triplet
;
524 data
->w1_ch
[idx
].w1_bm
.reset_bus
= ds2482_w1_reset_bus
;
525 data
->w1_ch
[idx
].w1_bm
.set_pullup
= ds2482_w1_set_pullup
;
527 err
= w1_add_master_device(&data
->w1_ch
[idx
].w1_bm
);
529 data
->w1_ch
[idx
].pdev
= NULL
;
537 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
538 if (data
->w1_ch
[idx
].pdev
!= NULL
)
539 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
547 static int ds2482_remove(struct i2c_client
*client
)
549 struct ds2482_data
*data
= i2c_get_clientdata(client
);
552 /* Unregister the 1-wire bridge(s) */
553 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
554 if (data
->w1_ch
[idx
].pdev
!= NULL
)
555 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
558 /* Free the memory */
563 module_i2c_driver(ds2482_driver
);
565 MODULE_PARM_DESC(active_pullup
, "Active pullup (apply to all buses): " \
566 "0-disable, 1-enable (default)");
567 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
568 MODULE_DESCRIPTION("DS2482 driver");
569 MODULE_LICENSE("GPL");