1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ds2482.c - provides i2c to w1-master bridge(s) 4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com> 5 * 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 * https://www.analog.com/en/products/ds2482-100.html 11 */ 12 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 <linux/regulator/consumer.h> 19 20 #include <linux/w1.h> 21 22 /* 23 * Allow the active pullup to be disabled, default is enabled. 24 * 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. 31 */ 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)"); 36 37 /* extra configurations - e.g. 1WS */ 38 static int extra_config; 39 module_param(extra_config, int, 0644); 40 MODULE_PARM_DESC(extra_config, "Extra Configuration settings 1=APU,2=PPM,3=SPU,8=1WS"); 41 42 /* 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. 45 * 46 * To read the data, issue a register read for any address 47 */ 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) */ 58 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 64 65 /* 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. 69 */ 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 */ 74 75 76 /* 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. 80 */ 81 static const u8 ds2482_chan_wr[8] = { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 }; 82 static const u8 ds2482_chan_rd[8] = { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 }; 83 84 85 /* 86 * Status Register bit definitions (read only) 87 */ 88 #define DS2482_REG_STS_DIR 0x80 89 #define DS2482_REG_STS_TSB 0x40 90 #define DS2482_REG_STS_SBR 0x20 91 #define DS2482_REG_STS_RST 0x10 92 #define DS2482_REG_STS_LL 0x08 93 #define DS2482_REG_STS_SD 0x04 94 #define DS2482_REG_STS_PPD 0x02 95 #define DS2482_REG_STS_1WB 0x01 96 97 /* 98 * Client data (each client gets its own) 99 */ 100 101 struct ds2482_data; 102 103 struct ds2482_w1_chan { 104 struct ds2482_data *pdev; 105 u8 channel; 106 struct w1_bus_master w1_bm; 107 }; 108 109 struct ds2482_data { 110 struct i2c_client *client; 111 struct mutex access_lock; 112 113 /* 1-wire interface(s) */ 114 int w1_count; /* 1 or 8 */ 115 struct ds2482_w1_chan w1_ch[8]; 116 117 /* per-device values */ 118 u8 channel; 119 u8 read_prt; /* see DS2482_PTR_CODE_xxx */ 120 u8 reg_config; 121 }; 122 123 124 /** 125 * ds2482_calculate_config - Helper to calculate values for configuration register 126 * @conf: the raw config value 127 * Return: the value w/ complements that can be written to register 128 */ 129 static inline u8 ds2482_calculate_config(u8 conf) 130 { 131 conf |= extra_config; 132 133 if (ds2482_active_pullup) 134 conf |= DS2482_REG_CFG_APU; 135 136 return conf | ((~conf & 0x0f) << 4); 137 } 138 139 140 /** 141 * ds2482_select_register - Sets the read pointer. 142 * @pdev: The ds2482 client pointer 143 * @read_ptr: see DS2482_PTR_CODE_xxx above 144 * Return: -1 on failure, 0 on success 145 */ 146 static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr) 147 { 148 if (pdev->read_prt != read_ptr) { 149 if (i2c_smbus_write_byte_data(pdev->client, 150 DS2482_CMD_SET_READ_PTR, 151 read_ptr) < 0) 152 return -1; 153 154 pdev->read_prt = read_ptr; 155 } 156 return 0; 157 } 158 159 /** 160 * ds2482_send_cmd - Sends a command without a parameter 161 * @pdev: The ds2482 client pointer 162 * @cmd: DS2482_CMD_RESET, 163 * DS2482_CMD_1WIRE_RESET, 164 * DS2482_CMD_1WIRE_READ_BYTE 165 * Return: -1 on failure, 0 on success 166 */ 167 static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd) 168 { 169 if (i2c_smbus_write_byte(pdev->client, cmd) < 0) 170 return -1; 171 172 pdev->read_prt = DS2482_PTR_CODE_STATUS; 173 return 0; 174 } 175 176 /** 177 * ds2482_send_cmd_data - Sends a command with a parameter 178 * @pdev: The ds2482 client pointer 179 * @cmd: DS2482_CMD_WRITE_CONFIG, 180 * DS2482_CMD_1WIRE_SINGLE_BIT, 181 * DS2482_CMD_1WIRE_WRITE_BYTE, 182 * DS2482_CMD_1WIRE_TRIPLET 183 * @byte: The data to send 184 * Return: -1 on failure, 0 on success 185 */ 186 static inline int ds2482_send_cmd_data(struct ds2482_data *pdev, 187 u8 cmd, u8 byte) 188 { 189 if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0) 190 return -1; 191 192 /* all cmds leave in STATUS, except CONFIG */ 193 pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ? 194 DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG; 195 return 0; 196 } 197 198 199 /* 200 * 1-Wire interface code 201 */ 202 203 #define DS2482_WAIT_IDLE_TIMEOUT 100 204 205 /** 206 * ds2482_wait_1wire_idle - Waits until the 1-wire interface is idle (not busy) 207 * 208 * @pdev: Pointer to the device structure 209 * Return: the last value read from status or -1 (failure) 210 */ 211 static int ds2482_wait_1wire_idle(struct ds2482_data *pdev) 212 { 213 int temp = -1; 214 int retries = 0; 215 216 if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) { 217 do { 218 temp = i2c_smbus_read_byte(pdev->client); 219 } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) && 220 (++retries < DS2482_WAIT_IDLE_TIMEOUT)); 221 } 222 223 if (retries >= DS2482_WAIT_IDLE_TIMEOUT) 224 pr_err("%s: timeout on channel %d\n", 225 __func__, pdev->channel); 226 227 return temp; 228 } 229 230 /** 231 * ds2482_set_channel - Selects a w1 channel. 232 * The 1-wire interface must be idle before calling this function. 233 * 234 * @pdev: The ds2482 client pointer 235 * @channel: 0-7 236 * Return: -1 (failure) or 0 (success) 237 */ 238 static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel) 239 { 240 if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT, 241 ds2482_chan_wr[channel]) < 0) 242 return -1; 243 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; 248 return 0; 249 } 250 return -1; 251 } 252 253 254 /** 255 * ds2482_w1_touch_bit - Performs the touch-bit function, which writes a 0 or 1 and reads the level. 256 * 257 * @data: The ds2482 channel pointer 258 * @bit: The level to write: 0 or non-zero 259 * Return: The level read: 0 or 1 260 */ 261 static u8 ds2482_w1_touch_bit(void *data, u8 bit) 262 { 263 struct ds2482_w1_chan *pchan = data; 264 struct ds2482_data *pdev = pchan->pdev; 265 int status = -1; 266 267 mutex_lock(&pdev->access_lock); 268 269 /* Select the channel */ 270 ds2482_wait_1wire_idle(pdev); 271 if (pdev->w1_count > 1) 272 ds2482_set_channel(pdev, pchan->channel); 273 274 /* Send the touch command, wait until 1WB == 0, return the status */ 275 if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT, 276 bit ? 0xFF : 0)) 277 status = ds2482_wait_1wire_idle(pdev); 278 279 mutex_unlock(&pdev->access_lock); 280 281 return (status & DS2482_REG_STS_SBR) ? 1 : 0; 282 } 283 284 /** 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: 287 * 00 => dbit, 01 => 0, 10 => 1 288 * 289 * @data: The ds2482 channel pointer 290 * @dbit: The direction to choose if both branches are valid 291 * Return: b0=read1 b1=read2 b3=bit written 292 */ 293 static u8 ds2482_w1_triplet(void *data, u8 dbit) 294 { 295 struct ds2482_w1_chan *pchan = data; 296 struct ds2482_data *pdev = pchan->pdev; 297 int status = (3 << 5); 298 299 mutex_lock(&pdev->access_lock); 300 301 /* Select the channel */ 302 ds2482_wait_1wire_idle(pdev); 303 if (pdev->w1_count > 1) 304 ds2482_set_channel(pdev, pchan->channel); 305 306 /* Send the triplet command, wait until 1WB == 0, return the status */ 307 if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET, 308 dbit ? 0xFF : 0)) 309 status = ds2482_wait_1wire_idle(pdev); 310 311 mutex_unlock(&pdev->access_lock); 312 313 /* Decode the status */ 314 return (status >> 5); 315 } 316 317 /** 318 * ds2482_w1_write_byte - Performs the write byte function. 319 * 320 * @data: The ds2482 channel pointer 321 * @byte: The value to write 322 */ 323 static void ds2482_w1_write_byte(void *data, u8 byte) 324 { 325 struct ds2482_w1_chan *pchan = data; 326 struct ds2482_data *pdev = pchan->pdev; 327 328 mutex_lock(&pdev->access_lock); 329 330 /* Select the channel */ 331 ds2482_wait_1wire_idle(pdev); 332 if (pdev->w1_count > 1) 333 ds2482_set_channel(pdev, pchan->channel); 334 335 /* Send the write byte command */ 336 ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte); 337 338 mutex_unlock(&pdev->access_lock); 339 } 340 341 /** 342 * ds2482_w1_read_byte - Performs the read byte function. 343 * 344 * @data: The ds2482 channel pointer 345 * Return: The value read 346 */ 347 static u8 ds2482_w1_read_byte(void *data) 348 { 349 struct ds2482_w1_chan *pchan = data; 350 struct ds2482_data *pdev = pchan->pdev; 351 int result; 352 353 mutex_lock(&pdev->access_lock); 354 355 /* Select the channel */ 356 ds2482_wait_1wire_idle(pdev); 357 if (pdev->w1_count > 1) 358 ds2482_set_channel(pdev, pchan->channel); 359 360 /* Send the read byte command */ 361 ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE); 362 363 /* Wait until 1WB == 0 */ 364 ds2482_wait_1wire_idle(pdev); 365 366 /* Select the data register */ 367 ds2482_select_register(pdev, DS2482_PTR_CODE_DATA); 368 369 /* Read the data byte */ 370 result = i2c_smbus_read_byte(pdev->client); 371 372 mutex_unlock(&pdev->access_lock); 373 374 return result; 375 } 376 377 378 /** 379 * ds2482_w1_reset_bus - Sends a reset on the 1-wire interface 380 * 381 * @data: The ds2482 channel pointer 382 * Return: 0=Device present, 1=No device present or error 383 */ 384 static u8 ds2482_w1_reset_bus(void *data) 385 { 386 struct ds2482_w1_chan *pchan = data; 387 struct ds2482_data *pdev = pchan->pdev; 388 int err; 389 u8 retval = 1; 390 391 mutex_lock(&pdev->access_lock); 392 393 /* Select the channel */ 394 ds2482_wait_1wire_idle(pdev); 395 if (pdev->w1_count > 1) 396 ds2482_set_channel(pdev, pchan->channel); 397 398 /* Send the reset command */ 399 err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET); 400 if (err >= 0) { 401 /* Wait until the reset is complete */ 402 err = ds2482_wait_1wire_idle(pdev); 403 retval = !(err & DS2482_REG_STS_PPD); 404 405 /* If the chip did reset since detect, re-config it */ 406 if (err & DS2482_REG_STS_RST) 407 ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG, 408 ds2482_calculate_config(0x00)); 409 } 410 411 mutex_unlock(&pdev->access_lock); 412 413 return retval; 414 } 415 416 static u8 ds2482_w1_set_pullup(void *data, int delay) 417 { 418 struct ds2482_w1_chan *pchan = data; 419 struct ds2482_data *pdev = pchan->pdev; 420 u8 retval = 1; 421 422 /* if delay is non-zero activate the pullup, 423 * the strong pullup will be automatically deactivated 424 * by the master, so do not explicitly deactive it 425 */ 426 if (delay) { 427 /* both waits are crucial, otherwise devices might not be 428 * powered long enough, causing e.g. a w1_therm sensor to 429 * provide wrong conversion results 430 */ 431 ds2482_wait_1wire_idle(pdev); 432 /* note: it seems like both SPU and APU have to be set! */ 433 retval = ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG, 434 ds2482_calculate_config(DS2482_REG_CFG_SPU | 435 DS2482_REG_CFG_APU)); 436 ds2482_wait_1wire_idle(pdev); 437 } 438 439 return retval; 440 } 441 442 443 static int ds2482_probe(struct i2c_client *client) 444 { 445 struct ds2482_data *data; 446 int err = -ENODEV; 447 int temp1; 448 int idx; 449 int ret; 450 451 if (!i2c_check_functionality(client->adapter, 452 I2C_FUNC_SMBUS_WRITE_BYTE_DATA | 453 I2C_FUNC_SMBUS_BYTE)) 454 return -ENODEV; 455 456 data = devm_kzalloc(&client->dev, sizeof(struct ds2482_data), GFP_KERNEL); 457 if (!data) 458 return -ENOMEM; 459 460 ret = devm_regulator_get_enable(&client->dev, "vcc"); 461 if (ret) 462 return dev_err_probe(&client->dev, ret, "Failed to enable regulator\n"); 463 464 data->client = client; 465 i2c_set_clientdata(client, data); 466 467 /* Reset the device (sets the read_ptr to status) */ 468 if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) { 469 dev_warn(&client->dev, "DS2482 reset failed.\n"); 470 return err; 471 } 472 473 /* Sleep at least 525ns to allow the reset to complete */ 474 ndelay(525); 475 476 /* Read the status byte - only reset bit and line should be set */ 477 temp1 = i2c_smbus_read_byte(client); 478 if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) { 479 dev_warn(&client->dev, "DS2482 reset status " 480 "0x%02X - not a DS2482\n", temp1); 481 return err; 482 } 483 484 /* Detect the 8-port version */ 485 data->w1_count = 1; 486 if (ds2482_set_channel(data, 7) == 0) 487 data->w1_count = 8; 488 489 /* Set all config items to 0 (off) */ 490 ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, 491 ds2482_calculate_config(0x00)); 492 493 mutex_init(&data->access_lock); 494 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; 499 500 /* Populate all the w1 bus master stuff */ 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; 508 509 err = w1_add_master_device(&data->w1_ch[idx].w1_bm); 510 if (err) { 511 data->w1_ch[idx].pdev = NULL; 512 goto exit_w1_remove; 513 } 514 } 515 516 return 0; 517 518 exit_w1_remove: 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); 522 } 523 return err; 524 } 525 526 static void ds2482_remove(struct i2c_client *client) 527 { 528 struct ds2482_data *data = i2c_get_clientdata(client); 529 int idx; 530 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); 535 } 536 } 537 538 /* 539 * Driver data (common to all clients) 540 */ 541 static const struct i2c_device_id ds2482_id[] = { 542 { "ds2482" }, 543 { "ds2484" }, 544 { } 545 }; 546 MODULE_DEVICE_TABLE(i2c, ds2482_id); 547 548 static struct i2c_driver ds2482_driver = { 549 .driver = { 550 .name = "ds2482", 551 }, 552 .probe = ds2482_probe, 553 .remove = ds2482_remove, 554 .id_table = ds2482_id, 555 }; 556 module_i2c_driver(ds2482_driver); 557 558 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>"); 559 MODULE_DESCRIPTION("DS2482 driver"); 560 561 MODULE_LICENSE("GPL"); 562