1 // SPDX-License-Identifier: GPL-2.0-only
3 * ds2482.c - provides i2c to w1-master bridge(s)
4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
6 * The DS2482 is a sensor chip made by Dallas Semiconductor (Maxim).
7 * It is a I2C to 1-wire bridge.
8 * There are two variations: -100 and -800, which have 1 or 8 1-wire ports.
9 * The complete datasheet can be obtained from MAXIM's website at:
10 * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4382
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <asm/delay.h>
23 * Allow the active pullup to be disabled, default is enabled.
25 * Note from the DS2482 datasheet:
26 * The APU bit controls whether an active pullup (controlled slew-rate
27 * transistor) or a passive pullup (Rwpu resistor) will be used to drive
28 * a 1-Wire line from low to high. When APU = 0, active pullup is disabled
29 * (resistor mode). Active Pullup should always be selected unless there is
30 * only a single slave on the 1-Wire line.
32 static int ds2482_active_pullup
= 1;
33 module_param_named(active_pullup
, ds2482_active_pullup
, int, 0644);
34 MODULE_PARM_DESC(active_pullup
, "Active pullup (apply to all buses): " \
35 "0-disable, 1-enable (default)");
37 /* extra configurations - e.g. 1WS */
38 static int extra_config
;
39 module_param(extra_config
, int, S_IRUGO
| S_IWUSR
);
40 MODULE_PARM_DESC(extra_config
, "Extra Configuration settings 1=APU,2=PPM,3=SPU,8=1WS");
43 * The DS2482 registers - there are 3 registers that are addressed by a read
44 * pointer. The read pointer is set by the last command executed.
46 * To read the data, issue a register read for any address
48 #define DS2482_CMD_RESET 0xF0 /* No param */
49 #define DS2482_CMD_SET_READ_PTR 0xE1 /* Param: DS2482_PTR_CODE_xxx */
50 #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */
51 #define DS2482_CMD_WRITE_CONFIG 0xD2 /* Param: Config byte */
52 #define DS2482_CMD_1WIRE_RESET 0xB4 /* Param: None */
53 #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87 /* Param: Bit byte (bit7) */
54 #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */
55 #define DS2482_CMD_1WIRE_READ_BYTE 0x96 /* Param: None */
56 /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
57 #define DS2482_CMD_1WIRE_TRIPLET 0x78 /* Param: Dir byte (bit7) */
59 /* Values for DS2482_CMD_SET_READ_PTR */
60 #define DS2482_PTR_CODE_STATUS 0xF0
61 #define DS2482_PTR_CODE_DATA 0xE1
62 #define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */
63 #define DS2482_PTR_CODE_CONFIG 0xC3
66 * Configure Register bit definitions
67 * The top 4 bits always read 0.
68 * To write, the top nibble must be the 1's compl. of the low nibble.
70 #define DS2482_REG_CFG_1WS 0x08 /* 1-wire speed */
71 #define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */
72 #define DS2482_REG_CFG_PPM 0x02 /* presence pulse masking */
73 #define DS2482_REG_CFG_APU 0x01 /* active pull-up */
77 * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
78 * To set the channel, write the value at the index of the channel.
79 * Read and compare against the corresponding value to verify the change.
81 static const u8 ds2482_chan_wr
[8] =
82 { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
83 static const u8 ds2482_chan_rd
[8] =
84 { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
88 * Status Register bit definitions (read only)
90 #define DS2482_REG_STS_DIR 0x80
91 #define DS2482_REG_STS_TSB 0x40
92 #define DS2482_REG_STS_SBR 0x20
93 #define DS2482_REG_STS_RST 0x10
94 #define DS2482_REG_STS_LL 0x08
95 #define DS2482_REG_STS_SD 0x04
96 #define DS2482_REG_STS_PPD 0x02
97 #define DS2482_REG_STS_1WB 0x01
100 * Client data (each client gets its own)
105 struct ds2482_w1_chan
{
106 struct ds2482_data
*pdev
;
108 struct w1_bus_master w1_bm
;
112 struct i2c_client
*client
;
113 struct mutex access_lock
;
115 /* 1-wire interface(s) */
116 int w1_count
; /* 1 or 8 */
117 struct ds2482_w1_chan w1_ch
[8];
119 /* per-device values */
121 u8 read_prt
; /* see DS2482_PTR_CODE_xxx */
127 * Helper to calculate values for configuration register
128 * @param conf the raw config value
129 * @return the value w/ complements that can be written to register
131 static inline u8
ds2482_calculate_config(u8 conf
)
133 conf
|= extra_config
;
135 if (ds2482_active_pullup
)
136 conf
|= DS2482_REG_CFG_APU
;
138 return conf
| ((~conf
& 0x0f) << 4);
143 * Sets the read pointer.
144 * @param pdev The ds2482 client pointer
145 * @param read_ptr see DS2482_PTR_CODE_xxx above
146 * @return -1 on failure, 0 on success
148 static inline int ds2482_select_register(struct ds2482_data
*pdev
, u8 read_ptr
)
150 if (pdev
->read_prt
!= read_ptr
) {
151 if (i2c_smbus_write_byte_data(pdev
->client
,
152 DS2482_CMD_SET_READ_PTR
,
156 pdev
->read_prt
= read_ptr
;
162 * Sends a command without a parameter
163 * @param pdev The ds2482 client pointer
164 * @param cmd DS2482_CMD_RESET,
165 * DS2482_CMD_1WIRE_RESET,
166 * DS2482_CMD_1WIRE_READ_BYTE
167 * @return -1 on failure, 0 on success
169 static inline int ds2482_send_cmd(struct ds2482_data
*pdev
, u8 cmd
)
171 if (i2c_smbus_write_byte(pdev
->client
, cmd
) < 0)
174 pdev
->read_prt
= DS2482_PTR_CODE_STATUS
;
179 * Sends a command with a parameter
180 * @param pdev The ds2482 client pointer
181 * @param cmd DS2482_CMD_WRITE_CONFIG,
182 * DS2482_CMD_1WIRE_SINGLE_BIT,
183 * DS2482_CMD_1WIRE_WRITE_BYTE,
184 * DS2482_CMD_1WIRE_TRIPLET
185 * @param byte The data to send
186 * @return -1 on failure, 0 on success
188 static inline int ds2482_send_cmd_data(struct ds2482_data
*pdev
,
191 if (i2c_smbus_write_byte_data(pdev
->client
, cmd
, byte
) < 0)
194 /* all cmds leave in STATUS, except CONFIG */
195 pdev
->read_prt
= (cmd
!= DS2482_CMD_WRITE_CONFIG
) ?
196 DS2482_PTR_CODE_STATUS
: DS2482_PTR_CODE_CONFIG
;
202 * 1-Wire interface code
205 #define DS2482_WAIT_IDLE_TIMEOUT 100
208 * Waits until the 1-wire interface is idle (not busy)
210 * @param pdev Pointer to the device structure
211 * @return the last value read from status or -1 (failure)
213 static int ds2482_wait_1wire_idle(struct ds2482_data
*pdev
)
218 if (!ds2482_select_register(pdev
, DS2482_PTR_CODE_STATUS
)) {
220 temp
= i2c_smbus_read_byte(pdev
->client
);
221 } while ((temp
>= 0) && (temp
& DS2482_REG_STS_1WB
) &&
222 (++retries
< DS2482_WAIT_IDLE_TIMEOUT
));
225 if (retries
>= DS2482_WAIT_IDLE_TIMEOUT
)
226 pr_err("%s: timeout on channel %d\n",
227 __func__
, pdev
->channel
);
233 * Selects a w1 channel.
234 * The 1-wire interface must be idle before calling this function.
236 * @param pdev The ds2482 client pointer
238 * @return -1 (failure) or 0 (success)
240 static int ds2482_set_channel(struct ds2482_data
*pdev
, u8 channel
)
242 if (i2c_smbus_write_byte_data(pdev
->client
, DS2482_CMD_CHANNEL_SELECT
,
243 ds2482_chan_wr
[channel
]) < 0)
246 pdev
->read_prt
= DS2482_PTR_CODE_CHANNEL
;
248 if (i2c_smbus_read_byte(pdev
->client
) == ds2482_chan_rd
[channel
]) {
249 pdev
->channel
= channel
;
257 * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
259 * @param data The ds2482 channel pointer
260 * @param bit The level to write: 0 or non-zero
261 * @return The level read: 0 or 1
263 static u8
ds2482_w1_touch_bit(void *data
, u8 bit
)
265 struct ds2482_w1_chan
*pchan
= data
;
266 struct ds2482_data
*pdev
= pchan
->pdev
;
269 mutex_lock(&pdev
->access_lock
);
271 /* Select the channel */
272 ds2482_wait_1wire_idle(pdev
);
273 if (pdev
->w1_count
> 1)
274 ds2482_set_channel(pdev
, pchan
->channel
);
276 /* Send the touch command, wait until 1WB == 0, return the status */
277 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_SINGLE_BIT
,
279 status
= ds2482_wait_1wire_idle(pdev
);
281 mutex_unlock(&pdev
->access_lock
);
283 return (status
& DS2482_REG_STS_SBR
) ? 1 : 0;
287 * Performs the triplet function, which reads two bits and writes a bit.
288 * The bit written is determined by the two reads:
289 * 00 => dbit, 01 => 0, 10 => 1
291 * @param data The ds2482 channel pointer
292 * @param dbit The direction to choose if both branches are valid
293 * @return b0=read1 b1=read2 b3=bit written
295 static u8
ds2482_w1_triplet(void *data
, u8 dbit
)
297 struct ds2482_w1_chan
*pchan
= data
;
298 struct ds2482_data
*pdev
= pchan
->pdev
;
299 int status
= (3 << 5);
301 mutex_lock(&pdev
->access_lock
);
303 /* Select the channel */
304 ds2482_wait_1wire_idle(pdev
);
305 if (pdev
->w1_count
> 1)
306 ds2482_set_channel(pdev
, pchan
->channel
);
308 /* Send the triplet command, wait until 1WB == 0, return the status */
309 if (!ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_TRIPLET
,
311 status
= ds2482_wait_1wire_idle(pdev
);
313 mutex_unlock(&pdev
->access_lock
);
315 /* Decode the status */
316 return (status
>> 5);
320 * Performs the write byte function.
322 * @param data The ds2482 channel pointer
323 * @param byte The value to write
325 static void ds2482_w1_write_byte(void *data
, u8 byte
)
327 struct ds2482_w1_chan
*pchan
= data
;
328 struct ds2482_data
*pdev
= pchan
->pdev
;
330 mutex_lock(&pdev
->access_lock
);
332 /* Select the channel */
333 ds2482_wait_1wire_idle(pdev
);
334 if (pdev
->w1_count
> 1)
335 ds2482_set_channel(pdev
, pchan
->channel
);
337 /* Send the write byte command */
338 ds2482_send_cmd_data(pdev
, DS2482_CMD_1WIRE_WRITE_BYTE
, byte
);
340 mutex_unlock(&pdev
->access_lock
);
344 * Performs the read byte function.
346 * @param data The ds2482 channel pointer
347 * @return The value read
349 static u8
ds2482_w1_read_byte(void *data
)
351 struct ds2482_w1_chan
*pchan
= data
;
352 struct ds2482_data
*pdev
= pchan
->pdev
;
355 mutex_lock(&pdev
->access_lock
);
357 /* Select the channel */
358 ds2482_wait_1wire_idle(pdev
);
359 if (pdev
->w1_count
> 1)
360 ds2482_set_channel(pdev
, pchan
->channel
);
362 /* Send the read byte command */
363 ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_READ_BYTE
);
365 /* Wait until 1WB == 0 */
366 ds2482_wait_1wire_idle(pdev
);
368 /* Select the data register */
369 ds2482_select_register(pdev
, DS2482_PTR_CODE_DATA
);
371 /* Read the data byte */
372 result
= i2c_smbus_read_byte(pdev
->client
);
374 mutex_unlock(&pdev
->access_lock
);
381 * Sends a reset on the 1-wire interface
383 * @param data The ds2482 channel pointer
384 * @return 0=Device present, 1=No device present or error
386 static u8
ds2482_w1_reset_bus(void *data
)
388 struct ds2482_w1_chan
*pchan
= data
;
389 struct ds2482_data
*pdev
= pchan
->pdev
;
393 mutex_lock(&pdev
->access_lock
);
395 /* Select the channel */
396 ds2482_wait_1wire_idle(pdev
);
397 if (pdev
->w1_count
> 1)
398 ds2482_set_channel(pdev
, pchan
->channel
);
400 /* Send the reset command */
401 err
= ds2482_send_cmd(pdev
, DS2482_CMD_1WIRE_RESET
);
403 /* Wait until the reset is complete */
404 err
= ds2482_wait_1wire_idle(pdev
);
405 retval
= !(err
& DS2482_REG_STS_PPD
);
407 /* If the chip did reset since detect, re-config it */
408 if (err
& DS2482_REG_STS_RST
)
409 ds2482_send_cmd_data(pdev
, DS2482_CMD_WRITE_CONFIG
,
410 ds2482_calculate_config(0x00));
413 mutex_unlock(&pdev
->access_lock
);
418 static u8
ds2482_w1_set_pullup(void *data
, int delay
)
420 struct ds2482_w1_chan
*pchan
= data
;
421 struct ds2482_data
*pdev
= pchan
->pdev
;
424 /* if delay is non-zero activate the pullup,
425 * the strong pullup will be automatically deactivated
426 * by the master, so do not explicitly deactive it
429 /* both waits are crucial, otherwise devices might not be
430 * powered long enough, causing e.g. a w1_therm sensor to
431 * provide wrong conversion results
433 ds2482_wait_1wire_idle(pdev
);
434 /* note: it seems like both SPU and APU have to be set! */
435 retval
= ds2482_send_cmd_data(pdev
, DS2482_CMD_WRITE_CONFIG
,
436 ds2482_calculate_config(DS2482_REG_CFG_SPU
|
437 DS2482_REG_CFG_APU
));
438 ds2482_wait_1wire_idle(pdev
);
445 static int ds2482_probe(struct i2c_client
*client
,
446 const struct i2c_device_id
*id
)
448 struct ds2482_data
*data
;
453 if (!i2c_check_functionality(client
->adapter
,
454 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
455 I2C_FUNC_SMBUS_BYTE
))
458 if (!(data
= kzalloc(sizeof(struct ds2482_data
), GFP_KERNEL
))) {
463 data
->client
= client
;
464 i2c_set_clientdata(client
, data
);
466 /* Reset the device (sets the read_ptr to status) */
467 if (ds2482_send_cmd(data
, DS2482_CMD_RESET
) < 0) {
468 dev_warn(&client
->dev
, "DS2482 reset failed.\n");
472 /* Sleep at least 525ns to allow the reset to complete */
475 /* Read the status byte - only reset bit and line should be set */
476 temp1
= i2c_smbus_read_byte(client
);
477 if (temp1
!= (DS2482_REG_STS_LL
| DS2482_REG_STS_RST
)) {
478 dev_warn(&client
->dev
, "DS2482 reset status "
479 "0x%02X - not a DS2482\n", temp1
);
483 /* Detect the 8-port version */
485 if (ds2482_set_channel(data
, 7) == 0)
488 /* Set all config items to 0 (off) */
489 ds2482_send_cmd_data(data
, DS2482_CMD_WRITE_CONFIG
,
490 ds2482_calculate_config(0x00));
492 mutex_init(&data
->access_lock
);
494 /* Register 1-wire interface(s) */
495 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
496 data
->w1_ch
[idx
].pdev
= data
;
497 data
->w1_ch
[idx
].channel
= idx
;
499 /* Populate all the w1 bus master stuff */
500 data
->w1_ch
[idx
].w1_bm
.data
= &data
->w1_ch
[idx
];
501 data
->w1_ch
[idx
].w1_bm
.read_byte
= ds2482_w1_read_byte
;
502 data
->w1_ch
[idx
].w1_bm
.write_byte
= ds2482_w1_write_byte
;
503 data
->w1_ch
[idx
].w1_bm
.touch_bit
= ds2482_w1_touch_bit
;
504 data
->w1_ch
[idx
].w1_bm
.triplet
= ds2482_w1_triplet
;
505 data
->w1_ch
[idx
].w1_bm
.reset_bus
= ds2482_w1_reset_bus
;
506 data
->w1_ch
[idx
].w1_bm
.set_pullup
= ds2482_w1_set_pullup
;
508 err
= w1_add_master_device(&data
->w1_ch
[idx
].w1_bm
);
510 data
->w1_ch
[idx
].pdev
= NULL
;
518 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
519 if (data
->w1_ch
[idx
].pdev
!= NULL
)
520 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
528 static int ds2482_remove(struct i2c_client
*client
)
530 struct ds2482_data
*data
= i2c_get_clientdata(client
);
533 /* Unregister the 1-wire bridge(s) */
534 for (idx
= 0; idx
< data
->w1_count
; idx
++) {
535 if (data
->w1_ch
[idx
].pdev
!= NULL
)
536 w1_remove_master_device(&data
->w1_ch
[idx
].w1_bm
);
539 /* Free the memory */
545 * Driver data (common to all clients)
547 static const struct i2c_device_id ds2482_id
[] = {
551 MODULE_DEVICE_TABLE(i2c
, ds2482_id
);
553 static struct i2c_driver ds2482_driver
= {
557 .probe
= ds2482_probe
,
558 .remove
= ds2482_remove
,
559 .id_table
= ds2482_id
,
561 module_i2c_driver(ds2482_driver
);
563 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
564 MODULE_DESCRIPTION("DS2482 driver");
566 MODULE_LICENSE("GPL");