Lines Matching +full:two +full:- +full:wire

1 // SPDX-License-Identifier: GPL-2.0-only
3 * ds2482.c - provides i2c to w1-master bridge(s)
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.
10 * https://www.analog.com/en/products/ds2482-100.html
26 * The APU bit controls whether an active pullup (controlled slew-rate
28 * a 1-Wire line from low to high. When APU = 0, active pullup is disabled
30 * only a single slave on the 1-Wire line.
35 "0-disable, 1-enable (default)");
37 /* extra configurations - e.g. 1WS */
43 * The DS2482 registers - there are 3 registers that are addressed by a read
50 #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */
62 #define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */
70 #define DS2482_REG_CFG_1WS 0x08 /* 1-wire speed */
71 #define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */
73 #define DS2482_REG_CFG_APU 0x01 /* active pull-up */
77 * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
113 /* 1-wire interface(s) */
117 /* per-device values */
125 * ds2482_calculate_config - Helper to calculate values for configuration register
141 * ds2482_select_register - Sets the read pointer.
144 * Return: -1 on failure, 0 on success
148 if (pdev->read_prt != read_ptr) { in ds2482_select_register()
149 if (i2c_smbus_write_byte_data(pdev->client, in ds2482_select_register()
152 return -1; in ds2482_select_register()
154 pdev->read_prt = read_ptr; in ds2482_select_register()
160 * ds2482_send_cmd - Sends a command without a parameter
165 * Return: -1 on failure, 0 on success
169 if (i2c_smbus_write_byte(pdev->client, cmd) < 0) in ds2482_send_cmd()
170 return -1; in ds2482_send_cmd()
172 pdev->read_prt = DS2482_PTR_CODE_STATUS; in ds2482_send_cmd()
177 * ds2482_send_cmd_data - Sends a command with a parameter
184 * Return: -1 on failure, 0 on success
189 if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0) in ds2482_send_cmd_data()
190 return -1; in ds2482_send_cmd_data()
193 pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ? in ds2482_send_cmd_data()
200 * 1-Wire interface code
206 * ds2482_wait_1wire_idle - Waits until the 1-wire interface is idle (not busy)
209 * Return: the last value read from status or -1 (failure)
213 int temp = -1; in ds2482_wait_1wire_idle()
218 temp = i2c_smbus_read_byte(pdev->client); in ds2482_wait_1wire_idle()
225 __func__, pdev->channel); in ds2482_wait_1wire_idle()
231 * ds2482_set_channel - Selects a w1 channel.
232 * The 1-wire interface must be idle before calling this function.
235 * @channel: 0-7
236 * Return: -1 (failure) or 0 (success)
240 if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT, in ds2482_set_channel()
242 return -1; in ds2482_set_channel()
244 pdev->read_prt = DS2482_PTR_CODE_CHANNEL; in ds2482_set_channel()
245 pdev->channel = -1; in ds2482_set_channel()
246 if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) { in ds2482_set_channel()
247 pdev->channel = channel; in ds2482_set_channel()
250 return -1; in ds2482_set_channel()
255 * ds2482_w1_touch_bit - Performs the touch-bit function, which writes a 0 or 1 and reads the level.
258 * @bit: The level to write: 0 or non-zero
264 struct ds2482_data *pdev = pchan->pdev; in ds2482_w1_touch_bit()
265 int status = -1; in ds2482_w1_touch_bit()
267 mutex_lock(&pdev->access_lock); in ds2482_w1_touch_bit()
271 if (pdev->w1_count > 1) in ds2482_w1_touch_bit()
272 ds2482_set_channel(pdev, pchan->channel); in ds2482_w1_touch_bit()
279 mutex_unlock(&pdev->access_lock); in ds2482_w1_touch_bit()
285 * ds2482_w1_triplet - Performs the triplet function, which reads two bits and writes a bit.
286 * The bit written is determined by the two reads:
296 struct ds2482_data *pdev = pchan->pdev; in ds2482_w1_triplet()
299 mutex_lock(&pdev->access_lock); in ds2482_w1_triplet()
303 if (pdev->w1_count > 1) in ds2482_w1_triplet()
304 ds2482_set_channel(pdev, pchan->channel); in ds2482_w1_triplet()
311 mutex_unlock(&pdev->access_lock); in ds2482_w1_triplet()
318 * ds2482_w1_write_byte - Performs the write byte function.
326 struct ds2482_data *pdev = pchan->pdev; in ds2482_w1_write_byte()
328 mutex_lock(&pdev->access_lock); in ds2482_w1_write_byte()
332 if (pdev->w1_count > 1) in ds2482_w1_write_byte()
333 ds2482_set_channel(pdev, pchan->channel); in ds2482_w1_write_byte()
338 mutex_unlock(&pdev->access_lock); in ds2482_w1_write_byte()
342 * ds2482_w1_read_byte - Performs the read byte function.
350 struct ds2482_data *pdev = pchan->pdev; in ds2482_w1_read_byte()
353 mutex_lock(&pdev->access_lock); in ds2482_w1_read_byte()
357 if (pdev->w1_count > 1) in ds2482_w1_read_byte()
358 ds2482_set_channel(pdev, pchan->channel); in ds2482_w1_read_byte()
370 result = i2c_smbus_read_byte(pdev->client); in ds2482_w1_read_byte()
372 mutex_unlock(&pdev->access_lock); in ds2482_w1_read_byte()
379 * ds2482_w1_reset_bus - Sends a reset on the 1-wire interface
387 struct ds2482_data *pdev = pchan->pdev; in ds2482_w1_reset_bus()
391 mutex_lock(&pdev->access_lock); in ds2482_w1_reset_bus()
395 if (pdev->w1_count > 1) in ds2482_w1_reset_bus()
396 ds2482_set_channel(pdev, pchan->channel); in ds2482_w1_reset_bus()
405 /* If the chip did reset since detect, re-config it */ in ds2482_w1_reset_bus()
411 mutex_unlock(&pdev->access_lock); in ds2482_w1_reset_bus()
419 struct ds2482_data *pdev = pchan->pdev; in ds2482_w1_set_pullup()
422 /* if delay is non-zero activate the pullup, in ds2482_w1_set_pullup()
446 int err = -ENODEV; in ds2482_probe()
451 if (!i2c_check_functionality(client->adapter, in ds2482_probe()
454 return -ENODEV; in ds2482_probe()
456 data = devm_kzalloc(&client->dev, sizeof(struct ds2482_data), GFP_KERNEL); in ds2482_probe()
458 return -ENOMEM; in ds2482_probe()
460 ret = devm_regulator_get_enable(&client->dev, "vcc"); in ds2482_probe()
462 return dev_err_probe(&client->dev, ret, "Failed to enable regulator\n"); in ds2482_probe()
464 data->client = client; in ds2482_probe()
469 dev_warn(&client->dev, "DS2482 reset failed.\n"); in ds2482_probe()
476 /* Read the status byte - only reset bit and line should be set */ in ds2482_probe()
479 dev_warn(&client->dev, "DS2482 reset status " in ds2482_probe()
480 "0x%02X - not a DS2482\n", temp1); in ds2482_probe()
484 /* Detect the 8-port version */ in ds2482_probe()
485 data->w1_count = 1; in ds2482_probe()
487 data->w1_count = 8; in ds2482_probe()
493 mutex_init(&data->access_lock); in ds2482_probe()
495 /* Register 1-wire interface(s) */ in ds2482_probe()
496 for (idx = 0; idx < data->w1_count; idx++) { in ds2482_probe()
497 data->w1_ch[idx].pdev = data; in ds2482_probe()
498 data->w1_ch[idx].channel = idx; in ds2482_probe()
501 data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx]; in ds2482_probe()
502 data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte; in ds2482_probe()
503 data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte; in ds2482_probe()
504 data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit; in ds2482_probe()
505 data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet; in ds2482_probe()
506 data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus; in ds2482_probe()
507 data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup; in ds2482_probe()
509 err = w1_add_master_device(&data->w1_ch[idx].w1_bm); in ds2482_probe()
511 data->w1_ch[idx].pdev = NULL; in ds2482_probe()
519 for (idx = 0; idx < data->w1_count; idx++) { in ds2482_probe()
520 if (data->w1_ch[idx].pdev != NULL) in ds2482_probe()
521 w1_remove_master_device(&data->w1_ch[idx].w1_bm); in ds2482_probe()
531 /* Unregister the 1-wire bridge(s) */ in ds2482_remove()
532 for (idx = 0; idx < data->w1_count; idx++) { in ds2482_remove()
533 if (data->w1_ch[idx].pdev != NULL) in ds2482_remove()
534 w1_remove_master_device(&data->w1_ch[idx].w1_bm); in ds2482_remove()