w1_ds2780.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <linux/w1.h>
  23. #include "w1_ds2780.h"
  24. #define W1_FAMILY_DS2780 0x32
  25. static int w1_ds2780_do_io(struct device *dev, char *buf, int addr,
  26. size_t count, int io)
  27. {
  28. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  29. if (addr > DS2780_DATA_SIZE || addr < 0)
  30. return 0;
  31. count = min_t(int, count, DS2780_DATA_SIZE - addr);
  32. if (w1_reset_select_slave(sl) == 0) {
  33. if (io) {
  34. w1_write_8(sl->master, W1_DS2780_WRITE_DATA);
  35. w1_write_8(sl->master, addr);
  36. w1_write_block(sl->master, buf, count);
  37. } else {
  38. w1_write_8(sl->master, W1_DS2780_READ_DATA);
  39. w1_write_8(sl->master, addr);
  40. count = w1_read_block(sl->master, buf, count);
  41. }
  42. }
  43. return count;
  44. }
  45. int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count,
  46. int io)
  47. {
  48. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  49. int ret;
  50. if (!dev)
  51. return -ENODEV;
  52. mutex_lock(&sl->master->bus_mutex);
  53. ret = w1_ds2780_do_io(dev, buf, addr, count, io);
  54. mutex_unlock(&sl->master->bus_mutex);
  55. return ret;
  56. }
  57. EXPORT_SYMBOL(w1_ds2780_io);
  58. int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd)
  59. {
  60. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  61. if (!dev)
  62. return -EINVAL;
  63. mutex_lock(&sl->master->bus_mutex);
  64. if (w1_reset_select_slave(sl) == 0) {
  65. w1_write_8(sl->master, cmd);
  66. w1_write_8(sl->master, addr);
  67. }
  68. mutex_unlock(&sl->master->bus_mutex);
  69. return 0;
  70. }
  71. EXPORT_SYMBOL(w1_ds2780_eeprom_cmd);
  72. static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
  73. struct bin_attribute *bin_attr, char *buf,
  74. loff_t off, size_t count)
  75. {
  76. struct device *dev = container_of(kobj, struct device, kobj);
  77. return w1_ds2780_io(dev, buf, off, count, 0);
  78. }
  79. static BIN_ATTR_RO(w1_slave, DS2780_DATA_SIZE);
  80. static struct bin_attribute *w1_ds2780_bin_attrs[] = {
  81. &bin_attr_w1_slave,
  82. NULL,
  83. };
  84. static const struct attribute_group w1_ds2780_group = {
  85. .bin_attrs = w1_ds2780_bin_attrs,
  86. };
  87. static const struct attribute_group *w1_ds2780_groups[] = {
  88. &w1_ds2780_group,
  89. NULL,
  90. };
  91. static int w1_ds2780_add_slave(struct w1_slave *sl)
  92. {
  93. int ret;
  94. struct platform_device *pdev;
  95. pdev = platform_device_alloc("ds2780-battery", PLATFORM_DEVID_AUTO);
  96. if (!pdev)
  97. return -ENOMEM;
  98. pdev->dev.parent = &sl->dev;
  99. ret = platform_device_add(pdev);
  100. if (ret)
  101. goto pdev_add_failed;
  102. dev_set_drvdata(&sl->dev, pdev);
  103. return 0;
  104. pdev_add_failed:
  105. platform_device_put(pdev);
  106. return ret;
  107. }
  108. static void w1_ds2780_remove_slave(struct w1_slave *sl)
  109. {
  110. struct platform_device *pdev = dev_get_drvdata(&sl->dev);
  111. platform_device_unregister(pdev);
  112. }
  113. static struct w1_family_ops w1_ds2780_fops = {
  114. .add_slave = w1_ds2780_add_slave,
  115. .remove_slave = w1_ds2780_remove_slave,
  116. .groups = w1_ds2780_groups,
  117. };
  118. static struct w1_family w1_ds2780_family = {
  119. .fid = W1_FAMILY_DS2780,
  120. .fops = &w1_ds2780_fops,
  121. };
  122. module_w1_family(w1_ds2780_family);
  123. MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
  124. MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");
  125. MODULE_LICENSE("GPL");
  126. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2780));