w1_ds2760.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * 1-Wire implementation for the ds2760 chip
  3. *
  4. * Copyright © 2004-2005, Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
  5. *
  6. * Use consistent with the GNU GPL is permitted,
  7. * provided that this copyright notice is
  8. * preserved in its entirety in all copies and derived works.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/types.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mutex.h>
  17. #include <linux/idr.h>
  18. #include <linux/gfp.h>
  19. #include "../w1.h"
  20. #include "../w1_int.h"
  21. #include "../w1_family.h"
  22. #include "w1_ds2760.h"
  23. static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
  24. int io)
  25. {
  26. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  27. if (!dev)
  28. return 0;
  29. mutex_lock(&sl->master->mutex);
  30. if (addr > DS2760_DATA_SIZE || addr < 0) {
  31. count = 0;
  32. goto out;
  33. }
  34. if (addr + count > DS2760_DATA_SIZE)
  35. count = DS2760_DATA_SIZE - addr;
  36. if (!w1_reset_select_slave(sl)) {
  37. if (!io) {
  38. w1_write_8(sl->master, W1_DS2760_READ_DATA);
  39. w1_write_8(sl->master, addr);
  40. count = w1_read_block(sl->master, buf, count);
  41. } else {
  42. w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
  43. w1_write_8(sl->master, addr);
  44. w1_write_block(sl->master, buf, count);
  45. /* XXX w1_write_block returns void, not n_written */
  46. }
  47. }
  48. out:
  49. mutex_unlock(&sl->master->mutex);
  50. return count;
  51. }
  52. int w1_ds2760_read(struct device *dev, char *buf, int addr, size_t count)
  53. {
  54. return w1_ds2760_io(dev, buf, addr, count, 0);
  55. }
  56. int w1_ds2760_write(struct device *dev, char *buf, int addr, size_t count)
  57. {
  58. return w1_ds2760_io(dev, buf, addr, count, 1);
  59. }
  60. static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
  61. {
  62. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  63. if (!dev)
  64. return -EINVAL;
  65. mutex_lock(&sl->master->mutex);
  66. if (w1_reset_select_slave(sl) == 0) {
  67. w1_write_8(sl->master, cmd);
  68. w1_write_8(sl->master, addr);
  69. }
  70. mutex_unlock(&sl->master->mutex);
  71. return 0;
  72. }
  73. int w1_ds2760_store_eeprom(struct device *dev, int addr)
  74. {
  75. return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
  76. }
  77. int w1_ds2760_recall_eeprom(struct device *dev, int addr)
  78. {
  79. return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
  80. }
  81. static ssize_t w1_ds2760_read_bin(struct file *filp, struct kobject *kobj,
  82. struct bin_attribute *bin_attr,
  83. char *buf, loff_t off, size_t count)
  84. {
  85. struct device *dev = container_of(kobj, struct device, kobj);
  86. return w1_ds2760_read(dev, buf, off, count);
  87. }
  88. static struct bin_attribute w1_ds2760_bin_attr = {
  89. .attr = {
  90. .name = "w1_slave",
  91. .mode = S_IRUGO,
  92. },
  93. .size = DS2760_DATA_SIZE,
  94. .read = w1_ds2760_read_bin,
  95. };
  96. static DEFINE_IDA(bat_ida);
  97. static int w1_ds2760_add_slave(struct w1_slave *sl)
  98. {
  99. int ret;
  100. int id;
  101. struct platform_device *pdev;
  102. id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
  103. if (id < 0) {
  104. ret = id;
  105. goto noid;
  106. }
  107. pdev = platform_device_alloc("ds2760-battery", id);
  108. if (!pdev) {
  109. ret = -ENOMEM;
  110. goto pdev_alloc_failed;
  111. }
  112. pdev->dev.parent = &sl->dev;
  113. ret = platform_device_add(pdev);
  114. if (ret)
  115. goto pdev_add_failed;
  116. ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2760_bin_attr);
  117. if (ret)
  118. goto bin_attr_failed;
  119. dev_set_drvdata(&sl->dev, pdev);
  120. goto success;
  121. bin_attr_failed:
  122. pdev_add_failed:
  123. platform_device_unregister(pdev);
  124. pdev_alloc_failed:
  125. ida_simple_remove(&bat_ida, id);
  126. noid:
  127. success:
  128. return ret;
  129. }
  130. static void w1_ds2760_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_ds2760_bin_attr);
  137. }
  138. static struct w1_family_ops w1_ds2760_fops = {
  139. .add_slave = w1_ds2760_add_slave,
  140. .remove_slave = w1_ds2760_remove_slave,
  141. };
  142. static struct w1_family w1_ds2760_family = {
  143. .fid = W1_FAMILY_DS2760,
  144. .fops = &w1_ds2760_fops,
  145. };
  146. static int __init w1_ds2760_init(void)
  147. {
  148. printk(KERN_INFO "1-Wire driver for the DS2760 battery monitor "
  149. " chip - (c) 2004-2005, Szabolcs Gyurko\n");
  150. ida_init(&bat_ida);
  151. return w1_register_family(&w1_ds2760_family);
  152. }
  153. static void __exit w1_ds2760_exit(void)
  154. {
  155. w1_unregister_family(&w1_ds2760_family);
  156. ida_destroy(&bat_ida);
  157. }
  158. EXPORT_SYMBOL(w1_ds2760_read);
  159. EXPORT_SYMBOL(w1_ds2760_write);
  160. EXPORT_SYMBOL(w1_ds2760_store_eeprom);
  161. EXPORT_SYMBOL(w1_ds2760_recall_eeprom);
  162. module_init(w1_ds2760_init);
  163. module_exit(w1_ds2760_exit);
  164. MODULE_LICENSE("GPL");
  165. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>");
  166. MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");