w1_ds2780.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * 1-Wire implementation for the ds2780 chip
  3. *
  4. * Copyright (C) 2010 Indesign, LLC
  5. *
  6. * Author: Clifton Barnes <cabarnes@indesign-llc.com>
  7. *
  8. * Based on w1-ds2760 driver
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/types.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mutex.h>
  21. #include <linux/idr.h>
  22. #include "../w1.h"
  23. #include "../w1_int.h"
  24. #include "../w1_family.h"
  25. #include "w1_ds2780.h"
  26. static int w1_ds2780_do_io(struct device *dev, char *buf, int addr,
  27. size_t count, int io)
  28. {
  29. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  30. if (addr > DS2780_DATA_SIZE || addr < 0)
  31. return 0;
  32. count = min_t(int, count, DS2780_DATA_SIZE - addr);
  33. if (w1_reset_select_slave(sl) == 0) {
  34. if (io) {
  35. w1_write_8(sl->master, W1_DS2780_WRITE_DATA);
  36. w1_write_8(sl->master, addr);
  37. w1_write_block(sl->master, buf, count);
  38. } else {
  39. w1_write_8(sl->master, W1_DS2780_READ_DATA);
  40. w1_write_8(sl->master, addr);
  41. count = w1_read_block(sl->master, buf, count);
  42. }
  43. }
  44. return count;
  45. }
  46. int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count,
  47. int io)
  48. {
  49. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  50. int ret;
  51. if (!dev)
  52. return -ENODEV;
  53. mutex_lock(&sl->master->mutex);
  54. ret = w1_ds2780_do_io(dev, buf, addr, count, io);
  55. mutex_unlock(&sl->master->mutex);
  56. return ret;
  57. }
  58. EXPORT_SYMBOL(w1_ds2780_io);
  59. int w1_ds2780_io_nolock(struct device *dev, char *buf, int addr, size_t count,
  60. int io)
  61. {
  62. int ret;
  63. if (!dev)
  64. return -ENODEV;
  65. ret = w1_ds2780_do_io(dev, buf, addr, count, io);
  66. return ret;
  67. }
  68. EXPORT_SYMBOL(w1_ds2780_io_nolock);
  69. int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd)
  70. {
  71. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  72. if (!dev)
  73. return -EINVAL;
  74. mutex_lock(&sl->master->mutex);
  75. if (w1_reset_select_slave(sl) == 0) {
  76. w1_write_8(sl->master, cmd);
  77. w1_write_8(sl->master, addr);
  78. }
  79. mutex_unlock(&sl->master->mutex);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL(w1_ds2780_eeprom_cmd);
  83. static ssize_t w1_ds2780_read_bin(struct file *filp,
  84. struct kobject *kobj,
  85. struct bin_attribute *bin_attr,
  86. char *buf, loff_t off, size_t count)
  87. {
  88. struct device *dev = container_of(kobj, struct device, kobj);
  89. return w1_ds2780_io(dev, buf, off, count, 0);
  90. }
  91. static struct bin_attribute w1_ds2780_bin_attr = {
  92. .attr = {
  93. .name = "w1_slave",
  94. .mode = S_IRUGO,
  95. },
  96. .size = DS2780_DATA_SIZE,
  97. .read = w1_ds2780_read_bin,
  98. };
  99. static DEFINE_IDA(bat_ida);
  100. static int w1_ds2780_add_slave(struct w1_slave *sl)
  101. {
  102. int ret;
  103. int id;
  104. struct platform_device *pdev;
  105. id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
  106. if (id < 0) {
  107. ret = id;
  108. goto noid;
  109. }
  110. pdev = platform_device_alloc("ds2780-battery", id);
  111. if (!pdev) {
  112. ret = -ENOMEM;
  113. goto pdev_alloc_failed;
  114. }
  115. pdev->dev.parent = &sl->dev;
  116. ret = platform_device_add(pdev);
  117. if (ret)
  118. goto pdev_add_failed;
  119. ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
  120. if (ret)
  121. goto bin_attr_failed;
  122. dev_set_drvdata(&sl->dev, pdev);
  123. return 0;
  124. bin_attr_failed:
  125. pdev_add_failed:
  126. platform_device_unregister(pdev);
  127. pdev_alloc_failed:
  128. ida_simple_remove(&bat_ida, id);
  129. noid:
  130. return ret;
  131. }
  132. static void w1_ds2780_remove_slave(struct w1_slave *sl)
  133. {
  134. struct platform_device *pdev = dev_get_drvdata(&sl->dev);
  135. int id = pdev->id;
  136. platform_device_unregister(pdev);
  137. ida_simple_remove(&bat_ida, id);
  138. sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
  139. }
  140. static struct w1_family_ops w1_ds2780_fops = {
  141. .add_slave = w1_ds2780_add_slave,
  142. .remove_slave = w1_ds2780_remove_slave,
  143. };
  144. static struct w1_family w1_ds2780_family = {
  145. .fid = W1_FAMILY_DS2780,
  146. .fops = &w1_ds2780_fops,
  147. };
  148. static int __init w1_ds2780_init(void)
  149. {
  150. ida_init(&bat_ida);
  151. return w1_register_family(&w1_ds2780_family);
  152. }
  153. static void __exit w1_ds2780_exit(void)
  154. {
  155. w1_unregister_family(&w1_ds2780_family);
  156. ida_destroy(&bat_ida);
  157. }
  158. module_init(w1_ds2780_init);
  159. module_exit(w1_ds2780_exit);
  160. MODULE_LICENSE("GPL");
  161. MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
  162. MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");