w1_ds2760.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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->bus_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->bus_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->bus_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->bus_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_slave_read(struct file *filp, struct kobject *kobj,
  82. struct bin_attribute *bin_attr, char *buf,
  83. 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 BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE);
  89. static struct bin_attribute *w1_ds2760_bin_attrs[] = {
  90. &bin_attr_w1_slave,
  91. NULL,
  92. };
  93. static const struct attribute_group w1_ds2760_group = {
  94. .bin_attrs = w1_ds2760_bin_attrs,
  95. };
  96. static const struct attribute_group *w1_ds2760_groups[] = {
  97. &w1_ds2760_group,
  98. NULL,
  99. };
  100. static DEFINE_IDA(bat_ida);
  101. static int w1_ds2760_add_slave(struct w1_slave *sl)
  102. {
  103. int ret;
  104. int id;
  105. struct platform_device *pdev;
  106. id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
  107. if (id < 0) {
  108. ret = id;
  109. goto noid;
  110. }
  111. pdev = platform_device_alloc("ds2760-battery", id);
  112. if (!pdev) {
  113. ret = -ENOMEM;
  114. goto pdev_alloc_failed;
  115. }
  116. pdev->dev.parent = &sl->dev;
  117. ret = platform_device_add(pdev);
  118. if (ret)
  119. goto pdev_add_failed;
  120. dev_set_drvdata(&sl->dev, pdev);
  121. goto success;
  122. pdev_add_failed:
  123. platform_device_put(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. }
  137. static struct w1_family_ops w1_ds2760_fops = {
  138. .add_slave = w1_ds2760_add_slave,
  139. .remove_slave = w1_ds2760_remove_slave,
  140. .groups = w1_ds2760_groups,
  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. pr_info("1-Wire driver for the DS2760 battery monitor chip - (c) 2004-2005, Szabolcs Gyurko\n");
  149. ida_init(&bat_ida);
  150. return w1_register_family(&w1_ds2760_family);
  151. }
  152. static void __exit w1_ds2760_exit(void)
  153. {
  154. w1_unregister_family(&w1_ds2760_family);
  155. ida_destroy(&bat_ida);
  156. }
  157. EXPORT_SYMBOL(w1_ds2760_read);
  158. EXPORT_SYMBOL(w1_ds2760_write);
  159. EXPORT_SYMBOL(w1_ds2760_store_eeprom);
  160. EXPORT_SYMBOL(w1_ds2760_recall_eeprom);
  161. module_init(w1_ds2760_init);
  162. module_exit(w1_ds2760_exit);
  163. MODULE_LICENSE("GPL");
  164. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>");
  165. MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
  166. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));