w1_ds2780.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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->bus_mutex);
  54. ret = w1_ds2780_do_io(dev, buf, addr, count, io);
  55. mutex_unlock(&sl->master->bus_mutex);
  56. return ret;
  57. }
  58. EXPORT_SYMBOL(w1_ds2780_io);
  59. int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd)
  60. {
  61. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  62. if (!dev)
  63. return -EINVAL;
  64. mutex_lock(&sl->master->bus_mutex);
  65. if (w1_reset_select_slave(sl) == 0) {
  66. w1_write_8(sl->master, cmd);
  67. w1_write_8(sl->master, addr);
  68. }
  69. mutex_unlock(&sl->master->bus_mutex);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(w1_ds2780_eeprom_cmd);
  73. static ssize_t w1_ds2780_read_bin(struct file *filp,
  74. struct kobject *kobj,
  75. struct bin_attribute *bin_attr,
  76. char *buf, loff_t off, size_t count)
  77. {
  78. struct device *dev = container_of(kobj, struct device, kobj);
  79. return w1_ds2780_io(dev, buf, off, count, 0);
  80. }
  81. static struct bin_attribute w1_ds2780_bin_attr = {
  82. .attr = {
  83. .name = "w1_slave",
  84. .mode = S_IRUGO,
  85. },
  86. .size = DS2780_DATA_SIZE,
  87. .read = w1_ds2780_read_bin,
  88. };
  89. static DEFINE_IDA(bat_ida);
  90. static int w1_ds2780_add_slave(struct w1_slave *sl)
  91. {
  92. int ret;
  93. int id;
  94. struct platform_device *pdev;
  95. id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
  96. if (id < 0) {
  97. ret = id;
  98. goto noid;
  99. }
  100. pdev = platform_device_alloc("ds2780-battery", id);
  101. if (!pdev) {
  102. ret = -ENOMEM;
  103. goto pdev_alloc_failed;
  104. }
  105. pdev->dev.parent = &sl->dev;
  106. ret = platform_device_add(pdev);
  107. if (ret)
  108. goto pdev_add_failed;
  109. ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
  110. if (ret)
  111. goto bin_attr_failed;
  112. dev_set_drvdata(&sl->dev, pdev);
  113. return 0;
  114. bin_attr_failed:
  115. pdev_add_failed:
  116. platform_device_unregister(pdev);
  117. pdev_alloc_failed:
  118. ida_simple_remove(&bat_ida, id);
  119. noid:
  120. return ret;
  121. }
  122. static void w1_ds2780_remove_slave(struct w1_slave *sl)
  123. {
  124. struct platform_device *pdev = dev_get_drvdata(&sl->dev);
  125. int id = pdev->id;
  126. platform_device_unregister(pdev);
  127. ida_simple_remove(&bat_ida, id);
  128. sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
  129. }
  130. static struct w1_family_ops w1_ds2780_fops = {
  131. .add_slave = w1_ds2780_add_slave,
  132. .remove_slave = w1_ds2780_remove_slave,
  133. };
  134. static struct w1_family w1_ds2780_family = {
  135. .fid = W1_FAMILY_DS2780,
  136. .fops = &w1_ds2780_fops,
  137. };
  138. static int __init w1_ds2780_init(void)
  139. {
  140. ida_init(&bat_ida);
  141. return w1_register_family(&w1_ds2780_family);
  142. }
  143. static void __exit w1_ds2780_exit(void)
  144. {
  145. w1_unregister_family(&w1_ds2780_family);
  146. ida_destroy(&bat_ida);
  147. }
  148. module_init(w1_ds2780_init);
  149. module_exit(w1_ds2780_exit);
  150. MODULE_LICENSE("GPL");
  151. MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
  152. MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");