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 * The DS2482 registers - there are 3 registers that are addressed by a read
28 * pointer. The read pointer is set by the last command executed.
30 * To read the data, issue a register read for any address
32 #define DS2482_CMD_RESET 0xF0 /* No param */
33 #define DS2482_CMD_SET_READ_PTR 0xE1 /* Param: DS2482_PTR_CODE_xxx */
34 #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */
35 #define DS2482_CMD_WRITE_CONFIG 0xD2 /* Param: Config byte */
36 #define DS2482_CMD_1WIRE_RESET 0xB4 /* Param: None */
37 #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87 /* Param: Bit byte (bit7) */
38 #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */
39 #define DS2482_CMD_1WIRE_READ_BYTE 0x96 /* Param: None */
40 /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
41 #define DS2482_CMD_1WIRE_TRIPLET 0x78 /* Param: Dir byte (bit7) */
43 /* Values for DS2482_CMD_SET_READ_PTR */
44 #define DS2482_PTR_CODE_STATUS 0xF0
45 #define DS2482_PTR_CODE_DATA 0xE1
46 #define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */
47 #define DS2482_PTR_CODE_CONFIG 0xC3
50 * Configure Register bit definitions
51 * The top 4 bits always read 0.
52 * To write, the top nibble must be the 1's compl. of the low nibble.
54 #define DS2482_REG_CFG_1WS 0x08 /* 1-wire speed */
55 #define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */
56 #define DS2482_REG_CFG_PPM 0x02 /* presence pulse masking */
57 #define DS2482_REG_CFG_APU 0x01 /* active pull-up */
61 * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
62 * To set the channel, write the value at the index of the channel.
63 * Read and compare against the corresponding value to verify the change.
65 static const u8 ds2482_chan_wr
[8] =
66 { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
67 static const u8 ds2482_chan_rd
[8] =
68 { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
72 * Status Register bit definitions (read only)
74 #define DS2482_REG_STS_DIR 0x80
75 #define DS2482_REG_STS_TSB 0x40
76 #define DS2482_REG_STS_SBR 0x20
77 #define DS2482_REG_STS_RST 0x10
78 #define DS2482_REG_STS_LL 0x08
79 #define DS2482_REG_STS_SD 0x04
80 #define DS2482_REG_STS_PPD 0x02
81 #define DS2482_REG_STS_1WB 0x01
84 static int ds2482_probe(struct i2c_client
*client
,
85 const struct i2c_device_id
*id
);
86 static int ds2482_remove(struct i2c_client
*client
);
90 * Driver data (common to all clients)
92 static const struct i2c_device_id ds2482_id
[] = {
97 static struct i2c_driver ds2482_driver
= {
102 .probe
= ds2482_probe
,
103 .remove
= ds2482_remove
,
104 .id_table
= ds2482_id
,
108 * Client data (each client gets its own)
113 struct ds2482_w1_chan
{
114 struct ds2482_data
*pdev
;
116 struct w1_bus_master w1_bm
;
120 struct i2c_client
*client
;
121 struct mutex access_lock
;
123 /* 1-wire interface(s) */
124 int w1_count
; /* 1 or 8 */
125 struct ds2482_w1_chan w1_ch
[8];
127 /* per-device values */
129 u8 read_prt
; /* see DS2482_PTR_CODE_xxx */
135 * Helper to calculate values for configuration register
136 * @param conf the raw config value
137 * @return the value w/ complements that can be written to register
139 static inline u8
ds2482_calculate_config(u8 conf
)
141 return conf
| ((~conf
& 0x0f) << 4);
146 * Sets the read pointer.
147 * @param pdev The ds2482 client pointer
148 * @param read_ptr see DS2482_PTR_CODE_xxx above
149 * @return -1 on failure, 0 on success
151 static inline int ds2482_select_register(struct ds2482_data
*pdev
, u8 read_ptr
)
153 if (pdev
->read_prt
!= read_ptr
) {
154 if (i2c_smbus_write_byte_data(pdev
->client
,
155 DS2482_CMD_SET_READ_PTR
,
159 pdev
->read_prt
= read_ptr
;
165 * Sends a command without a parameter
166 * @param pdev The ds2482 client pointer
167 * @param cmd DS2482_CMD_RESET,
168 * DS2482_CMD_1WIRE_RESET,
169 * DS2482_CMD_1WIRE_READ_BYTE
170 * @return -1 on failure, 0 on success
172 static inline int ds2482_send_cmd(struct ds2482_data
*pdev
, u8 cmd
)
174 if (i2c_smbus_write_byte(pdev
->client
, cmd
) < 0)
177 pdev
->read_prt
= DS2482_PTR_CODE_STATUS
;
182 * Sends a command with a parameter
183 * @param pdev The ds2482 client pointer
184 * @param cmd DS2482_CMD_WRITE_CONFIG,
185 * DS2482_CMD_1WIRE_SINGLE_BIT,
186 * DS2482_CMD_1WIRE_WRITE_BYTE,
187 * DS2482_CMD_1WIRE_TRIPLET
188 * @param byte The data to send
189 * @return -1 on failure, 0 on success
191 static inline int ds2482_send_cmd_data(struct ds2482_data
*pdev
,
194 if (i2c_smbus_write_byte_data(pdev
->client
, cmd
, byte
) < 0)
197 /* all cmds leave in STATUS, except CONFIG */
198 pdev
->read_prt
= (cmd
!= DS2482_CMD_WRITE_CONFIG
) ?
199 DS2482_PTR_CODE_STATUS
: DS2482_PTR_CODE_CONFIG
;
205 * 1-Wire interface code
208 #define DS2482_WAIT_IDLE_TIMEOUT 100
211 * Waits until the 1-wire interface is idle (not busy)
213 * @param pdev Pointer to the device structure
214 * @return the last value read from status or -1 (failure)
216 static int ds2482_wait_1wire_idle(struct ds2482_data
*pdev
)
221 if (!ds2482_select_register(pdev
, DS2482_PTR_CODE_STATUS
)) {
223 temp
= i2c_smbus_read_byte(pdev
->client
);
224 } while ((temp
>= 0) && (temp
& DS2482_REG_STS_1WB
) &&
225 (++retries
< DS2482_WAIT_IDLE_TIMEOUT
));
228 if (retries
>= DS2482_WAIT_IDLE_TIMEOUT
)
229 pr_err("%s: timeout on channel %d\n",
230 __func__
, pdev
->channel
);
236 * Selects a w1 channel.
237 * The 1-wire interface must be idle before calling this function.
239 * @param pdev The ds2482 client pointer
241 * @return -1 (failure) or 0 (success)
243 static int ds2482_set_channel(struct ds2482_data
*pdev
, u8 channel
)
245 if (i2c_smbus_write_byte_data(pdev
->client
, DS2482_CMD_CHANNEL_SELECT
,
246 ds2482_chan_wr
[channel
]) < 0)
249 pdev
->read_prt
= DS2482_PTR_CODE_CHANNEL
;
251 if (i2c_smbus_read_byte(pdev
->client
) == ds2482_chan_rd
[channel
]) {
252 pdev
->channel
= channel
;
260 * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
262 * @param data The ds2482 channel pointer
263 * @param bit The level to write: 0 or non-zero
264 * @return The level read: 0 or 1
266 static u8
ds2482_w1_touch_bit(void *data
, u8 bit
)
268 struct ds2482_w1_chan
*pchan
= data
;
269 struct ds2482_data
*pdev
= pchan
->pdev
;
272 mutex_lock(&pdev
->access_lock
);
274 /* Select the channel */
275 ds2482_wait_1wire_idle(pdev
);
276 if (pdev
->w1_count
> 1)
277 ds2482_set_channel(pdev
, pchan
->channel
);
279 /* Send the touch command, wait until 1WB == 0, return the status */
280 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_SINGLE_BIT
,
282 status
= ds2482_wait_1wire_idle(pdev
);
284 mutex_unlock(&pdev
->access_lock
);
286 return (status
& DS2482_REG_STS_SBR
) ? 1 : 0;
290 * Performs the triplet function, which reads two bits and writes a bit.
291 * The bit written is determined by the two reads:
292 * 00 => dbit, 01 => 0, 10 => 1
294 * @param data The ds2482 channel pointer
295 * @param dbit The direction to choose if both branches are valid
296 * @return b0=read1 b1=read2 b3=bit written
298 static u8
ds2482_w1_triplet(void *data
, u8 dbit
)
300 struct ds2482_w1_chan
*pchan
= data
;
301 struct ds2482_data
*pdev
= pchan
->pdev
;
302 int status
= (3 << 5);
304 mutex_lock(&pdev
->access_lock
);
306 /* Select the channel */
307 ds2482_wait_1wire_idle(pdev
);
308 if (pdev
->w1_count
> 1)
309 ds2482_set_channel(pdev
, pchan
->channel
);
311 /* Send the triplet command, wait until 1WB == 0, return the status */
312 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_TRIPLET
,
314 status
= ds2482_wait_1wire_idle(pdev
);
316 mutex_unlock(&pdev
->access_lock
);
318 /* Decode the status */
319 return (status
>> 5);
323 * Performs the write byte function.
325 * @param data The ds2482 channel pointer
326 * @param byte The value to write
328 static void ds2482_w1_write_byte(void *data
, u8 byte
)
330 struct ds2482_w1_chan
*pchan
= data
;
331 struct ds2482_data
*pdev
= pchan
->pdev
;
333 mutex_lock(&pdev
->access_lock
);
335 /* Select the channel */
336 ds2482_wait_1wire_idle(pdev
);
337 if (pdev
->w1_count
> 1)
338 ds2482_set_channel(pdev
, pchan
->channel
);
340 /* Send the write byte command */
341 ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_WRITE_BYTE
, byte
);
343 mutex_unlock(&pdev
->access_lock
);
347 * Performs the read byte function.
349 * @param data The ds2482 channel pointer
350 * @return The value read
352 static u8
ds2482_w1_read_byte(void *data
)
354 struct ds2482_w1_chan
*pchan
= data
;
355 struct ds2482_data
*pdev
= pchan
->pdev
;
358 mutex_lock(&pdev
->access_lock
);
360 /* Select the channel */
361 ds2482_wait_1wire_idle(pdev
);
362 if (pdev
->w1_count
> 1)
363 ds2482_set_channel(pdev
, pchan
->channel
);
365 /* Send the read byte command */
366 ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_READ_BYTE
);
368 /* Wait until 1WB == 0 */
369 ds2482_wait_1wire_idle(pdev
);
371 /* Select the data register */
372 ds2482_select_register(pdev
, DS2482_PTR_CODE_DATA
);
374 /* Read the data byte */
375 result
= i2c_smbus_read_byte(pdev
->client
);
377 mutex_unlock(&pdev
->access_lock
);
384 * Sends a reset on the 1-wire interface
386 * @param data The ds2482 channel pointer
387 * @return 0=Device present, 1=No device present or error
389 static u8
ds2482_w1_reset_bus(void *data
)
391 struct ds2482_w1_chan
*pchan
= data
;
392 struct ds2482_data
*pdev
= pchan
->pdev
;
396 mutex_lock(&pdev
->access_lock
);
398 /* Select the channel */
399 ds2482_wait_1wire_idle(pdev
);
400 if (pdev
->w1_count
> 1)
401 ds2482_set_channel(pdev
, pchan
->channel
);
403 /* Send the reset command */
404 err
= ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_RESET
);
406 /* Wait until the reset is complete */
407 err
= ds2482_wait_1wire_idle(pdev
);
408 retval
= !(err
& DS2482_REG_STS_PPD
);
410 /* If the chip did reset since detect, re-config it */
411 if (err
& DS2482_REG_STS_RST
)
412 ds2482_send_cmd_data(pdev
, DS2482_CMD_WRITE_CONFIG
,
413 ds2482_calculate_config(0x00));
416 mutex_unlock(&pdev
->access_lock
);
421 static u8
ds2482_w1_set_pullup(void *data
, int delay
)
423 struct ds2482_w1_chan
*pchan
= data
;
424 struct ds2482_data
*pdev
= pchan
->pdev
;
427 /* if delay is non-zero activate the pullup,
428 * the strong pullup will be automatically deactivated
429 * by the master, so do not explicitly deactive it
432 /* both waits are crucial, otherwise devices might not be
433 * powered long enough, causing e.g. a w1_therm sensor to
434 * provide wrong conversion results
436 ds2482_wait_1wire_idle(pdev
);
437 /* note: it seems like both SPU and APU have to be set! */
438 retval
= ds2482_send_cmd_data(pdev
, DS2482_CMD_WRITE_CONFIG
,
439 ds2482_calculate_config(DS2482_REG_CFG_SPU
|
440 DS2482_REG_CFG_APU
));
441 ds2482_wait_1wire_idle(pdev
);
448 static int ds2482_probe(struct i2c_client
*client
,
449 const struct i2c_device_id
*id
)
451 struct ds2482_data
*data
;
456 if (!i2c_check_functionality(client
->adapter
,
457 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
458 I2C_FUNC_SMBUS_BYTE
))
461 if (!(data
= kzalloc(sizeof(struct ds2482_data
), GFP_KERNEL
))) {
466 data
->client
= client
;
467 i2c_set_clientdata(client
, data
);
469 /* Reset the device (sets the read_ptr to status) */
470 if (ds2482_send_cmd(data
, DS2482_CMD_RESET
) < 0) {
471 dev_warn(&client
->dev
, "DS2482 reset failed.\n");
475 /* Sleep at least 525ns to allow the reset to complete */
478 /* Read the status byte - only reset bit and line should be set */
479 temp1
= i2c_smbus_read_byte(client
);
480 if (temp1
!= (DS2482_REG_STS_LL
| DS2482_REG_STS_RST
)) {
481 dev_warn(&client
->dev
, "DS2482 reset status "
482 "0x%02X - not a DS2482\n", temp1
);
486 /* Detect the 8-port version */
488 if (ds2482_set_channel(data
, 7) == 0)
491 /* Set all config items to 0 (off) */
492 ds2482_send_cmd_data(data
, DS2482_CMD_WRITE_CONFIG
,
493 ds2482_calculate_config(0x00));
495 mutex_init(&data
->access_lock
);
497 /* Register 1-wire interface(s) */
498 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
499 data
->w1_ch
[idx
].pdev
= data
;
500 data
->w1_ch
[idx
].channel
= idx
;
502 /* Populate all the w1 bus master stuff */
503 data
->w1_ch
[idx
].w1_bm
.data
= &data
->w1_ch
[idx
];
504 data
->w1_ch
[idx
].w1_bm
.read_byte
= ds2482_w1_read_byte
;
505 data
->w1_ch
[idx
].w1_bm
.write_byte
= ds2482_w1_write_byte
;
506 data
->w1_ch
[idx
].w1_bm
.touch_bit
= ds2482_w1_touch_bit
;
507 data
->w1_ch
[idx
].w1_bm
.triplet
= ds2482_w1_triplet
;
508 data
->w1_ch
[idx
].w1_bm
.reset_bus
= ds2482_w1_reset_bus
;
509 data
->w1_ch
[idx
].w1_bm
.set_pullup
= ds2482_w1_set_pullup
;
511 err
= w1_add_master_device(&data
->w1_ch
[idx
].w1_bm
);
513 data
->w1_ch
[idx
].pdev
= NULL
;
521 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
522 if (data
->w1_ch
[idx
].pdev
!= NULL
)
523 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
531 static int ds2482_remove(struct i2c_client
*client
)
533 struct ds2482_data
*data
= i2c_get_clientdata(client
);
536 /* Unregister the 1-wire bridge(s) */
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
);
542 /* Free the memory */
547 module_i2c_driver(ds2482_driver
);
549 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
550 MODULE_DESCRIPTION("DS2482 driver");
551 MODULE_LICENSE("GPL");