0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/crc16.h>
0009 #include <linux/delay.h>
0010 #include <linux/device.h>
0011 #include <linux/i2c.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/moduleparam.h>
0015 #include <linux/slab.h>
0016 #include <linux/types.h>
0017 #include <linux/uaccess.h>
0018
0019 #define CRC16_INIT 0
0020
0021 #include <linux/w1.h>
0022
0023 #define W1_FAMILY_DS28E17 0x19
0024
0025
0026 MODULE_LICENSE("GPL v2");
0027 MODULE_AUTHOR("Jan Kandziora <jjj@gmx.de>");
0028 MODULE_DESCRIPTION("w1 family 19 driver for DS28E17, 1-wire to I2C master bridge");
0029 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS28E17));
0030
0031
0032
0033 static int i2c_speed = 100;
0034 module_param_named(speed, i2c_speed, int, (S_IRUSR | S_IWUSR));
0035 MODULE_PARM_DESC(speed, "Default I2C speed to be set when a DS28E17 is detected");
0036
0037
0038 static char i2c_stretch = 1;
0039 module_param_named(stretch, i2c_stretch, byte, (S_IRUSR | S_IWUSR));
0040 MODULE_PARM_DESC(stretch, "Default I2C stretch value to be set when a DS28E17 is detected");
0041
0042
0043 #define W1_F19_WRITE_DATA_WITH_STOP 0x4B
0044 #define W1_F19_WRITE_DATA_NO_STOP 0x5A
0045 #define W1_F19_WRITE_DATA_ONLY 0x69
0046 #define W1_F19_WRITE_DATA_ONLY_WITH_STOP 0x78
0047 #define W1_F19_READ_DATA_WITH_STOP 0x87
0048 #define W1_F19_WRITE_READ_DATA_WITH_STOP 0x2D
0049 #define W1_F19_WRITE_CONFIGURATION 0xD2
0050 #define W1_F19_READ_CONFIGURATION 0xE1
0051 #define W1_F19_ENABLE_SLEEP_MODE 0x1E
0052 #define W1_F19_READ_DEVICE_REVISION 0xC4
0053
0054
0055 #define W1_F19_STATUS_CRC 0x01
0056 #define W1_F19_STATUS_ADDRESS 0x02
0057 #define W1_F19_STATUS_START 0x08
0058
0059
0060
0061
0062
0063 #define W1_F19_WRITE_DATA_LIMIT 255
0064
0065
0066 #define W1_F19_READ_DATA_LIMIT 255
0067
0068
0069 #define W1_F19_BUSY_TIMEBASES { 90, 23, 10 }
0070 #define W1_F19_BUSY_GRATUITY 1000
0071
0072
0073 #define W1_F19_BUSY_CHECKS 1000
0074
0075
0076
0077 struct w1_f19_data {
0078 u8 speed;
0079 u8 stretch;
0080 struct i2c_adapter adapter;
0081 };
0082
0083
0084
0085 static int w1_f19_i2c_busy_wait(struct w1_slave *sl, size_t count)
0086 {
0087 const unsigned long timebases[3] = W1_F19_BUSY_TIMEBASES;
0088 struct w1_f19_data *data = sl->family_data;
0089 unsigned int checks;
0090
0091
0092 if (w1_touch_bit(sl->master, 1) == 0)
0093 return 0;
0094
0095
0096
0097
0098
0099
0100 usleep_range(timebases[data->speed] * (data->stretch) * count,
0101 timebases[data->speed] * (data->stretch) * count
0102 + W1_F19_BUSY_GRATUITY);
0103
0104
0105 checks = W1_F19_BUSY_CHECKS;
0106 while ((checks--) > 0) {
0107
0108 if (w1_touch_bit(sl->master, 1) == 0)
0109 return 0;
0110
0111
0112 udelay(timebases[data->speed]);
0113 }
0114
0115
0116 dev_warn(&sl->dev, "busy timeout\n");
0117 return -ETIMEDOUT;
0118 }
0119
0120
0121
0122 static size_t w1_f19_error(struct w1_slave *sl, u8 w1_buf[])
0123 {
0124
0125 if (w1_buf[0] & W1_F19_STATUS_CRC)
0126 dev_warn(&sl->dev, "crc16 mismatch\n");
0127 if (w1_buf[0] & W1_F19_STATUS_ADDRESS)
0128 dev_warn(&sl->dev, "i2c device not responding\n");
0129 if ((w1_buf[0] & (W1_F19_STATUS_CRC | W1_F19_STATUS_ADDRESS)) == 0
0130 && w1_buf[1] != 0) {
0131 dev_warn(&sl->dev, "i2c short write, %d bytes not acknowledged\n",
0132 w1_buf[1]);
0133 }
0134
0135
0136 if (w1_buf[0] & W1_F19_STATUS_ADDRESS)
0137 return -ENXIO;
0138 if (w1_buf[0] & W1_F19_STATUS_START)
0139 return -EAGAIN;
0140 if (w1_buf[0] != 0 || w1_buf[1] != 0)
0141 return -EIO;
0142
0143
0144 return 0;
0145 }
0146
0147
0148
0149 static int __w1_f19_i2c_write(struct w1_slave *sl,
0150 const u8 *command, size_t command_count,
0151 const u8 *buffer, size_t count)
0152 {
0153 u16 crc;
0154 int error;
0155 u8 w1_buf[2];
0156
0157
0158 crc = crc16(CRC16_INIT, command, command_count);
0159 w1_write_block(sl->master, command, command_count);
0160
0161 w1_buf[0] = count;
0162 crc = crc16(crc, w1_buf, 1);
0163 w1_write_8(sl->master, w1_buf[0]);
0164
0165 crc = crc16(crc, buffer, count);
0166 w1_write_block(sl->master, buffer, count);
0167
0168 w1_buf[0] = ~(crc & 0xFF);
0169 w1_buf[1] = ~((crc >> 8) & 0xFF);
0170 w1_write_block(sl->master, w1_buf, 2);
0171
0172
0173 if (w1_f19_i2c_busy_wait(sl, count + 1) < 0)
0174 return -ETIMEDOUT;
0175
0176
0177 w1_read_block(sl->master, w1_buf, 2);
0178
0179
0180 error = w1_f19_error(sl, w1_buf);
0181 if (error < 0)
0182 return error;
0183
0184
0185 return count;
0186 }
0187
0188
0189
0190 static int w1_f19_i2c_write(struct w1_slave *sl, u16 i2c_address,
0191 const u8 *buffer, size_t count, bool stop)
0192 {
0193 int result;
0194 int remaining = count;
0195 const u8 *p;
0196 u8 command[2];
0197
0198
0199 if (count == 0)
0200 return -EOPNOTSUPP;
0201
0202
0203 if (count <= W1_F19_WRITE_DATA_LIMIT) {
0204
0205
0206
0207
0208
0209
0210 command[0] = (stop ? W1_F19_WRITE_DATA_WITH_STOP
0211 : W1_F19_WRITE_DATA_NO_STOP);
0212 command[1] = i2c_address << 1;
0213 result = __w1_f19_i2c_write(sl, command, 2, buffer, count);
0214 } else {
0215
0216
0217
0218 p = buffer;
0219 command[0] = W1_F19_WRITE_DATA_NO_STOP;
0220 command[1] = i2c_address << 1;
0221 result = __w1_f19_i2c_write(sl, command, 2, p,
0222 W1_F19_WRITE_DATA_LIMIT);
0223 if (result < 0)
0224 return result;
0225
0226
0227 if (w1_reset_resume_command(sl->master))
0228 return -EIO;
0229
0230
0231 p += W1_F19_WRITE_DATA_LIMIT;
0232 remaining -= W1_F19_WRITE_DATA_LIMIT;
0233
0234 while (remaining > W1_F19_WRITE_DATA_LIMIT) {
0235
0236 command[0] = W1_F19_WRITE_DATA_ONLY;
0237 result = __w1_f19_i2c_write(sl, command, 1, p,
0238 W1_F19_WRITE_DATA_LIMIT);
0239 if (result < 0)
0240 return result;
0241
0242
0243 if (w1_reset_resume_command(sl->master))
0244 return -EIO;
0245
0246
0247 p += W1_F19_WRITE_DATA_LIMIT;
0248 remaining -= W1_F19_WRITE_DATA_LIMIT;
0249 }
0250
0251
0252 command[0] = (stop ? W1_F19_WRITE_DATA_ONLY_WITH_STOP
0253 : W1_F19_WRITE_DATA_ONLY);
0254 result = __w1_f19_i2c_write(sl, command, 1, p, remaining);
0255 }
0256
0257 return result;
0258 }
0259
0260
0261
0262 static int w1_f19_i2c_read(struct w1_slave *sl, u16 i2c_address,
0263 u8 *buffer, size_t count)
0264 {
0265 u16 crc;
0266 int error;
0267 u8 w1_buf[5];
0268
0269
0270 if (count == 0)
0271 return -EOPNOTSUPP;
0272
0273
0274 w1_buf[0] = W1_F19_READ_DATA_WITH_STOP;
0275 w1_buf[1] = i2c_address << 1 | 0x01;
0276 w1_buf[2] = count;
0277 crc = crc16(CRC16_INIT, w1_buf, 3);
0278 w1_buf[3] = ~(crc & 0xFF);
0279 w1_buf[4] = ~((crc >> 8) & 0xFF);
0280 w1_write_block(sl->master, w1_buf, 5);
0281
0282
0283 if (w1_f19_i2c_busy_wait(sl, count + 1) < 0)
0284 return -ETIMEDOUT;
0285
0286
0287 w1_buf[0] = w1_read_8(sl->master);
0288 w1_buf[1] = 0;
0289
0290
0291 error = w1_f19_error(sl, w1_buf);
0292 if (error < 0)
0293 return error;
0294
0295
0296 return w1_read_block(sl->master, buffer, count);
0297 }
0298
0299
0300
0301 static int w1_f19_i2c_write_read(struct w1_slave *sl, u16 i2c_address,
0302 const u8 *wbuffer, size_t wcount, u8 *rbuffer, size_t rcount)
0303 {
0304 u16 crc;
0305 int error;
0306 u8 w1_buf[3];
0307
0308
0309 if (wcount == 0 || rcount == 0)
0310 return -EOPNOTSUPP;
0311
0312
0313 w1_buf[0] = W1_F19_WRITE_READ_DATA_WITH_STOP;
0314 w1_buf[1] = i2c_address << 1;
0315 w1_buf[2] = wcount;
0316 crc = crc16(CRC16_INIT, w1_buf, 3);
0317 w1_write_block(sl->master, w1_buf, 3);
0318
0319 crc = crc16(crc, wbuffer, wcount);
0320 w1_write_block(sl->master, wbuffer, wcount);
0321
0322 w1_buf[0] = rcount;
0323 crc = crc16(crc, w1_buf, 1);
0324 w1_buf[1] = ~(crc & 0xFF);
0325 w1_buf[2] = ~((crc >> 8) & 0xFF);
0326 w1_write_block(sl->master, w1_buf, 3);
0327
0328
0329 if (w1_f19_i2c_busy_wait(sl, wcount + rcount + 2) < 0)
0330 return -ETIMEDOUT;
0331
0332
0333 w1_read_block(sl->master, w1_buf, 2);
0334
0335
0336 error = w1_f19_error(sl, w1_buf);
0337 if (error < 0)
0338 return error;
0339
0340
0341 return w1_read_block(sl->master, rbuffer, rcount);
0342 }
0343
0344
0345
0346 static int w1_f19_i2c_master_transfer(struct i2c_adapter *adapter,
0347 struct i2c_msg *msgs, int num)
0348 {
0349 struct w1_slave *sl = (struct w1_slave *) adapter->algo_data;
0350 int i = 0;
0351 int result = 0;
0352
0353
0354 mutex_lock(&sl->master->bus_mutex);
0355
0356
0357 if (w1_reset_select_slave(sl)) {
0358 i = -EIO;
0359 goto error;
0360 }
0361
0362
0363 while (i < num) {
0364
0365
0366
0367
0368 if (i < (num-1)
0369 && msgs[i].addr == msgs[i+1].addr
0370 && !(msgs[i].flags & I2C_M_RD)
0371 && (msgs[i+1].flags & I2C_M_RD)
0372 && (msgs[i].len <= W1_F19_WRITE_DATA_LIMIT)) {
0373
0374
0375
0376
0377 result = w1_f19_i2c_write_read(sl, msgs[i].addr,
0378 msgs[i].buf, msgs[i].len,
0379 msgs[i+1].buf, msgs[i+1].len);
0380 if (result < 0) {
0381 i = result;
0382 goto error;
0383 }
0384
0385
0386
0387
0388
0389
0390
0391 if (msgs[i+1].flags & I2C_M_RECV_LEN) {
0392 result = w1_f19_i2c_read(sl, msgs[i+1].addr,
0393 &(msgs[i+1].buf[1]), msgs[i+1].buf[0]);
0394 if (result < 0) {
0395 i = result;
0396 goto error;
0397 }
0398 }
0399
0400
0401 i++;
0402 } else if (msgs[i].flags & I2C_M_RD) {
0403
0404 result = w1_f19_i2c_read(sl, msgs[i].addr,
0405 msgs[i].buf, msgs[i].len);
0406 if (result < 0) {
0407 i = result;
0408 goto error;
0409 }
0410
0411
0412
0413
0414
0415
0416
0417 if (msgs[i].flags & I2C_M_RECV_LEN) {
0418 result = w1_f19_i2c_read(sl,
0419 msgs[i].addr,
0420 &(msgs[i].buf[1]),
0421 msgs[i].buf[0]);
0422 if (result < 0) {
0423 i = result;
0424 goto error;
0425 }
0426 }
0427 } else {
0428
0429
0430
0431
0432
0433 result = w1_f19_i2c_write(sl,
0434 msgs[i].addr,
0435 msgs[i].buf,
0436 msgs[i].len,
0437 i == (num-1));
0438 if (result < 0) {
0439 i = result;
0440 goto error;
0441 }
0442 }
0443
0444
0445 i++;
0446
0447
0448 if (i < num) {
0449
0450 if (w1_reset_resume_command(sl->master)) {
0451 i = -EIO;
0452 goto error;
0453 }
0454 }
0455 }
0456
0457 error:
0458
0459 mutex_unlock(&sl->master->bus_mutex);
0460
0461
0462 return i;
0463 }
0464
0465
0466
0467 static u32 w1_f19_i2c_functionality(struct i2c_adapter *adapter)
0468 {
0469
0470
0471
0472
0473
0474
0475
0476 return I2C_FUNC_I2C |
0477 I2C_FUNC_SMBUS_BYTE |
0478 I2C_FUNC_SMBUS_BYTE_DATA |
0479 I2C_FUNC_SMBUS_WORD_DATA |
0480 I2C_FUNC_SMBUS_PROC_CALL |
0481 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA |
0482 I2C_FUNC_SMBUS_I2C_BLOCK |
0483 I2C_FUNC_SMBUS_PEC;
0484 }
0485
0486
0487
0488 static const struct i2c_adapter_quirks w1_f19_i2c_adapter_quirks = {
0489 .max_read_len = W1_F19_READ_DATA_LIMIT,
0490 };
0491
0492
0493 static const struct i2c_algorithm w1_f19_i2c_algorithm = {
0494 .master_xfer = w1_f19_i2c_master_transfer,
0495 .functionality = w1_f19_i2c_functionality,
0496 };
0497
0498
0499
0500 static int w1_f19_get_i2c_speed(struct w1_slave *sl)
0501 {
0502 struct w1_f19_data *data = sl->family_data;
0503 int result = -EIO;
0504
0505
0506 mutex_lock(&sl->master->bus_mutex);
0507
0508
0509 if (w1_reset_select_slave(sl))
0510 goto error;
0511
0512
0513 w1_write_8(sl->master, W1_F19_READ_CONFIGURATION);
0514 result = w1_read_8(sl->master);
0515 if (result < 0 || result > 2) {
0516 result = -EIO;
0517 goto error;
0518 }
0519
0520
0521 data->speed = result;
0522
0523 error:
0524
0525 mutex_unlock(&sl->master->bus_mutex);
0526
0527 return result;
0528 }
0529
0530
0531
0532 static int __w1_f19_set_i2c_speed(struct w1_slave *sl, u8 speed)
0533 {
0534 struct w1_f19_data *data = sl->family_data;
0535 const int i2c_speeds[3] = { 100, 400, 900 };
0536 u8 w1_buf[2];
0537
0538
0539 if (w1_reset_select_slave(sl))
0540 return -EIO;
0541
0542 w1_buf[0] = W1_F19_WRITE_CONFIGURATION;
0543 w1_buf[1] = speed;
0544 w1_write_block(sl->master, w1_buf, 2);
0545
0546
0547 data->speed = speed;
0548
0549 dev_info(&sl->dev, "i2c speed set to %d kBaud\n", i2c_speeds[speed]);
0550
0551 return 0;
0552 }
0553
0554 static int w1_f19_set_i2c_speed(struct w1_slave *sl, u8 speed)
0555 {
0556 int result;
0557
0558
0559 mutex_lock(&sl->master->bus_mutex);
0560
0561
0562 result = __w1_f19_set_i2c_speed(sl, speed);
0563
0564
0565 mutex_unlock(&sl->master->bus_mutex);
0566
0567 return result;
0568 }
0569
0570
0571
0572
0573
0574 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
0575 char *buf)
0576 {
0577 struct w1_slave *sl = dev_to_w1_slave(dev);
0578 int result;
0579
0580
0581 result = w1_f19_get_i2c_speed(sl);
0582 if (result < 0)
0583 return result;
0584
0585
0586 return sprintf(buf, "%d\n", result);
0587 }
0588
0589 static ssize_t speed_store(struct device *dev, struct device_attribute *attr,
0590 const char *buf, size_t count)
0591 {
0592 struct w1_slave *sl = dev_to_w1_slave(dev);
0593 int error;
0594
0595
0596 if (count < 3 || count > 4 || !buf)
0597 return -EINVAL;
0598 if (count == 4 && buf[3] != '\n')
0599 return -EINVAL;
0600 if (buf[1] != '0' || buf[2] != '0')
0601 return -EINVAL;
0602
0603
0604 switch (buf[0]) {
0605 case '1':
0606 error = w1_f19_set_i2c_speed(sl, 0);
0607 break;
0608 case '4':
0609 error = w1_f19_set_i2c_speed(sl, 1);
0610 break;
0611 case '9':
0612 error = w1_f19_set_i2c_speed(sl, 2);
0613 break;
0614 default:
0615 return -EINVAL;
0616 }
0617
0618 if (error < 0)
0619 return error;
0620
0621
0622 return count;
0623 }
0624
0625 static DEVICE_ATTR_RW(speed);
0626
0627
0628
0629 static ssize_t stretch_show(struct device *dev, struct device_attribute *attr,
0630 char *buf)
0631 {
0632 struct w1_slave *sl = dev_to_w1_slave(dev);
0633 struct w1_f19_data *data = sl->family_data;
0634
0635
0636 return sprintf(buf, "%d\n", data->stretch);
0637 }
0638
0639 static ssize_t stretch_store(struct device *dev, struct device_attribute *attr,
0640 const char *buf, size_t count)
0641 {
0642 struct w1_slave *sl = dev_to_w1_slave(dev);
0643 struct w1_f19_data *data = sl->family_data;
0644
0645
0646 if (count < 1 || count > 2 || !buf)
0647 return -EINVAL;
0648 if (count == 2 && buf[1] != '\n')
0649 return -EINVAL;
0650 if (buf[0] < '1' || buf[0] > '9')
0651 return -EINVAL;
0652
0653
0654 data->stretch = buf[0] & 0x0F;
0655
0656
0657 return count;
0658 }
0659
0660 static DEVICE_ATTR_RW(stretch);
0661
0662
0663
0664 static struct attribute *w1_f19_attrs[] = {
0665 &dev_attr_speed.attr,
0666 &dev_attr_stretch.attr,
0667 NULL,
0668 };
0669
0670 static const struct attribute_group w1_f19_group = {
0671 .attrs = w1_f19_attrs,
0672 };
0673
0674 static const struct attribute_group *w1_f19_groups[] = {
0675 &w1_f19_group,
0676 NULL,
0677 };
0678
0679
0680
0681 static int w1_f19_add_slave(struct w1_slave *sl)
0682 {
0683 struct w1_f19_data *data = NULL;
0684
0685
0686 data = devm_kzalloc(&sl->dev, sizeof(*data), GFP_KERNEL);
0687 if (!data)
0688 return -ENOMEM;
0689 sl->family_data = data;
0690
0691
0692 switch (i2c_speed) {
0693 case 100:
0694 __w1_f19_set_i2c_speed(sl, 0);
0695 break;
0696 case 400:
0697 __w1_f19_set_i2c_speed(sl, 1);
0698 break;
0699 case 900:
0700 __w1_f19_set_i2c_speed(sl, 2);
0701 break;
0702 default:
0703
0704
0705
0706
0707
0708
0709 data->speed = 1;
0710 }
0711
0712
0713
0714
0715
0716 data->stretch = i2c_stretch;
0717
0718
0719 data->adapter.owner = THIS_MODULE;
0720 data->adapter.algo = &w1_f19_i2c_algorithm;
0721 data->adapter.algo_data = sl;
0722 strcpy(data->adapter.name, "w1-");
0723 strcat(data->adapter.name, sl->name);
0724 data->adapter.dev.parent = &sl->dev;
0725 data->adapter.quirks = &w1_f19_i2c_adapter_quirks;
0726
0727 return i2c_add_adapter(&data->adapter);
0728 }
0729
0730 static void w1_f19_remove_slave(struct w1_slave *sl)
0731 {
0732 struct w1_f19_data *family_data = sl->family_data;
0733
0734
0735 i2c_del_adapter(&family_data->adapter);
0736
0737
0738 devm_kfree(&sl->dev, family_data);
0739 sl->family_data = NULL;
0740 }
0741
0742
0743
0744 static const struct w1_family_ops w1_f19_fops = {
0745 .add_slave = w1_f19_add_slave,
0746 .remove_slave = w1_f19_remove_slave,
0747 .groups = w1_f19_groups,
0748 };
0749
0750 static struct w1_family w1_family_19 = {
0751 .fid = W1_FAMILY_DS28E17,
0752 .fops = &w1_f19_fops,
0753 };
0754
0755 module_w1_family(w1_family_19);