Lines Matching +full:data +full:- +full:channel
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
46 * To read the data, issue a register read for any address
50 #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */
54 #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */
56 /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
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).
78 * To set the channel, write the value at the index of the channel.
98 * Client data (each client gets its own)
105 u8 channel;
113 /* 1-wire interface(s) */
117 /* per-device values */
118 u8 channel;
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) {
149 if (i2c_smbus_write_byte_data(pdev->client,
152 return -1;
154 pdev->read_prt = read_ptr;
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)
170 return -1;
172 pdev->read_prt = DS2482_PTR_CODE_STATUS;
177 * ds2482_send_cmd_data - Sends a command with a parameter
183 * @byte: The data to send
184 * Return: -1 on failure, 0 on success
189 if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
190 return -1;
193 pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
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;
218 temp = i2c_smbus_read_byte(pdev->client);
224 pr_err("%s: timeout on channel %d\n",
225 __func__, pdev->channel);
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)
238 static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
240 if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
241 ds2482_chan_wr[channel]) < 0)
242 return -1;
244 pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
245 pdev->channel = -1;
246 if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
247 pdev->channel = channel;
250 return -1;
255 * ds2482_w1_touch_bit - Performs the touch-bit function, which writes a 0 or 1 and reads the level.
257 * @data: The ds2482 channel pointer
258 * @bit: The level to write: 0 or non-zero
261 static u8 ds2482_w1_touch_bit(void *data, u8 bit)
263 struct ds2482_w1_chan *pchan = data;
264 struct ds2482_data *pdev = pchan->pdev;
265 int status = -1;
267 mutex_lock(&pdev->access_lock);
269 /* Select the channel */
271 if (pdev->w1_count > 1)
272 ds2482_set_channel(pdev, pchan->channel);
279 mutex_unlock(&pdev->access_lock);
285 * ds2482_w1_triplet - Performs the triplet function, which reads two bits and writes a bit.
289 * @data: The ds2482 channel pointer
293 static u8 ds2482_w1_triplet(void *data, u8 dbit)
295 struct ds2482_w1_chan *pchan = data;
296 struct ds2482_data *pdev = pchan->pdev;
299 mutex_lock(&pdev->access_lock);
301 /* Select the channel */
303 if (pdev->w1_count > 1)
304 ds2482_set_channel(pdev, pchan->channel);
311 mutex_unlock(&pdev->access_lock);
318 * ds2482_w1_write_byte - Performs the write byte function.
320 * @data: The ds2482 channel pointer
323 static void ds2482_w1_write_byte(void *data, u8 byte)
325 struct ds2482_w1_chan *pchan = data;
326 struct ds2482_data *pdev = pchan->pdev;
328 mutex_lock(&pdev->access_lock);
330 /* Select the channel */
332 if (pdev->w1_count > 1)
333 ds2482_set_channel(pdev, pchan->channel);
338 mutex_unlock(&pdev->access_lock);
342 * ds2482_w1_read_byte - Performs the read byte function.
344 * @data: The ds2482 channel pointer
347 static u8 ds2482_w1_read_byte(void *data)
349 struct ds2482_w1_chan *pchan = data;
350 struct ds2482_data *pdev = pchan->pdev;
353 mutex_lock(&pdev->access_lock);
355 /* Select the channel */
357 if (pdev->w1_count > 1)
358 ds2482_set_channel(pdev, pchan->channel);
366 /* Select the data register */
369 /* Read the data byte */
370 result = i2c_smbus_read_byte(pdev->client);
372 mutex_unlock(&pdev->access_lock);
379 * ds2482_w1_reset_bus - Sends a reset on the 1-wire interface
381 * @data: The ds2482 channel pointer
384 static u8 ds2482_w1_reset_bus(void *data)
386 struct ds2482_w1_chan *pchan = data;
387 struct ds2482_data *pdev = pchan->pdev;
391 mutex_lock(&pdev->access_lock);
393 /* Select the channel */
395 if (pdev->w1_count > 1)
396 ds2482_set_channel(pdev, pchan->channel);
405 /* If the chip did reset since detect, re-config it */
411 mutex_unlock(&pdev->access_lock);
416 static u8 ds2482_w1_set_pullup(void *data, int delay)
418 struct ds2482_w1_chan *pchan = data;
419 struct ds2482_data *pdev = pchan->pdev;
422 /* if delay is non-zero activate the pullup,
445 struct ds2482_data *data;
446 int err = -ENODEV;
451 if (!i2c_check_functionality(client->adapter,
454 return -ENODEV;
456 data = devm_kzalloc(&client->dev, sizeof(struct ds2482_data), GFP_KERNEL);
457 if (!data)
458 return -ENOMEM;
460 ret = devm_regulator_get_enable(&client->dev, "vcc");
462 return dev_err_probe(&client->dev, ret, "Failed to enable regulator\n");
464 data->client = client;
465 i2c_set_clientdata(client, data);
468 if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
469 dev_warn(&client->dev, "DS2482 reset failed.\n");
476 /* Read the status byte - only reset bit and line should be set */
479 dev_warn(&client->dev, "DS2482 reset status "
480 "0x%02X - not a DS2482\n", temp1);
484 /* Detect the 8-port version */
485 data->w1_count = 1;
486 if (ds2482_set_channel(data, 7) == 0)
487 data->w1_count = 8;
490 ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG,
493 mutex_init(&data->access_lock);
495 /* Register 1-wire interface(s) */
496 for (idx = 0; idx < data->w1_count; idx++) {
497 data->w1_ch[idx].pdev = data;
498 data->w1_ch[idx].channel = idx;
501 data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx];
502 data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte;
503 data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
504 data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit;
505 data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet;
506 data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus;
507 data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup;
509 err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
511 data->w1_ch[idx].pdev = NULL;
519 for (idx = 0; idx < data->w1_count; idx++) {
520 if (data->w1_ch[idx].pdev != NULL)
521 w1_remove_master_device(&data->w1_ch[idx].w1_bm);
528 struct ds2482_data *data = i2c_get_clientdata(client);
531 /* Unregister the 1-wire bridge(s) */
532 for (idx = 0; idx < data->w1_count; idx++) {
533 if (data->w1_ch[idx].pdev != NULL)
534 w1_remove_master_device(&data->w1_ch[idx].w1_bm);
539 * Driver data (common to all clients)