w1_ds2781.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * 1-Wire implementation for the ds2781 chip
  3. *
  4. * Author: Renata Sayakhova <renata@oktetlabs.ru>
  5. *
  6. * Based on w1-ds2780 driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/types.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mutex.h>
  19. #include <linux/idr.h>
  20. #include "../w1.h"
  21. #include "../w1_int.h"
  22. #include "../w1_family.h"
  23. #include "w1_ds2781.h"
  24. static int w1_ds2781_do_io(struct device *dev, char *buf, int addr,
  25. size_t count, int io)
  26. {
  27. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  28. if (addr > DS2781_DATA_SIZE || addr < 0)
  29. return 0;
  30. count = min_t(int, count, DS2781_DATA_SIZE - addr);
  31. if (w1_reset_select_slave(sl) == 0) {
  32. if (io) {
  33. w1_write_8(sl->master, W1_DS2781_WRITE_DATA);
  34. w1_write_8(sl->master, addr);
  35. w1_write_block(sl->master, buf, count);
  36. } else {
  37. w1_write_8(sl->master, W1_DS2781_READ_DATA);
  38. w1_write_8(sl->master, addr);
  39. count = w1_read_block(sl->master, buf, count);
  40. }
  41. }
  42. return count;
  43. }
  44. int w1_ds2781_io(struct device *dev, char *buf, int addr, size_t count,
  45. int io)
  46. {
  47. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  48. int ret;
  49. if (!dev)
  50. return -ENODEV;
  51. mutex_lock(&sl->master->bus_mutex);
  52. ret = w1_ds2781_do_io(dev, buf, addr, count, io);
  53. mutex_unlock(&sl->master->bus_mutex);
  54. return ret;
  55. }
  56. EXPORT_SYMBOL(w1_ds2781_io);
  57. int w1_ds2781_eeprom_cmd(struct device *dev, int addr, int cmd)
  58. {
  59. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  60. if (!dev)
  61. return -EINVAL;
  62. mutex_lock(&sl->master->bus_mutex);
  63. if (w1_reset_select_slave(sl) == 0) {
  64. w1_write_8(sl->master, cmd);
  65. w1_write_8(sl->master, addr);
  66. }
  67. mutex_unlock(&sl->master->bus_mutex);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(w1_ds2781_eeprom_cmd);
  71. static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
  72. struct bin_attribute *bin_attr, char *buf,
  73. loff_t off, size_t count)
  74. {
  75. struct device *dev = container_of(kobj, struct device, kobj);
  76. return w1_ds2781_io(dev, buf, off, count, 0);
  77. }
  78. static BIN_ATTR_RO(w1_slave, DS2781_DATA_SIZE);
  79. static struct bin_attribute *w1_ds2781_bin_attrs[] = {
  80. &bin_attr_w1_slave,
  81. NULL,
  82. };
  83. static const struct attribute_group w1_ds2781_group = {
  84. .bin_attrs = w1_ds2781_bin_attrs,
  85. };
  86. static const struct attribute_group *w1_ds2781_groups[] = {
  87. &w1_ds2781_group,
  88. NULL,
  89. };
  90. static DEFINE_IDA(bat_ida);
  91. static int w1_ds2781_add_slave(struct w1_slave *sl)
  92. {
  93. int ret;
  94. int id;
  95. struct platform_device *pdev;
  96. id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
  97. if (id < 0) {
  98. ret = id;
  99. goto noid;
  100. }
  101. pdev = platform_device_alloc("ds2781-battery", id);
  102. if (!pdev) {
  103. ret = -ENOMEM;
  104. goto pdev_alloc_failed;
  105. }
  106. pdev->dev.parent = &sl->dev;
  107. ret = platform_device_add(pdev);
  108. if (ret)
  109. goto pdev_add_failed;
  110. dev_set_drvdata(&sl->dev, pdev);
  111. return 0;
  112. pdev_add_failed:
  113. platform_device_put(pdev);
  114. pdev_alloc_failed:
  115. ida_simple_remove(&bat_ida, id);
  116. noid:
  117. return ret;
  118. }
  119. static void w1_ds2781_remove_slave(struct w1_slave *sl)
  120. {
  121. struct platform_device *pdev = dev_get_drvdata(&sl->dev);
  122. int id = pdev->id;
  123. platform_device_unregister(pdev);
  124. ida_simple_remove(&bat_ida, id);
  125. }
  126. static struct w1_family_ops w1_ds2781_fops = {
  127. .add_slave = w1_ds2781_add_slave,
  128. .remove_slave = w1_ds2781_remove_slave,
  129. .groups = w1_ds2781_groups,
  130. };
  131. static struct w1_family w1_ds2781_family = {
  132. .fid = W1_FAMILY_DS2781,
  133. .fops = &w1_ds2781_fops,
  134. };
  135. static int __init w1_ds2781_init(void)
  136. {
  137. ida_init(&bat_ida);
  138. return w1_register_family(&w1_ds2781_family);
  139. }
  140. static void __exit w1_ds2781_exit(void)
  141. {
  142. w1_unregister_family(&w1_ds2781_family);
  143. ida_destroy(&bat_ida);
  144. }
  145. module_init(w1_ds2781_init);
  146. module_exit(w1_ds2781_exit);
  147. MODULE_LICENSE("GPL");
  148. MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
  149. MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC");
  150. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2781));