w1_ds2780.c 3.9 KB

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