w1_ds2780.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_slave_read(struct file *filp, struct kobject *kobj,
  74. struct bin_attribute *bin_attr, char *buf,
  75. loff_t off, size_t count)
  76. {
  77. struct device *dev = container_of(kobj, struct device, kobj);
  78. return w1_ds2780_io(dev, buf, off, count, 0);
  79. }
  80. static BIN_ATTR_RO(w1_slave, DS2780_DATA_SIZE);
  81. static struct bin_attribute *w1_ds2780_bin_attrs[] = {
  82. &bin_attr_w1_slave,
  83. NULL,
  84. };
  85. static const struct attribute_group w1_ds2780_group = {
  86. .bin_attrs = w1_ds2780_bin_attrs,
  87. };
  88. static const struct attribute_group *w1_ds2780_groups[] = {
  89. &w1_ds2780_group,
  90. NULL,
  91. };
  92. static DEFINE_IDA(bat_ida);
  93. static int w1_ds2780_add_slave(struct w1_slave *sl)
  94. {
  95. int ret;
  96. int id;
  97. struct platform_device *pdev;
  98. id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
  99. if (id < 0) {
  100. ret = id;
  101. goto noid;
  102. }
  103. pdev = platform_device_alloc("ds2780-battery", id);
  104. if (!pdev) {
  105. ret = -ENOMEM;
  106. goto pdev_alloc_failed;
  107. }
  108. pdev->dev.parent = &sl->dev;
  109. ret = platform_device_add(pdev);
  110. if (ret)
  111. goto pdev_add_failed;
  112. dev_set_drvdata(&sl->dev, pdev);
  113. return 0;
  114. pdev_add_failed:
  115. platform_device_put(pdev);
  116. pdev_alloc_failed:
  117. ida_simple_remove(&bat_ida, id);
  118. noid:
  119. return ret;
  120. }
  121. static void w1_ds2780_remove_slave(struct w1_slave *sl)
  122. {
  123. struct platform_device *pdev = dev_get_drvdata(&sl->dev);
  124. int id = pdev->id;
  125. platform_device_unregister(pdev);
  126. ida_simple_remove(&bat_ida, id);
  127. }
  128. static struct w1_family_ops w1_ds2780_fops = {
  129. .add_slave = w1_ds2780_add_slave,
  130. .remove_slave = w1_ds2780_remove_slave,
  131. .groups = w1_ds2780_groups,
  132. };
  133. static struct w1_family w1_ds2780_family = {
  134. .fid = W1_FAMILY_DS2780,
  135. .fops = &w1_ds2780_fops,
  136. };
  137. static int __init w1_ds2780_init(void)
  138. {
  139. ida_init(&bat_ida);
  140. return w1_register_family(&w1_ds2780_family);
  141. }
  142. static void __exit w1_ds2780_exit(void)
  143. {
  144. w1_unregister_family(&w1_ds2780_family);
  145. ida_destroy(&bat_ida);
  146. }
  147. module_init(w1_ds2780_init);
  148. module_exit(w1_ds2780_exit);
  149. MODULE_LICENSE("GPL");
  150. MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
  151. MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");
  152. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2780));