w1_ds2781.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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->mutex);
  52. ret = w1_ds2781_do_io(dev, buf, addr, count, io);
  53. mutex_unlock(&sl->master->mutex);
  54. return ret;
  55. }
  56. EXPORT_SYMBOL(w1_ds2781_io);
  57. int w1_ds2781_io_nolock(struct device *dev, char *buf, int addr, size_t count,
  58. int io)
  59. {
  60. int ret;
  61. if (!dev)
  62. return -ENODEV;
  63. ret = w1_ds2781_do_io(dev, buf, addr, count, io);
  64. return ret;
  65. }
  66. EXPORT_SYMBOL(w1_ds2781_io_nolock);
  67. int w1_ds2781_eeprom_cmd(struct device *dev, int addr, int cmd)
  68. {
  69. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  70. if (!dev)
  71. return -EINVAL;
  72. mutex_lock(&sl->master->mutex);
  73. if (w1_reset_select_slave(sl) == 0) {
  74. w1_write_8(sl->master, cmd);
  75. w1_write_8(sl->master, addr);
  76. }
  77. mutex_unlock(&sl->master->mutex);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL(w1_ds2781_eeprom_cmd);
  81. static ssize_t w1_ds2781_read_bin(struct file *filp,
  82. struct kobject *kobj,
  83. struct bin_attribute *bin_attr,
  84. char *buf, loff_t off, size_t count)
  85. {
  86. struct device *dev = container_of(kobj, struct device, kobj);
  87. return w1_ds2781_io(dev, buf, off, count, 0);
  88. }
  89. static struct bin_attribute w1_ds2781_bin_attr = {
  90. .attr = {
  91. .name = "w1_slave",
  92. .mode = S_IRUGO,
  93. },
  94. .size = DS2781_DATA_SIZE,
  95. .read = w1_ds2781_read_bin,
  96. };
  97. static DEFINE_IDA(bat_ida);
  98. static int w1_ds2781_add_slave(struct w1_slave *sl)
  99. {
  100. int ret;
  101. int id;
  102. struct platform_device *pdev;
  103. id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
  104. if (id < 0) {
  105. ret = id;
  106. goto noid;
  107. }
  108. pdev = platform_device_alloc("ds2781-battery", id);
  109. if (!pdev) {
  110. ret = -ENOMEM;
  111. goto pdev_alloc_failed;
  112. }
  113. pdev->dev.parent = &sl->dev;
  114. ret = platform_device_add(pdev);
  115. if (ret)
  116. goto pdev_add_failed;
  117. ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2781_bin_attr);
  118. if (ret)
  119. goto bin_attr_failed;
  120. dev_set_drvdata(&sl->dev, pdev);
  121. return 0;
  122. bin_attr_failed:
  123. pdev_add_failed:
  124. platform_device_unregister(pdev);
  125. pdev_alloc_failed:
  126. ida_simple_remove(&bat_ida, id);
  127. noid:
  128. return ret;
  129. }
  130. static void w1_ds2781_remove_slave(struct w1_slave *sl)
  131. {
  132. struct platform_device *pdev = dev_get_drvdata(&sl->dev);
  133. int id = pdev->id;
  134. platform_device_unregister(pdev);
  135. ida_simple_remove(&bat_ida, id);
  136. sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2781_bin_attr);
  137. }
  138. static struct w1_family_ops w1_ds2781_fops = {
  139. .add_slave = w1_ds2781_add_slave,
  140. .remove_slave = w1_ds2781_remove_slave,
  141. };
  142. static struct w1_family w1_ds2781_family = {
  143. .fid = W1_FAMILY_DS2781,
  144. .fops = &w1_ds2781_fops,
  145. };
  146. static int __init w1_ds2781_init(void)
  147. {
  148. ida_init(&bat_ida);
  149. return w1_register_family(&w1_ds2781_family);
  150. }
  151. static void __exit w1_ds2781_exit(void)
  152. {
  153. w1_unregister_family(&w1_ds2781_family);
  154. ida_destroy(&bat_ida);
  155. }
  156. module_init(w1_ds2781_init);
  157. module_exit(w1_ds2781_exit);
  158. MODULE_LICENSE("GPL");
  159. MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
  160. MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC");